Ejemplo n.º 1
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);
        }
Ejemplo n.º 3
0
        public void TestDeactivation()
        {
            var sensorId = SensorId.NewId();
            var sensorDeactivatedEvent = new SensorDeactivatedEvent(sensorId);
            var controllerMock         = new Mock <IBindingController>(MockBehavior.Strict);

            controllerMock.Setup(controller => controller.ProcessSensorDeactivation(sensorId)).Returns(Task.CompletedTask);;
            var eventsSourceMock = new Mock <IEventSource>(MockBehavior.Strict);

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

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

            handler.Run(CancellationToken.None);

            controllerMock.Verify(controller => controller.ProcessSensorDeactivation(sensorId), Times.Once);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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));
        }
Ejemplo n.º 8
0
        public void TestWhenAdapterReceivedCommand_ThenRaiseEvent(byte command, int expectedActivateCallCount, int expectedDeactivateCallCount)
        {
            var sensorConfig = new NooliteSensorInfo {
                SensorId = SensorId.NewId(), Channel = 17
            };
            var configMock = new Mock <INooliteSensorInfoRepository>();

            configMock.Setup(cfg => cfg.GetAll()).Returns(Task.FromResult <IReadOnlyCollection <NooliteSensorInfo> >(new[] { sensorConfig }));

            var gateMock = new Mock <IEventSender>(MockBehavior.Strict);

            gateMock.Setup(g => g.SendEvent(It.Is <SensorActivatedEvent>(e => e.SensorId == sensorConfig.SensorId)));
            gateMock.Setup(g => g.SendEvent(It.Is <SensorDeactivatedEvent>(e => e.SensorId == sensorConfig.SensorId)));

            var adapterMock = new Mock <IRX2164Adapter>();
            var sensor      = new NooliteSensor(gateMock.Object, adapterMock.Object, configMock.Object, Mock.Of <ILogger>());

            sensor.Activate();

            adapterMock.Raise(ad => ad.CommandReceived += null, CreateCommandData(command, sensorConfig.Channel));

            gateMock.Verify(m => m.SendEvent(It.Is <SensorActivatedEvent>(e => e.SensorId == sensorConfig.SensorId)), Times.Exactly(expectedActivateCallCount));
            gateMock.Verify(m => m.SendEvent(It.Is <SensorDeactivatedEvent>(e => e.SensorId == sensorConfig.SensorId)), Times.Exactly(expectedDeactivateCallCount));
        }