Ejemplo n.º 1
0
        public async Task ExecuteBuiltinCommandAsyncGroupOnTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var groupOnIdsRequestSentToAdapter = new List <Group>();
            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ActivateGroupAsyncGroup = async g => groupOnIdsRequestSentToAdapter.Add(g)
                }
            };
            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device  = UnitTesting.CreateFakeDevice();
            var device2 = UnitTesting.CreateFakeDevice();
            var device3 = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                context.Devices.Add(device2);
                context.Devices.Add(device3);

                var group = new Group
                {
                    Name = "Test Group"
                };
                group.Devices.Add(device);
                group.Devices.Add(device2);
                context.Groups.Add(group);

                var builtinCommand = new BuiltinCommand
                {
                    Name             = "Turn on a Group",
                    UniqueIdentifier = "GROUP_ON"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

                //Act
                var result =
                    await
                    commmandProcessor.ExecuteBuiltinCommandAsync(builtinCommand,
                                                                 group.Id.ToString(CultureInfo.InvariantCulture), "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(groupOnIdsRequestSentToAdapter.Count == 2, "Process did not run the correct amount of commands.");
                Assert.IsTrue(group.Id == groupOnIdsRequestSentToAdapter[0].Id, "Ran the wrong group!");
            }
        }
        public async Task RegisterAsyncNewDeviceValueTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var dvb = new DeviceValueBuilder( dbConnection);

            var device = UnitTesting.CreateFakeDevice();
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                await context.SaveChangesAsync();

                var deviceValue = new DeviceValue
                {
                    Description = "Testing Value Description Here",
                    Name = "Test Value",
                    ValueType = DataType.BOOL,
                    Value = true.ToString(),
                    DeviceId = device.Id
                };

                //act
                var result = await dvb.RegisterAsync(deviceValue, device, CancellationToken.None);
                var dv = await context.DeviceValues.FirstOrDefaultAsync(o => o.Name == deviceValue.Name);


                //assert 
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsNotNull(dv, "Registered device value count not be found in database.");
                Console.WriteLine(result.Message);
            }
        }
Ejemplo n.º 3
0
        public async Task ExecuteBuiltinCommandAsyncGroupOnBadIdTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager    = new StubIAdapterManager();
            var log               = new StubIFeedback <LogEntry>();
            var cts               = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device  = UnitTesting.CreateFakeDevice();
            var device2 = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                var builtinCommand = new BuiltinCommand
                {
                    UniqueIdentifier = "GROUP_ON"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

                //Act
                var result = await commmandProcessor.ExecuteBuiltinCommandAsync(builtinCommand, "0", "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsTrue(result.HasError);
                Assert.IsTrue(result.Message.Contains("Invalid group"), "Expected to see 'Invalid group' in log");
            }
        }
        public async Task RegisterAsyncNewTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var dsb = new DeviceSettingBuilder(dbConnection);

            var deviceSetting = new DeviceSetting
            {
                Name = "Unit Test Device Setting",
                UniqueIdentifier = "DEVICE_SETTING1"
            };

            //act
            var result = await dsb.RegisterAsync(deviceSetting, CancellationToken.None);

            DeviceSetting setting;
            using (var context = new ZvsContext(dbConnection))
            {
                setting =
                    await
                        context.DeviceSettings.FirstOrDefaultAsync(
                            o => o.UniqueIdentifier == deviceSetting.UniqueIdentifier);

            }

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new device setting saved to DB");
        }
Ejemplo n.º 5
0
        public async Task TestPropertyUpdatingOnDatabaseSettingChange()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    return(Task.FromResult(0));
                }
            };
            var unitTestingPlugin = new StubUnitTestPlugin
            {
                PluginGuidGet  = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Plugin",
                DescriptionGet = () => "",
                OnSettingsCreatingPluginSettingBuilder       = s => Task.FromResult(0),
                OnDeviceSettingsCreatingDeviceSettingBuilder = s => Task.FromResult(0),
                OnSceneSettingsCreatingSceneSettingBuilder   = s => Task.FromResult(0),
            };

            var plugin = new Plugin
            {
                PluginGuid  = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                Name        = "Unit Testing Plugin",
                Description = ""
            };

            plugin.Settings.Add(new PluginSetting
            {
                UniqueIdentifier = "PropertyTest",
                Value            = "2",
                ValueType        = DataType.INTEGER
            });

            using (var context = new ZvsContext(dbConnection))
            {
                context.Plugins.Add(plugin);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var pluginManager = new PluginManager(new List <ZvsPlugin> {
                unitTestingPlugin
            }, dbConnection, log, new StubIAdapterManager());
            await pluginManager.StartAsync(CancellationToken.None);

            //act
            using (var context = new ZvsContext(dbConnection))
            {
                context.PluginSettings.First().Value = "55";
                await context.SaveChangesAsync(CancellationToken.None);
            }

            //assert
            Assert.IsTrue(unitTestingPlugin.PropertyTest == 55, "The property test property on the zvsPlugin did not properly update when the database value was changed.");
        }
Ejemplo n.º 6
0
        public async Task RegisterNewDeviceTypeSettingTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var adapterBuilder    = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var deviceType        = UnitTesting.CreateFakeDeviceType();
            var deviceTypeSetting = new DeviceTypeSetting
            {
                Name       = "Device type Setting 1",
                ValueType  = DataType.STRING,
                Value      = "Hello World",
                DeviceType = deviceType
            };

            //act
            var result = await adapterBuilder.RegisterDeviceTypeSettingAsync(deviceTypeSetting);

            DeviceTypeSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.DeviceTypeSettings.FirstOrDefaultAsync();
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsNotNull(setting, "Setting not saved!");
            Assert.IsTrue(setting.Name == deviceTypeSetting.Name, "Device type setting name mismatch");
        }
