public void Test_SetPower_ChangesAdapterLevel(byte fullPower, byte zeroPower, double requestedPower, byte expectedLevel)
        {
            var configRepositoryMock = new Mock <INooliteSwitchInfoRepository>(MockBehavior.Strict);
            var config = new NooliteSwitchInfo
            {
                SwitchId       = SwitchId.NewId(),
                Channel        = 98,
                FullPowerLevel = fullPower,
                ZeroPowerLevel = zeroPower
            };

            configRepositoryMock
            .Setup(repository => repository.ContainsConfig(config.SwitchId))
            .Returns(Task.FromResult(true));
            configRepositoryMock
            .Setup(repository => repository.GetConfig(config.SwitchId))
            .Returns(Task.FromResult(config));
            var adapterMock = new Mock <IPC11XXAdapter>(MockBehavior.Strict);

            adapterMock.Setup(adapter => adapter.SendCommand(PC11XXCommand.SetLevel, config.Channel, expectedLevel));
            var controller = new NooliteSwitchController(configRepositoryMock.Object, adapterMock.Object);

            controller.SetPower(config.SwitchId, requestedPower);

            adapterMock.Verify(adapter => adapter.SendCommand(PC11XXCommand.SetLevel, config.Channel, expectedLevel), Times.Once);
        }
        public void Test_IfRepoDoesNotContainConfig_ThenCantHandle()
        {
            var configRepositoryMock = new Mock <INooliteSwitchInfoRepository>(MockBehavior.Strict);

            configRepositoryMock
            .Setup(repository => repository.ContainsConfig(It.IsAny <SwitchId>()))
            .Returns(Task.FromResult(false));

            var controller = new NooliteSwitchController(configRepositoryMock.Object, Mock.Of <IPC11XXAdapter>());

            Assert.False(controller.CanHandleSwitch(SwitchId.NewId()));

            configRepositoryMock.Verify(repo => repo.ContainsConfig(It.IsAny <SwitchId>()), Times.Once);
        }
        public void Test_IfRepoContainsConfig_ThenCanHandle()
        {
            var configRepositoryMock = new Mock <INooliteSwitchInfoRepository>(MockBehavior.Strict);
            var switchId             = SwitchId.NewId();

            configRepositoryMock
            .Setup(repository => repository.ContainsConfig(switchId))
            .Returns(Task.FromResult(true));

            var controller = new NooliteSwitchController(configRepositoryMock.Object, Mock.Of <IPC11XXAdapter>());

            Assert.True(controller.CanHandleSwitch(switchId));

            configRepositoryMock.Verify(repo => repo.ContainsConfig(switchId), Times.Once);
        }
示例#4
0
        public async void Test_ConsecutiveDisableEvents_AreOk()
        {
            var config = new SwitchToSensorBinding {
                SwitchId = SwitchId.NewId(), SensorId = SensorId.NewId()
            };
            IReadOnlyCollection <SwitchToSensorBinding> configCollection = new [] { config };
            var bindingsRepoMock = new Mock <ISwitchToSensorBindingsRepository>(MockBehavior.Strict);

            bindingsRepoMock.Setup(m => m.GetAll()).Returns(Task.FromResult(configCollection));
            var handler = new BindingController(Mock.Of <IEventSender>(), bindingsRepoMock.Object);

            await handler.DisableBinding(config.SwitchId, config.SensorId);

            await handler.DisableBinding(config.SwitchId, config.SensorId);
        }
        public void Test_WhenReceivedDisableEvent_ThenDisableBinding()
        {
            var @event           = new DisableBindingEvent(SwitchId.NewId(), SensorId.NewId());
            var stateManagerMock = new Mock <IBindingStateManager>(MockBehavior.Strict);

            stateManagerMock.Setup(manager => manager.DisableBinding(@event.SwitchId, @event.SensorId)).Returns(Task.CompletedTask);;
            var eventsSourceMock = new Mock <IEventSource>(MockBehavior.Strict);

            eventsSourceMock.Setup(e => e.ReceiveEvents <AbstractBindingEvent>()).Returns(Observable.Repeat(@event, 1));
            var handler = new BindingEventsProcessor(stateManagerMock.Object, eventsSourceMock.Object, Mock.Of <ILogger>());

            handler.Run(CancellationToken.None);

            stateManagerMock.Verify(manager => manager.DisableBinding(@event.SwitchId, @event.SensorId), Times.Once);
        }
