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.");
            }
        }
        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 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!");
            }
        }
Exemple #4
0
 public void TestMethod1()
 {
     using (var context = new ZvsContext(new UnitTestDbConnection()))
     {
         var device = UnitTesting.CreateFakeDevice();
         context.Devices.Add(device);
         context.SaveChanges();
     }
 }
        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 async Task ExecuteDeviceCommandAsyncOkTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var commandsSendToAdapter = new List <int>();
            var adapterManager        = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceCommandAsyncDeviceDeviceCommandStringString = (adapterDevice, command, argument, argument2) =>
                    {
                        commandsSendToAdapter.Add(command.Id);
                        return(Task.FromResult(0));
                    }
                }
            };

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

            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 commmandProcessor.ExecuteDeviceCommandAsync(deviceCommand, "1", "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(commandsSendToAdapter.Count == 1, "Process did not run the correct amount of commands.");
                Assert.IsTrue(commandsSendToAdapter[0] == deviceCommand.Id, "Wrong command processed");
            }
        }
        public async Task ExecuteBuiltinCommandAsyncRepollAllTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var repollDeviceIdRequestSentToAdapter = new List <int>();
            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled         = true,
                    RepollAsyncDevice = async d => repollDeviceIdRequestSentToAdapter.Add(d.Id)
                }
            };
            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);
                context.Devices.Add(device2);
                var builtinCommand = new BuiltinCommand
                {
                    Name             = "Repoll all",
                    UniqueIdentifier = "REPOLL_ALL"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

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

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(repollDeviceIdRequestSentToAdapter.Count == 2, "Process did not run the correct amount of commands.");
                Assert.IsTrue(repollDeviceIdRequestSentToAdapter[0] == device.Id, "Wrong command processed");
                Assert.IsTrue(repollDeviceIdRequestSentToAdapter[1] == device2.Id, "Wrong command processed");
            }
        }
        public async Task ValueChangeTest()
        {
            //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 cts = new CancellationTokenSource();
            var tm  = new ChangeListener(log, dbConnection);

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

            var device = UnitTesting.CreateFakeDevice();

            device.Values.Add(new DeviceValue
            {
                UniqueIdentifier = "DeviceTestValue1",
                Value            = "0"
            });

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

                device.Values.First().Value = "1";
                await context.SaveChangesAsync(CancellationToken.None);
            }
            await Task.Delay(100, CancellationToken.None);

            await tm.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntries.Count == 3, "Unexpected number of log entries.");
            Assert.IsTrue(logEntries[1].Message.Contains("changed"), "Log message didn't contain changed.");
        }
        public async Task RegisterAsyncUpdatedDeviceValueTest()
        {
            //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();

                deviceValue.Value = false.ToString();

                //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.");
                Assert.AreEqual(dv.Value, false.ToString(), "Device value not updated properly");
                Console.WriteLine(result.Message);
            }
        }
        public async Task RegisterAsyncUpdateCommandTest()
        {
            //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"
            };

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

            deviceCommand.Name = "New 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 ExecuteDeviceTypeCommandAsyncAdapterNotEnabledTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = false,
                }
            };
            var ranstoredCommands = new List <int>();
            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

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

                //Act
                var result = await commmandProcessor.ExecuteDeviceTypeCommandAsync(deviceTypeCommand, "", device.Id.ToString(CultureInfo.InvariantCulture), 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("adapter is disabled"), "Expect error message to contain 'adapter is disabled'");
            }
        }
        public async Task RunCommandAsyncDeviceCommand()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceCommandAsyncDeviceDeviceCommandStringString = (adapterDevice, command, argument, argument2) => Task.FromResult(0)
                }
            };
            var log = new StubIFeedback <LogEntry>();

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

            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 commmandProcessor.RunCommandAsync(deviceCommand.Id, "", "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
            }
        }
        public async Task ExecuteBuiltinCommandAsyncRepollDisabledAdapterMeTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = false,
                }
            };
            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

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

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

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsTrue(result.HasError);
                Assert.IsTrue(result.Message.Contains("adapter is disabled"), "Expect error message to contain 'adapter is disabled'");
            }
        }
        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);
            }
        }
Exemple #15
0
        public async Task ContraLessThanTest()
        {
            //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     = "1",
                        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.All(o => o.Level != LogEntryLevel.Error), "Expected no error log entries");
            Assert.IsTrue(ranstoredCommands.Count == 0, "Trigger runner did not run the correct amount of commands.");
        }
Exemple #16
0
        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 ExecuteBuiltinCommandAsyncSceneTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var deviceCommandIds = new List <int>();
            var adapterManager   = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceCommandAsyncDeviceDeviceCommandStringString = async(adapterDevice, command, argument, argument2) => deviceCommandIds.Add(command.Id)
                }
            };
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    return(Task.FromResult(0));
                }
            };

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

            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);

                var scene = new Scene
                {
                    Name = "Test Scene"
                };
                scene.Commands.Add(new SceneStoredCommand
                {
                    Command  = deviceCommand,
                    Argument = "0"
                });
                context.Scenes.Add(scene);

                var builtinCommand = new BuiltinCommand
                {
                    Name             = "Activate Scene",
                    UniqueIdentifier = "RUN_SCENE"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

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

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(deviceCommandIds.Count == 1, "Process did not run the correct amount of commands.");
                Assert.IsTrue(deviceCommand.Id == deviceCommandIds[0], "Ran the wrong scene!");
            }
        }