Skip to content

Commit

Permalink
Fix #2145 - Move Savechanges at last step of CRUD in edge model service
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand authored and hocinehacherouf committed Jun 1, 2023
1 parent 2f17bca commit 0b057e1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 95 deletions.
64 changes: 31 additions & 33 deletions src/AzureIoTHub.Portal.Application/Services/IEdgeModelService.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<IoTEdgeModelListItem>> GetEdgeModels(EdgeModelFilter edgeModelFilter);

Task<IoTEdgeModel> GetEdgeModel(string modelId);

Task CreateEdgeModel(IoTEdgeModel edgeModel);
Task UpdateEdgeModel(IoTEdgeModel edgeModel);

Task DeleteEdgeModel(string edgeModelId);

Task<string> GetEdgeModelAvatar(string edgeModelId);

Task<string> UpdateEdgeModelAvatar(string edgeModelId, IFormFile file);

Task DeleteEdgeModelAvatar(string edgeModelId);

Task SaveModuleCommands(IoTEdgeModel deviceModelObject);

Task<IEnumerable<IoTEdgeModule>> 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<IEnumerable<IoTEdgeModelListItem>> GetEdgeModels(EdgeModelFilter edgeModelFilter);

Task<IoTEdgeModel> GetEdgeModel(string modelId);

Task CreateEdgeModel(IoTEdgeModel edgeModel);
Task UpdateEdgeModel(IoTEdgeModel edgeModel);

Task DeleteEdgeModel(string edgeModelId);

Task<string> GetEdgeModelAvatar(string edgeModelId);

Task<string> UpdateEdgeModelAvatar(string edgeModelId, IFormFile file);

Task DeleteEdgeModelAvatar(string edgeModelId);

Task<IEnumerable<IoTEdgeModule>> GetPublicEdgeModules();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public async Task<IEnumerable<IoTEdgeModelListItem>> GetEdgeModels(EdgeModelFilt
public async Task CreateEdgeModel(IoTEdgeModel edgeModel)
{
var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel?.ModelId);

if (edgeModelEntity == null)
{
edgeModelEntity = this.mapper.Map<EdgeDeviceModel>(edgeModel);
await this.edgeModelRepository.InsertAsync(edgeModelEntity);
await this.unitOfWork.SaveAsync();
}
else
{
Expand All @@ -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();
}

/// <summary>
Expand All @@ -133,9 +134,8 @@ public async Task CreateEdgeModel(IoTEdgeModel edgeModel)
/// <param name="deviceModelObject">The device model object.</param>
/// <returns></returns>
/// <exception cref="InternalServerErrorException"></exception>
public async Task SaveModuleCommands(IoTEdgeModel deviceModelObject)
private async Task SaveModuleCommands(IoTEdgeModel deviceModelObject)
{

IEnumerable<IoTEdgeModuleCommand> moduleCommands = deviceModelObject.EdgeModules
.SelectMany(x => x.Commands.Select(cmd => new IoTEdgeModuleCommand
{
Expand All @@ -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);
Expand All @@ -155,7 +156,6 @@ public async Task SaveModuleCommands(IoTEdgeModel deviceModelObject)
{
await this.commandRepository.InsertAsync(this.mapper.Map<EdgeDeviceModelCommand>(cmd));
}
await this.unitOfWork.SaveAsync();
}

/// <summary>
Expand Down Expand Up @@ -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();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,59 +618,5 @@ public async Task DeleteEdgeDeviceModelAvatarShouldDeleteEdgeDeviceModelAvatar()
// Assert
MockRepository.VerifyAll();
}

[Test]
public async Task SaveModuleCommandsShouldUpdateDatabase()
{
// Arrange
var edgeDeviceModel = Fixture.Create<IoTEdgeModel>();

var expectedCommands = Fixture.CreateMany<EdgeDeviceModelCommand>(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<string>()));
_ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.InsertAsync(It.IsAny<EdgeDeviceModelCommand>()))
.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<IoTEdgeModel>();

var expectedCommands = Fixture.CreateMany<EdgeDeviceModelCommand>(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<string>()));
_ = this.mockEdgeDeviceModelCommandRepository.Setup(x => x.InsertAsync(It.IsAny<EdgeDeviceModelCommand>()))
.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<InternalServerErrorException>();
}
}
}

0 comments on commit 0b057e1

Please sign in to comment.