Skip to content

Commit

Permalink
#2110 Add AWSExternalDeviceService TU
Browse files Browse the repository at this point in the history
  • Loading branch information
delager committed Jun 9, 2023
1 parent edd72cc commit 5daadb3
Showing 1 changed file with 212 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ namespace IoTHub.Portal.Tests.Unit.Infrastructure.Services
using Amazon.IotData;
using ListTagsForResourceRequest = Amazon.IoT.Model.ListTagsForResourceRequest;
using ListTagsForResourceResponse = Amazon.IoT.Model.ListTagsForResourceResponse;
using IoTHub.Portal.Domain.Entities;
using System.Net;
using Tag = Amazon.IoT.Model.Tag;
using Device = Portal.Domain.Entities.Device;

[TestFixture]
public class AwsExternalDeviceServiceTests : BackendUnitTest
Expand Down Expand Up @@ -426,7 +430,7 @@ public async Task RetrieveLastConfigurationShouldThrowNotImplementedException()
public async Task UpdateDeviceShouldThrowNotImplementedException()
{
// Act
var act = () => this.externalDeviceService.UpdateDevice(new Device());
var act = () => this.externalDeviceService.UpdateDevice(new Microsoft.Azure.Devices.Device());

// Assert
_ = await act.Should().ThrowAsync<NotImplementedException>();
Expand Down Expand Up @@ -598,5 +602,212 @@ public async Task RemoveDeviceCredentialsShouldRemoveDeviceCredentials()
_ = result.Should().NotBeNull();
}