Ejemplo n.º 7
0
        public async Task StopPath1()
        {
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            //Arrange
            LogEntry logEntry = null;
            var      log      = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString()); logEntry = e;
                    return(Task.FromResult(0));
                }
            };
            var cts = new CancellationTokenSource();
            var tm  = new TriggerRunner(log, new StubICommandProcessor(), dbConnection);

            //Act
            await tm.StartAsync(cts.Token);

            await tm.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntry.Level == LogEntryLevel.Info);
            Assert.IsTrue(logEntry.Message.Contains("stopped"), "Manger not started or word started not in the log.");
        }
Ejemplo n.º 8
0
        public async Task RunDeviceCommandTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            int?commandIdRan = 0;
            var arg1Ran      = "";
            var arg2Ran      = "";

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    commandIdRan = commandId;
                    arg1Ran      = argument;
                    arg2Ran      = argument2;
                    return(Task.FromResult(Result.ReportSuccess()));
                }
            };
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    return(Task.FromResult(0));
                }
            };
            var          cts    = new CancellationTokenSource();
            var          runner = new JavaScriptRunner(log, commandProcessor, dbConnection);
            const string script = @"
function f1() { 
       var result = runDeviceNameCommandName('Light Switch','Turn On', '99'); 
       logInfo(result.Message);
};
f1();";

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceCommand = new DeviceCommand
                {
                    Name = "Turn On"
                };
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                //Act
                var result = await runner.ExecuteScriptAsync(script, cts.Token);

                //Assert
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsTrue(deviceCommand.Id == commandIdRan, "Wrong command ran!");
                Assert.IsTrue("99" == arg1Ran, "command arguments not passed in correctly.");
                Assert.IsTrue(string.Empty == arg2Ran, "command arguments not passed in correctly.");
            }
        }
Ejemplo n.º 9
0
        public async Task SetPluginPropertyCastPropertyTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };
            var unitTestingPlugin = new StubUnitTestPlugin();
            var pluginManager     = new PluginManager(new List <ZvsPlugin> {
                unitTestingPlugin
            }, dbConnection, log, new StubIAdapterManager());

            //act
            await pluginManager.SetPluginProperty(unitTestingPlugin, "PropertyTest", "abc", CancellationToken.None);

            //assert
            Assert.IsTrue(unitTestingPlugin.PropertyTest == 0, "Expected TestSetting property to be 0");
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error && o.Message.Contains("Cannot cast value")) == 1, "Expected 1 cannot cast value on plugin setting error");
        }
        public async Task LoadAdaptersNameTooLongAsyncTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var longNameAdapter = new StubZvsAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque magna diam, pellentesque et orci eget, pellentesque iaculis odio. Ut ultrices est sapien, ac pellentesque odio malesuada a. Etiam in neque ullamcorper massa gravida ullamcorper vel a posuere.",
                DescriptionGet = () => "",
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter>() { longNameAdapter }, dbConnection, log);
            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 1, "Expected 1 error");
        }
Ejemplo n.º 11
0
        public async Task RunBadDeviceCommandTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries       = new List <LogEntry>();
            var commandProcessor = new StubICommandProcessor();
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };
            var          cts    = new CancellationTokenSource();
            var          runner = new JavaScriptRunner(log, commandProcessor, dbConnection);
            const string script = @"
function f1() { 
       var result = runDeviceNameCommandName('Light Switch','Turn On', '99'); 
       logInfo(result.Message);
};
f1();";
            //Act
            var result = await runner.ExecuteScriptAsync(script, cts.Token);

            //Assert
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsTrue(logEntries.Any(o => o.Message.Contains("Cannot find")));
        }
        public async Task RegisterAsyncNewDeviceTypeTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var dtb = new DeviceTypeBuilder( dbConnection);
            var adapter = UnitTesting.CreateFakeAdapter();
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync();
            }

            var dt = new DeviceType
            {
                AdapterId = adapter.Id,
                UniqueIdentifier = "UNIT_TEST_DEVICE_TYPE1",
                Name = "Unit Test Device Type"
            };

            //act
            var result = await dtb.RegisterAsync(adapter.AdapterGuid, dt, CancellationToken.None);

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsTrue(dt.Id > 0, "Expected device type to have a database generated Id");
        }
Ejemplo n.º 13
0
        public async Task GetZvsPluginsTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback <LogEntry>();
            var unitTestingPlugin = new StubUnitTestPlugin
            {
                PluginGuidGet  = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Plugin",
                DescriptionGet = () => "",
                OnSettingsCreatingPluginSettingBuilder       = s => Task.FromResult(0),
                OnDeviceSettingsCreatingDeviceSettingBuilder = s => Task.FromResult(0),
                OnSceneSettingsCreatingSceneSettingBuilder   = s => Task.FromResult(0)
            };

            var pluginManager = new PluginManager(new List <ZvsPlugin> {
                unitTestingPlugin
            }, dbConnection, log, new StubIAdapterManager());

            //act
            var result = pluginManager.GetZvsPlugins();

            //assert
            Assert.IsTrue(result.Count() == 1, "Expected 1 plugin in the list");
        }
