Skip to content

Commit

Permalink
Merge pull request #18 from interappconnectorproject/feat-17-custom-m…
Browse files Browse the repository at this point in the history
…essage-validator

Feature #17 custom error message in custom argument validation added
  • Loading branch information
gcramarossa authored Dec 12, 2023
2 parents cf450a6 + 0d730d3 commit 90a47b8
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/InterAppConnector/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,21 @@ where method.GetCustomAttribute<CustomInputStringAttribute>() != 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");
}
Expand Down
20 changes: 19 additions & 1 deletion tests/InterAppConnector.Test.Library/CommandManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SetArgumentCommand, SetArgumentMethodDataModel>();

Action wrongAction = () =>
{
manager.SetArguments(new List<string>(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()
{
Expand Down Expand Up @@ -224,8 +240,10 @@ public void SetArgument_WithValidatorAndCustomInputStringWrongValue_ReturnArgume
manager.SetArguments(new List<string>(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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ValidatedValueTypeDataModel>
{
public string Main(ValidatedValueTypeDataModel arguments)
{
return CommandOutput.Ok(arguments.Age);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
74 changes: 74 additions & 0 deletions tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidatedValueTypeCommand, ValidatedValueTypeDataModel>();
dynamic dynamic = new ExpandoObject();
dynamic.Age = 28;
int returnedValue = 0;

Action connectorAction = () =>
{
InterAppCommunication connector = new InterAppCommunication(command);
CommandResult<int> commandExecution = connector.ExecuteAsBatch<int>("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<ValidatedValueTypeCommand, ValidatedValueTypeDataModel>();
dynamic dynamic = new ExpandoObject();
dynamic.Age = 28;
dynamic.OptionalDate = "12081800";
int returnedValue = 0;

Action connectorAction = () =>
{
InterAppCommunication connector = new InterAppCommunication(command);
CommandResult<int> commandExecution = connector.ExecuteAsBatch<int>("validatedvaluetype", dynamic);
returnedValue = commandExecution.Message;
};

Assert.That(connectorAction, Throws.ArgumentException);
}

[Test]
public void ExecuteAsBatch_WithAllValueTypeArgumentsSet_ReturnValue()
{
Expand Down Expand Up @@ -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<ValueTypeCommand, ValueTypeDataModel>();
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<ValidatedValueTypeCommand, ValidatedValueTypeDataModel>();
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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 90a47b8

Please sign in to comment.