diff --git a/src/InterAppConnector/CommandManager.cs b/src/InterAppConnector/CommandManager.cs index e64138c..bc4a57c 100644 --- a/src/InterAppConnector/CommandManager.cs +++ b/src/InterAppConnector/CommandManager.cs @@ -225,7 +225,21 @@ where method.GetCustomAttribute() != null where methood.Name == "ValidateValue" select methood).First(); - if (!((bool)validateInputMethod.Invoke(constructor, new[] { _arguments[selectedCommand].Arguments[findArgument.Name].Value })!)) + bool customValidationErrorMessageMissing = false; + + try + { + if (!((bool)validateInputMethod.Invoke(constructor, new[] { _arguments[selectedCommand].Arguments[findArgument.Name].Value })!)) + { + customValidationErrorMessageMissing = true; + } + } + catch (Exception exc) + { + throw new ArgumentException("The value provided to argument " + item.Name + " is not acceptable. Reason: " + exc.GetBaseException().Message, item.Name, exc.InnerException); + } + + if (customValidationErrorMessageMissing) { throw new ArgumentException("The value provided to argument " + item.Name + " is not valid according to the validation procedure"); } diff --git a/tests/InterAppConnector.Test.Library/CommandManagerTest.cs b/tests/InterAppConnector.Test.Library/CommandManagerTest.cs index cc5d3fb..f846325 100644 --- a/tests/InterAppConnector.Test.Library/CommandManagerTest.cs +++ b/tests/InterAppConnector.Test.Library/CommandManagerTest.cs @@ -188,6 +188,22 @@ public void SetArgument_ParameterWithSingleAlias_ReturnArgumentSet() Assert.That(((LicensePlate) manager._arguments[typeof(SetArgumentCommand).FullName].Arguments["plate"].Value).Plate, Is.EqualTo("abcd1234")); } + [Test] + public void SetArgument_WithCustomMessageValidatorError_ReturnArgumentSet() + { + Argument arguments = Argument.Parse(new[] { "setargument", "-validatedvalue", "120" }, "-"); + CommandManager manager = new CommandManager(); + manager.AddCommand(); + + Action wrongAction = () => + { + manager.SetArguments(new List(new[] { "setargument" }), arguments.Arguments.Values.ToList()); + }; + + Assert.That(wrongAction, Throws.ArgumentException + .And.Message.Contain("The age must be between 0 and 100. For instance, a valid value is 25")); + } + [Test] public void SetArgument_WithValidatorNumberAndValueInRange_ReturnArgumentSet() { @@ -224,8 +240,10 @@ public void SetArgument_WithValidatorAndCustomInputStringWrongValue_ReturnArgume manager.SetArguments(new List(new[] { "setargument" }), arguments.Arguments.Values.ToList()); }; - Assert.That(wrongAction, Throws.ArgumentException); + Assert.That(wrongAction, Throws.ArgumentException + .And.Message.Contains("The value provided to argument birthdate is not valid according to the validation procedure")); } + [TestCase("validatedguid")] [TestCase("validateotherguid")] public void SetArgument_WithValidatorNumberCustomInputStringAndValueInRange_ReturnArgumentSet(string argument) diff --git a/tests/InterAppConnector.Test.Library/Commands/ValidatedValueTypeCommand.cs b/tests/InterAppConnector.Test.Library/Commands/ValidatedValueTypeCommand.cs new file mode 100644 index 0000000..080b602 --- /dev/null +++ b/tests/InterAppConnector.Test.Library/Commands/ValidatedValueTypeCommand.cs @@ -0,0 +1,15 @@ +using InterAppConnector.Attributes; +using InterAppConnector.Interfaces; +using InterAppConnector.Test.Library.DataModels; + +namespace InterAppConnector.Test.Library.Commands +{ + [Command("validatedvaluetype")] + public class ValidatedValueTypeCommand : ICommand + { + public string Main(ValidatedValueTypeDataModel arguments) + { + return CommandOutput.Ok(arguments.Age); + } + } +} diff --git a/tests/InterAppConnector.Test.Library/DataModels/SetArgumentMethodDataModel.cs b/tests/InterAppConnector.Test.Library/DataModels/SetArgumentMethodDataModel.cs index 7e0d580..0853640 100644 --- a/tests/InterAppConnector.Test.Library/DataModels/SetArgumentMethodDataModel.cs +++ b/tests/InterAppConnector.Test.Library/DataModels/SetArgumentMethodDataModel.cs @@ -79,6 +79,9 @@ public class SetArgumentMethodDataModel [CustomInputString("ddMMyyyy")] public DateTime BirthDate { get; set; } + [ValueValidator(typeof(AgeValidatorWithCustomErrorMessageValidator))] + public uint ValidatedValue { get; set; } + /* * To check. There may be a bug here * [ValueValidator(typeof(CustomStringClassvalidator))] diff --git a/tests/InterAppConnector.Test.Library/DataModels/ValidatedValueTypeDataModel.cs b/tests/InterAppConnector.Test.Library/DataModels/ValidatedValueTypeDataModel.cs new file mode 100644 index 0000000..70eaee4 --- /dev/null +++ b/tests/InterAppConnector.Test.Library/DataModels/ValidatedValueTypeDataModel.cs @@ -0,0 +1,15 @@ +using InterAppConnector.Attributes; +using InterAppConnector.Test.Library.Validators; + +namespace InterAppConnector.Test.Library.DataModels +{ + public class ValidatedValueTypeDataModel + { + [ValueValidator(typeof(AgeValidatorWithCustomErrorMessageValidator))] + public uint Age { get; set; } + + [ValueValidator(typeof(BirthDateValidator))] + [CustomInputString("ddMMyyyy")] + public DateTime? OptionalDate { get; set; } + } +} diff --git a/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs b/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs index 8055a90..82ff962 100644 --- a/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs +++ b/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs @@ -190,6 +190,46 @@ public void ExecuteAsBatch_WithMissingOptionalValueTypeArguments_ReturnValue() Assert.That(returnedValue, Is.EqualTo(5)); } + [Test] + public void ExecuteAsBatch_WithMissingValidatedOptionalValueTypeArguments_ReturnNoErrors() + { + CommandManager command = new CommandManager(); + command.AddCommand(); + dynamic dynamic = new ExpandoObject(); + dynamic.Age = 28; + int returnedValue = 0; + + Action connectorAction = () => + { + InterAppCommunication connector = new InterAppCommunication(command); + CommandResult commandExecution = connector.ExecuteAsBatch("validatedvaluetype", dynamic); + returnedValue = commandExecution.Message; + }; + + Assert.That(connectorAction, Throws.Nothing); + Assert.That(returnedValue, Is.EqualTo(28)); + } + + [Test] + public void ExecuteAsBatch_WithWrongOptionalValueTypeArguments_ReturnArgumetnExceptionError() + { + CommandManager command = new CommandManager(); + command.AddCommand(); + dynamic dynamic = new ExpandoObject(); + dynamic.Age = 28; + dynamic.OptionalDate = "12081800"; + int returnedValue = 0; + + Action connectorAction = () => + { + InterAppCommunication connector = new InterAppCommunication(command); + CommandResult commandExecution = connector.ExecuteAsBatch("validatedvaluetype", dynamic); + returnedValue = commandExecution.Message; + }; + + Assert.That(connectorAction, Throws.ArgumentException); + } + [Test] public void ExecuteAsBatch_WithAllValueTypeArgumentsSet_ReturnValue() { @@ -296,6 +336,40 @@ public void ExecuteAsInteractiveCLI_WithMissingOptionalValueTypeArguments_Return Assert.That(Environment.ExitCode, Is.EqualTo(0)); } + [Test] + public void ExecuteAsInteractiveCLI_WithMissingOptionalValueTypeArguments_ReturnError() + { + CommandManager command = new CommandManager(); + command.AddCommand(); + string[] arguments = "validatedvaluetype -age 30 -OptionalDate 12051800".Split(" "); + + Action connectorAction = () => + { + InterAppCommunication connector = new InterAppCommunication(command); + connector.ExecuteAsInteractiveCLI(arguments); + }; + + Assert.That(connectorAction, Throws.Nothing); + Assert.That(Environment.ExitCode, Is.EqualTo(3)); + } + + [Test] + public void ExecuteAsInteractiveCLI_WithMissingValidatedOptionalValueTypeArguments_ReturnValue() + { + CommandManager command = new CommandManager(); + command.AddCommand(); + string[] arguments = "validatedvaluetype -age 30".Split(" "); + + Action connectorAction = () => + { + InterAppCommunication connector = new InterAppCommunication(command); + connector.ExecuteAsInteractiveCLI(arguments); + }; + + Assert.That(connectorAction, Throws.Nothing); + Assert.That(Environment.ExitCode, Is.EqualTo(0)); + } + [Test] public void ExecuteAsInteractiveCLI_WithAllValueTypeArgumentsSet_ReturnValue() { diff --git a/tests/InterAppConnector.Test.Library/Validators/AgeValidatorWithCustomErrorMessageValidator.cs b/tests/InterAppConnector.Test.Library/Validators/AgeValidatorWithCustomErrorMessageValidator.cs new file mode 100644 index 0000000..51881c9 --- /dev/null +++ b/tests/InterAppConnector.Test.Library/Validators/AgeValidatorWithCustomErrorMessageValidator.cs @@ -0,0 +1,25 @@ +using InterAppConnector.Interfaces; + +namespace InterAppConnector.Test.Library.Validators +{ + public class AgeValidatorWithCustomErrorMessageValidator : IValueValidator + { + public object GetSampleValidValue() + { + return 25; + } + + public bool ValidateValue(object value) + { + bool validated = true; + uint number = (uint)value; + + if (number > 100) + { + throw new ArgumentException("The age must be between 0 and 100. For instance, a valid value is " + GetSampleValidValue()); + } + + return validated; + } + } +}