From edd72cc52090d795f2d136c75451abc2dc7f9201 Mon Sep 17 00:00:00 2001 From: delager Date: Fri, 9 Jun 2023 10:08:13 +0200 Subject: [PATCH] #2110 Add missing TU --- .../Services/AWSDevicePropertyServiceTests.cs | 57 +++++ .../Services/AWSDeviceServiceTests.cs | 218 +++++++++++++++++- .../Services/ExternalDeviceServiceTests.cs | 70 ++++++ 3 files changed, 342 insertions(+), 3 deletions(-) diff --git a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs index 91a11531b..ee5aac341 100644 --- a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs +++ b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs @@ -90,6 +90,33 @@ public async Task WhenDeviceIdNotExistGetPropertiesShouldThrowResourceNotFoundEx MockRepository.VerifyAll(); } + [Test] + public async Task GetPropertiesShouldThrowInternalServerErrorExceptionWhenHttpStatusCodeIsNotOKForGetThingShadow() + { + // Arrange + var device = new Device() + { + Id = "aaa", + DeviceModelId = "bbb" + }; + + _ = this.mockDeviceRepository.Setup(c => c.GetByIdAsync("aaa")) + .ReturnsAsync(device); + + _ = this.mockAmazonIotDataClient.Setup(iotDataClient => iotDataClient.GetThingShadowAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new GetThingShadowResponse + { + HttpStatusCode = HttpStatusCode.BadRequest + }); + + // Act + var act = () => this.awsDevicePropertyService.GetProperties(device.Id); + + // Assert + _ = await act.Should().ThrowAsync(); + MockRepository.VerifyAll(); + } + [Test] public async Task GetPropertiesShouldThrowInternalServerErrorExceptionWhenIssueOccursOnGettingProperties() { @@ -227,5 +254,35 @@ public async Task SetPropertiesShouldThrowInternalServerErrorExceptionWhenIssueO _ = await act.Should().ThrowAsync(); MockRepository.VerifyAll(); } + + [Test] + public async Task SetPropertiesShouldThrowInternalServerErrorExceptionWhenHttpStatusIsNotOKForUpdateThingShadow() + { + // Arrange + var device = new Device() + { + Id = "aaa", + DeviceModelId = "bbb" + }; + + _ = this.mockDeviceRepository.Setup(c => c.GetByIdAsync(device.Id)) + .ReturnsAsync(device); + + _ = this.mockDeviceModelPropertiesService.Setup(c => c.GetModelProperties(device.DeviceModelId)) + .ReturnsAsync(Enumerable.Empty()); + + _ = this.mockAmazonIotDataClient.Setup(iotDataClient => iotDataClient.UpdateThingShadowAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new UpdateThingShadowResponse + { + HttpStatusCode = HttpStatusCode.BadRequest + }); + + // Act + var act = () => this.awsDevicePropertyService.SetProperties(device.Id, null); + + // Assert + _ = await act.Should().ThrowAsync(); + MockRepository.VerifyAll(); + } } } diff --git a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs index 7ace262fa..f6282b98d 100644 --- a/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs +++ b/src/IoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs @@ -31,6 +31,7 @@ namespace IoTHub.Portal.Tests.Unit.Infrastructure.Services using Device = Portal.Domain.Entities.Device; using ResourceNotFoundException = Portal.Domain.Exceptions.ResourceNotFoundException; using System.Threading; + using IoTHub.Portal.Domain.Exceptions; [TestFixture] public class AWSDeviceServiceTests : BackendUnitTest @@ -120,6 +121,59 @@ public async Task CreateADeviceShouldReturnAValue() MockRepository.VerifyAll(); } + [Test] + public async Task CreateDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForCreateThing() + { + // Arrange + var deviceDto = new DeviceDetails() + { + DeviceID = Fixture.Create() + }; + + _ = this.mockAmazonIotClient.Setup(service => service.CreateThingAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new CreateThingResponse() + { + HttpStatusCode = HttpStatusCode.BadRequest + }); + + //Act + var result = () => this.awsDeviceService.CreateDevice(deviceDto); + + //Assert + _ = await result.Should().ThrowAsync(); + MockRepository.VerifyAll(); + } + + [Test] + public async Task CreateDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForUpdateThingShadow() + { + // Arrange + var deviceDto = new DeviceDetails() + { + DeviceID = Fixture.Create() + }; + + _ = this.mockAmazonIotClient.Setup(service => service.CreateThingAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new CreateThingResponse() + { + ThingId = deviceDto.DeviceID, + HttpStatusCode = HttpStatusCode.OK + }); + + _ = this.mockAmazonIotDataClient.Setup(service => service.UpdateThingShadowAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new UpdateThingShadowResponse() + { + HttpStatusCode = HttpStatusCode.BadRequest + }); + + //Act + var result = () => this.awsDeviceService.CreateDevice(deviceDto); + + //Assert + _ = await result.Should().ThrowAsync(); + MockRepository.VerifyAll(); + } + [Test] public async Task CreateDeviceDuplicateExceptionIsThrown() { @@ -205,6 +259,30 @@ public async Task UpdateDeviceShouldReturnValue() MockRepository.VerifyAll(); } + [Test] + public async Task UpdateDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForUpdateThing() + { + // Arrange + var deviceDto = new DeviceDetails + { + DeviceID = Fixture.Create(), + DeviceName = Fixture.Create(), + }; + + _ = this.mockAmazonIotClient.Setup(service => service.UpdateThingAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new UpdateThingResponse() + { + HttpStatusCode = HttpStatusCode.BadRequest + }); + + // Act + var result = () => this.awsDeviceService.UpdateDevice(deviceDto); + + // Assert + _ = await result.Should().ThrowAsync(); + MockRepository.VerifyAll(); + } + [Test] public async Task UpdateDeviceThatNotExistThrowResourceNotFoundException() { @@ -287,6 +365,16 @@ public async Task DeleteDevice() _ = this.mockAmazonIotClient.Setup(service => service.ListThingPrincipalsAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new ListThingPrincipalsResponse() + { + Principals = new List() + { + Fixture.Create() + }, + HttpStatusCode = HttpStatusCode.OK + }); + + _ = this.mockAmazonIotClient.Setup(service => service.DetachThingPrincipalAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new DetachThingPrincipalResponse() { HttpStatusCode = HttpStatusCode.OK }); @@ -323,7 +411,7 @@ public async Task DeleteDevice() } [Test] - public Task DeleteDeviceResourceNotFoundExceptionIfDeviceNotExist() + public async Task DeleteDeviceResourceNotFoundExceptionIfDeviceNotExist() { // Arrange var deviceDto = new DeviceDetails @@ -338,9 +426,133 @@ public Task DeleteDeviceResourceNotFoundExceptionIfDeviceNotExist() var response = () => this.awsDeviceService.DeleteDevice(deviceDto.DeviceID); // Assert - _ = response.Should().ThrowAsync(); + _ = await response.Should().ThrowAsync(); + MockRepository.VerifyAll(); + } + + [Test] + public async Task DeleteDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForListThingPrincipals() + { + // Arrange + var deviceDto = new DeviceDetails + { + DeviceID = Fixture.Create() + }; + + var device = new Device + { + Id = deviceDto.DeviceID, + Tags = Fixture.CreateMany(5).ToList(), + Labels = Fixture.CreateMany