Ejemplo n.º 14
0
        public async Task RegisterAsyncUpdatedDeviceTypeTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var dtb     = new DeviceTypeBuilder(dbConnection);
            var adapter = UnitTesting.CreateFakeAdapter();
            var dt      = new DeviceType
            {
                AdapterId        = adapter.Id,
                UniqueIdentifier = "UNIT_TEST_DEVICE_TYPE1",
                Name             = "Unit Test Device Type"
            };

            adapter.DeviceTypes.Add(dt);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync();
            }

            dt.Name = "New Unit Test Device Type Name";

            //act
            var result = await dtb.RegisterAsync(adapter.AdapterGuid, dt, CancellationToken.None);

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
        }
Ejemplo n.º 15
0
        public async Task ExecuteBuiltinCommandAsyncUnknownCommandTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var deviceCommandIds = new List <int>();
            var adapterManager   = new StubIAdapterManager();
            var log = new StubIFeedback <LogEntry>();

            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            using (var context = new ZvsContext(dbConnection))
            {
                var builtinCommand = new BuiltinCommand
                {
                    Name             = "Unknown Built-in Command",
                    UniqueIdentifier = "UNKNOWN_COMMAND_TYPE"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

                //Act
                var result = await commmandProcessor.ExecuteBuiltinCommandAsync(builtinCommand, "", "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsTrue(result.HasError);
                Assert.IsTrue(deviceCommandIds.Count == 0, "Process did not run the correct amount of commands.");
            }
        }
Ejemplo n.º 16
0
        public async Task GetZvsAdaptersTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback <LogEntry>();
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            var result = adapterManager.GetZvsAdapters();

            //assert
            Assert.IsTrue(result.Count() == 1, "Expected 1 adapter in the list");
        }
Ejemplo n.º 17
0
        public async Task RegisterAdapterSettingOptionAddedTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var adapterBuilder = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var dbAdapter      = UnitTesting.CreateFakeAdapter();

            var adapterSetting = new AdapterSetting
            {
                Name             = "Adapter Setting 1",
                ValueType        = DataType.STRING,
                Value            = "Hello World",
                UniqueIdentifier = "PropertyTest"
            };

            dbAdapter.Settings.Add(adapterSetting);
            var option1 = new AdapterSettingOption
            {
                Name = "Option 1",
            };
            var option2 = new AdapterSettingOption
            {
                Name = "Option 2",
            };

            adapterSetting.Options.Add(option1);


            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(dbAdapter);
                await context.SaveChangesAsync();
            }
            var adapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => dbAdapter.AdapterGuid
            };

            adapterSetting.Options.Add(option2);

            //act
            var result = await adapterBuilder.Adapter(adapter).RegisterAdapterSettingAsync(adapterSetting, o => o.PropertyTest);

            AdapterSetting a;

            using (var context = new ZvsContext(dbConnection))
            {
                a = await context.AdapterSettings
                    .Include(o => o.Options)
                    .FirstOrDefaultAsync(o => o.Id == adapterSetting.Id);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsTrue(a.Options.Count == 2, "Expected 2 adapter setting options");
            Assert.IsTrue(a.Options[1].Name == option2.Name, "Adapter option name mismatch");
        }
Ejemplo n.º 18
0
        public async Task FindZvsPluginInvalidIdAsyncTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var pluginManager = new PluginManager(new List <ZvsPlugin>(), dbConnection, log, new StubIAdapterManager());
            await pluginManager.StartAsync(CancellationToken.None);

            //act
            var plugin = pluginManager.FindZvsPlugin(1);

            //assert
            Assert.IsNull(plugin, "Found a plugin?");
        }
        public async Task RegisterAsyncNewTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var bcb = new BuiltinCommandBuilder(dbConnection);

            var builtinCommand = new BuiltinCommand
            {
                Name = "Unit Test Builtin Command",
                UniqueIdentifier = "BUILTIN_COMMAND1"
            };

            //act
            var result = await bcb.RegisterAsync(builtinCommand, CancellationToken.None);

            BuiltinCommand setting;
            using (var context = new ZvsContext(dbConnection))
            {
                setting =
                    await
                        context.BuiltinCommands.FirstOrDefaultAsync(
                            o => o.UniqueIdentifier == builtinCommand.UniqueIdentifier);

            }

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new builtin command setting saved to DB");
        }
        public async Task RegisterAsyncNewTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var bcb = new BuiltinCommandBuilder(dbConnection);

            var builtinCommand = new BuiltinCommand
            {
                Name             = "Unit Test Builtin Command",
                UniqueIdentifier = "BUILTIN_COMMAND1"
            };

            //act
            var result = await bcb.RegisterAsync(builtinCommand, CancellationToken.None);

            BuiltinCommand setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting =
                    await
                    context.BuiltinCommands.FirstOrDefaultAsync(
                        o => o.UniqueIdentifier == builtinCommand.UniqueIdentifier);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new builtin command setting saved to DB");
        }
