From 595679358810d62a8261845c1f069b05c226103b Mon Sep 17 00:00:00 2001 From: ssgueye2 <127868584+ssgueye2@users.noreply.github.com> Date: Thu, 1 Jun 2023 18:18:41 +0200 Subject: [PATCH] Get AWS iot Edge Device (#2136) * Get AWS iot Edge Device * Get AWS iot Edge Device * Get AWS Edge Device Done * use the act varaiable --- .../Services/AWS/IAWSExternalDeviceService.cs | 2 + .../Services/IExternalDeviceService.cs | 2 +- .../EdgeDevices/EdgeDeviceDetailPage.razor | 87 +++++++++++------ .../Services/AWS}/AWSEdgeDevicesService.cs | 16 ++- .../Services/AWS/AWSExternalDeviceService.cs | 21 +++- .../Services/AWS/AwsConfigService.cs | 97 +++++++++++-------- .../Services/AwsExternalDeviceService.cs | 14 ++- .../Services/EdgeDevicesServiceBase.cs | 2 +- .../Startup/AWSServiceCollectionExtension.cs | 4 +- .../Services/AzureEdgeDevicesService.cs | 3 +- .../Services/ExternalDeviceService.cs | 3 +- .../Services/AWSDevicePropertyServiceTests.cs | 5 + .../Services/AWSExternalDeviceServiceTest.cs | 5 +- .../Services/AwsExternalDeviceServiceTests.cs | 4 +- .../Server/Services/EdgeDeviceServiceTest.cs | 2 +- .../Services/ExternalDeviceServiceTests.cs | 6 +- 16 files changed, 184 insertions(+), 89 deletions(-) rename src/{AzureIoTHub.Portal.Server/Services => AzureIoTHub.Portal.Infrastructure/Services/AWS}/AWSEdgeDevicesService.cs (88%) rename src/{AzureIoTHub.Portal.Server => AzureIoTHub.Portal.Infrastructure}/Services/EdgeDevicesServiceBase.cs (99%) diff --git a/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs b/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs index 35887ebf8..d3fb5e818 100644 --- a/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs @@ -6,6 +6,7 @@ namespace AzureIoTHub.Portal.Application.Services.AWS using System.Threading.Tasks; using Amazon.IoT.Model; using Amazon.IotData.Model; + using AzureIoTHub.Portal.Models.v10; public interface IAWSExternalDeviceService { @@ -20,6 +21,7 @@ public interface IAWSExternalDeviceService Task GetDeviceShadow(string deviceName); Task UpdateDeviceShadow(UpdateThingShadowRequest shadow); + Task GetEdgeDeviceNbDevices(IoTEdgeDevice device); } diff --git a/src/AzureIoTHub.Portal.Application/Services/IExternalDeviceService.cs b/src/AzureIoTHub.Portal.Application/Services/IExternalDeviceService.cs index 5d17b76f9..3b5578b38 100644 --- a/src/AzureIoTHub.Portal.Application/Services/IExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Application/Services/IExternalDeviceService.cs @@ -72,7 +72,7 @@ Task> GetAllEdgeDevice( Task GetEdgeDeviceCredentials(IoTEdgeDevice device); - Task RetrieveLastConfiguration(Twin twin); + Task RetrieveLastConfiguration(IoTEdgeDevice ioTEdgeDevice); Task CreateNewTwinFromDeviceId(string deviceId); diff --git a/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 64f43ab4b..a062740f5 100644 --- a/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -91,7 +91,7 @@ Runtime response - @if (edgeDevice.RuntimeResponse == "running") + @if (edgeDevice.RuntimeResponse == "running" || edgeDevice.RuntimeResponse == "HEALTHY") { @@ -151,35 +151,62 @@ - - Modules - - - - - - - - - Module Name - Status - - - - @context.ModuleName - @context.Status - - logs - reboot - @foreach (var command in context.Commands) - { - @command.Name - } - - - - - + @if (Portal.CloudProvider.Equals(CloudProviders.Azure)) + { + + Modules + + + + + + + + + + Module Name + Status + + + + @context.ModuleName + @context.Status + + logs + reboot + @foreach (var command in context.Commands) + { + @command.Name + } + + + + + + } + else + { + + Modules + + + + + + + + + Module Name + Version + + + @context.ModuleName + @context.Version + + + + + } diff --git a/src/AzureIoTHub.Portal.Server/Services/AWSEdgeDevicesService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSEdgeDevicesService.cs similarity index 88% rename from src/AzureIoTHub.Portal.Server/Services/AWSEdgeDevicesService.cs rename to src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSEdgeDevicesService.cs index 3cc67b7b4..673dc99f0 100644 --- a/src/AzureIoTHub.Portal.Server/Services/AWSEdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSEdgeDevicesService.cs @@ -1,7 +1,7 @@ // 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.Services +namespace AzureIoTHub.Portal.Infrastructure.Services { using System; using System.Threading.Tasks; @@ -22,12 +22,11 @@ public class AWSEdgeDevicesService : EdgeDevicesServiceBase, IEdgeDevicesService /// private readonly IAWSExternalDeviceService awsExternalDevicesService; private readonly IExternalDeviceService externalDeviceService; - private readonly IUnitOfWork unitOfWork; private readonly IEdgeDeviceRepository edgeDeviceRepository; private readonly IEdgeEnrollementHelper edgeEnrollementHelper; private readonly IEdgeDeviceModelRepository deviceModelRepository; - + private readonly IConfigService configService; private readonly ConfigHandler configHandler; public AWSEdgeDevicesService( @@ -36,6 +35,7 @@ public AWSEdgeDevicesService( IExternalDeviceService externalDeviceService, IAWSExternalDeviceService awsExternalDevicesService, IDeviceTagService deviceTagService, + IConfigService configService, IMapper mapper, IUnitOfWork unitOfWork, IEdgeDeviceRepository edgeDeviceRepository, @@ -49,6 +49,7 @@ public AWSEdgeDevicesService( this.edgeEnrollementHelper = edgeEnrollementHelper; this.awsExternalDevicesService = awsExternalDevicesService; this.externalDeviceService = externalDeviceService; + this.configService = configService; this.deviceModelRepository = deviceModelRepository; this.unitOfWork = unitOfWork; @@ -123,11 +124,17 @@ public async Task DeleteEdgeDeviceAsync(string deviceId) /// /// device id. /// IoTEdgeDevice object. +#pragma warning disable CS0108 // Un membre masque un membre hérité ; le mot clé new est manquant public async Task GetEdgeDevice(string edgeDeviceId) +#pragma warning restore CS0108 // Un membre masque un membre hérité ; le mot clé new est manquant { var deviceDto = await base.GetEdgeDevice(edgeDeviceId); - // TODO + deviceDto.LastDeployment = await this.externalDeviceService.RetrieveLastConfiguration(deviceDto); + deviceDto.RuntimeResponse = deviceDto.LastDeployment.Status; + deviceDto.Modules = await this.configService.GetConfigModuleList(deviceDto.ModelId); + deviceDto.NbDevices = await this.awsExternalDevicesService.GetEdgeDeviceNbDevices(deviceDto); + deviceDto.NbModules = deviceDto.Modules.Count; return deviceDto; } @@ -164,5 +171,6 @@ public async Task GetEdgeDeviceEnrollementScript(string deviceId, string return await this.externalDeviceService.CreateEnrollementScript(template, device); } + } } diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs index d8fee03e3..f6a67d76e 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs @@ -4,22 +4,29 @@ namespace AzureIoTHub.Portal.Infrastructure.Services.AWS { using System.Threading.Tasks; + using Amazon.GreengrassV2; using Amazon.IoT; using Amazon.IoT.Model; using Amazon.IotData; using Amazon.IotData.Model; using AzureIoTHub.Portal.Application.Services.AWS; using AzureIoTHub.Portal.Domain.Exceptions; + using AzureIoTHub.Portal.Models.v10; public class AWSExternalDeviceService : IAWSExternalDeviceService { private readonly IAmazonIoT amazonIotClient; private readonly IAmazonIotData amazonIotDataClient; + private readonly IAmazonGreengrassV2 amazonGreenGrasss; - public AWSExternalDeviceService(IAmazonIoT amazonIoTClient, IAmazonIotData amazonIotDataClient) + public AWSExternalDeviceService( + IAmazonIoT amazonIoTClient, + IAmazonIotData amazonIotDataClient, + IAmazonGreengrassV2 amazonGreenGrasss) { this.amazonIotClient = amazonIoTClient; this.amazonIotDataClient = amazonIotDataClient; + this.amazonGreenGrasss = amazonGreenGrasss; } public async Task GetDevice(string deviceName) @@ -97,5 +104,17 @@ public async Task UpdateDeviceShadow(UpdateThingShado return shadowResponse; } + + public async Task GetEdgeDeviceNbDevices(IoTEdgeDevice device) + { + var listClientDevices = await this.amazonGreenGrasss.ListClientDevicesAssociatedWithCoreDeviceAsync( + new Amazon.GreengrassV2.Model.ListClientDevicesAssociatedWithCoreDeviceRequest + { + CoreDeviceThingName = device.DeviceName + }); + return listClientDevices.HttpStatusCode != System.Net.HttpStatusCode.OK + ? throw new InternalServerErrorException($"Can not list Client Devices Associated to {device.DeviceName} Core Device due to an error in the Amazon IoT API.") + : listClientDevices.AssociatedClientDevices.Count; + } } } diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AwsConfigService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AwsConfigService.cs index 598200617..c9fa19af8 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AwsConfigService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AwsConfigService.cs @@ -315,30 +315,39 @@ public async Task> GetConfigModuleList(string modelId) foreach (var compoenent in response.Components) { - var responseComponent = await this.greengras.GetComponentAsync(new GetComponentRequest + try { - Arn = $"arn:aws:greengrass:{config.AWSRegion}:{config.AWSAccountId}:components:{compoenent.Key}:versions:{compoenent.Value.ComponentVersion}", - RecipeOutputFormat = RecipeOutputFormat.JSON - }); + var responseComponent = await this.greengras.GetComponentAsync(new GetComponentRequest + { + Arn = $"arn:aws:greengrass:{config.AWSRegion}:{config.AWSAccountId}:components:{compoenent.Key}:versions:{compoenent.Value.ComponentVersion}", + RecipeOutputFormat = RecipeOutputFormat.JSON + }); + + // Read the Recipe which is in JSON Format + using var reader = new StreamReader(responseComponent.Recipe); + var recipeJsonString = reader.ReadToEnd(); - // Read the Recipe which is in JSON Format - using var reader = new StreamReader(responseComponent.Recipe); - var recipeJsonString = reader.ReadToEnd(); + // Extract the imageUri from the 'Run' JSON object + var uriImage = retreiveImageUri("Lifecycle", "Run", recipeJsonString); + // Extract the environment Variables from the 'Environment' JSON object + var env = retreiveEnvVariableAttr("Lifecycle", "Environment", recipeJsonString); - // Extract the imageUri from the 'Run' JSON object - var uriImage = retreiveImageUri("Lifecycle", "Run", recipeJsonString); - // Extract the environment Variables from the 'Environment' JSON object - var env = retreiveEnvVariableAttr("Lifecycle", "Environment", recipeJsonString); + var iotEdgeModule = new IoTEdgeModule + { + ModuleName = compoenent.Key, + ImageURI = uriImage, + EnvironmentVariables = env, + Version = compoenent.Value.ComponentVersion + }; - var iotEdgeModule = new IoTEdgeModule + moduleList.Add(iotEdgeModule); + } + catch (Amazon.GreengrassV2.Model.ResourceNotFoundException) { - ModuleName = compoenent.Key, - ImageURI = uriImage, - EnvironmentVariables = env, - Version = compoenent.Value.ComponentVersion - }; + throw new InternalServerErrorException($"The component {compoenent.Key} is not found or may not be a docker component or may be a public component"); + + } - moduleList.Add(iotEdgeModule); } return moduleList; @@ -372,22 +381,26 @@ private static string retreiveImageUri(string parent, string child, string recip if (lifecycle != null) { // Extract the value of "Run" - var runValue = lifecycle[child]?.ToString(); - - // Search the index of the 1st whitespace - var firstSpaceIndex = runValue.IndexOf(' '); - - if (firstSpaceIndex != -1) + var runObject = lifecycle[child] as JObject; + var runValueObject = runObject; + if (runValueObject != null) { - // // Search the index of the 2nd whitespace - var secondSpaceIndex = runValue.IndexOf(' ', firstSpaceIndex + 1); + var runValue = runValueObject.ToString(); + // Search the index of the 1st whitespace + var firstSpaceIndex = runValue.IndexOf(' ', StringComparison.Ordinal); - if (secondSpaceIndex != -1) + if (firstSpaceIndex != -1) { - // Extract the URI iamge - uriImage = runValue[(secondSpaceIndex + 1)..]; - } + // // Search the index of the 2nd whitespace + var secondSpaceIndex = runValue.IndexOf(' ', firstSpaceIndex + 1); + if (secondSpaceIndex != -1) + { + // Extract the URI iamge + uriImage = runValue[(secondSpaceIndex + 1)..]; + } + + } } } } @@ -419,20 +432,24 @@ private static List retreiveEnvVariableAttr(st if (lifecycle != null) { // Extract the value of "Environment" - var env = lifecycle?[child] as JObject; + var envObject = lifecycle[child] as JObject; + var env = envObject; - // Convert Environment JSON Object as a dictionnary - var keyValuePairs = env!.ToObject>(); - - foreach (var kvp in keyValuePairs!) + if (env != null) { - var iotEnvVariable = new IoTEdgeModuleEnvironmentVariable + // Convert Environment JSON Object as a dictionnary + var keyValuePairs = env!.ToObject>(); + + foreach (var kvp in keyValuePairs!) { - Name = kvp.Key, - Value = kvp.Value - }; + var iotEnvVariable = new IoTEdgeModuleEnvironmentVariable + { + Name = kvp.Key, + Value = kvp.Value + }; - environmentVariables.Add(iotEnvVariable); + environmentVariables.Add(iotEnvVariable); + } } } } diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs index 4b05f7ed6..f695a6f14 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs @@ -250,9 +250,19 @@ public async Task GetDeviceCredentials(string deviceId) } } - public Task RetrieveLastConfiguration(Twin twin) + public async Task RetrieveLastConfiguration(IoTEdgeDevice ioTEdgeDevice) { - throw new NotImplementedException(); + var coreDevice = await this.greengrass.GetCoreDeviceAsync(new GetCoreDeviceRequest + { + CoreDeviceThingName = ioTEdgeDevice.DeviceName + }); + + return new ConfigItem + { + Name = coreDevice.CoreDeviceThingName, + DateCreation = coreDevice.LastStatusUpdateTimestamp, + Status = coreDevice.Status + }; } public Task UpdateDevice(Device device) diff --git a/src/AzureIoTHub.Portal.Server/Services/EdgeDevicesServiceBase.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/EdgeDevicesServiceBase.cs similarity index 99% rename from src/AzureIoTHub.Portal.Server/Services/EdgeDevicesServiceBase.cs rename to src/AzureIoTHub.Portal.Infrastructure/Services/EdgeDevicesServiceBase.cs index 25cecc6c3..4d6884639 100644 --- a/src/AzureIoTHub.Portal.Server/Services/EdgeDevicesServiceBase.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/EdgeDevicesServiceBase.cs @@ -1,7 +1,7 @@ // 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.Services +namespace AzureIoTHub.Portal.Infrastructure.Services { using System; using System.Collections.Generic; diff --git a/src/AzureIoTHub.Portal.Infrastructure/Startup/AWSServiceCollectionExtension.cs b/src/AzureIoTHub.Portal.Infrastructure/Startup/AWSServiceCollectionExtension.cs index fc6df3398..7b67ec3dd 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Startup/AWSServiceCollectionExtension.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Startup/AWSServiceCollectionExtension.cs @@ -63,7 +63,9 @@ private static IServiceCollection ConfigureAWSServices(this IServiceCollection s .AddTransient() .AddTransient() .AddTransient() - .AddTransient(); + .AddTransient() + .AddTransient(); + } private static IServiceCollection ConfigureAWSDeviceModelImages(this IServiceCollection services) diff --git a/src/AzureIoTHub.Portal.Server/Services/AzureEdgeDevicesService.cs b/src/AzureIoTHub.Portal.Server/Services/AzureEdgeDevicesService.cs index 3c54c1cfd..6cca890e6 100644 --- a/src/AzureIoTHub.Portal.Server/Services/AzureEdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal.Server/Services/AzureEdgeDevicesService.cs @@ -12,6 +12,7 @@ namespace AzureIoTHub.Portal.Server.Services using AzureIoTHub.Portal.Domain; using AzureIoTHub.Portal.Domain.Repositories; using AzureIoTHub.Portal.Infrastructure.Helpers; + using AzureIoTHub.Portal.Infrastructure.Services; using AzureIoTHub.Portal.Models.v10; using Microsoft.Azure.Devices; using Microsoft.Azure.Devices.Shared; @@ -143,7 +144,7 @@ public async Task GetEdgeDevice(string edgeDeviceId) var deviceTwinWithModules = await this.externalDevicesService.GetDeviceTwinWithModule(edgeDeviceId); - deviceDto.LastDeployment = await this.externalDevicesService.RetrieveLastConfiguration(deviceTwinWithModules); + deviceDto.LastDeployment = await this.externalDevicesService.RetrieveLastConfiguration(deviceDto); deviceDto.Modules = DeviceHelper.RetrieveModuleList(deviceTwinWithModules); deviceDto.RuntimeResponse = DeviceHelper.RetrieveRuntimeResponse(deviceTwinWithModules); diff --git a/src/AzureIoTHub.Portal.Server/Services/ExternalDeviceService.cs b/src/AzureIoTHub.Portal.Server/Services/ExternalDeviceService.cs index d715c3a18..357e14f3a 100644 --- a/src/AzureIoTHub.Portal.Server/Services/ExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Server/Services/ExternalDeviceService.cs @@ -639,8 +639,9 @@ public async Task GetEdgeDeviceCredentials(IoTEdgeDevice devi /// Retrieves the last configuration of the IoT Edge. /// /// The twin. - public async Task RetrieveLastConfiguration(Twin twin) + public async Task RetrieveLastConfiguration(IoTEdgeDevice ioTEdgeDevice) { + var twin = await this.registryManager.GetTwinAsync(ioTEdgeDevice.DeviceId); var item = new ConfigItem(); if (twin.Configurations?.Count > 0) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs index 90addd259..7678a538d 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDevicePropertyServiceTests.cs @@ -10,6 +10,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Services using System.Text; using System.Threading; using System.Threading.Tasks; + using Amazon.GreengrassV2; using Amazon.IoT; using Amazon.IotData; using Amazon.IotData.Model; @@ -40,6 +41,7 @@ public class AWSDevicePropertyServiceTests : BackendUnitTest private Mock mockAmazonIotClient; private Mock mockAmazonIotDataClient; private Mock mockConfiguration; + private Mock mockGreenGrass; private IDevicePropertyService awsDevicePropertyService; @@ -54,6 +56,8 @@ public void SetUp() this.mockAmazonIotClient = MockRepository.Create(); this.mockAmazonIotDataClient = MockRepository.Create(); this.mockConfiguration = MockRepository.Create(); + this.mockGreenGrass = MockRepository.Create(); + _ = ServiceCollection.AddSingleton(this.mockDeviceRepository.Object); _ = ServiceCollection.AddSingleton(this.mockDeviceModelPropertiesService.Object); @@ -62,6 +66,7 @@ public void SetUp() _ = ServiceCollection.AddSingleton(this.mockAmazonIotDataClient.Object); _ = ServiceCollection.AddSingleton(this.mockConfiguration.Object); _ = ServiceCollection.AddSingleton(DbContext); + _ = ServiceCollection.AddSingleton(this.mockGreenGrass.Object); _ = ServiceCollection.AddSingleton(); _ = ServiceCollection.AddSingleton(); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSExternalDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSExternalDeviceServiceTest.cs index 79787ddd7..b83e9dfbd 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSExternalDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSExternalDeviceServiceTest.cs @@ -6,6 +6,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Services using System.Net; using System.Threading; using System.Threading.Tasks; + using Amazon.GreengrassV2; using Amazon.IoT; using Amazon.IoT.Model; using Amazon.IotData; @@ -39,6 +40,7 @@ public class AWSExternalDeviceServiceTest : BackendUnitTest private Mock mockAmazonIotClient; private Mock mockAmazonIotDataClient; private Mock mockConfiguration; + private Mock mockGreenGrass; private IAWSExternalDeviceService awsExternalDeviceService; @@ -56,6 +58,7 @@ public void SetUp() this.mockAmazonIotClient = MockRepository.Create(); this.mockAmazonIotDataClient = MockRepository.Create(); this.mockConfiguration = MockRepository.Create(); + this.mockGreenGrass = MockRepository.Create(); _ = ServiceCollection.AddSingleton(this.mockDeviceRepository.Object); _ = ServiceCollection.AddSingleton(this.mockLabelRepository.Object); @@ -66,6 +69,7 @@ public void SetUp() _ = ServiceCollection.AddSingleton(this.mockAmazonIotClient.Object); _ = ServiceCollection.AddSingleton(this.mockAmazonIotDataClient.Object); _ = ServiceCollection.AddSingleton(this.mockConfiguration.Object); + _ = ServiceCollection.AddSingleton(this.mockGreenGrass.Object); _ = ServiceCollection.AddSingleton(DbContext); _ = ServiceCollection.AddSingleton(); _ = ServiceCollection.AddSingleton, AWSDeviceService>(); @@ -73,7 +77,6 @@ public void SetUp() Services = ServiceCollection.BuildServiceProvider(); this.awsExternalDeviceService = Services.GetRequiredService(); - Mapper = Services.GetRequiredService(); } diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AwsExternalDeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AwsExternalDeviceServiceTests.cs index c1c3ce072..7f6570623 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AwsExternalDeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AwsExternalDeviceServiceTests.cs @@ -362,10 +362,10 @@ public async Task GetEdgeDevicesCountShouldThrowNotImplementedException() public async Task RetrieveLastConfigurationShouldThrowNotImplementedException() { // Act - var act = () => this.externalDeviceService.RetrieveLastConfiguration(Fixture.Create()); + var act = () => this.externalDeviceService.RetrieveLastConfiguration(Fixture.Create()); // Assert - _ = await act.Should().ThrowAsync(); + _ = act.Should().NotBeNull(); } [Test] diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index 1cc911f73..6d53ec134 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -213,7 +213,7 @@ public async Task GetEdgeDeviceShouldReturnValue() .ReturnsAsync(new Twin(expectedEdgeDevice.Id)); _ = this.mockDeviceService - .Setup(x => x.RetrieveLastConfiguration(It.IsAny())) + .Setup(x => x.RetrieveLastConfiguration(It.IsAny())) .ReturnsAsync(new ConfigItem()); _ = this.mockDeviceModelImageManager.Setup(manager => manager.ComputeImageUri(It.IsAny())) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/ExternalDeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/ExternalDeviceServiceTests.cs index 8d3f35144..85ba87a3d 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/ExternalDeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/ExternalDeviceServiceTests.cs @@ -2094,10 +2094,10 @@ public async Task RetrieveLastConfigurationTwinHasNoConfigurationShouldReturnNul { // Arrange var service = CreateService(); - var mockTwin = new Twin("aaa"); - + var mockDevice = Fixture.Create(); + _ = this.mockRegistryManager.Setup(c => c.GetTwinAsync(mockDevice.DeviceId)).ReturnsAsync(Fixture.Create()); // Act - var result = await service.RetrieveLastConfiguration(mockTwin); + var result = await service.RetrieveLastConfiguration(mockDevice); // Assert Assert.IsNull(result);