From 5cd52861d41f4f29926a148680e222a0bb8248ce Mon Sep 17 00:00:00 2001 From: Michiel Oda Date: Mon, 26 Feb 2024 11:49:45 +0100 Subject: [PATCH 1/2] Added the Decimals check on DateTime parameters --- .../Display/Decimals/CheckDecimalsTag.cs | 56 +++++++++++ Protocol/ErrorMessages.xml | 25 +++++ .../Display/Decimals/CheckDecimalsTag.cs | 69 ++++++++++++++ .../CheckDecimalsTag/CheckDecimalsTag.cs | 95 +++++++++++++++++++ .../Invalid/InvalidDecimalsForDateTime.xml | 12 +++ .../Samples/Validate/Valid/Valid.xml | 12 +++ 6 files changed, 269 insertions(+) create mode 100644 Protocol/Error Messages/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs create mode 100644 Protocol/Tests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs create mode 100644 ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/CheckDecimalsTag.cs create mode 100644 ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Invalid/InvalidDecimalsForDateTime.xml create mode 100644 ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Valid/Valid.xml diff --git a/Protocol/Error Messages/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs b/Protocol/Error Messages/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs new file mode 100644 index 0000000..aad81c9 --- /dev/null +++ b/Protocol/Error Messages/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs @@ -0,0 +1,56 @@ +// This is auto-generated code by Validator Management Tool. Do not modify. +namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.Params.Param.Display.Decimals.CheckDecimalsTag +{ + using System; + using System.Collections.Generic; + + using Skyline.DataMiner.CICD.Models.Protocol.Read; + using Skyline.DataMiner.CICD.Validators.Common.Interfaces; + using Skyline.DataMiner.CICD.Validators.Common.Model; + using Skyline.DataMiner.CICD.Validators.Protocol.Common; + using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces; + + internal static class Error + { + public static IValidationResult InvalidDecimalsForDateTime(IValidate test, IReadable referenceNode, IReadable positionNode, string paramId) + { + return new ValidationResult + { + Test = test, + CheckId = CheckId.CheckDecimalsTag, + ErrorId = ErrorIds.InvalidDecimalsForDateTime, + FullId = "2.75.1", + Category = Category.Param, + Severity = Severity.Major, + Certainty = Certainty.Certain, + Source = Source.Validator, + FixImpact = FixImpact.NonBreaking, + GroupDescription = "", + Description = String.Format("Missing tag '{0}' with expected value '{1}' for {2} '{3}'.", "Display/Decimals", "8", "Param", paramId), + HowToFix = "Add the Protocol/Params/Param/Display/Decimals tag with value 8.", + ExampleCode = "" + Environment.NewLine + " true" + Environment.NewLine + " 8" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + " number" + Environment.NewLine + "" + Environment.NewLine + "", + Details = "By default, only 6 decimals are saved in memory. Parameters holding datetime values need at least 8 decimals to be accurate." + Environment.NewLine + "Otherwise, there might be rounding issues when retrieving the parameter from an external source like an Automation script." + Environment.NewLine + "", + HasCodeFix = false, + + PositionNode = positionNode, + ReferenceNode = referenceNode, + }; + } + } + + internal static class ErrorIds + { + public const uint InvalidDecimalsForDateTime = 1; + } + + /// + /// Contains the identifiers of the checks. + /// + public static class CheckId + { + /// + /// The check identifier. + /// + public const uint CheckDecimalsTag = 75; + } +} \ No newline at end of file diff --git a/Protocol/ErrorMessages.xml b/Protocol/ErrorMessages.xml index b5df2b7..177e7d8 100644 --- a/Protocol/ErrorMessages.xml +++ b/Protocol/ErrorMessages.xml @@ -9643,6 +9643,31 @@ + + CheckDecimalsTag + + + InvalidDecimalsForDateTime + + + + tagName + expectedValue + itemKind + paramId + + + Major + Certain + Validator + NonBreaking + False + + [NewLine] true[NewLine] 8[NewLine][NewLine][NewLine] number[NewLine][NewLine]]]> +
+
+
+
diff --git a/Protocol/Tests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs b/Protocol/Tests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs new file mode 100644 index 0000000..2bb84b7 --- /dev/null +++ b/Protocol/Tests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs @@ -0,0 +1,69 @@ +namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.Params.Param.Display.Decimals.CheckDecimalsTag +{ + using System; + using System.Collections.Generic; + + using Skyline.DataMiner.CICD.Validators.Common.Interfaces; + using Skyline.DataMiner.CICD.Validators.Common.Model; + using Skyline.DataMiner.CICD.Validators.Protocol.Common; + using Skyline.DataMiner.CICD.Validators.Protocol.Common.Attributes; + using Skyline.DataMiner.CICD.Validators.Protocol.Common.Extensions; + using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces; + + [Test(CheckId.CheckDecimalsTag, Category.Param)] + internal class CheckDecimalsTag : IValidate/*, ICodeFix, ICompare*/ + { + // Please comment out the interfaces that aren't used together with the respective methods. + + public List Validate(ValidatorContext context) + { + List results = new List(); + + foreach (var param in context.EachParamWithValidId()) + { + // Early Return pattern. Only check number types. + if (!param.IsNumber()) continue; + + // Only check if there are options. + var allOptions = param.Measurement?.Type?.Options?.Value?.Split(';'); + if (allOptions == null) continue; + + // Is there an option involving date or datetime? + List possibleLowerCaseDateSyntax = new List() { "date", "datetime", "datetime:minute" }; + bool foundDateTime = Array.Exists(allOptions, option => possibleLowerCaseDateSyntax.Contains(option.ToLower())); + + // Verify valid decimals. + var decimalsTag = param.Display?.Decimals; + if (foundDateTime && decimalsTag?.Value != 8) + { + results.Add(Error.InvalidDecimalsForDateTime(this, param, decimalsTag, param.Id.RawValue)); + } + } + + return results; + } + + + ////public ICodeFixResult Fix(CodeFixContext context) + ////{ + //// CodeFixResult result = new CodeFixResult(); + + //// switch (context.Result.ErrorId) + //// { + + //// default: + //// result.Message = $"This error ({context.Result.ErrorId}) isn't implemented."; + //// break; + //// } + + //// return result; + ////} + + ////public List Compare(MajorChangeCheckContext context) + ////{ + //// List results = new List(); + + //// return results; + ////} + } +} \ No newline at end of file diff --git a/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/CheckDecimalsTag.cs b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/CheckDecimalsTag.cs new file mode 100644 index 0000000..4f34e4a --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/CheckDecimalsTag.cs @@ -0,0 +1,95 @@ +namespace ProtocolTests.Protocol.Params.Param.Display.Decimals.CheckDecimalsTag +{ + using System; + using System.Collections.Generic; + + using FluentAssertions; + + using Microsoft.VisualStudio.TestTools.UnitTesting; + + using Skyline.DataMiner.CICD.Validators.Common.Interfaces; + using Skyline.DataMiner.CICD.Validators.Common.Model; + using Skyline.DataMiner.CICD.Validators.Protocol.Common; + using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces; + using Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.Params.Param.Display.Decimals.CheckDecimalsTag; + + [TestClass] + public class Validate + { + private readonly IValidate check = new CheckDecimalsTag(); + + #region Valid Checks + + [TestMethod] + public void Param_CheckDecimalsTag_Valid() + { + Generic.ValidateData data = new Generic.ValidateData + { + TestType = Generic.TestType.Valid, + FileName = "Valid", + ExpectedResults = new List() + }; + + Generic.Validate(check, data); + } + + #endregion + + #region Invalid Checks + + [TestMethod] + public void Param_CheckDecimalsTag_InvalidDecimalsForDateTime() + { + Generic.ValidateData data = new Generic.ValidateData + { + TestType = Generic.TestType.Invalid, + FileName = "InvalidDecimalsForDateTime", + ExpectedResults = new List + { + Error.InvalidDecimalsForDateTime(null, null, null, "10"), + } + }; + + Generic.Validate(check, data); + } + + #endregion + } + + [TestClass] + public class ErrorMessages + { + [TestMethod] + public void Param_CheckDecimalsTag_InvalidDecimalsForDateTime() + { + // Create ErrorMessage + var message = Error.InvalidDecimalsForDateTime(null, null, null, "paramId"); + + var expected = new ValidationResult + { + Severity = Severity.Major, + Certainty = Certainty.Certain, + FixImpact = FixImpact.NonBreaking, + GroupDescription = "", + Description = "Missing tag 'Display/Decimals' with expected value '8' for Param 'paramId'.", + Details = "By default, only 6 decimals are saved in memory. Parameters holding datetime values need at least 8 decimals to be accurate." + Environment.NewLine + "Otherwise, there might be rounding issues when retrieving the parameter from an external source like an Automation script." + Environment.NewLine + "", + HasCodeFix = false, + }; + + // Assert + message.Should().BeEquivalentTo(expected, Generic.ExcludePropertiesForErrorMessages); + } + } + + [TestClass] + public class Attribute + { + private readonly IRoot check = new CheckDecimalsTag(); + + [TestMethod] + public void Param_CheckDecimalsTag_CheckCategory() => Generic.CheckCategory(check, Category.Param); + + [TestMethod] + public void Param_CheckDecimalsTag_CheckId() => Generic.CheckId(check, CheckId.CheckDecimalsTag); + } +} \ No newline at end of file diff --git a/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Invalid/InvalidDecimalsForDateTime.xml b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Invalid/InvalidDecimalsForDateTime.xml new file mode 100644 index 0000000..8fc6541 --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Invalid/InvalidDecimalsForDateTime.xml @@ -0,0 +1,12 @@ + + + + + true + + + number + + + + \ No newline at end of file diff --git a/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Valid/Valid.xml b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Valid/Valid.xml new file mode 100644 index 0000000..a9490bd --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Valid/Valid.xml @@ -0,0 +1,12 @@ + + + + + 8 + + + number + + + + \ No newline at end of file From fefa1701b28f3c2ff5a40d82c7e0259e3101e151 Mon Sep 17 00:00:00 2001 From: Michiel Oda <97458010+MichielOda@users.noreply.github.com> Date: Fri, 8 Mar 2024 14:07:06 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9191886..aba7a83 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,7 @@ This will inform the owners that you suggested additions and changes. Those can For this exercise, this will trigger an automatic pipeline that closes your pull request and forwards the results to Skyline. + > [!NOTE] > Skyline will review your submission. Upon successful review, you will be awarded the appropriate DevOps points as a token of your accomplishment.