diff --git a/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/DevicesControllerTests.cs b/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/DevicesControllerTests.cs index 03f47d285..6e1cc2fc1 100644 --- a/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/DevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/DevicesControllerTests.cs @@ -191,6 +191,9 @@ public async Task CreateDeviceAsyncStateUnderTestExpectedBehavior() Twin twin = null; + _ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny())) + .ReturnsAsync((Device)null); + _ = this.mockDeviceTwinMapper.Setup(c => c.UpdateTwin(It.Is(x => x.DeviceId == device.DeviceID), It.Is(x => x == device))) .Callback((t, _) => twin = t); @@ -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())) + .ReturnsAsync(new Device()); + + // Act + //var result = await existingDevice.GetItem(deviceID); + var result = async () => await devicesController.CreateDeviceAsync(new DeviceDetails()); + + // Assert + //Assert.IsNotNull(result); + //Assert.IsAssignableFrom(result); + _ = await result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + } } diff --git a/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs index 4237f61d8..5457d6491 100644 --- a/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs @@ -396,6 +396,9 @@ public async Task CreateDeviceAsyncStateUnderTestExpectedBehavior() Twin twin = null; + _ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny())) + .ReturnsAsync((Device)null); + _ = this.mockDeviceTwinMapper.Setup(c => c.UpdateTwin(It.Is(x => x.DeviceId == device.DeviceID), It.Is(x => x == device))) .Callback((t, _) => twin = t); diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/DevicesControllerBase.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/DevicesControllerBase.cs index f8151a615..247ae155e 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/DevicesControllerBase.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/DevicesControllerBase.cs @@ -152,6 +152,12 @@ public virtual async Task 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() {