From 53f685ea306f90106f2d5acdfd434939e35c4090 Mon Sep 17 00:00:00 2001 From: Alexander van Delft <56023674+lxatrhea@users.noreply.github.com> Date: Mon, 15 Apr 2024 10:22:47 +0200 Subject: [PATCH] Time of day parameter type converts to a wrong value; fixes #328 * Always store TimeOfDay Parameter as format 'HH:mm:ss' * Fix unit tests * Fix some more tests * Fix return original value when value is correct * Minor fix and unit tests * Build fixed? --- .../DtoValueValidatorTestFixture.cs | 24 ++++++-- .../DtoValueValidatorTestFixture.cs | 25 ++++++-- CDP4Common/Validation/ObjectValueValidator.cs | 59 +++++-------------- CDP4Dal/CDP4Dal.csproj | 2 +- 4 files changed, 57 insertions(+), 53 deletions(-) diff --git a/CDP4Common.NetCore.Tests/Validation/DtoValueValidatorTestFixture.cs b/CDP4Common.NetCore.Tests/Validation/DtoValueValidatorTestFixture.cs index f3851c94..a853b54e 100644 --- a/CDP4Common.NetCore.Tests/Validation/DtoValueValidatorTestFixture.cs +++ b/CDP4Common.NetCore.Tests/Validation/DtoValueValidatorTestFixture.cs @@ -431,7 +431,7 @@ public void VerifyTimeOfDayParameterTypeValidationAndCleanup() { Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); Assert.That(this.valueSet.Manual[0], Is.EqualTo("14:00:12")); - Assert.That(this.valueSet.Computed[0], Is.EqualTo("14:00:12Z")); + Assert.That(this.valueSet.Computed[0], Is.EqualTo("0001-01-01T14:00:12.0000000Z")); Assert.That(this.valueSet.Reference[0], Is.EqualTo("17:49:30.453Z")); }); @@ -441,19 +441,35 @@ public void VerifyTimeOfDayParameterTypeValidationAndCleanup() Assert.Multiple(() => { Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); - Assert.That(this.valueSet.Computed[0], Is.EqualTo("14:00:12")); + Assert.That(this.valueSet.Computed[0], Is.EqualTo("0001-01-01T14:00:12.0000000")); }); this.valueSet.Manual[0] = "0001-01-02T14:00:12.0000000"; result = this.timeOfDayParameterType.ValidateAndCleanup(this.valueSet, null); Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Invalid)); - result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Utc), out var cleanedValue); + result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Utc), out var cleanedValue1); Assert.Multiple(() => { Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); - Assert.That(cleanedValue, Is.EqualTo("12:45:35Z")); + Assert.That(cleanedValue1, Is.EqualTo("0001-01-01T12:45:35.0000000Z")); + }); + + result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Unspecified), out var cleanedValue2); + + Assert.Multiple(() => + { + Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); + Assert.That(cleanedValue2, Is.EqualTo("0001-01-01T12:45:35.0000000")); + }); + + result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Local), out var cleanedValue3); + + Assert.Multiple(() => + { + Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); + Assert.That(cleanedValue3, Is.EqualTo("0001-01-01T12:45:35.0000000+01:00")); }); result = this.timeOfDayParameterType.Validate(new DateTime(2001, 1, 1, 12, 45, 35, DateTimeKind.Utc), out _); diff --git a/CDP4Common.Tests/Validation/DtoValueValidatorTestFixture.cs b/CDP4Common.Tests/Validation/DtoValueValidatorTestFixture.cs index 6658c6a6..7dac6191 100644 --- a/CDP4Common.Tests/Validation/DtoValueValidatorTestFixture.cs +++ b/CDP4Common.Tests/Validation/DtoValueValidatorTestFixture.cs @@ -414,7 +414,6 @@ public void VerifyTextParameterTypeValidationAndCleanup() Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Invalid)); } - [Test] public void VerifyTimeOfDayParameterTypeValidationAndCleanup() { @@ -432,7 +431,7 @@ public void VerifyTimeOfDayParameterTypeValidationAndCleanup() { Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); Assert.That(this.valueSet.Manual[0], Is.EqualTo("14:00:12")); - Assert.That(this.valueSet.Computed[0], Is.EqualTo("14:00:12Z")); + Assert.That(this.valueSet.Computed[0], Is.EqualTo("0001-01-01T14:00:12.0000000Z")); Assert.That(this.valueSet.Reference[0], Is.EqualTo("17:49:30.453Z")); }); @@ -442,19 +441,35 @@ public void VerifyTimeOfDayParameterTypeValidationAndCleanup() Assert.Multiple(() => { Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); - Assert.That(this.valueSet.Computed[0], Is.EqualTo("14:00:12")); + Assert.That(this.valueSet.Computed[0], Is.EqualTo("0001-01-01T14:00:12.0000000")); }); this.valueSet.Manual[0] = "0001-01-02T14:00:12.0000000"; result = this.timeOfDayParameterType.ValidateAndCleanup(this.valueSet, null); Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Invalid)); - result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Utc), out var cleanedValue); + result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Utc), out var cleanedValue1); + + Assert.Multiple(() => + { + Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); + Assert.That(cleanedValue1, Is.EqualTo("0001-01-01T12:45:35.0000000Z")); + }); + + result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Unspecified), out var cleanedValue2); + + Assert.Multiple(() => + { + Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); + Assert.That(cleanedValue2, Is.EqualTo("0001-01-01T12:45:35.0000000")); + }); + + result = this.timeOfDayParameterType.Validate(new DateTime(1, 1, 1, 12, 45, 35, DateTimeKind.Local), out var cleanedValue3); Assert.Multiple(() => { Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Valid)); - Assert.That(cleanedValue, Is.EqualTo("12:45:35Z")); + Assert.That(cleanedValue3, Is.EqualTo("0001-01-01T12:45:35.0000000+01:00")); }); result = this.timeOfDayParameterType.Validate(new DateTime(2001, 1, 1, 12, 45, 35, DateTimeKind.Utc), out _); diff --git a/CDP4Common/Validation/ObjectValueValidator.cs b/CDP4Common/Validation/ObjectValueValidator.cs index 02423149..f444bcbd 100644 --- a/CDP4Common/Validation/ObjectValueValidator.cs +++ b/CDP4Common/Validation/ObjectValueValidator.cs @@ -28,7 +28,6 @@ namespace CDP4Common.Validation using System.Collections.Generic; using System.Globalization; using System.Linq; - using System.Text.RegularExpressions; using CDP4Common.Helpers; using CDP4Common.SiteDirectoryData; @@ -63,7 +62,7 @@ public static class ObjectValueValidator /// /// The to check /// True if the provided is a defualt - public static bool IsDefaultDateTime(this DateTime dateTime) + public static bool IsDefaultDate(this DateTime dateTime) { return dateTime.Year == 1 && dateTime.Month == 1 && dateTime.Day == 1; } @@ -478,9 +477,9 @@ public static ValidationResult ValidateTimeOfDay(this object value, out string c var isDateTime = DateTime.TryParse(parsedString, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.RoundtripKind, out var dateTime); - if (isDateTime && dateTime.IsDefaultDateTime()) + if (isDateTime && dateTime.IsDefaultDate()) { - cleanedValue = ToTimeOfDay(dateTime); + cleanedValue = parsedString; Logger.Debug("TimeOfDay {0} validated", parsedString); return ValidationResult.ValidResult(); } @@ -490,12 +489,21 @@ public static ValidationResult ValidateTimeOfDay(this object value, out string c { var timeOfDayValue = Convert.ToDateTime(value, CultureInfo.InvariantCulture); - if (timeOfDayValue.IsDefaultDateTime()) + if (timeOfDayValue.IsDefaultDate()) { - cleanedValue = ToTimeOfDay(timeOfDayValue); + if (value is string stringValue) + { + cleanedValue = stringValue; + Logger.Debug("TimeOfDay {0} validated", cleanedValue); + return ValidationResult.ValidResult(); + } - Logger.Debug("TimeOfDay {0} validated", value); - return ValidationResult.ValidResult(); + if (value is DateTime dtValue) + { + cleanedValue = dtValue.ToString("o"); + Logger.Debug("TimeOfDay {0} validated", cleanedValue); + return ValidationResult.ValidResult(); + } } } catch (Exception ex) @@ -511,40 +519,5 @@ public static ValidationResult ValidateTimeOfDay(this object value, out string c Message = $"'{value}' is not a valid Time of Day, for valid Time Of Day formats see http://www.w3.org/TR/xmlschema-2/#time." }; } - - /// - /// Converts a object to a CDP compliant TimeOfDay string representation - /// - /// The - /// The correct string format - private static string ToTimeOfDay(DateTime dateTime) - { - string cleanedValue; - - if (dateTime.Kind == DateTimeKind.Utc) - { - if (dateTime.Millisecond > 0) - { - cleanedValue = dateTime.ToString("HH:mm:ss.fffZ"); - } - else - { - cleanedValue = dateTime.ToString("HH:mm:ssZ"); - } - } - else - { - if (dateTime.Millisecond > 0) - { - cleanedValue = dateTime.ToString("HH:mm:ss.fff"); - } - else - { - cleanedValue = dateTime.ToString("HH:mm:ss"); - } - } - - return cleanedValue; - } } } diff --git a/CDP4Dal/CDP4Dal.csproj b/CDP4Dal/CDP4Dal.csproj index 553348dc..e657fea6 100644 --- a/CDP4Dal/CDP4Dal.csproj +++ b/CDP4Dal/CDP4Dal.csproj @@ -20,7 +20,7 @@ CDP COMET ECSS-E-TM-10-25 LGPL-3.0-only - [BUMP] To CDP4Common 26.6.2 + [BUMP] To CDP4Common 26.6.2 README.md