Skip to content

Commit

Permalink
Fix #843 - Issue when creating a new device if the DeviceID already e…
Browse files Browse the repository at this point in the history
…xists (#891)

* bug/843_DeviceID_already_exists

* Test unitaire

Co-authored-by: crib <[email protected]>
Co-authored-by: hocine hacherouf <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2022
1 parent 8d9b86c commit 0a10fa0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public async Task CreateDeviceAsyncStateUnderTestExpectedBehavior()

Twin twin = null;

_ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny<string>()))
.ReturnsAsync((Device)null);

_ = this.mockDeviceTwinMapper.Setup(c => c.UpdateTwin(It.Is<Twin>(x => x.DeviceId == device.DeviceID), It.Is<DeviceDetails>(x => x == device)))
.Callback<Twin, DeviceDetails>((t, _) => twin = t);

Expand Down Expand Up @@ -782,5 +785,26 @@ public async Task WhenPropertyNotInModelSetPropertiesShouldNotUpdateDesiredPrope

this.mockRepository.VerifyAll();
}

[Test]
public async Task CreateDeviceAsyncDuplicatedDeviceIdShouldThrowInternalServerErrorException()
{
// Arrange
var devicesController = CreateDevicesController();

_ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny<string>()))
.ReturnsAsync(new Device());

// Act
//var result = await existingDevice.GetItem(deviceID);
var result = async () => await devicesController.CreateDeviceAsync(new DeviceDetails());

// Assert
//Assert.IsNotNull(result);
//Assert.IsAssignableFrom<OkResult>(result);
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
this.mockRepository.VerifyAll();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ public async Task CreateDeviceAsyncStateUnderTestExpectedBehavior()

Twin twin = null;

_ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny<string>()))
.ReturnsAsync((Device)null);

_ = this.mockDeviceTwinMapper.Setup(c => c.UpdateTwin(It.Is<Twin>(x => x.DeviceId == device.DeviceID), It.Is<LoRaDeviceDetails>(x => x == device)))
.Callback<Twin, LoRaDeviceDetails>((t, _) => twin = t);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public virtual async Task<IActionResult> CreateDeviceAsync(TModel device)
throw new ProblemDetailsException(validation);
}

var existingDevice = await this.devicesService.GetDevice(device.DeviceID);
if (existingDevice != null)
{
throw new InternalServerErrorException($"The device with ID {device.DeviceID} already exists");
}

// Create a new Twin from the form's fields.
var newTwin = new Twin()
{
Expand Down

0 comments on commit 0a10fa0

Please sign in to comment.