Ejemplo n.º 21
0
        public async Task StopPath2()
        {
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            //Arrange
            LogEntry logEntry = null;
            var      log      = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntry = e;
                    return(Task.FromResult(0));
                }
            };
            var cts = new CancellationTokenSource();
            var tm  = new TriggerRunner(log, new StubICommandProcessor(), dbConnection);

            //Act
            await tm.StopAsync(cts.Token);

            //Assert
            Assert.AreEqual(logEntry.Level, LogEntryLevel.Warn);
        }
        public async Task RegisterAsyncNewTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var dsb = new DeviceSettingBuilder(dbConnection);

            var deviceSetting = new DeviceSetting
            {
                Name             = "Unit Test Device Setting",
                UniqueIdentifier = "DEVICE_SETTING1"
            };

            //act
            var result = await dsb.RegisterAsync(deviceSetting, CancellationToken.None);

            DeviceSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting =
                    await
                    context.DeviceSettings.FirstOrDefaultAsync(
                        o => o.UniqueIdentifier == deviceSetting.UniqueIdentifier);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new device setting saved to DB");
        }
        public async Task RegisterPluginSettingOptionRemovedTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var pluginBuilder = new PluginSettingBuilder(dbConnection, CancellationToken.None);
            var dbPlugin      = UnitTesting.CreateFakePlugin();

            var pluginSetting = new PluginSetting
            {
                Name             = "Plugin Setting 1",
                ValueType        = DataType.STRING,
                Value            = "Hello World",
                UniqueIdentifier = "PropertyTest"
            };

            dbPlugin.Settings.Add(pluginSetting);
            var option1 = new PluginSettingOption
            {
                Name = "Option 1",
            };
            var option2 = new PluginSettingOption
            {
                Name = "Option 2",
            };

            pluginSetting.Options.Add(option1);
            pluginSetting.Options.Add(option2);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Plugins.Add(dbPlugin);
                await context.SaveChangesAsync();
            }
            var plugin = new StubUnitTestPlugin
            {
                PluginGuidGet = () => dbPlugin.PluginGuid
            };

            pluginSetting.Options.Remove(option2);

            //act
            var result = await pluginBuilder.Plugin(plugin).RegisterPluginSettingAsync(pluginSetting, o => o.PropertyTest);

            PluginSetting a;

            using (var context = new ZvsContext(dbConnection))
            {
                a = await context.PluginSettings
                    .Include(o => o.Options)
                    .FirstOrDefaultAsync(o => o.Id == pluginSetting.Id);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsTrue(a.Options.Count == 1, "Expected 2 plugin setting options");
            Assert.IsTrue(a.Options[0].Name == option1.Name, "Plugin option name mismatch");
        }
 public void ConstructorNullArg1Test()
 {
     //arrange 
     var dbConnection = new UnitTestDbConnection();
     //act
     new DeviceValueBuilder(null);
     //assert - throws exception
 }
        public void ConstructorNullArg1Test()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            //act
            new DeviceCommandBuilder(null);
            //assert - throws exception
        }
 public void ConstructorTest()
 {
     //arrange 
     var dbConnection = new UnitTestDbConnection();
     //act
     var ssb = new SceneSettingBuilder(dbConnection);
     //assert 
     Assert.IsNotNull(ssb);
 }
 public void ConstructorNullArg1Test()
 {
     var dbConnection = new UnitTestDbConnection();
     Database.SetInitializer(new CreateFreshDbInitializer());
     //arrange 
     //act
     new TriggerRunner(null, new StubICommandProcessor(), new UnitTestDbConnection());
     //assert - throws exception
 }
Ejemplo n.º 28
0
        public async Task RunSceneAsyncSceneIsRunningTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries        = new List <LogEntry>();
            var ranstoredCommands = new List <int>();

            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    if (commandId.HasValue)
                    {
                        ranstoredCommands.Add(commandId.Value);
                    }
                    return(Task.FromResult(Result.ReportSuccess()));
                }
            };

            var scene = new Scene
            {
                Name      = "I Am Running Test",
                IsRunning = true
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.Scenes.Add(scene);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var cts         = new CancellationTokenSource();
            var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection);

            //Act
            var result = await sceneRunner.RunSceneAsync(scene.Id, cts.Token);

            Console.WriteLine(result.Message);

            //Assert
            Assert.IsTrue(result.HasError);
            Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Warn), "Expected only info type log entries");
            Assert.IsTrue(ranstoredCommands.Count == 0, "Scene runner did not run the correct amount of commands.");
        }
 public void ConstructorTest()
 {
     //arrange 
     var dbConnection = new UnitTestDbConnection();
     //act
     var bcb = new BuiltinCommandBuilder(dbConnection);
     //assert 
     Assert.IsNotNull(bcb);
 }
 public void ConstructorTest()
 {
     //arrange 
     var dbConnection = new UnitTestDbConnection();
     //act
     var dvb = new DeviceValueBuilder(dbConnection);
     //assert 
     Assert.IsNotNull(dvb);
 }
Ejemplo n.º 31
0
        public async Task StartTwiceTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    logEntries.Add(e);
                    Console.WriteLine(e.ToString());
                    return(Task.FromResult(0));
                }
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled   = true
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var isStartedCount     = 0;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder  = s => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
                StartAsync01 = () =>
                {
                    isStartedCount++;
                    return(Task.FromResult(0));
                }
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Warn) == 1);
            Assert.IsTrue(isStartedCount == 1, "Adapter started too many or too few times");
        }
