public async Task Handler_should_return_true()
        {
            var fakeSensor = new StaticSensor()
            {
                Id        = 1,
                IsVisible = false
            };
            var fakeSensorDbSet = new List <StaticSensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.StaticSensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeStaticSensorVisibilityStateCommand(fakeSensor.Id, !fakeSensor.IsVisible);
            var handler           =
                new ChangeStaticSensorVisibilityStateCommandHandler(_dataContextFactoryMock.Object,
                                                                    _mediatorMock.Object);

            //Act
            var result = await handler.Handle(command, cancellationToken);

            //Assert
            Assert.True(result);
        }
        public async Task Handler_database_should_be_updated()
        {
            var fakeSensor = new StaticSensor()
            {
                Id        = 1,
                IsVisible = false
            };
            var fakeSensorDbSet = new List <StaticSensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.StaticSensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeStaticSensorVisibilityStateCommand(fakeSensor.Id, !fakeSensor.IsVisible);
            var handler           =
                new ChangeStaticSensorVisibilityStateCommandHandler(_dataContextFactoryMock.Object,
                                                                    _mediatorMock.Object);

            //Act
            await handler.Handle(command, cancellationToken);

            //Assert
            _dataContextMock.Verify(x => x.SaveChangesAsync(It.Is <CancellationToken>(it => it == cancellationToken)),
                                    Times.Once);
        }
        public async Task Handler_should_publish_notification()
        {
            var fakeSensor = new StaticSensor
            {
                Id       = 1,
                IsActive = false
            };
            var fakeSensorDbSet = new List <StaticSensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.StaticSensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeStaticSensorVisibilityStateCommand(fakeSensor.Id, !fakeSensor.IsActive);
            var handler           =
                new ChangeStaticSensorVisibilityStateCommandHandler(_dataContextFactoryMock.Object,
                                                                    _mediatorMock.Object);

            //Act
            await handler.Handle(command, cancellationToken);

            //Assert
            _mediatorMock.Verify(x =>
                                 x.Publish(It.Is <StaticSensorVisibilityStateChangedNotification>(it => it.SensorId == fakeSensor.Id),
                                           It.Is <CancellationToken>(it => it == cancellationToken)),
                                 Times.Once());
        }
        public async Task Handler_activation_state_should_be_changed(bool newVisibilityState)
        {
            var fakeSensor = new StaticSensor()
            {
                Id        = 1,
                IsVisible = !newVisibilityState
            };
            var fakeSensorDbSet = new List <StaticSensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.StaticSensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeStaticSensorVisibilityStateCommand(fakeSensor.Id, newVisibilityState);
            var handler           =
                new ChangeStaticSensorVisibilityStateCommandHandler(_dataContextFactoryMock.Object,
                                                                    _mediatorMock.Object);

            //Act
            await handler.Handle(command, cancellationToken);

            //Assert
            Assert.Equal(newVisibilityState, fakeSensor.IsVisible);
        }
        public async Task Handler_should_throw_not_found_exception_if_sensor_doesnt_exist()
        {
            _dataContextMock.Setup(x => x.StaticSensors).ReturnsDbSet(new List <StaticSensor>());
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeStaticSensorVisibilityStateCommand(1, false);
            var handler           =
                new ChangeStaticSensorVisibilityStateCommandHandler(_dataContextFactoryMock.Object,
                                                                    _mediatorMock.Object);

            //Act
            Task Act() => handler.Handle(command, cancellationToken);

            //Assert
            await Assert.ThrowsAsync <SensorNotFoundException>(Act);
        }