Skip to content

Commit

Permalink
The default image of a new device model or edge model should be the d… (
Browse files Browse the repository at this point in the history
#1684)

* The default image of a new device model or edge model should be the default image

* Code ok

* Test createDeviceModelShouldCreateDeviceModel

* Test ChangeDeviceModelImageShouldSetDefaultImageToModel

* Test CreateEdgeModelShouldCreateEdgeModel

* Remove unused nuget ref to Microsoft.Extensions.Logging.Abstractions

Co-authored-by: crib <christophe.ribeiro@cgi.com>
Co-authored-by: Hocine Hacherouf <hacherouf.hocine@gmail.com>
  • Loading branch information
3 people authored Dec 21, 2022
1 parent 93e9511 commit 2374b8e
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public interface IDeviceModelImageManager
Task InitializeDefaultImageBlob();

Task SyncImagesCacheControl();

Task<string> SetDefaultImageToModel(string deviceModelId);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public async Task<string> ChangeDeviceModelImageAsync(string deviceModelId, Stre
return blobClient.Uri.ToString();
}

public async Task<string> SetDefaultImageToModel(string deviceModelId)
{
var blobContainer = this.blobService.GetBlobContainerClient(this.deviceModelImageOptions.Value.ImageContainerName);

var blobClient = blobContainer.GetBlobClient(deviceModelId);

this.logger.LogInformation($"Uploading to Blob storage as blob:\n\t {blobClient.Uri}\n");

var currentAssembly = Assembly.GetExecutingAssembly();

var defaultImageStream = currentAssembly
.GetManifestResourceStream($"{currentAssembly.GetName().Name}.Resources.{this.deviceModelImageOptions.Value.DefaultImageName}");

_ = await blobClient.UploadAsync(defaultImageStream, true);

_ = await blobClient.SetHttpHeadersAsync(new BlobHttpHeaders { CacheControl = $"max-age={this.configHandler.StorageAccountDeviceModelImageMaxAge}, must-revalidate" });

return blobClient.Uri.ToString();
}

public async Task DeleteDeviceModelImageAsync(string deviceModelId)
{
var blobContainer = this.blobService.GetBlobContainerClient(this.deviceModelImageOptions.Value.ImageContainerName);
Expand Down
2 changes: 2 additions & 0 deletions src/AzureIoTHub.Portal.Server/Services/DeviceModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public async Task CreateDeviceModel(TModel deviceModel)
await this.deviceModelRepository.InsertAsync(deviceModelEntity);
await this.unitOfWork.SaveAsync();

_ = this.deviceModelImageManager.SetDefaultImageToModel(deviceModel.ModelId);

await CreateDeviceModelConfiguration(deviceModel);
}

Expand Down
2 changes: 2 additions & 0 deletions src/AzureIoTHub.Portal.Server/Services/EdgeModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public async Task CreateEdgeModel(IoTEdgeModel edgeModel)
throw new ResourceAlreadyExistsException($"The edge model with id {edgeModel?.ModelId} already exists");
}

_ = await this.deviceModelImageManager.SetDefaultImageToModel(edgeModel?.ModelId);

await SaveModuleCommands(edgeModel);
await this.configService.RollOutEdgeModelConfiguration(edgeModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,54 @@ public async Task ChangeDeviceModelImageShouldUploadImageAndReturnItsUri()
MockRepository.VerifyAll();
}

[Test]
public async Task ChangeDeviceModelImageShouldSetDefaultImageToModel()
{
// Arrange
var deviceModelId = Fixture.Create<string>();
var expectedImageUri = Fixture.Create<Uri>();
using var imageAsMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(Fixture.Create<string>()));

var mockOptions = new DeviceModelImageOptions()
{
BaseUri = Fixture.Create<Uri>()
};

_ = this.mockDeviceModelImageOptions.Setup(x => x.Value).Returns(mockOptions);

_ = this.mockBlobServiceClient
.Setup(x => x.GetBlobContainerClient(It.IsAny<string>()))
.Returns(this.mockBlobContainerClient.Object);

_ = this.mockBlobContainerClient
.Setup(x => x.GetBlobClient(deviceModelId))
.Returns(this.mockBlobClient.Object);

_ = this.mockBlobClient
.Setup(client =>
client.SetHttpHeadersAsync(It.IsAny<BlobHttpHeaders>(), It.IsAny<BlobRequestConditions>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Response.FromValue(BlobsModelFactory.BlobInfo(ETag.All, DateTimeOffset.Now), Mock.Of<Response>()));

_ = this.mockBlobClient
.Setup(client => client.UploadAsync(It.IsAny<Stream>(), true, It.IsAny<CancellationToken>()))
.ReturnsAsync(Response.FromValue(
BlobsModelFactory.BlobContentInfo(ETag.All, DateTimeOffset.Now, Array.Empty<byte>(), string.Empty,
1L), Mock.Of<Response>()));

_ = this.mockBlobClient
.Setup(client => client.Uri)
.Returns(expectedImageUri);

_ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(3600);

// Act
var result = await this.deviceModelImageManager.SetDefaultImageToModel(deviceModelId);

// Assert
_ = result.Should().Be(expectedImageUri.ToString());
MockRepository.VerifyAll();
}

[Test]
public async Task WhenDeleteAsyncFailedDeleteDeviceModelImageAsyncShouldThrowAnInternalServerErrorException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public async Task CreateDeviceModelShouldCreateDeviceModel()
{
// Arrange
var deviceModelDto = Fixture.Create<DeviceModelDto>();
var expectedAvatarUrl = Fixture.Create<string>();

_ = this.mockDeviceModelRepository.Setup(repository => repository.InsertAsync(It.IsAny<DeviceModel>()))
.Returns(Task.CompletedTask);
Expand All @@ -166,6 +167,10 @@ public async Task CreateDeviceModelShouldCreateDeviceModel()
It.IsAny<Dictionary<string, object>>()))
.Returns(Task.CompletedTask);

_ = this.mockDeviceModelImageManager.Setup(manager =>
manager.SetDefaultImageToModel(deviceModelDto.ModelId))
.ReturnsAsync(expectedAvatarUrl);

// Act
await this.deviceModelService.CreateDeviceModel(deviceModelDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public async Task CreateEdgeModelShouldCreateEdgeModel()
{
// Arrange
var edgeDeviceModel = Fixture.Create<IoTEdgeModel>();
var expectedImageUri = Fixture.Create<Uri>();

_ = this.mockEdgeDeviceModelRepository.Setup(x => x.GetByIdAsync(It.IsAny<string>()))
.ReturnsAsync((EdgeDeviceModel)null);
Expand All @@ -202,6 +203,10 @@ public async Task CreateEdgeModelShouldCreateEdgeModel()
_ = this.mockConfigService.Setup(x => x.RollOutEdgeModelConfiguration(It.IsAny<IoTEdgeModel>()))
.Returns(Task.CompletedTask);

_ = this.mockDeviceModelImageManager.Setup(manager =>
manager.SetDefaultImageToModel(It.IsAny<string>()))
.ReturnsAsync(expectedImageUri.ToString());

// Act
await this.edgeDeviceModelService.CreateEdgeModel(edgeDeviceModel);

Expand Down

0 comments on commit 2374b8e

Please sign in to comment.