Ejemplo n.º 32
0
        public async Task RegisterAsyncRemvoedCommandOptionDeviceTypeTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var dtb     = new DeviceTypeBuilder(dbConnection);
            var adapter = UnitTesting.CreateFakeAdapter();
            var dt      = new DeviceType
            {
                AdapterId        = adapter.Id,
                UniqueIdentifier = "UNIT_TEST_DEVICE_TYPE1",
                Name             = "Unit Test Device Type"
            };
            var dtc = new DeviceTypeCommand
            {
                UniqueIdentifier = "DTC1",
                Name             = "Test Device Type Command"
            };
            var option1 = new CommandOption
            {
                Name = "Option 1"
            };
            var option2 = new CommandOption
            {
                Name = "Option 2"
            };

            dt.Commands.Add(dtc);
            adapter.DeviceTypes.Add(dt);
            dtc.Options.Add(option1);
            dtc.Options.Add(option2);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync();
            }

            dtc.Options.Remove(option1);

            //act
            var result = await dtb.RegisterAsync(adapter.AdapterGuid, dt, CancellationToken.None);

            using (var context = new ZvsContext(dbConnection))
            {
                var dtc1 = context.DeviceTypeCommands
                           .Include(o => o.Options)
                           .First();

                //assert
                Console.WriteLine(result.Message);
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(dtc1.Options.Count == 1, "Expected 2 options");
            }
        }
        public async Task RegisterAsyncRemovedCommandOptionsTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var dvb = new DeviceCommandBuilder(dbConnection);

            var device        = UnitTesting.CreateFakeDevice();
            var deviceCommand = new DeviceCommand
            {
                Name = "Unit Testing Command",
            };
            var option1 = new CommandOption
            {
                Name = "Option 1"
            };
            var option2 = new CommandOption
            {
                Name = "Option 2"
            };

            deviceCommand.Options.Add(option1);
            deviceCommand.Options.Add(option2);

            using (var context = new ZvsContext(dbConnection))
            {
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync();
            }

            deviceCommand.Options.Remove(option1);

            //act
            var result = await dvb.RegisterAsync(device.Id, deviceCommand, CancellationToken.None);

            Console.WriteLine(result.Message);

            List <DeviceCommand> deviceCommands;

            using (var context = new ZvsContext(dbConnection))
            {
                deviceCommands = await context.DeviceCommands
                                 .Include(o => o.Options)
                                 .Include(o => o.Device)
                                 .Where(o => o.Id == deviceCommand.Id)
                                 .ToListAsync();
            }

            //assert
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsTrue(deviceCommands.Count == 1, "Device has an unexpected number of commands");
            Assert.IsTrue(deviceCommands[0].Options.Count == 1, "Option not removed as expected");
            Assert.IsTrue(deviceCommands[0].Options[0].Name == option2.Name, "Option mismatch");
        }
        public void ConstructorTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();
            //act
            var bcb = new BuiltinCommandBuilder(dbConnection);

            //assert
            Assert.IsNotNull(bcb);
        }
        public void ConstructorTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();
            //act
            var dvb = new DeviceCommandBuilder(dbConnection);

            //assert
            Assert.IsNotNull(dvb);
        }
        public void ConstructorTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();
            //act
            var dsb = new DeviceSettingBuilder(dbConnection);

            //assert
            Assert.IsNotNull(dsb);
        }
Ejemplo n.º 37
0
        public void ConstructorNullArg1Test()
        {
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            //arrange
            //act
            new SceneRunner(null, new StubICommandProcessor(), new UnitTestDbConnection());
            //assert - throws exception
        }
