public void OptionBuilder_CreateOption_Command_Found_No_SubCommands() { string expectedCommand = "Storage"; int expectedNumberOfOptions = 0; string environmentSetting = $"{{\"UseLiveClient\": \"True\"}}"; NullLogger <Microsoft.Extensions.Logging.ILogger> logger = new NullLogger <Microsoft.Extensions.Logging.ILogger>(); BuildEnvironment(environmentSetting); CfgCommand command = TestDataBuilder.CreateCfgCommand(expectedCommand); CfgCommandList commandList = new CfgCommandList(); commandList.Commands = new List <CfgCommand>(); commandList.Commands.Add(command); OptionBuilder builder = OptionBuilder.Instance(logger); builder.CfgCommands = commandList; GetDataHandler handler = new GetDataHandler(new NullLogger <GetDataHandler>()); List <IOption> selectableOptions = builder.BuildAvailableOptions(handler); Assert.AreEqual(expectedNumberOfOptions, selectableOptions.Count); }
public void OptionBuilder_CreateOption_Command_Found_One_Option() { string expectedCommand = "Storage"; string expectedSubCommand = "GetData"; string expectedOption = "TestOption"; string expectedDataType = "string"; string expectedDescription = "Option test description"; string expectedApiName = "TestApiName"; int expectedNumberOfOptions = 1; string environmentSetting = $"{{\"UseLiveClient\": \"True\"}}"; NullLogger <Microsoft.Extensions.Logging.ILogger> logger = new NullLogger <Microsoft.Extensions.Logging.ILogger>(); BuildEnvironment(environmentSetting); CfgCommand command = TestDataBuilder.CreateCfgCommand(expectedCommand); CfgSubCommand subCommand = TestDataBuilder.CreateCfgSubCommand(expectedSubCommand); command.SubCommands.Add(subCommand); CfgOption option = TestDataBuilder.CreateCfgOption(expectedOption, expectedDataType, expectedDescription, expectedApiName); subCommand.Options.Add(option); CfgCommandList commandList = new CfgCommandList(); commandList.Commands = new List <CfgCommand>(); commandList.Commands.Add(command); OptionBuilder builder = OptionBuilder.Instance(logger); builder.CfgCommands = commandList; GetDataHandler handler = new GetDataHandler(new NullLogger <GetDataHandler>()); List <IOption> selectableOptions = builder.BuildAvailableOptions(handler); Assert.AreEqual(expectedNumberOfOptions, selectableOptions.Count); IOption selectableOption = selectableOptions[0]; Assert.AreEqual(expectedOption, selectableOption.Name); Assert.AreEqual(expectedDescription, selectableOption.Description); Assert.AreEqual(expectedApiName, selectableOption.ApiName); Assert.IsFalse(selectableOption.IsAssigned); Assert.IsInstanceOfType(selectableOption, typeof(NumberOption <string>)); }
public void OptionBuilder_CreateOption_No_Commands() { int expectedNumberOfOptions = 0; string environmentSetting = $"{{\"UseLiveClient\": \"True\"}}"; NullLogger <Microsoft.Extensions.Logging.ILogger> logger = new NullLogger <Microsoft.Extensions.Logging.ILogger>(); BuildEnvironment(environmentSetting); // Not commands in the Log CfgCommandList commandList = new CfgCommandList(); OptionBuilder builder = OptionBuilder.Instance(logger); builder.CfgCommands = commandList; GetDataHandler handler = new GetDataHandler(new NullLogger <GetDataHandler>()); List <IOption> selectableOptions = builder.BuildAvailableOptions(handler); Assert.AreEqual(expectedNumberOfOptions, selectableOptions.Count); }
public void Storage_UploadData__File_Exits_File_Uploaded() { string expectedOwnerId = "ownerId"; string expectedOwnerIdValue = "50013748"; string expectedInstanceId = "instanceId"; string expectedInstanceIdValue = "bd5d5066-5ae4-42eb-8d5d-076a600acdd5"; string expectedElementType = "elementType"; string expectedElementTypeValue = "kvittering"; string expectedFile = "file"; string expectedFileValue = @"c:\Temp\test.xml"; int expectedLogEntries = 1; string expectedFileUploadedMessage = "was successfully uploaded"; string environmentSetting = "{\"UseLiveClient\": \"false\"}"; NullLogger <OptionBuilder> logger = new NullLogger <OptionBuilder>(); TextWriter textWriter = new StringWriter(); ConfigureLogging(textWriter); BuildEnvironment(environmentSetting); Dictionary <string, string> cliOptions = new Dictionary <string, string>(); cliOptions.Add(expectedOwnerId, expectedOwnerIdValue); cliOptions.Add(expectedInstanceId, expectedInstanceIdValue); cliOptions.Add(expectedElementType, expectedElementTypeValue); cliOptions.Add(expectedFile, expectedFileValue); // Define which command and sub command that shall be registered in service provider List <Type> availableCommandTypes = new List <Type>(); availableCommandTypes.Add(typeof(StorageCommand)); List <Type> availableSubCommands = new List <Type>(); availableSubCommands.Add(typeof(UploadData)); // Register commands and sub commands ServiceProvider serviceProvider = TestDataBuilder.BuildServiceProvider(availableCommandTypes, availableSubCommands, Log.Logger); // Fetch UploadData subCommand and assign available options by use og OptionBuilder. Options for the command is fetched from // the Command resource file defined in the Cli project var sssList = serviceProvider.GetServices <ISubCommandHandler>().ToList(); ISubCommandHandler subCommandHandler = sssList.First(x => x.Name == "UploadData"); OptionBuilder builder = OptionBuilder.Instance(logger); subCommandHandler.SelectableCliOptions = builder.BuildAvailableOptions(subCommandHandler); subCommandHandler.DictOptions = cliOptions; // Need to mock the FileOption to avoid dependency to disk, so replace the registered option with a mock option FileOption <FileStream> mockFileOption = Mock.Of <FileOption <FileStream> >(x => x.Validate()); ReplaceSelectableOption("file", mockFileOption, subCommandHandler.SelectableCliOptions); // Mock the file wrapper Mock <IFileWrapper> mockedWrapper = new Mock <IFileWrapper>(); mockedWrapper.Setup(x => x.GetFile(It.IsAny <string>())).Returns(new MemoryStream()); subCommandHandler.CliFileWrapper = mockedWrapper.Object; // Set response from ClientWrapper StorageClientFileWrapper.IsSuccessStatusCode = true; // Assign the input options to the subCommand subCommandHandler.DictOptions = cliOptions; // Assign and validate the input options to the selectable options builder.AssignValueToCliOptions(subCommandHandler); // Run the command subCommandHandler.Run(); // Verify that the log contain expected result List <string> logEntries = GetLogEntries(textWriter); Assert.AreEqual(expectedLogEntries, logEntries.Count); string logMessage = logEntries.FirstOrDefault(x => x.Contains(expectedFileUploadedMessage)); Assert.IsFalse(string.IsNullOrEmpty(logMessage)); Assert.IsTrue(logMessage.Contains(expectedFileValue)); textWriter.Dispose(); builder.CfgCommands = null; }
public void Storage_UploadData_No_File() { string expectedOwnerId = "ownerId"; string expectedOwnerIdValue = "50013748"; string expectedInstanceId = "instanceId"; string expectedInstanceIdValue = "bd5d5066-5ae4-42eb-8d5d-076a600acdd5"; string expectedElementType = "elementType"; string expectedElementTypeValue = "kvittering"; string expectedFile = "file"; string expectedFileValue = @"c:\Temp\test.xml"; string expectedLogMessage = "No valid combination"; int expectedLogEntries = 2; string expectedFileNotFoundErrorMessage = "Invalid value for parameter"; string environmentSetting = "{\"UseLiveClient\": \"false\"}"; NullLogger <OptionBuilder> logger = new NullLogger <OptionBuilder>(); // Configure logger which is set on registered classes/objects TextWriter textWriter = new StringWriter(); ConfigureLogging(textWriter); BuildEnvironment(environmentSetting); // Build command options Dictionary <string, string> cliOptions = new Dictionary <string, string>(); cliOptions.Add(expectedOwnerId, expectedOwnerIdValue); cliOptions.Add(expectedInstanceId, expectedInstanceIdValue); cliOptions.Add(expectedElementType, expectedElementTypeValue); cliOptions.Add(expectedFile, expectedFileValue); // Define which command and sub command that shall be registered in service provider List <Type> availableCommandTypes = new List <Type>(); availableCommandTypes.Add(typeof(StorageCommand)); List <Type> availableSubCommands = new List <Type>(); availableSubCommands.Add(typeof(UploadData)); // Register commands and sub commands ServiceProvider serviceProvider = TestDataBuilder.BuildServiceProvider(availableCommandTypes, availableSubCommands, Log.Logger); // Fetch UploadData subCommand and assign available options by use og OptionBuilder. Options for the command is fetched from // the Command resource file defined in the Cli project var sssList = serviceProvider.GetServices <ISubCommandHandler>().ToList(); ISubCommandHandler subCommandHandler = sssList.First(x => x.Name == "UploadData"); OptionBuilder builder = OptionBuilder.Instance(logger); subCommandHandler.SelectableCliOptions = builder.BuildAvailableOptions(subCommandHandler); subCommandHandler.DictOptions = cliOptions; // Need to mock the FileOption to avoid dependency to disk, so replace the registered option with a mock option FileOption <FileStream> mockFileOption = Mock.Of <FileOption <FileStream> >(x => x.Validate() == false && x.ErrorMessage == expectedFileNotFoundErrorMessage); ReplaceSelectableOption("file", mockFileOption, subCommandHandler.SelectableCliOptions); // Assign the input options to the subCommand subCommandHandler.DictOptions = cliOptions; // Assign and validate the input options to the selectable options builder.AssignValueToCliOptions(subCommandHandler); // Run the command subCommandHandler.Run(); // Verify that the log contain expected result List <string> logEntries = GetLogEntries(textWriter); Assert.AreEqual(expectedLogEntries, logEntries.Count); string logMessage = logEntries.FirstOrDefault(x => x.Contains(expectedLogMessage)); Assert.IsFalse(string.IsNullOrEmpty(logMessage)); string fileNotFoundMessage = logEntries.FirstOrDefault(x => x.Contains(expectedFileNotFoundErrorMessage)); Assert.IsFalse(string.IsNullOrEmpty(fileNotFoundMessage)); textWriter.Dispose(); builder.CfgCommands = null; }
public void Storage_UploadData_Wrong_Option_Combination() { string expectedOption = "ownerId"; string expectedValue = "50013748"; string expectedLogMessage = "No valid combination"; int expectedLogEntries = 1; string environmentSetting = "{\"UseLiveClient\": \"false\"}"; NullLogger <OptionBuilder> logger = new NullLogger <OptionBuilder>(); // Configure logger which is set on registered classes/objects TextWriter textWriter = new StringWriter(); ConfigureLogging(textWriter); BuildEnvironment(environmentSetting); // Build command options Dictionary <string, string> cliOptions = new Dictionary <string, string>(); cliOptions.Add(expectedOption, expectedValue); // Define which command and sub command that shall be registered in service provider List <Type> availableCommandTypes = new List <Type>(); availableCommandTypes.Add(typeof(StorageCommand)); List <Type> availableSubCommands = new List <Type>(); availableSubCommands.Add(typeof(UploadData)); // Register commands and sub commands ServiceProvider serviceProvider = TestDataBuilder.BuildServiceProvider(availableCommandTypes, availableSubCommands, Log.Logger); // Fetch GetDataHandler subCommand and assign available options by use og OptionBuilder. Options for the command is fetched from // the Command resource file defined in the Cli project var sssList = serviceProvider.GetServices <ISubCommandHandler>().ToList(); ISubCommandHandler subCommandHandler = sssList.First(x => x.Name == "UploadData"); OptionBuilder builder = OptionBuilder.Instance(logger); subCommandHandler.SelectableCliOptions = builder.BuildAvailableOptions(subCommandHandler); subCommandHandler.DictOptions = cliOptions; // Assign option values to the sub command builder.AssignValueToCliOptions(subCommandHandler); // Run the command subCommandHandler.Run(); // Verify that the log contain expected result List <string> logEntries = GetLogEntries(textWriter); Assert.AreEqual(expectedLogEntries, logEntries.Count); string logMessage = logEntries.FirstOrDefault(x => x.Contains(expectedLogMessage)); Assert.IsFalse(string.IsNullOrEmpty(logMessage)); textWriter.Dispose(); builder.CfgCommands = null; }