public async Task Handler_should_return_true() { var fakeSensor = new PortableSensor { Id = 1, IsActive = false }; var fakeSensorDbSet = new List <Sensor> { fakeSensor }; _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(fakeSensorDbSet); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); var cancellationToken = new CancellationToken(); var command = new ChangeSensorActivationStateCommand(fakeSensor.Id, !fakeSensor.IsActive); var handler = new ChangeSensorActivationStateCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act var result = await handler.Handle(command, cancellationToken); //Assert Assert.True(result); }
public async Task Handler_should_publish_notification() { var fakeSensor = new PortableSensor { Id = 1, IsActive = false }; var fakeSensorDbSet = new List <Sensor> { fakeSensor }; _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(fakeSensorDbSet); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); var cancellationToken = new CancellationToken(); var command = new ChangeSensorActivationStateCommand(fakeSensor.Id, !fakeSensor.IsActive); var handler = new ChangeSensorActivationStateCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act await handler.Handle(command, cancellationToken); //Assert _mediatorMock.Verify(x => x.Publish(It.Is <SensorActivationStateChangedNotification>(it => it.SensorId == fakeSensor.Id), It.Is <CancellationToken>(it => it == cancellationToken)), Times.Once()); }
public async Task Handler_should_dispatch_coordinates_for_portable_sensor() { //Arrange var fakeSensor = new PortableSensor { Id = 1 }; var fakeSensorReading = new PortableSensorReading { Id = 1, Latitude = 53, Longitude = 53 }; var cancellationToken = new CancellationToken(); var notification = new PortableReadingCreatedNotification(fakeSensor.Id, fakeSensorReading); var handler = new PortableSensorReadingCreatedNotificationHandler(_adminDispatchHelperMock.Object); //Act await handler.Handle(notification, cancellationToken); //Assert _adminDispatchHelperMock.Verify(x => x.DispatchCoordinatesForPortableSensorAsync(It.Is <int>(it => it == fakeSensor.Id), It.Is <double>(it => it == fakeSensorReading.Latitude), It.Is <double>(it => it == fakeSensorReading.Longitude)), Times.Once); }
public async Task Handler_return_true_if_reading_is_for_portable_sensor() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); var fakePortableSensor = new PortableSensor() { Id = 1, ApiKey = apiKey }; var fakeReading = new SensorReadingDTO(); _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor> { fakePortableSensor }); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); //Act var cancellationToken = new CancellationToken(); var command = new CreateReadingCommand(fakeReading, apiKey); var handler = new CreateReadingCommandHandler(_mediatorMock.Object, _dataContextFactoryMock.Object, _mapperMock.Object); var result = await handler.Handle(command, cancellationToken); //Assert Assert.True(result); }
public async Task Handler_publish_notification_if_reading_is_for_portable_sensor() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); var fakePortableSensor = new PortableSensor() { Id = 1, ApiKey = apiKey }; var fakePortableSensorReading = new PortableSensorReading(); var fakeReading = new SensorReadingDTO(); _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor> { fakePortableSensor }); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); _mapperMock.Setup(x => x.Map <SensorReadingDTO, PortableSensorReading>(fakeReading)) .Returns(fakePortableSensorReading); //Act var cancellationToken = new CancellationToken(); var command = new CreateReadingCommand(fakeReading, apiKey); var handler = new CreateReadingCommandHandler(_mediatorMock.Object, _dataContextFactoryMock.Object, _mapperMock.Object); var result = await handler.Handle(command, cancellationToken); //Assert _mediatorMock.Verify( x => x.Publish(It.Is <PortableReadingCreatedNotification>(it => it.SensorId == fakePortableSensor.Id && it.Reading == fakePortableSensorReading), It.Is <CancellationToken>(it => it == cancellationToken)), Times.Once); }
private (SensorEmulator emulator, PortableSensor sensor) GetPortableFakeSensor(string guid) { var apiKey = CryptoHelper.GenerateApiKey(); var sensorEmulator = new SensorEmulator(guid, _appSettings.ServerUrl, apiKey, typeof(PortableSensor)); var sensor = new PortableSensor { ApiKey = apiKey }; return(sensorEmulator, sensor); }
public async Task <PortableSensorDTO> Handle(CreatePortableSensorCommand request, CancellationToken cancellationToken) { await using var context = _dataContextFactory.Create(); var sensor = new PortableSensor { ApiKey = request.ApiKey }; await context.PortableSensors.AddAsync(sensor, cancellationToken); await context.SaveChangesAsync(cancellationToken); return(_mapper.Map <PortableSensor, PortableSensorDTO>(sensor)); }