Ejemplo n.º 38
0
        public async Task RegisterRemovedDeviceTypeSettingOptionTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterBuilder    = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var deviceType        = UnitTesting.CreateFakeDeviceType();
            var deviceTypeSetting = new DeviceTypeSetting
            {
                Name       = "Device Type Setting 1",
                ValueType  = DataType.STRING,
                Value      = "Hello World",
                DeviceType = deviceType
            };
            var option1 = new DeviceTypeSettingOption
            {
                Name = "Option 1",
            };
            var option2 = new DeviceTypeSettingOption
            {
                Name = "Option 2",
            };

            deviceTypeSetting.Options.Add(option1);
            deviceTypeSetting.Options.Add(option2);
            using (var context = new ZvsContext(dbConnection))
            {
                context.DeviceTypeSettings.Add(deviceTypeSetting);
                await context.SaveChangesAsync();
            }

            deviceTypeSetting.Options.Remove(option2);

            //act
            var result = await adapterBuilder.RegisterDeviceTypeSettingAsync(deviceTypeSetting);

            DeviceTypeSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.DeviceTypeSettings
                          .Include(o => o.Options)
                          .FirstOrDefaultAsync();
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsNotNull(setting, "Setting not found");
            Assert.IsTrue(setting.Options.Count == 1, "Expected 2 options");
            Assert.IsTrue(setting.Options[0].Name == option1.Name, "Name mismatch");
        }
        public async Task RegisterAsyncOptionRemoveTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var dsb = new DeviceSettingBuilder(dbConnection);

            var deviceSetting = new DeviceSetting
            {
                Name             = "Unit Test Device Setting",
                UniqueIdentifier = "DEVICE_SETTING1"
            };

            var option1 = new DeviceSettingOption
            {
                Name = "Option 1"
            };
            var option2 = new DeviceSettingOption
            {
                Name = "Option 2"
            };

            deviceSetting.Options.Add(option1);
            deviceSetting.Options.Add(option2);

            using (var context = new ZvsContext(dbConnection))
            {
                context.DeviceSettings.Add(deviceSetting);
                await context.SaveChangesAsync();
            }

            deviceSetting.Options.Remove(option2);

            //act
            var result = await dsb.RegisterAsync(deviceSetting, CancellationToken.None);

            DeviceSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.DeviceSettings
                          .Include(o => o.Options)
                          .FirstOrDefaultAsync(o => o.UniqueIdentifier == deviceSetting.UniqueIdentifier);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new device setting saved to DB");
            Assert.IsTrue(setting.Options.Count == 1, "Expected 1 option!");
            Assert.IsTrue(setting.Options[0].Name == option1.Name);
        }
        public async Task RegisterAsyncOptionRemoveTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var bcb = new BuiltinCommandBuilder(dbConnection);

            var builtinCommand = new BuiltinCommand
            {
                Name             = "Unit Test Builtin Command",
                UniqueIdentifier = "BUILTIN_COMMAND1",
            };

            var option1 = new CommandOption
            {
                Name = "Option 1"
            };
            var option2 = new CommandOption
            {
                Name = "Option 2"
            };

            builtinCommand.Options.Add(option1);
            builtinCommand.Options.Add(option2);

            using (var context = new ZvsContext(dbConnection))
            {
                context.BuiltinCommands.Add(builtinCommand);
                await context.SaveChangesAsync();
            }

            builtinCommand.Options.Remove(option2);

            //act
            var result = await bcb.RegisterAsync(builtinCommand, CancellationToken.None);

            BuiltinCommand setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.BuiltinCommands
                          .Include(o => o.Options)
                          .FirstOrDefaultAsync(o => o.UniqueIdentifier == builtinCommand.UniqueIdentifier);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new builtin command setting saved to DB");
            Assert.IsTrue(setting.Options.Count == 1, "Expected 1 option!");
            Assert.IsTrue(setting.Options[0].Name == option1.Name);
        }
        public async Task RegisterAsyncNullDeviceValueTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            var dvb = new DeviceValueBuilder( dbConnection);

            //act
            var result = await dvb.RegisterAsync(null, new Device(), CancellationToken.None);

            //assert 
            Assert.IsTrue(result.HasError);
        }
        public async Task RegisterAsyncNullDeviceValueTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            var dvb = new DeviceCommandBuilder( dbConnection);

            //act
            var result = await dvb.RegisterAsync(1, null, CancellationToken.None);

            //assert 
            Assert.IsTrue(result.HasError);
            Console.WriteLine(result.Message);
        }
        public async Task RegisterAsyncNullDeviceTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            var dsb = new DeviceSettingBuilder(dbConnection);

            //act
            var result = await dsb.RegisterAsync(null, CancellationToken.None);

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsTrue(result.HasError, result.Message);
        }
        public async Task RegisterAsyncNullBuiltinCommandTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            var bcb = new BuiltinCommandBuilder(dbConnection);

            //act
            var result = await bcb.RegisterAsync(null, CancellationToken.None);

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsTrue(result.HasError, result.Message);
        }
Ejemplo n.º 45
0
        public void ConstructorNullArg3Test()
        {
            //arrange 
            var fb = new StubIFeedback<LogEntry>();
            var am = new StubIAdapterManager();
            var connection = new UnitTestDbConnection();
            var tr = new TriggerRunner(fb, new StubICommandProcessor(), connection);
            var st = new StubScheduledTaskRunner(fb, new StubICommandProcessor(), connection, new StubITimeProvider());

            //act
            new ZvsEngine(fb, am, null, connection, tr, st);
            //assert - throws exception
        }
        public async Task RegisterAsyncInvalidAdapterTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var dtb = new DeviceTypeBuilder( dbConnection);

            //act
            var result = await dtb.RegisterAsync(new Guid(), new DeviceType(), CancellationToken.None);

            //assert 
            Assert.IsTrue(result.HasError);
            Console.WriteLine(result.Message);
        }
        public async Task RegisterPluginSettingNullPluginTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var pluginBuilder = new PluginSettingBuilder( dbConnection, CancellationToken.None);
            var plugin = new StubUnitTestPlugin();

            //act
            var result = await pluginBuilder.Plugin(plugin).RegisterPluginSettingAsync(new PluginSetting(), o => o.PropertyTest);

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsTrue(result.HasError);
        }
        public async Task RegisterAsyncInvalidDeviceIdTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var dvb = new DeviceCommandBuilder(dbConnection);

            var deviceCommand = new DeviceCommand();

            //act
            var result = await dvb.RegisterAsync(3, deviceCommand, CancellationToken.None);

            //assert 
            Assert.IsTrue(result.HasError);
            Console.WriteLine(result.Message);
        }
        public async Task RegisterPluginSettingPluginTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var pluginBuilder = new PluginSettingBuilder(dbConnection, CancellationToken.None);
            var dbPlugin = UnitTesting.CreateFakePlugin();
            using (var context = new ZvsContext(dbConnection))
            {
                context.Plugins.Add(dbPlugin);
                await context.SaveChangesAsync();
            }
            var plugin = new StubUnitTestPlugin
            {
                PluginGuidGet = () => dbPlugin.PluginGuid
            };

            var pluginSetting = new PluginSetting
            {
                Name = "Plugin Setting 1",
                ValueType = DataType.STRING,
                Value = "Hello World"
            };
               
            //act
            var result = await pluginBuilder.Plugin(plugin).RegisterPluginSettingAsync(pluginSetting, o => o.PropertyTest);

            Plugin a;
            using (var context = new ZvsContext(dbConnection))
            {
                a = await context.Plugins
                    .Include(o=> o.Settings)
                    .FirstOrDefaultAsync(o => o.PluginGuid == dbPlugin.PluginGuid);
            }

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsTrue(a.Settings.Count == 1, "Expected 1 plugin setting");
            Assert.IsTrue(a.Settings[0].Name == pluginSetting.Name, "Plugin setting name mismatch");
        }
        public async Task ExecuteDeviceCommandAsyncInvalidIdTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager();
            var ranstoredCommands = new List<int>();
            var log = new StubIFeedback<LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            //Act
            var result = await commmandProcessor.ExecuteDeviceCommandAsync(new DeviceCommand(), "", "", cts.Token);
            Console.WriteLine(result.Message);

            //Assert
            Assert.IsTrue(result.HasError);
            Assert.IsTrue(ranstoredCommands.Count == 0, "Process did not run the correct amount of commands.");
            Assert.IsTrue(result.Message.Contains("Cannot locate"), "Expect error message to contain 'Cannot locate'");
        }
