From 844282938076811492753a641d69fe36c426c074 Mon Sep 17 00:00:00 2001 From: Edib Supic <142238909+EdibSU@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:18:52 +0200 Subject: [PATCH] Added check for use of exceptions tag with write params. (#40) * Added check for use of exceptions tag with write params. * Moved check for parameter type on parameters with exceptions to CheckExceptionsTag * Reordered CheckExceptionsTag * Moved ErrorMessage for write param type combination with exceptions tag to CheckExceptionsTag in ErrorMessages.xml * Updated FullId of Error.ExceptionIncompatibleWithParamType * Update ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/CheckExceptionsTag.cs Co-authored-by: Michiel Oda <97458010+MichielOda@users.noreply.github.com> --------- Co-authored-by: Michiel Oda <97458010+MichielOda@users.noreply.github.com> --- .../Exceptions/CheckExceptionsTag.cs | 29 ++++++++ Protocol/ErrorMessages.xml | 19 +++++ .../Exceptions/CheckExceptionsTag.cs | 17 ++++- .../CheckExceptionsTag/CheckExceptionsTag.cs | 72 +++++++++++++++++++ .../ExceptionIncompatibleWithParamType.xml | 18 +++++ .../Samples/Validate/Valid/Valid.xml | 10 +++ 6 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Invalid/ExceptionIncompatibleWithParamType.xml create mode 100644 ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Valid/Valid.xml diff --git a/Protocol/Error Messages/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs b/Protocol/Error Messages/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs index 96a26c9f..da05361c 100644 --- a/Protocol/Error Messages/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs +++ b/Protocol/Error Messages/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs @@ -89,11 +89,40 @@ public static IValidationResult AddedException(IReadable referenceNode, IReadabl } } + internal static class Error + { + public static IValidationResult ExceptionIncompatibleWithParamType(IValidate test, IReadable referenceNode, IReadable positionNode, string paramType, string paramId) + { + return new ValidationResult + { + Test = test, + CheckId = CheckId.CheckExceptionsTag, + ErrorId = ErrorIds.ExceptionIncompatibleWithParamType, + FullId = "2.36.4", + Category = Category.Param, + Severity = Severity.Minor, + Certainty = Certainty.Certain, + Source = Source.Validator, + FixImpact = FixImpact.NonBreaking, + GroupDescription = "", + Description = String.Format("Interprete/Exceptions is incompatible with Param/Type '{0}'. Param ID '{1}'.", paramType, paramId), + HowToFix = "Use Measurement.Discreets.Discreet tags.", + ExampleCode = "", + Details = "Do not use Exception tags to add exceptions to write parameters.", + HasCodeFix = false, + + PositionNode = positionNode, + ReferenceNode = referenceNode, + }; + } + } + internal static class ErrorIds { public const uint UpdatedExceptionValueTag = 1; public const uint RemovedException = 2; public const uint AddedException = 3; + public const uint ExceptionIncompatibleWithParamType = 4; } /// diff --git a/Protocol/ErrorMessages.xml b/Protocol/ErrorMessages.xml index 2b974a15..93bd3cbe 100644 --- a/Protocol/ErrorMessages.xml +++ b/Protocol/ErrorMessages.xml @@ -6322,6 +6322,25 @@
+ + ExceptionIncompatibleWithParamType + + + Interprete/Exceptions is incompatible with Param/Type '{0}'. Param ID '{1}'. + + paramType + paramId + + + Minor + Certain + Validator + NonBreaking + False + + +
+
diff --git a/Protocol/Tests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs b/Protocol/Tests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs index 26b29b7c..dccf85f9 100644 --- a/Protocol/Tests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs +++ b/Protocol/Tests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag.cs @@ -13,8 +13,23 @@ namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.Params.Param using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces; [Test(CheckId.CheckExceptionsTag, Category.Param)] - internal class CheckExceptionsTag : ICompare + internal class CheckExceptionsTag : IValidate, ICompare { + public List Validate(ValidatorContext context) + { + List results = new List(); + + foreach (var param in context.EachParamWithValidId()) + { + if (param.IsWrite() && param.Interprete?.Exceptions != null) + { + results.Add(Error.ExceptionIncompatibleWithParamType(this, param, param.Interprete.Exceptions, param.Type.RawValue, param.Id.RawValue)); + } + } + + return results; + } + public List Compare(MajorChangeCheckContext context) { var results = new List(); diff --git a/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/CheckExceptionsTag.cs b/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/CheckExceptionsTag.cs index 283a7136..5a303325 100644 --- a/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/CheckExceptionsTag.cs +++ b/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/CheckExceptionsTag.cs @@ -1,14 +1,61 @@ namespace ProtocolTests.Protocol.Params.Param.Interprete.Exceptions.CheckExceptionsTag { + 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.Interprete.Exceptions.CheckExceptionsTag; + [TestClass] + public class Validate + { + private readonly IValidate check = new CheckExceptionsTag(); + + #region Valid Checks + + [TestMethod] + public void Param_CheckExceptionsTag_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_CheckExceptionsTag_Type() + { + Generic.ValidateData data = new Generic.ValidateData + { + TestType = Generic.TestType.Invalid, + FileName = "ExceptionIncompatibleWithParamType", + ExpectedResults = new List + { + Error.ExceptionIncompatibleWithParamType(null, null, null, "write", "1"), + Error.ExceptionIncompatibleWithParamType(null, null, null, "write bit", "2"), + } + }; + + Generic.Validate(check, data); + } + + #endregion + } + [TestClass] public class Compare { @@ -92,6 +139,31 @@ public void Param_CheckExceptionsTag_UpdatedExceptionValueTag() #endregion } + [TestClass] + public class ErrorMessages + { + [TestMethod] + public void Param_CheckExceptionsTag_ExceptionIncompatibleWithParamType() + { + // Create ErrorMessage + var message = Error.ExceptionIncompatibleWithParamType(null, null, null, "paramType", "paramId"); + + var expected = new ValidationResult + { + Severity = Severity.Minor, + Certainty = Certainty.Certain, + FixImpact = FixImpact.NonBreaking, + GroupDescription = "", + Description = "Interprete/Exceptions is incompatible with Param/Type 'paramType'. Param ID 'paramId'.", + Details = "Do not use Exception tags to add exceptions to write parameters.", + HasCodeFix = false, + }; + + // Assert + message.Should().BeEquivalentTo(expected, Generic.ExcludePropertiesForErrorMessages); + } + } + [TestClass] public class Attribute { diff --git a/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Invalid/ExceptionIncompatibleWithParamType.xml b/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Invalid/ExceptionIncompatibleWithParamType.xml new file mode 100644 index 00000000..a6c66413 --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Invalid/ExceptionIncompatibleWithParamType.xml @@ -0,0 +1,18 @@ + + + + write + + + + + + + write bit + + + + + + + \ No newline at end of file diff --git a/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Valid/Valid.xml b/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Valid/Valid.xml new file mode 100644 index 00000000..2c7eb9c6 --- /dev/null +++ b/ProtocolTests/Protocol/Params/Param/Interprete/Exceptions/CheckExceptionsTag/Samples/Validate/Valid/Valid.xml @@ -0,0 +1,10 @@ + + + + write + + + write bit + + + \ No newline at end of file