Skip to content

Commit

Permalink
Time of day parameter type converts to a wrong value; fixes #328
Browse files Browse the repository at this point in the history
* 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?
  • Loading branch information
lxatstariongroup authored Apr 15, 2024
1 parent 6668b10 commit 53f685e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});

Expand All @@ -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 _);
Expand Down
25 changes: 20 additions & 5 deletions CDP4Common.Tests/Validation/DtoValueValidatorTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ public void VerifyTextParameterTypeValidationAndCleanup()
Assert.That(result.ResultKind, Is.EqualTo(ValidationResultKind.Invalid));
}


[Test]
public void VerifyTimeOfDayParameterTypeValidationAndCleanup()
{
Expand All @@ -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"));
});

Expand All @@ -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 _);
Expand Down
59 changes: 16 additions & 43 deletions CDP4Common/Validation/ObjectValueValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +62,7 @@ public static class ObjectValueValidator
/// </summary>
/// <param name="dateTime">The <see cref="DateTime" /> to check</param>
/// <returns>True if the provided <see cref="DateTime" /> is a defualt <see cref="DateTime" /></returns>
public static bool IsDefaultDateTime(this DateTime dateTime)
public static bool IsDefaultDate(this DateTime dateTime)
{
return dateTime.Year == 1 && dateTime.Month == 1 && dateTime.Day == 1;
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand All @@ -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."
};
}

/// <summary>
/// Converts a <see cref="DateTime"/> object to a CDP compliant TimeOfDay string representation
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/></param>
/// <returns>The correct string format</returns>
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;
}
}
}
2 changes: 1 addition & 1 deletion CDP4Dal/CDP4Dal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 26.6.2
[BUMP] To CDP4Common 26.6.2
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down

0 comments on commit 53f685e

Please sign in to comment.