Ejemplo n.º 51
0
        public async Task RunSceneAsyncInvalidSceneTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var ranstoredCommands = new List<int>();

            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    if (commandId.HasValue) ranstoredCommands.Add(commandId.Value);
                    return Task.FromResult(Result.ReportSuccess());
                }
            };

            var cts = new CancellationTokenSource();
            var sceneRunner = new SceneRunner(log, commandProcessor, dbConnection);

            //Act
            var result = await sceneRunner.RunSceneAsync(-1, cts.Token);

            //Assert
            Assert.IsTrue(result.HasError);
            Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Warn), "Expected only info type log entries");
            Assert.IsTrue(ranstoredCommands.Count == 0, "Scene runner did not run the correct amount of commands.");
        }
        public async Task RegisterAsyncAddNewCommandTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var dvb = new DeviceCommandBuilder( dbConnection);

            var device = UnitTesting.CreateFakeDevice();
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                await context.SaveChangesAsync();
            }

            var deviceCommand = new DeviceCommand()
            {
                Name = "Unit Testing Command"
            };
            
            //act
            var result = await dvb.RegisterAsync(device.Id, deviceCommand, CancellationToken.None);
            Console.WriteLine(result.Message);

            Device d;
            using (var context = new ZvsContext(dbConnection))
            {
                d = await context.Devices
                    .Include(o=> o.Commands)
                    .FirstOrDefaultAsync(o => o.Id == device.Id);
            }

            //assert 
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(d, "device not found!");
            Assert.IsTrue(d.Commands.Count == 1, "Device has an unexpected number of commands");
            Assert.IsTrue(d.Commands[0].Name == deviceCommand.Name, "Device command did not save correctly");
        }
        public async Task RegisterAsyncOptionRemoveTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var bcb = new BuiltinCommandBuilder(dbConnection);

            var builtinCommand = new BuiltinCommand
            {
                Name = "Unit Test Builtin Command",
                UniqueIdentifier = "BUILTIN_COMMAND1",

            };

            var option1 = new CommandOption
            {
                Name = "Option 1"
            };
            var option2 = new CommandOption
            {
                Name = "Option 2"
            };
            builtinCommand.Options.Add(option1);
            builtinCommand.Options.Add(option2);

            using (var context = new ZvsContext(dbConnection))
            {
                context.BuiltinCommands.Add(builtinCommand);
                await context.SaveChangesAsync();
            }

            builtinCommand.Options.Remove(option2);

            //act
            var result = await bcb.RegisterAsync(builtinCommand, CancellationToken.None);

            BuiltinCommand setting;
            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.BuiltinCommands
                        .Include(o => o.Options)
                        .FirstOrDefaultAsync(o => o.UniqueIdentifier == builtinCommand.UniqueIdentifier);

            }

            //assert 
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(setting, "Expected new builtin command setting saved to DB");
            Assert.IsTrue(setting.Options.Count == 1, "Expected 1 option!");
            Assert.IsTrue(setting.Options[0].Name == option1.Name);
        }
        public async Task RunBadDeviceCommandTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var commandProcessor = new StubICommandProcessor();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };
            var cts = new CancellationTokenSource();
            var runner = new JavaScriptRunner(log, commandProcessor, dbConnection);
            const string script = @"
function f1() { 
       var result = runDeviceNameCommandName('Light Switch','Turn On', '99'); 
       logInfo(result.Message);
};
f1();";
            //Act
            var result = await runner.ExecuteScriptAsync(script, cts.Token);

            //Assert
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsTrue(logEntries.Any(o => o.Message.Contains("Cannot find")));
        }
        public async Task RegisterAsyncNothingToUpdateTest()
        {
            //arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var dvb = new DeviceValueBuilder(dbConnection);

            var device = UnitTesting.CreateFakeDevice();
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                var deviceValue = new DeviceValue
                {
                    UniqueIdentifier = "UNIT_TESTING_VALUE1",
                    CommandClass = "Command Class 1",
                    CustomData1 = "Custom Data 1",
                    CustomData2 = "Custom Data 2",
                    Description = "Testing Value Description Here",
                    Name = "Test Value",
                    ValueType = DataType.BOOL,
                    Value = true.ToString(),
                    Genre = "Genre",
                    Index = "Index",
                    IsReadOnly = true
                };
                device.Values.Add(deviceValue);
                await context.SaveChangesAsync();

                //act
                var result = await dvb.RegisterAsync(deviceValue, device, CancellationToken.None);
                var dv = await context.DeviceValues.FirstOrDefaultAsync(o => o.Name == deviceValue.Name);

                //assert 
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsNotNull(dv, "Registered device value count not be found in database.");
                Console.WriteLine(result.Message);
            }

        }
        public async Task RunCommandTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            int? commandIdRan = 0;
            var arg1Ran = "";
            var arg2Ran = "";

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    commandIdRan = commandId;
                    arg1Ran = argument;
                    arg2Ran = argument2;
                    return Task.FromResult(Result.ReportSuccess());
                }
            };
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    return Task.FromResult(0);
                }
            };

            var device0 = UnitTesting.CreateFakeDevice();
            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceCommand = new DeviceCommand
                {
                    Name = "Turn On"
                };
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device0);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                var cts = new CancellationTokenSource();
                var runner = new JavaScriptRunner(log, commandProcessor, dbConnection);
                var script =
                    $@"
