diff --git a/src/AzureIoTHub.Portal.Application/Services/IEdgeModelService.cs b/src/AzureIoTHub.Portal.Application/Services/IEdgeModelService.cs index ba522ce24..8e430ea07 100644 --- a/src/AzureIoTHub.Portal.Application/Services/IEdgeModelService.cs +++ b/src/AzureIoTHub.Portal.Application/Services/IEdgeModelService.cs @@ -1,33 +1,31 @@ -// Copyright (c) CGI France. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace AzureIoTHub.Portal.Application.Services -{ - using System.Collections.Generic; - using System.Threading.Tasks; - using AzureIoTHub.Portal.Models.v10; - using AzureIoTHub.Portal.Shared.Models.v10.Filters; - using Microsoft.AspNetCore.Http; - - public interface IEdgeModelService - { - Task> GetEdgeModels(EdgeModelFilter edgeModelFilter); - - Task GetEdgeModel(string modelId); - - Task CreateEdgeModel(IoTEdgeModel edgeModel); - Task UpdateEdgeModel(IoTEdgeModel edgeModel); - - Task DeleteEdgeModel(string edgeModelId); - - Task GetEdgeModelAvatar(string edgeModelId); - - Task UpdateEdgeModelAvatar(string edgeModelId, IFormFile file); - - Task DeleteEdgeModelAvatar(string edgeModelId); - - Task SaveModuleCommands(IoTEdgeModel deviceModelObject); - - Task> GetPublicEdgeModules(); - } -} +// Copyright (c) CGI France. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace AzureIoTHub.Portal.Application.Services +{ + using System.Collections.Generic; + using System.Threading.Tasks; + using AzureIoTHub.Portal.Models.v10; + using AzureIoTHub.Portal.Shared.Models.v10.Filters; + using Microsoft.AspNetCore.Http; + + public interface IEdgeModelService + { + Task> GetEdgeModels(EdgeModelFilter edgeModelFilter); + + Task GetEdgeModel(string modelId); + + Task CreateEdgeModel(IoTEdgeModel edgeModel); + Task UpdateEdgeModel(IoTEdgeModel edgeModel); + + Task DeleteEdgeModel(string edgeModelId); + + Task GetEdgeModelAvatar(string edgeModelId); + + Task UpdateEdgeModelAvatar(string edgeModelId, IFormFile file); + + Task DeleteEdgeModelAvatar(string edgeModelId); + + Task> GetPublicEdgeModules(); + } +} diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/EdgeModelService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/EdgeModelService.cs index 6f2e5bcb2..9030a9898 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/EdgeModelService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/EdgeModelService.cs @@ -104,11 +104,11 @@ public async Task> GetEdgeModels(EdgeModelFilt public async Task CreateEdgeModel(IoTEdgeModel edgeModel) { var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel?.ModelId); + if (edgeModelEntity == null) { edgeModelEntity = this.mapper.Map(edgeModel); await this.edgeModelRepository.InsertAsync(edgeModelEntity); - await this.unitOfWork.SaveAsync(); } else { @@ -123,8 +123,9 @@ public async Task CreateEdgeModel(IoTEdgeModel edgeModel) await SaveModuleCommands(edgeModel); } - await this.configService.RollOutEdgeModelConfiguration(edgeModel); - + await this.configService.RollOutEdgeModelConfiguration(edgeModel); + + await this.unitOfWork.SaveAsync(); } /// @@ -133,9 +134,8 @@ public async Task CreateEdgeModel(IoTEdgeModel edgeModel) /// The device model object. /// /// - public async Task SaveModuleCommands(IoTEdgeModel deviceModelObject) + private async Task SaveModuleCommands(IoTEdgeModel deviceModelObject) { - IEnumerable moduleCommands = deviceModelObject.EdgeModules .SelectMany(x => x.Commands.Select(cmd => new IoTEdgeModuleCommand { @@ -146,6 +146,7 @@ public async Task SaveModuleCommands(IoTEdgeModel deviceModelObject) })).ToArray(); var existingCommands = this.commandRepository.GetAll().Where(x => x.EdgeDeviceModelId == deviceModelObject.ModelId).ToList(); + foreach (var command in existingCommands) { this.commandRepository.Delete(command.Id); @@ -155,7 +156,6 @@ public async Task SaveModuleCommands(IoTEdgeModel deviceModelObject) { await this.commandRepository.InsertAsync(this.mapper.Map(cmd)); } - await this.unitOfWork.SaveAsync(); } /// @@ -262,12 +262,13 @@ public async Task UpdateEdgeModel(IoTEdgeModel edgeModel) _ = this.mapper.Map(edgeModel, edgeModelEntity); this.edgeModelRepository.Update(edgeModelEntity); - await this.unitOfWork.SaveAsync(); await SaveModuleCommands(edgeModel); } - await this.configService.RollOutEdgeModelConfiguration(edgeModel); + await this.configService.RollOutEdgeModelConfiguration(edgeModel); + + await this.unitOfWork.SaveAsync(); } /// diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs index a02deb186..aaee8d1a7 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs @@ -618,59 +618,5 @@ public async Task DeleteEdgeDeviceModelAvatarShouldDeleteEdgeDeviceModelAvatar() // Assert MockRepository.VerifyAll(); } - - [Test] - public async Task SaveModuleCommandsShouldUpdateDatabase() - { - // Arrange - var edgeDeviceModel = Fixture.Create(); - - var expectedCommands = Fixture.CreateMany(5).Select(command => - { - command.EdgeDeviceModelId = edgeDeviceModel.ModelId; - return command; - }) .ToList(); - _ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.GetAll()) - .Returns(expectedCommands); - _ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.Delete(It.IsAny())); - _ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.InsertAsync(It.IsAny())) - .Returns(Task.CompletedTask); - - _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) - .Returns(Task.CompletedTask); - - // Act - await this.edgeDeviceModelService.SaveModuleCommands(edgeDeviceModel); - - // Assert - MockRepository.VerifyAll(); - } - - [Test] - public void SaveModuleCommandsShouldThrowInternalServerErrorExceptionIfDbUpdateExceptionOccurs() - { - // Arrange - var edgeDeviceModel = Fixture.Create(); - - var expectedCommands = Fixture.CreateMany(5).Select(command => - { - command.EdgeDeviceModelId = edgeDeviceModel.ModelId; - return command; - }) .ToList(); - _ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.GetAll()) - .Returns(expectedCommands); - _ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.Delete(It.IsAny())); - _ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.InsertAsync(It.IsAny())) - .Returns(Task.CompletedTask); - - _ = this.mockUnitOfWork.Setup(work => work.SaveAsync()) - .Throws(new DbUpdateException()); - - // Act - var result = async () => await this.edgeDeviceModelService.SaveModuleCommands(edgeDeviceModel); - - // Assert - _ = result.Should().ThrowAsync(); - } } }