Skip to content

Commit

Permalink
#2110 Add missing TU
Browse files Browse the repository at this point in the history
  • Loading branch information
delager committed Jun 9, 2023
1 parent 842ef18 commit edd72cc
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetThingShadowRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetThingShadowResponse
{
HttpStatusCode = HttpStatusCode.BadRequest
});

// Act
var act = () => this.awsDevicePropertyService.GetProperties(device.Id);

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}

[Test]
public async Task GetPropertiesShouldThrowInternalServerErrorExceptionWhenIssueOccursOnGettingProperties()
{
Expand Down Expand Up @@ -227,5 +254,35 @@ public async Task SetPropertiesShouldThrowInternalServerErrorExceptionWhenIssueO
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
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<DeviceModelProperty>());

_ = this.mockAmazonIotDataClient.Setup(iotDataClient => iotDataClient.UpdateThingShadowAsync(It.IsAny<UpdateThingShadowRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new UpdateThingShadowResponse
{
HttpStatusCode = HttpStatusCode.BadRequest
});

// Act
var act = () => this.awsDevicePropertyService.SetProperties(device.Id, null);

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,6 +121,59 @@ public async Task CreateADeviceShouldReturnAValue()
MockRepository.VerifyAll();
}

[Test]
public async Task CreateDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForCreateThing()
{
// Arrange
var deviceDto = new DeviceDetails()
{
DeviceID = Fixture.Create<string>()
};

_ = this.mockAmazonIotClient.Setup(service => service.CreateThingAsync(It.IsAny<CreateThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateThingResponse()
{
HttpStatusCode = HttpStatusCode.BadRequest
});

//Act
var result = () => this.awsDeviceService.CreateDevice(deviceDto);

//Assert
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}

[Test]
public async Task CreateDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForUpdateThingShadow()
{
// Arrange
var deviceDto = new DeviceDetails()
{
DeviceID = Fixture.Create<string>()
};

_ = this.mockAmazonIotClient.Setup(service => service.CreateThingAsync(It.IsAny<CreateThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateThingResponse()
{
ThingId = deviceDto.DeviceID,
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIotDataClient.Setup(service => service.UpdateThingShadowAsync(It.IsAny<UpdateThingShadowRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new UpdateThingShadowResponse()
{
HttpStatusCode = HttpStatusCode.BadRequest
});

//Act
var result = () => this.awsDeviceService.CreateDevice(deviceDto);

//Assert
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}

[Test]
public async Task CreateDeviceDuplicateExceptionIsThrown()
{
Expand Down Expand Up @@ -205,6 +259,30 @@ public async Task UpdateDeviceShouldReturnValue()
MockRepository.VerifyAll();
}

[Test]
public async Task UpdateDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForUpdateThing()
{
// Arrange
var deviceDto = new DeviceDetails
{
DeviceID = Fixture.Create<string>(),
DeviceName = Fixture.Create<string>(),
};

_ = this.mockAmazonIotClient.Setup(service => service.UpdateThingAsync(It.IsAny<UpdateThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new UpdateThingResponse()
{
HttpStatusCode = HttpStatusCode.BadRequest
});

// Act
var result = () => this.awsDeviceService.UpdateDevice(deviceDto);

// Assert
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}

[Test]
public async Task UpdateDeviceThatNotExistThrowResourceNotFoundException()
{
Expand Down Expand Up @@ -287,6 +365,16 @@ public async Task DeleteDevice()

_ = this.mockAmazonIotClient.Setup(service => service.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse()
{
Principals = new List<string>()
{
Fixture.Create<string>()
},
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIotClient.Setup(service => service.DetachThingPrincipalAsync(It.IsAny<DetachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachThingPrincipalResponse()
{
HttpStatusCode = HttpStatusCode.OK
});
Expand Down Expand Up @@ -323,7 +411,7 @@ public async Task DeleteDevice()
}

[Test]
public Task DeleteDeviceResourceNotFoundExceptionIfDeviceNotExist()
public async Task DeleteDeviceResourceNotFoundExceptionIfDeviceNotExist()
{
// Arrange
var deviceDto = new DeviceDetails
Expand All @@ -338,9 +426,133 @@ public Task DeleteDeviceResourceNotFoundExceptionIfDeviceNotExist()
var response = () => this.awsDeviceService.DeleteDevice(deviceDto.DeviceID);

// Assert
_ = response.Should().ThrowAsync<ResourceNotFoundException>();
_ = await response.Should().ThrowAsync<ResourceNotFoundException>();
MockRepository.VerifyAll();
}

[Test]
public async Task DeleteDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForListThingPrincipals()
{
// Arrange
var deviceDto = new DeviceDetails
{
DeviceID = Fixture.Create<string>()
};

var device = new Device
{
Id = deviceDto.DeviceID,
Tags = Fixture.CreateMany<DeviceTagValue>(5).ToList(),
Labels = Fixture.CreateMany<Label>(5).ToList()
};

_ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(device);

_ = this.mockAmazonIotClient.Setup(service => service.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse()
{
HttpStatusCode = HttpStatusCode.BadRequest
});

// Act
var result = () => this.awsDeviceService.DeleteDevice(deviceDto.DeviceID);

// Assert
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}

[Test]
public async Task DeleteDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForDetachThingPrincipal()
{
// Arrange
var deviceDto = new DeviceDetails
{
DeviceID = Fixture.Create<string>()
};

var device = new Device
{
Id = deviceDto.DeviceID,
Tags = Fixture.CreateMany<DeviceTagValue>(5).ToList(),
Labels = Fixture.CreateMany<Label>(5).ToList()
};

_ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(device);

_ = this.mockAmazonIotClient.Setup(service => service.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse()
{
Principals = new List<string>()
{
Fixture.Create<string>()
},
HttpStatusCode = HttpStatusCode.OK
}); ;

_ = this.mockAmazonIotClient.Setup(service => service.DetachThingPrincipalAsync(It.IsAny<DetachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachThingPrincipalResponse()
{
HttpStatusCode = HttpStatusCode.BadRequest
});

// Act
var result = () => this.awsDeviceService.DeleteDevice(deviceDto.DeviceID);

// Assert
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
}

[Test]
public async Task DeleteDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOKForDeleteThing()
{
// Arrange
var deviceDto = new DeviceDetails
{
DeviceID = Fixture.Create<string>()
};

var device = new Device
{
Id = deviceDto.DeviceID,
Tags = Fixture.CreateMany<DeviceTagValue>(5).ToList(),
Labels = Fixture.CreateMany<Label>(5).ToList()
};

_ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(device);

_ = this.mockAmazonIotClient.Setup(service => service.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse()
{
Principals = new List<string>()
{
Fixture.Create<string>()
},
HttpStatusCode = HttpStatusCode.OK
}); ;

_ = this.mockAmazonIotClient.Setup(service => service.DetachThingPrincipalAsync(It.IsAny<DetachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachThingPrincipalResponse()
{
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIotClient.Setup(service => service.DeleteThingAsync(It.IsAny<DeleteThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteThingResponse()
{
HttpStatusCode = HttpStatusCode.BadRequest
});

// Act
var result = () => this.awsDeviceService.DeleteDevice(deviceDto.DeviceID);

// Assert
_ = await result.Should().ThrowAsync<InternalServerErrorException>();
MockRepository.VerifyAll();
return Task.CompletedTask;
}

[Test]
Expand Down
Loading

0 comments on commit edd72cc

Please sign in to comment.