示例#6
0
        public async void TestBindingEnabledByDefault_TurnOn()
        {
            var config = new SwitchToSensorBinding {
                SwitchId = SwitchId.NewId(), SensorId = SensorId.NewId()
            };
            IReadOnlyCollection <SwitchToSensorBinding> configCollection = new [] { config };
            var bindingsRepoMock = new Mock <ISwitchToSensorBindingsRepository>(MockBehavior.Strict);

            bindingsRepoMock.Setup(m => m.GetAll()).Returns(Task.FromResult(configCollection));
            var publisherMock = new Mock <IEventSender>(MockBehavior.Strict);

            publisherMock.Setup(m => m.SendEvent(It.Is <TurnOnEvent>(e => e.SwitchId == config.SwitchId)));
            var handler = new BindingController(publisherMock.Object, bindingsRepoMock.Object);

            await handler.ProcessSensorActivation(config.SensorId);

            publisherMock.Verify(m => m.SendEvent(It.IsAny <TurnOnEvent>()), Times.Once);
        }
        public void TestTurnOff()
        {
            var swicthId             = SwitchId.NewId();
            var offEvent             = new TurnOffEvent(swicthId);
            var switchControllerMock = new Mock <ISwitchController>(MockBehavior.Strict);

            switchControllerMock.Setup(cntr => cntr.CanHandleSwitch(swicthId)).Returns(true);
            switchControllerMock.Setup(cntr => cntr.TurnOff(swicthId));
            var eventsSourceMock = new Mock <IEventSource>(MockBehavior.Strict);

            eventsSourceMock.Setup(e => e.ReceiveEvents <AbstractSwitchEvent>()).Returns(Observable.Repeat(offEvent, 1));

            var handler = new SwitchEventsProcessor(switchControllerMock.Object, eventsSourceMock.Object, Mock.Of <ILogger>());

            handler.Run(CancellationToken.None);

            switchControllerMock.Verify(sc => sc.CanHandleSwitch(swicthId), Times.Once);
            switchControllerMock.Verify(sc => sc.TurnOff(swicthId), Times.Once);
        }
示例#8
0
        public async void TestWhenUnknownBinding_ThenError()
        {
            IReadOnlyCollection <SwitchToSensorBinding> config = new []
            {
                new SwitchToSensorBinding {
                    SwitchId = SwitchId.NewId(), SensorId = SensorId.NewId()
                }
            };
            var bindingsRepoMock = new Mock <ISwitchToSensorBindingsRepository>(MockBehavior.Strict);

            bindingsRepoMock.Setup(m => m.GetAll()).Returns(Task.FromResult(config));
            var publisherMock = new Mock <IEventSender>(MockBehavior.Strict);
            var handler       = new BindingController(publisherMock.Object, bindingsRepoMock.Object);

            await Assert.ThrowsAsync <InvalidOperationException>(() => handler.EnableBinding(SwitchId.NewId(), SensorId.NewId()));

            await Assert.ThrowsAsync <InvalidOperationException>(() => handler.DisableBinding(SwitchId.NewId(), SensorId.NewId()));

            publisherMock.Verify(m => m.SendEvent(It.IsAny <IEvent>()), Times.Never);
        }
示例#9
0
        public async void TestWnehUnknownSensor_ThenDoNothing()
        {
            IReadOnlyCollection <SwitchToSensorBinding> config = new []
            {
                new SwitchToSensorBinding {
                    SwitchId = SwitchId.NewId(), SensorId = SensorId.NewId()
                }
            };
            var bindingsRepoMock = new Mock <ISwitchToSensorBindingsRepository>(MockBehavior.Strict);

            bindingsRepoMock.Setup(m => m.GetAll()).Returns(Task.FromResult(config));
            var publisherMock = new Mock <IEventSender>(MockBehavior.Strict);
            var handler       = new BindingController(publisherMock.Object, bindingsRepoMock.Object);

            await handler.ProcessSensorActivation(SensorId.NewId());

            await handler.ProcessSensorDeactivation(SensorId.NewId());

            publisherMock.Verify(m => m.SendEvent(It.IsAny <IEvent>()), Times.Never);
        }
        public void Test_TurnOff_SendsAdapterOffCommand()
        {
            var switchId             = SwitchId.NewId();
            var configRepositoryMock = new Mock <INooliteSwitchInfoRepository>(MockBehavior.Strict);
            var adapterMock          = new Mock <IPC11XXAdapter>();
            var config = new NooliteSwitchInfo {
                Channel = 98
            };

            configRepositoryMock
            .Setup(repository => repository.ContainsConfig(switchId))
            .Returns(Task.FromResult(true));
            configRepositoryMock
            .Setup(repository => repository.GetConfig(switchId))
            .Returns(Task.FromResult(config));

            var controller = new NooliteSwitchController(configRepositoryMock.Object, adapterMock.Object);

            controller.TurnOff(switchId);

            adapterMock.Verify(adapter => adapter.SendCommand(PC11XXCommand.Off, config.Channel, 0), Times.Once);
        }
示例#11
0
        public async void TestWhenSensorBindToMultipleSwitches_ThenTurnOffThemAll()
        {
            var sensorId = SensorId.NewId();
            var config1  = new SwitchToSensorBinding {
                SwitchId = SwitchId.NewId(), SensorId = sensorId
            };
            var config2 = new SwitchToSensorBinding {
                SwitchId = SwitchId.NewId(), SensorId = sensorId
            };
            IReadOnlyCollection <SwitchToSensorBinding> configCollection = new [] { config1, config2 };
            var bindingsRepoMock = new Mock <ISwitchToSensorBindingsRepository>(MockBehavior.Strict);

            bindingsRepoMock.Setup(m => m.GetAll()).Returns(Task.FromResult(configCollection));
            var publisherMock = new Mock <IEventSender>(MockBehavior.Strict);

            publisherMock.Setup(m => m.SendEvent(It.Is <TurnOffEvent>(e => e.SwitchId == config1.SwitchId)));
            publisherMock.Setup(m => m.SendEvent(It.Is <TurnOffEvent>(e => e.SwitchId == config2.SwitchId)));
            var handler = new BindingController(publisherMock.Object, bindingsRepoMock.Object);

            await handler.ProcessSensorDeactivation(sensorId);

            publisherMock.Verify(m => m.SendEvent(It.IsAny <TurnOffEvent>()), Times.Exactly(2));
        }