From c50ca529cd2c0e9fa437d006c479380260b8be76 Mon Sep 17 00:00:00 2001 From: TimothyVanPoucke Date: Tue, 26 Mar 2024 10:03:11 +0100 Subject: [PATCH] Added the Decimals check on datetime parameters --- .../Display/Decimals/CheckDecimalsTag.cs | 56 +++++++++++ Protocol/ErrorMessages.xml | 25 +++++ .../Display/Decimals/CheckDecimalsTag.cs | 70 ++++++++++++++ .../CheckDecimalsTag/CheckDecimalsTag.cs | 95 +++++++++++++++++++ .../Invalid/InvalidTagForDateTime.xml | 12 +++ .../Samples/Validate/Valid/Valid.xml | 13 +++ 6 files changed, 271 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/InvalidTagForDateTime.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..9af5d83 --- /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 InvalidTagForDateTime(IValidate test, IReadable referenceNode, IReadable positionNode, string paramId) + { + return new ValidationResult + { + Test = test, + CheckId = CheckId.CheckDecimalsTag, + ErrorId = ErrorIds.InvalidTagForDateTime, + 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 a Protocol/Param/Display/Decimals tag with value8.", + ExampleCode = "" + Environment.NewLine + " true" + Environment.NewLine + " 8" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + " number" + 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.", + HasCodeFix = false, + + PositionNode = positionNode, + ReferenceNode = referenceNode, + }; + } + } + + internal static class ErrorIds + { + public const uint InvalidTagForDateTime = 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..c7416bf 100644 --- a/Protocol/ErrorMessages.xml +++ b/Protocol/ErrorMessages.xml @@ -9643,6 +9643,31 @@ + + CheckDecimalsTag + + + InvalidTagForDateTime + + + + tagName + expectedValue + itemKind + paramId + + + Major + Certain + Validator + NonBreaking + False + + [NewLine] true[NewLine] 8[NewLine][NewLine][NewLine] number[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..1129913 --- /dev/null +++ b/Protocol/Tests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag.cs @@ -0,0 +1,70 @@ +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.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()) + { + var displayTag = param.Display; + + // Early return pattern. Only check when there is a Display tag. + if (displayTag == null) continue; + + // Only check number types. + if (!param.IsNumber()) continue; + + // Only check if date or datetime parameter + if (!param.IsDateTime()) continue; + + // Verify valid decimals. + var decimalsTag = param.Display?.Decimals; + if (decimalsTag?.Value != 8) + { + var positionNode = decimalsTag ?? (IReadable)displayTag; + results.Add(Error.InvalidTagForDateTime(this, param, positionNode, 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..39ab3d2 --- /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_InvalidTagForDateTime() + { + Generic.ValidateData data = new Generic.ValidateData + { + TestType = Generic.TestType.Invalid, + FileName = "InvalidTagForDateTime", + ExpectedResults = new List + { + Error.InvalidTagForDateTime(null, null, null, "10"), + } + }; + + Generic.Validate(check, data); + } + + #endregion + } + + [TestClass] + public class ErrorMessages + { + [TestMethod] + public void Param_CheckDecimalsTag_InvalidTagForDateTime() + { + // Create ErrorMessage + var message = Error.InvalidTagForDateTime(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.", + 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/InvalidTagForDateTime.xml b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Invalid/InvalidTagForDateTime.xml new file mode 100644 index 0000000..8fc6541 --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Invalid/InvalidTagForDateTime.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..0ec63e1 --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Display/Decimals/CheckDecimalsTag/Samples/Validate/Valid/Valid.xml @@ -0,0 +1,13 @@ + + + + + true + 8 + + + number + + + + \ No newline at end of file