[Test]
public async Task IsEdgeThingTypeReturnTrue()
{
// Arrange
var thingType = new DescribeThingTypeResponse()
{
ThingTypeArn = Fixture.Create<string>(),
ThingTypeId = Fixture.Create<string>(),
ThingTypeName = Fixture.Create<string>(),
ThingTypeMetadata = new ThingTypeMetadata()
};

_ = this.mockAmazonIot.Setup(client => client.DescribeThingTypeAsync(It.IsAny<DescribeThingTypeRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingTypeResponse
{
ThingTypeId = thingType.ThingTypeId,
ThingTypeName = thingType.ThingTypeName,
ThingTypeArn = thingType.ThingTypeArn,
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIot.Setup(client => client.ListTagsForResourceAsync(It.Is<ListTagsForResourceRequest>(c => c.ResourceArn == thingType.ThingTypeArn), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListTagsForResourceResponse
{
NextToken = Fixture.Create<string>(),
Tags = new List<Tag>
{
new Tag
{
Key = "iotEdge",
Value = "true"
}
}
});

//Act
var result = await this.externalDeviceService.IsEdgeDeviceModel(this.Mapper.Map<ExternalDeviceModelDto>(thingType));

//Assert
_ = result.Should().BeTrue();
MockRepository.VerifyAll();
}

[Test]
public async Task IsNotEdgeThingTypeReturnFalse()
{
// Arrange
var thingType = new DescribeThingTypeResponse()
{
ThingTypeArn = Fixture.Create<string>(),
ThingTypeId = Fixture.Create<string>(),
ThingTypeName = Fixture.Create<string>(),
ThingTypeMetadata = new ThingTypeMetadata()
};

_ = this.mockAmazonIot.Setup(client => client.DescribeThingTypeAsync(It.IsAny<DescribeThingTypeRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingTypeResponse
{
ThingTypeId = thingType.ThingTypeId,
ThingTypeName = thingType.ThingTypeName,
ThingTypeArn = thingType.ThingTypeArn,
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIot.Setup(client => client.ListTagsForResourceAsync(It.Is<ListTagsForResourceRequest>(c => c.ResourceArn == thingType.ThingTypeArn), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListTagsForResourceResponse
{
NextToken = Fixture.Create<string>(),
Tags = new List<Tag>
{
new Tag
{
Key = "iotEdge",
Value = "false"
}
}
});

//Act
var result = await this.externalDeviceService.IsEdgeDeviceModel(this.Mapper.Map<ExternalDeviceModelDto>(thingType));

//Assert
_ = result.Should().BeFalse();
MockRepository.VerifyAll();
}

[Test]
public async Task EdgeThingTypeNullReturnNull()
{
// Arrange
var thingType = new DescribeThingTypeResponse()
{
ThingTypeArn = Fixture.Create<string>(),
ThingTypeId = Fixture.Create<string>(),
ThingTypeName = Fixture.Create<string>(),
ThingTypeMetadata = new ThingTypeMetadata()
};

_ = this.mockAmazonIot.Setup(client => client.DescribeThingTypeAsync(It.IsAny<DescribeThingTypeRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingTypeResponse
{
ThingTypeId = thingType.ThingTypeId,
ThingTypeName = thingType.ThingTypeName,
ThingTypeArn = thingType.ThingTypeArn,
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIot.Setup(client => client.ListTagsForResourceAsync(It.Is<ListTagsForResourceRequest>(c => c.ResourceArn == thingType.ThingTypeArn), It.IsAny<CancellationToken>()))
.ReturnsAsync((ListTagsForResourceResponse)null);

//Act
var result = await this.externalDeviceService.IsEdgeDeviceModel(this.Mapper.Map<ExternalDeviceModelDto>(thingType));

//Assert
_ = result.Should().BeNull();
MockRepository.VerifyAll();
}

[Test]
public async Task EdgeThingTypeNotBooleanReturnNull()
{
// Arrange
var thingType = new DescribeThingTypeResponse()
{
ThingTypeArn = Fixture.Create<string>(),
ThingTypeId = Fixture.Create<string>(),
ThingTypeName = Fixture.Create<string>(),
ThingTypeMetadata = new ThingTypeMetadata()
};

_ = this.mockAmazonIot.Setup(client => client.DescribeThingTypeAsync(It.IsAny<DescribeThingTypeRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingTypeResponse
{
ThingTypeId = thingType.ThingTypeId,
ThingTypeName = thingType.ThingTypeName,
ThingTypeArn = thingType.ThingTypeArn,
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIot.Setup(client => client.ListTagsForResourceAsync(It.Is<ListTagsForResourceRequest>(c => c.ResourceArn == thingType.ThingTypeArn), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListTagsForResourceResponse
{
NextToken = Fixture.Create<string>(),
Tags = new List<Tag>
{
new Tag
{
Key = "iotEdge",
Value = "123"
}
}
});

//Act
var result = await this.externalDeviceService.IsEdgeDeviceModel(this.Mapper.Map<ExternalDeviceModelDto>(thingType));

//Assert
_ = result.Should().BeNull();
MockRepository.VerifyAll();
}

[Test]
public async Task GetAllThingShouldReturnsAllAWSThings()
{

//Arrange
var expectedDeviceModel = Fixture.Create<DeviceModel>();
var newDevice = new Device
{
Id = Fixture.Create<string>(),
Name = Fixture.Create<string>(),
DeviceModel = expectedDeviceModel,
DeviceModelId = expectedDeviceModel.Id,
Version = 1
};

var thingsListing = new ListThingsResponse
{
Things = new List<ThingAttribute>()
{
new ThingAttribute
{
ThingName = newDevice.Name
}
}
};

_ = this.mockAmazonIot.Setup(client => client.ListThingsAsync(It.IsAny<ListThingsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(thingsListing);

_ = this.mockAmazonIot.Setup(client => client.DescribeThingAsync(It.IsAny<DescribeThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingResponse()
{
ThingId = newDevice.Id,
ThingName = newDevice.Name,
ThingTypeName = newDevice.DeviceModel.Name,
Version = newDevice.Version,
HttpStatusCode = HttpStatusCode.OK
});


//Act
var result = await this.externalDeviceService.GetAllThing();

//Assert
MockRepository.VerifyAll();
}
}
}

0 comments on commit 5daadb3

Please sign in to comment.