From 1c95626c0efb7d3bea6263c1c282400b0f18f7af Mon Sep 17 00:00:00 2001 From: Giuseppe Cramarossa <16326537+gcramarossa@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:00:30 +0100 Subject: [PATCH] fix #19 boolean arguments are not managed properly --- src/InterAppConnector/CommandManager.cs | 13 +++++- src/InterAppConnector/CommandOutput.cs | 1 - src/InterAppConnector/CommandUtil.cs | 2 - .../CommandManagerTest.cs | 4 -- .../Commands/BooleanCommand.cs | 15 +++++++ .../DataModels/DataModelWithBoolean.cs | 8 ++++ .../InterAppCommunicationTest.cs | 40 +++++++++++++++++++ 7 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 tests/InterAppConnector.Test.Library/Commands/BooleanCommand.cs create mode 100644 tests/InterAppConnector.Test.Library/DataModels/DataModelWithBoolean.cs diff --git a/src/InterAppConnector/CommandManager.cs b/src/InterAppConnector/CommandManager.cs index 76ea625..7986b34 100644 --- a/src/InterAppConnector/CommandManager.cs +++ b/src/InterAppConnector/CommandManager.cs @@ -204,8 +204,17 @@ where method.GetCustomAttribute() != null } else { - throw new ArgumentException(); - } + if (parameterType == typeof(bool)) + { + _arguments[selectedCommand].Arguments[findArgument.Name].Value = Convert.ChangeType(item.Value, parameterType); + _parameterObject[selectedCommand].GetType().GetProperty(findArgument.OriginalPropertyName)!.SetValue(_parameterObject[selectedCommand], Convert.ChangeType(item.Value, parameterType)); + _arguments[selectedCommand].Arguments[findArgument.Name].IsSetByUser = true; + } + else + { + throw new ArgumentException(); + } + } } catch (Exception exc) { diff --git a/src/InterAppConnector/CommandOutput.cs b/src/InterAppConnector/CommandOutput.cs index c7bc504..27572f6 100644 --- a/src/InterAppConnector/CommandOutput.cs +++ b/src/InterAppConnector/CommandOutput.cs @@ -1,5 +1,4 @@ using System.Text.Json; -using System.Text.Json.Serialization; using InterAppConnector.DataModels; using InterAppConnector.Enumerations; using InterAppConnector.Exceptions; diff --git a/src/InterAppConnector/CommandUtil.cs b/src/InterAppConnector/CommandUtil.cs index e5ba111..8eebc3b 100644 --- a/src/InterAppConnector/CommandUtil.cs +++ b/src/InterAppConnector/CommandUtil.cs @@ -2,9 +2,7 @@ using InterAppConnector.DataModels; using InterAppConnector.Enumerations; using InterAppConnector.Interfaces; -using System; using System.Collections; -using System.Diagnostics; using System.Reflection; using System.Text; diff --git a/tests/InterAppConnector.Test.Library/CommandManagerTest.cs b/tests/InterAppConnector.Test.Library/CommandManagerTest.cs index 5ba1e74..84dc38d 100644 --- a/tests/InterAppConnector.Test.Library/CommandManagerTest.cs +++ b/tests/InterAppConnector.Test.Library/CommandManagerTest.cs @@ -376,10 +376,6 @@ public void SetArgument_WithAnotherValidatorCustomInputStringAndValueNotInRange_ [TestCase("abcd123a")] [TestCase("ab")] - // - [TestCase("")] - [TestCase("abcdd123")] - // public void SetArgument_ParameterWithWrongCustomString_ReturnArgumentException(string value) { Argument arguments = Argument.Parse(new[] { "setargument", "-plate", value }, "-"); diff --git a/tests/InterAppConnector.Test.Library/Commands/BooleanCommand.cs b/tests/InterAppConnector.Test.Library/Commands/BooleanCommand.cs new file mode 100644 index 0000000..b034706 --- /dev/null +++ b/tests/InterAppConnector.Test.Library/Commands/BooleanCommand.cs @@ -0,0 +1,15 @@ +using InterAppConnector.Attributes; +using InterAppConnector.Interfaces; +using InterAppConnector.Test.Library.DataModels; + +namespace InterAppConnector.Test.Library.Commands +{ + [Command("booleancommand")] + public class BooleanCommand : ICommand + { + public string Main(DataModelWithBoolean arguments) + { + return CommandOutput.Ok(arguments); + } + } +} diff --git a/tests/InterAppConnector.Test.Library/DataModels/DataModelWithBoolean.cs b/tests/InterAppConnector.Test.Library/DataModels/DataModelWithBoolean.cs new file mode 100644 index 0000000..722a1fd --- /dev/null +++ b/tests/InterAppConnector.Test.Library/DataModels/DataModelWithBoolean.cs @@ -0,0 +1,8 @@ +namespace InterAppConnector.Test.Library.DataModels +{ + public class DataModelWithBoolean + { + public bool MandatorySwitch { get; set; } + public int Number { get; set; } + } +} diff --git a/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs b/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs index 7c2d0de..de44b1a 100644 --- a/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs +++ b/tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs @@ -36,6 +36,29 @@ public void ExecuteAsBatch_ExecuteBatchTestCommandWithSuccessfulMessage_ReturnSu Assert.That(result.MessageStatus, Is.EqualTo(CommandExecutionMessageType.Success)); } + [Test] + public void ExecuteAsBatch_ExecuteBatchTestCommandWithBooleanSet_ReturnSuccessfulMessage() + { + CommandManager manager = new CommandManager(); + manager.AddCommand(true); + DataModelWithBoolean batchCommandDataModel = new DataModelWithBoolean + { + MandatorySwitch = true, + Number = 50 + }; + dynamic parameters = new ExpandoObject(); + parameters.MandatorySwitch = batchCommandDataModel.MandatorySwitch; + parameters.Number = 50; + + InterAppCommunication connector = new InterAppCommunication(manager); + CommandResult result = connector.ExecuteAsBatch("booleancommand", parameters); + + Assert.That(result, Is.Not.Null); + Assert.That(result.MessageStatus, Is.EqualTo(CommandExecutionMessageType.Success)); + Assert.That(result.Message.MandatorySwitch, Is.True); + Assert.That(result.Message.Number, Is.EqualTo(50)); + } + [Test] public void ExecuteAsBatch_ExecuteBatchTestCommandWithWarningMessage_ReturnWarningMessage() { @@ -320,6 +343,23 @@ public void ExecuteAsInteractiveCLI_WithMissingValueTypeArguments_ReturnMissingA Assert.That(Environment.ExitCode, Is.EqualTo(3)); } + [Test] + public void ExecuteAsInteractiveCLI_WithBooleanValue_ReturnNoErrorMessage() + { + CommandManager command = new CommandManager(); + command.AddCommand(); + string[] arguments = "booleancommand -mandatoryswitch -number 50".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_WithMissingOptionalValueTypeArguments_ReturnValue() {