//TODO: think more about this test public async Task Handler_should_update_database_in_any_case(int dataSetIndex, bool isCompletely) { //Arrange var sensors = new List <Sensor>() { new PortableSensor { Id = 1, }, new StaticSensor { Id = 1, } }; var fakeSensor = sensors[dataSetIndex]; 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 DeleteSensorCommand(fakeSensor.Id, isCompletely); var handler = new DeleteSensorCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act var result = await handler.Handle(command, cancellationToken); //Assert _dataContextMock.Verify(x => x.SaveChangesAsync(It.Is <CancellationToken>(it => it == cancellationToken)), Times.Once); }
//TODO: think more about this test public async Task Handler_should_set_is_removed_to_true_if_not_removing_completely(int dataSetIndex) { //Arrange var sensors = new List <Sensor>() { new PortableSensor { Id = 1, }, new StaticSensor { Id = 1, } }; var fakeSensor = sensors[dataSetIndex]; 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 DeleteSensorCommand(fakeSensor.Id, false); var handler = new DeleteSensorCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act var result = await handler.Handle(command, cancellationToken); //Assert Assert.True(fakeSensor.IsDeleted); }
//TODO: think more about this test public async Task Handler_should_call_remove_if_removing_completely(int dataSetIndex) { //Arrange var sensors = new List <Sensor>() { new PortableSensor { Id = 1, }, new StaticSensor { Id = 1, } }; var fakeSensor = sensors[dataSetIndex]; 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 DeleteSensorCommand(fakeSensor.Id, true); var handler = new DeleteSensorCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act var result = await handler.Handle(command, cancellationToken); //Assert _dataContextMock.Verify(x => x.Sensors.Remove(It.Is <Sensor>(it => it == fakeSensor)), Times.Once); }
//TODO: think more about this test public async Task Handler_should_return_true(int dataSetIndex, bool isCompletely) { var sensors = new List <Sensor>() { new PortableSensor { Id = 1, }, new StaticSensor { Id = 1, } }; var fakeSensor = sensors[dataSetIndex]; 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 DeleteSensorCommand(fakeSensor.Id, isCompletely); var handler = new DeleteSensorCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act var result = await handler.Handle(command, cancellationToken); //Assert Assert.True(result); }
public CommandResult Delete(string sensorId) { DeleteSensorCommand command = new DeleteSensorCommand(); command.SetSensorId(sensorId); return(Execute <DeleteSensorCommand, CommandResult>(command)); }
public async Task Handler_should_throw_not_found_exception_if_sensor_doesnt_exist() { //Arrange _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor>()); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); var cancellationToken = new CancellationToken(); var command = new DeleteSensorCommand(1, false); var handler = new DeleteSensorCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object); //Act Task Act() => handler.Handle(command, cancellationToken); //Assert await Assert.ThrowsAsync <SensorNotFoundException>(Act); }
public CommandResult Handle(DeleteSensorCommand command) { CommandResult result = new CommandResult(); ObjectId sensorId = new ObjectId(); if (!ObjectId.TryParse(command.SensorId, out sensorId)) { AddNotification(nameof(command.SensorId), ENotifications.InvalidFormat); } if (Valid) { Sensor sensor = _sensorRepository.Get(sensorId); if (sensor == null && _sensorRepository.Valid) { AddNotification(nameof(command.SensorId), ENotifications.NotFound); } if (Valid) { _sensorRepository.Delete(sensor); if (_sensorRepository.Valid) { result = new CommandResult(HttpStatusCode.OK); } } else { result = new CommandResult(HttpStatusCode.BadRequest, Notifications); } } else { result = new CommandResult(HttpStatusCode.BadRequest, Notifications); } return(result); }
public async Task <ICommandResult> Handle(DeleteSensorCommand command) { command.Validate(); if (command.Invalid) { return(new GenericCommandResult(false, "Invalid operation!", command.Notifications)); } var developerDb = await _sensorRepository.GetByIdAsync(command.Id); if (developerDb == null) { return(new GenericCommandResult(false, "Sensor not found!", command.Notifications)); } await _sensorRepository.DeleteAsync(command.Id); return(new GenericCommandResult(true, "Sensor removed!", developerDb)); }
public async Task <GenericCommandResult> Delete([FromBody] DeleteSensorCommand command, [FromServices] SensorHandler handler) { return((GenericCommandResult)await handler.Handle(command)); }