function f1() {{ 
       var result = runCommand({deviceCommand.Id
                        },'98', '0'); 
       logInfo(result.Message);
}};
f1();";

                //Act
                var result = await runner.ExecuteScriptAsync(script, cts.Token);

                //Assert
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsTrue(deviceCommand.Id == commandIdRan, "Wrong command ran!");
                Assert.IsTrue("98" == arg1Ran, "command argument1 not passed in correctly.");
                Assert.IsTrue("0" == arg2Ran, "command argument2 not passed in correctly.");
            }
        }
        public async Task QuickFireTriggerTest()
        {

            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var ranstoredCommands = new List<int>();

            //Arrange 
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) =>
                {
                    if (commandId.HasValue) ranstoredCommands.Add(commandId.Value);
                    return Task.FromResult(Result.ReportSuccess());
                }
            };

            var cts = new CancellationTokenSource();
            var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection);
            await triggerManager.StartAsync(cts.Token);

            var cmd = new Command();
            var dv = new DeviceValue
            {
                Value = "first value",
                ValueType = DataType.STRING,
                Triggers = new ObservableCollection<DeviceValueTrigger> { 
                    new DeviceValueTrigger
                {
                     Name = "trigger1",
                     IsEnabled = true,
                     Operator = TriggerOperator.EqualTo,
                     Value = "some unique value",
                     Command = cmd
                } }
            };

            var device = UnitTesting.CreateFakeDevice();
            device.Values.Add(dv);

            //Act
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "Not It!";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "not this one";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "some unique value";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "not it";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "some unique value";
                await context.TrySaveChangesAsync(cts.Token);

                Console.WriteLine(context.DeviceValueTriggers.Count());
            }

            await Task.Delay(700, cts.Token);
            await triggerManager.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries");
            Assert.IsTrue(ranstoredCommands.Count == 2, "Trigger runner did not run the correct amount of commands.");
            Assert.IsTrue(ranstoredCommands.All(o => o == cmd.Id), "Scheduled task runner did not run the correct command.");
        }
        public async Task StopPath1()
        {
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            //Arrange 
            LogEntry logEntry = null;
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString()); logEntry = e;
                    return Task.FromResult(0);
                }
            };
            var cts = new CancellationTokenSource();
            var tm = new TriggerRunner(log, new StubICommandProcessor(), dbConnection);

            //Act
            await tm.StartAsync(cts.Token);
            await tm.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntry.Level == LogEntryLevel.Info);
            Assert.IsTrue(logEntry.Message.Contains("stopped"), "Manger not started or word started not in the log.");
        }
        public async Task StopPath2()
        {
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            //Arrange 
            LogEntry logEntry = null;
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntry = e;
                    return Task.FromResult(0);
                }
            };
            var cts = new CancellationTokenSource();
            var tm = new TriggerRunner(log, new StubICommandProcessor(), dbConnection);

            //Act
            await tm.StopAsync(cts.Token);

            //Assert
            Assert.AreEqual(logEntry.Level, LogEntryLevel.Warn);
        }
        public async Task LessThanInvalidValuesTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var ranstoredCommands = new List<int>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = ( commandId, argument, argument2, cancellationToken) =>
                {
                    if (commandId.HasValue) ranstoredCommands.Add(commandId.Value);
                    return Task.FromResult(Result.ReportSuccess());
                }
            };


            Database.SetInitializer(new CreateFreshDbInitializer());

            var cts = new CancellationTokenSource();
            var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection);
            await triggerManager.StartAsync(cts.Token);

            var cmd = new Command();
            var dv = new DeviceValue
            {
                Value = "first value",
                ValueType = DataType.STRING,
                Triggers = new ObservableCollection<DeviceValueTrigger> { 
                    new DeviceValueTrigger
                {
                     Name = "trigger1",
                     IsEnabled = true,
                     Operator = TriggerOperator.LessThan,
                     Value = "a",
                     Command = cmd
                } }
            };

            var device = UnitTesting.CreateFakeDevice();
            device.Values.Add(dv);

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                var r = await context.TrySaveChangesAsync(cts.Token);
                Assert.IsFalse(r.HasError, r.Message);
                dv.Value = "3";

                //Act
                var r2 = await context.TrySaveChangesAsync(cts.Token);
                Assert.IsFalse(r2.HasError, r2.Message);
            }

            await Task.Delay(700, cts.Token);
            await triggerManager.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntries.Any(o => o.Level == LogEntryLevel.Warn), "Expected some warning log entries");
            Assert.IsTrue(ranstoredCommands.Count == 0, "Trigger runner did not run the correct amount of commands.");
        }