From 5614981181217e3181f5c56fbf2600e7ad348a70 Mon Sep 17 00:00:00 2001 From: Hocine Hacherouf Date: Mon, 3 Oct 2022 18:50:47 +0200 Subject: [PATCH 1/2] Enable AutoMapper dependency injection to load all profiles #1264 --- .../Services/DeviceModelServiceTests.cs | 2 ++ .../Server/Services/EdgeModelServiceTest.cs | 2 ++ .../Services/LoRaWANCommandServiceTests.cs | 2 ++ .../Services/LoRaWanDeviceServiceTests.cs | 2 ++ .../UnitTests/Bases/BackendUnitTest.cs | 16 +++------ .../Server/Mappers/EnumerableProfile.cs | 17 +++++++++ src/AzureIoTHub.Portal/Server/Startup.cs | 36 ++++++------------- 7 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 src/AzureIoTHub.Portal/Server/Mappers/EnumerableProfile.cs diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceModelServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceModelServiceTests.cs index 0db7aaed9..7e7d1c74a 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceModelServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceModelServiceTests.cs @@ -9,6 +9,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using System.Linq; using System.Threading.Tasks; using AutoFixture; + using AutoMapper; using AzureIoTHub.Portal.Domain; using AzureIoTHub.Portal.Server.Services; using FluentAssertions; @@ -67,6 +68,7 @@ public override void Setup() Services = ServiceCollection.BuildServiceProvider(); this.deviceModelService = Services.GetRequiredService>(); + Mapper = Services.GetRequiredService(); } [Test] diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs index 9022f661a..a0deeebf7 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeModelServiceTest.cs @@ -9,6 +9,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using System.Linq; using System.Threading.Tasks; using AutoFixture; + using AutoMapper; using AzureIoTHub.Portal.Domain; using AzureIoTHub.Portal.Domain.Entities; using AzureIoTHub.Portal.Domain.Exceptions; @@ -59,6 +60,7 @@ public void SetUp() Services = ServiceCollection.BuildServiceProvider(); this.edgeDeviceModelService = Services.GetRequiredService(); + Mapper = Services.GetRequiredService(); } diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWANCommandServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWANCommandServiceTests.cs index c0ff97021..9a1881a76 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWANCommandServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWANCommandServiceTests.cs @@ -11,6 +11,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Domain.Services using System.Threading; using System.Threading.Tasks; using AutoFixture; + using AutoMapper; using Azure; using Azure.Data.Tables; using AzureIoTHub.Portal.Domain; @@ -79,6 +80,7 @@ public override void Setup() Services = ServiceCollection.BuildServiceProvider(); this.loRaWanCommandService = Services.GetRequiredService(); + Mapper = Services.GetRequiredService(); } [Test] diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs index 5c5177c36..1f4d7da7d 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs @@ -25,6 +25,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using Microsoft.Azure.Devices.Shared; using Microsoft.Azure.Devices; using Microsoft.EntityFrameworkCore; + using AutoMapper; [TestFixture] public class LoRaWanDeviceServiceTests : BackendUnitTest @@ -64,6 +65,7 @@ public override void Setup() Services = ServiceCollection.BuildServiceProvider(); this.lorawanDeviceService = Services.GetRequiredService>(); + Mapper = Services.GetRequiredService(); } [Test] diff --git a/src/AzureIoTHub.Portal.Tests.Unit/UnitTests/Bases/BackendUnitTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/UnitTests/Bases/BackendUnitTest.cs index b1ede3ecc..a7ceb1dcb 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/UnitTests/Bases/BackendUnitTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/UnitTests/Bases/BackendUnitTest.cs @@ -4,6 +4,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases { using System; + using System.Reflection; using AutoMapper; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; @@ -13,6 +14,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases using Moq; using NUnit.Framework; using Portal.Infrastructure; + using Portal.Server; using Portal.Server.Mappers; using RichardSzalay.MockHttp; @@ -47,18 +49,8 @@ public virtual void Setup() httpClient.BaseAddress = new Uri("http://fake.local"); _ = ServiceCollection.AddSingleton(httpClient); - // Add Mapper Configuration - var mappingConfig = new MapperConfiguration(mc => - { - mc.AddProfile(new DeviceTagProfile()); - mc.AddProfile(new EdgeDeviceModelProfile()); - mc.AddProfile(new EdgeDeviceModelCommandProfile()); - mc.AddProfile(new DeviceModelProfile()); - mc.AddProfile(new DeviceModelCommandProfile()); - mc.AddProfile(new DeviceProfile()); - }); - Mapper = mappingConfig.CreateMapper(); - _ = ServiceCollection.AddSingleton(Mapper); + // Add AutoMapper Configuration + _ = ServiceCollection.AddAutoMapper(typeof(Startup)); // Add InMemory Database var contextOptions = new DbContextOptionsBuilder() diff --git a/src/AzureIoTHub.Portal/Server/Mappers/EnumerableProfile.cs b/src/AzureIoTHub.Portal/Server/Mappers/EnumerableProfile.cs new file mode 100644 index 000000000..537046fb4 --- /dev/null +++ b/src/AzureIoTHub.Portal/Server/Mappers/EnumerableProfile.cs @@ -0,0 +1,17 @@ +// 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.Server.Mappers +{ + using System.Collections.Generic; + using AutoMapper; + using Azure; + + public class EnumerableProfile : Profile + { + public EnumerableProfile() + { + _ = CreateMap(typeof(AsyncPageable<>), typeof(List<>)); + } + } +} diff --git a/src/AzureIoTHub.Portal/Server/Startup.cs b/src/AzureIoTHub.Portal/Server/Startup.cs index 6fc56b27a..f52399d62 100644 --- a/src/AzureIoTHub.Portal/Server/Startup.cs +++ b/src/AzureIoTHub.Portal/Server/Startup.cs @@ -4,25 +4,22 @@ namespace AzureIoTHub.Portal.Server { using System; - using System.Collections.Generic; using System.IO; using System.Net; using System.Threading.Tasks; - using AutoMapper; - using Azure; using Azure.Storage.Blobs; - using AzureIoTHub.Portal.Domain; - using AzureIoTHub.Portal.Domain.Exceptions; - using AzureIoTHub.Portal.Domain.Repositories; - using AzureIoTHub.Portal.Infrastructure; - using AzureIoTHub.Portal.Infrastructure.Factories; - using AzureIoTHub.Portal.Infrastructure.Repositories; - using AzureIoTHub.Portal.Infrastructure.Seeds; - using AzureIoTHub.Portal.Server.Jobs; + using Domain; + using Domain.Exceptions; + using Domain.Repositories; using Extensions; using Hellang.Middleware.ProblemDetails; using Hellang.Middleware.ProblemDetails.Mvc; using Identity; + using Infrastructure; + using Infrastructure.Factories; + using Infrastructure.Repositories; + using Infrastructure.Seeds; + using Jobs; using Managers; using Mappers; using Microsoft.AspNetCore.Authentication.JwtBearer; @@ -274,21 +271,8 @@ Specify the authorization token got from your IDP as a header. new HeaderApiVersionReader("X-Version")); }); - var mapperConfig = new MapperConfiguration(mc => - { - _ = mc.CreateMap(typeof(AsyncPageable<>), typeof(List<>)); - - mc.AddProfile(new DevicePropertyProfile()); - mc.AddProfile(new DeviceTagProfile()); - mc.AddProfile(new EdgeDeviceModelProfile()); - mc.AddProfile(new EdgeDeviceModelCommandProfile()); - mc.AddProfile(new DeviceModelProfile()); - mc.AddProfile(new DeviceModelCommandProfile()); - mc.AddProfile(new DeviceProfile()); - }); - - var mapper = mapperConfig.CreateMapper(); - _ = services.AddSingleton(mapper); + // Add AutoMapper Configuration + _ = services.AddAutoMapper(typeof(Startup)); _ = services.AddHealthChecks() .AddDbContextCheck() From fdca68eb7e3eaf037eec148458b62e3f30175973 Mon Sep 17 00:00:00 2001 From: Hocine Hacherouf Date: Mon, 3 Oct 2022 19:01:08 +0200 Subject: [PATCH 2/2] Fix missing Mapper initialization --- .../Server/Services/DeviceServiceTests.cs | 2 ++ .../Server/Services/DeviceTagServiceTests.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs index 627ef11a5..9e03dcb85 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs @@ -25,6 +25,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using Device = Portal.Domain.Entities.Device; using Microsoft.EntityFrameworkCore; using Portal.Domain.Entities; + using AutoMapper; [TestFixture] public class DeviceServiceTests : BackendUnitTest @@ -64,6 +65,7 @@ public override void Setup() Services = ServiceCollection.BuildServiceProvider(); this.deviceService = Services.GetRequiredService>(); + Mapper = Services.GetRequiredService(); } [Test] diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceTagServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceTagServiceTests.cs index 0d272e9e4..b1d31106b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceTagServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceTagServiceTests.cs @@ -8,6 +8,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using System.Linq; using System.Threading.Tasks; using AutoFixture; + using AutoMapper; using AzureIoTHub.Portal.Domain; using AzureIoTHub.Portal.Domain.Exceptions; using AzureIoTHub.Portal.Server.Services; @@ -43,6 +44,7 @@ public override void Setup() Services = ServiceCollection.BuildServiceProvider(); this.deviceTagService = Services.GetRequiredService(); + Mapper = Services.GetRequiredService(); } [Test]