Пример #1
0
        public void OptionBuilder_CreateOption_Command_Found_One_Option_AssignValue()
        {
            string expectedOption      = "TestOption";
            string expectedDataType    = "string";
            string expectedDescription = "Option test description";
            string expectedApiName     = "TestApiName";
            string expectedValue       = "TestValue";

            int expectedNumberOfOptions = 1;

            string environmentSetting          = $"{{\"UseLiveClient\": \"True\"}}";
            NullLogger <GetDataHandler> logger = new NullLogger <GetDataHandler>();

            BuildEnvironment(environmentSetting);

            OptionBuilder  builder           = OptionBuilder.Instance(logger);
            List <IOption> selectableOptions = new List <IOption>();

            selectableOptions.Add(TestDataBuilder.CreateOption(expectedOption, expectedDataType, expectedDescription, expectedApiName));
            Dictionary <string, string> cliOptions = new Dictionary <string, string>();

            cliOptions.Add(expectedOption, expectedValue);

            ISubCommandHandler subCommandHandler = new GetDataHandler(logger)
            {
                SelectableCliOptions = selectableOptions,
                DictOptions          = cliOptions
            };

            bool isValid = builder.AssignValueToCliOptions(subCommandHandler);

            Assert.IsTrue(isValid);
            Assert.AreEqual(expectedNumberOfOptions, subCommandHandler.SelectableCliOptions.Count);
            IOption selectableOption = subCommandHandler.SelectableCliOptions[0];

            Assert.AreEqual(expectedOption, selectableOption.Name);
            Assert.AreEqual(expectedDescription, selectableOption.Description);
            Assert.AreEqual(expectedApiName, selectableOption.ApiName);
            Assert.IsTrue(selectableOption.IsAssigned);
            Assert.AreEqual(expectedValue, selectableOption.Value);

            Assert.IsInstanceOfType(selectableOption, typeof(NumberOption <String>));
        }
Пример #2
0
        public void Storage_GetData_Wrong_Option_Combination()
        {
            string expectedOption      = "TestOption";
            string expectedDataType    = "string";
            string expectedDescription = "Option test description";
            string expectedApiName     = "TestApiName";
            string expectedValue       = "TestValue";

            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
            OptionBuilder  builder           = OptionBuilder.Instance(logger);
            List <IOption> selectableOptions = new List <IOption>();

            selectableOptions.Add(TestDataBuilder.CreateOption(expectedOption, expectedDataType, expectedDescription, expectedApiName));
            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(GetDataHandler));

            // Register commands and sub commands
            ServiceProvider serviceProvider = TestDataBuilder.BuildServiceProvider(availableCommandTypes, availableSubCommands, Log.Logger);

            // Fetch GetDataHandler subCommand
            var sssList = serviceProvider.GetServices <ISubCommandHandler>().ToList();
            ISubCommandHandler subCommandHandler = sssList.First(x => x.Name == "GetData");

            subCommandHandler.SelectableCliOptions = selectableOptions;
            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;
        }
Пример #3
0
        public void Storage_GetData_Data_For_Instance()
        {
            string expectedOrgOption      = "org";
            string expectedOrgDataType    = "string";
            string expectedOrgDescription = "org";
            string expectedOrgApiName     = "org";
            string expectedOrgValue       = "5533";

            string expectedFetchMessage = "Fetched 1 instances.";
            string expectedSaveMessage  = "File:Kvittering";
            int    expectedLogEntries   = 2;

            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
            OptionBuilder  builder           = OptionBuilder.Instance(logger);
            List <IOption> selectableOptions = new List <IOption>();

            selectableOptions.Add(TestDataBuilder.CreateOption(expectedOrgOption, expectedOrgDataType, expectedOrgDescription, expectedOrgApiName));
            Dictionary <string, string> cliOptions = new Dictionary <string, string>();

            cliOptions.Add(expectedOrgOption, expectedOrgValue);

            // 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(GetDataHandler));

            // Register commands and sub commands
            ServiceProvider serviceProvider = TestDataBuilder.BuildServiceProvider(availableCommandTypes, availableSubCommands, Log.Logger);

            // Set response from ClientWrapper
            InstanceResponseMessage response = TestDataBuilder.CreateInstanceResponse(1);

            StorageClientFileWrapper.InstanceResponse = response;
            StorageClientFileWrapper.DataContent      = new MemoryStream();

            // Fetch GetDataHandler subCommand
            var sssList = serviceProvider.GetServices <ISubCommandHandler>().ToList();
            ISubCommandHandler subCommandHandler = sssList.First(x => x.Name == "GetData");

            subCommandHandler.SelectableCliOptions = selectableOptions;
            subCommandHandler.DictOptions          = cliOptions;

            // Mock the file wrapper
            Mock <IFileWrapper> mockedWrapper = new Mock <IFileWrapper>();

            mockedWrapper.Setup(x => x.SaveToFile(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Stream>())).Returns(true);
            subCommandHandler.CliFileWrapper = mockedWrapper.Object;

            // 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 fetchMessage = logEntries.FirstOrDefault(x => x.Contains(expectedFetchMessage));

            Assert.IsFalse(string.IsNullOrEmpty(fetchMessage));

            string saveMessage = logEntries.FirstOrDefault(x => x.Contains(expectedSaveMessage));

            Assert.IsFalse(string.IsNullOrEmpty(saveMessage));

            textWriter.Dispose();
            builder.CfgCommands = null;
        }