From cf4448c002adf018d3e8bf8ffc1fe6babf651c04 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 14:56:18 +0200 Subject: [PATCH 01/30] resolve issue #1117 --- .../EdgeDevices/EdgeDeviceDetailPage.razor | 52 +++++++++++++++++-- .../Services/EdgeDeviceClientService.cs | 9 +++- .../Services/IEdgeDeviceClientService.cs | 2 + .../Controllers/v1.0/EdgeDevicesController.cs | 14 +++-- .../Server/Helpers/DeviceHelper.cs | 6 +++ .../Server/Services/DeviceService.cs | 20 +++++++ .../Server/Services/EdgeDevicesService.cs | 39 +++++++++++--- .../Server/Services/EdgeModelService.cs | 3 ++ .../Server/Services/IDeviceService.cs | 2 + .../Server/Services/IEdgeDevicesService.cs | 4 +- 10 files changed, 136 insertions(+), 15 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index ee7c18db8..7a9c15412 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -1,5 +1,7 @@ @page "/edge/devices/{deviceId}" + @using AzureIoTHub.Portal.Models.v10 +@using AzureIoTHub.Portal.Shared.Models.v10; @attribute [Authorize] @inject NavigationManager NavigationManager @@ -145,9 +147,13 @@ @context.ModuleName @context.Version @context.Status - + logs reboot + @foreach (var command in context.Commands) + { + @command.Name + } @@ -226,6 +232,17 @@ await LoadModel(edgeDevice.ModelId); + foreach (var edgeModelModule in edgeModel.EdgeModules) + { + foreach (var item in edgeDevice.Modules) + { + if (item.ModuleName.Equals(edgeModelModule.ModuleName, StringComparison.Ordinal)) + { + item.Commands = edgeModelModule.Commands; + } + } + } + TagList = await DeviceTagSettingsClientService.GetDeviceTags(); } catch (ProblemDetailsException exception) @@ -289,8 +306,6 @@ try { - //var result = await Http.PostAsJsonAsync($"api/edge/devices/{edgeDevice.DeviceId}/{module.ModuleName}/{methodName}", module); - var c2dResult = await EdgeDeviceClientService.ExecuteModuleMethod(edgeDevice.DeviceId, module, methodName); if (c2dResult.Status == 200) @@ -316,6 +331,37 @@ } } + public async Task ExecuteCommand(string moduleName, IoTEdgeModuleCommand command) + { + isProcessing = true; + + try + { + var c2dResult = await EdgeDeviceClientService.ExecuteModuleCommand(edgeDevice.DeviceId, moduleName, command.Name); + + if (c2dResult.Status == 200) + { + Snackbar.Add("Command successfully executed.", Severity.Success); + } + else + { + Snackbar.Add($"Error
Status : {c2dResult.Status};
Payload : {c2dResult.Payload};", Severity.Error, + (option) => + { + option.VisibleStateDuration = 10000; + }); + } + } + catch (ProblemDetailsException exception) + { + Error?.ProcessProblemDetails(exception); + } + finally + { + isProcessing = false; + } + } + public async Task ShowEdgeDeviceLogs(IoTEdgeModule module) { var parameter = new DialogParameters diff --git a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs index 0827c35a8..22d237c7c 100644 --- a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs @@ -57,7 +57,14 @@ public async Task> GetEdgeDeviceLogs(string deviceId, IoT public async Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName) { - var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}", edgeModule); + var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}"); + + return await response.Content.ReadFromJsonAsync(); + } + + public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) + { + var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}"); return await response.Content.ReadFromJsonAsync(); } diff --git a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs index ca070d0ef..a096d94cf 100644 --- a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs @@ -24,5 +24,7 @@ public interface IEdgeDeviceClientService Task> GetEdgeDeviceLogs(string deviceId, IoTEdgeModule edgeModule); Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName); + + Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName); } } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index 4c6588c3c..c7e418f88 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -157,13 +157,19 @@ public async Task> GetCredentials(string dev /// /// Executes the module method on the IoT Edge device. /// - /// The edge module. + /// The edge module name. /// The device identifier. /// Name of the method. - [HttpPost("{deviceId}/{moduleId}/{methodName}", Name = "POST Execute module command")] - public async Task ExecuteModuleMethod(IoTEdgeModule edgeModule, string deviceId, string methodName) + [HttpGet("{deviceId}/{moduleName}/{methodName}", Name = "Get Execute module command")] + public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) { - return await this.edgeDevicesService.ExecuteModuleMethod(edgeModule, deviceId, methodName); + return await this.edgeDevicesService.ExecuteModuleMethod(moduleName, deviceId, methodName); + } + + [HttpGet("{deviceId}/{moduleName}/custom/{commandName}", Name = "GET Execute custom module command")] + public async Task ExecuteCustomModuleMethod(string deviceId, string moduleName, string commandName) + { + return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, commandName); } /// diff --git a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs index d76cf9b89..b6bc05b6c 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs @@ -190,6 +190,12 @@ public static IReadOnlyCollection RetrieveModuleList(Twin twin) ModuleName = property.Key }; + if (propertyObject.TryGetValue("settings", out var moduleSettings)) + { + var setting = moduleSettings.ToObject>(); + module.ImageURI = setting["image"]; + } + if (propertyObject.TryGetValue("status", out var status)) { module.Status = status.Value(); diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs index b145d8d4b..88cf262b4 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs @@ -403,6 +403,26 @@ public async Task ExecuteC2DMethod(string deviceId, C } } + /// + /// C2DMethod for custom command. + /// + /// the deviceId. + /// the module name. + /// the C2DMethod. + /// + /// + public async Task ExecuteCustomCommandC2DMethod(string deviceId, string moduleName, CloudToDeviceMethod method) + { + try + { + return await this.serviceClient.InvokeDeviceMethodAsync(deviceId, moduleName, method); + } + catch (Exception e) + { + throw new InternalServerErrorException($"Unable to execute the cloud to device method {method.MethodName} on the device with id {deviceId}", e); + } + } + /// /// Get edge device logs /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index c1bd98583..04bd0ddc2 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -170,23 +170,23 @@ public async Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice) /// /// Executes the module method on the IoT Edge device. /// - /// + /// /// /// /// - public async Task ExecuteModuleMethod(IoTEdgeModule edgeModule, string deviceId, string methodName) + public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) { - ArgumentNullException.ThrowIfNull(edgeModule, nameof(edgeModule)); + ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); var method = new CloudToDeviceMethod(methodName); - var payload = string.Empty; + var payload = "{}"; if (methodName == "RestartModule") { payload = JsonConvert.SerializeObject(new { - id = edgeModule.ModuleName, - schemaVersion = edgeModule.Version + id = moduleName, + schemaVersion = "1.0" }); } @@ -201,6 +201,33 @@ public async Task ExecuteModuleMethod(IoTEdgeModule edgeModule, strin }; } + /// + /// Execute the custom module command. + /// + /// the device identifier. + /// the module name. + /// the command name. + /// + public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) + { + ArgumentNullException.ThrowIfNull(deviceId, nameof(deviceId)); + ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); + ArgumentNullException.ThrowIfNull(commandName, nameof(commandName)); + + var method = new CloudToDeviceMethod(commandName); + var payload = "{}"; + + _ = method.SetPayloadJson(payload); + + var result = await this.devicesService.ExecuteCustomCommandC2DMethod(deviceId,moduleName, method); + + return new C2Dresult() + { + Payload = result.GetPayloadAsJson(), + Status = result.Status + }; + } + /// /// Gets the IoT Edge device enrollement credentials. /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs index 480e1e635..9f5904683 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs @@ -89,10 +89,13 @@ public async Task GetEdgeModel(string modelId) var query = await this.tableClientFactory .GetEdgeDeviceTemplates() .GetEntityAsync(DefaultPartitionKey, modelId); + var modules = await this.configService.GetConfigModuleList(modelId); + var commands = this.tableClientFactory.GetEdgeModuleCommands() .Query(c => c.PartitionKey == modelId) .ToArray(); + return this.edgeDeviceModelMapper.CreateEdgeDeviceModel(query.Value, modules, commands); } catch (RequestFailedException e) diff --git a/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs index 2d748613a..45969d89e 100644 --- a/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs @@ -25,6 +25,8 @@ public interface IDeviceService Task ExecuteC2DMethod(string deviceId, CloudToDeviceMethod method); + Task ExecuteCustomCommandC2DMethod(string deviceId, string moduleName, CloudToDeviceMethod method); + Task DeleteDevice(string deviceId); Task> GetAllDevice( diff --git a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs index 16222e921..a7af647bf 100644 --- a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs @@ -24,8 +24,10 @@ PaginationResult GetEdgeDevicesPage(PaginationResult edge Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice); - Task ExecuteModuleMethod(IoTEdgeModule edgeModule, string deviceId, string methodName); + Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName); Task GetEdgeDeviceCredentials(string edgeDeviceId); + + Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName); } } From f1ec8df7e052710993749beaa754e677287e9791 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 15:15:40 +0200 Subject: [PATCH 02/30] fix unit test --- .../Server/Controllers/v1.0/EdgeDevicesControllerTests.cs | 4 ++-- .../Server/Services/EdgeDeviceServiceTest.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index bf3449017..ce0780fc9 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -387,13 +387,13 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName) var deviceId = Guid.NewGuid().ToString(); _ = this.mockEdgeDeviceService - .Setup(x => x.ExecuteModuleMethod(It.IsAny(), + .Setup(x => x.ExecuteModuleMethod(It.IsAny(), It.Is(c => c.Equals(deviceId, StringComparison.Ordinal)), It.Is(c => c.Equals(methodName, StringComparison.Ordinal)))) .ReturnsAsync(new C2Dresult()); // Act - var result = await edgeDeviceController.ExecuteModuleMethod(module, deviceId, methodName); + var result = await edgeDeviceController.ExecuteModuleMethod(module.ModuleName, deviceId, methodName); // Assert Assert.IsNotNull(result); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index bb45e8259..0dc29822b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -267,7 +267,7 @@ public void WhenEdgeDeviceIsNullUpdateEdgeDeviceShouldThrowArgumentNullException _ = Assert.ThrowsAsync(() => edgeDeviceService.UpdateEdgeDevice(null)); } - [TestCase("RestartModule", /*lang=json,strict*/ "{\"id\":\"aaa\",\"schemaVersion\":null}")] + [TestCase("RestartModule", /*lang=json,strict*/ "{\"id\":\"aaa\",\"schemaVersion\":\"1.0\"}")] public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string expected) { // Arrange @@ -292,7 +292,7 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string }); // Act - _ = await edgeDeviceService.ExecuteModuleMethod(edgeModule, deviceId, methodName); + _ = await edgeDeviceService.ExecuteModuleMethod(edgeModule.ModuleName, deviceId, methodName); // Assert this.mockRepository.VerifyAll(); From 178acc091771cc89d3fdede9a598adbdb4e46059 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 15:55:24 +0200 Subject: [PATCH 03/30] delete module version --- .../Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor | 5 +---- .../Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor | 2 -- src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs | 1 - src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs | 5 ----- src/AzureIoTHub.Portal/Server/Services/DeviceService.cs | 2 +- src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs | 5 ----- 6 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 7a9c15412..8a23f603a 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -134,20 +134,17 @@ - Module Name - Version Status @context.ModuleName - @context.Version @context.Status - + logs reboot @foreach (var command in context.Commands) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor index 723aca188..6a3c23443 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor @@ -69,7 +69,6 @@ IsLoading = true; currentModuleName = Module.ModuleName; - currentVersion = Module.Version; currentImageUri = Module.ImageURI; currentEnvironmentVariables = new List(Module.EnvironmentVariables.ToArray()); currentModuleIdentityTwinSettings = new List(Module.ModuleIdentityTwinSettings.ToArray()); @@ -81,7 +80,6 @@ public void Submit() { Module.ModuleName = currentModuleName; - Module.Version = currentVersion; Module.ImageURI = currentImageUri; Module.EnvironmentVariables = new List(currentEnvironmentVariables.ToArray()); Module.ModuleIdentityTwinSettings = new List(currentModuleIdentityTwinSettings.ToArray()); diff --git a/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs index 14225aa04..3e98f83b2 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs @@ -150,7 +150,6 @@ public static IoTEdgeModule CreateGatewayModule(Configuration config, JProperty ModuleName = module.Name, ImageURI = module.Value["settings"]["image"]?.Value(), Status = module.Value["status"]?.Value(), - Version = module.Value["version"]?.Value(), }; foreach (var item in GetEnvironmentVariables(module)) diff --git a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs index b6bc05b6c..fe0634306 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs @@ -201,11 +201,6 @@ public static IReadOnlyCollection RetrieveModuleList(Twin twin) module.Status = status.Value(); } - if (propertyObject.TryGetValue("version", out var version)) - { - module.Version = version.Value(); - } - list.Add(module); } diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs index 88cf262b4..f48bb7e7e 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs @@ -437,7 +437,7 @@ public async Task> GetEdgeDeviceLogs(string device var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs index 1c226709e..084779f93 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs @@ -18,11 +18,6 @@ public class IoTEdgeModule [Required(ErrorMessage = "The device model name is required.")] public string ModuleName { get; set; } - /// - /// The module configuration version. - /// - public string Version { get; set; } - /// /// the device image URI. /// From e624cfcb41695e6ef20c89fde41874095bf5dbb7 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 16:01:12 +0200 Subject: [PATCH 04/30] fix unit test --- .../Configurations/EdgeModule/ModuleDialogTab1Tests.cs | 3 --- .../Configurations/EdgeModule/ModuleDialogTab2Tests.cs | 3 --- .../Configurations/EdgeModule/ModuleDialogTab3Tests.cs | 3 --- .../Pages/Configurations/EdgeModule/ModuleDialogTests.cs | 2 -- .../Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs | 3 --- .../Client/Services/EdgeDeviceClientServiceTests.cs | 1 - .../Controllers/v1.0/EdgeDevicesControllerTests.cs | 1 - .../Server/Helpers/ConfigHelperTest.cs | 2 -- .../Server/Services/DeviceServiceTests.cs | 9 +++------ .../Pages/EdgeModels/EdgeModule/ModuleDialog.razor | 1 - 10 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs index 7de7554d4..6a1532171 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs @@ -22,7 +22,6 @@ public void ModuleDialogTab1ShouldBeRenderedProperly() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List() { @@ -78,7 +76,6 @@ public void ClickOnRemoveShouldDeleteRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List() { diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs index 9d3fc7ea3..9c86a66db 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs @@ -22,7 +22,6 @@ public void ModuleDialogTab2ShouldBeRenderedProperly() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List() @@ -78,7 +76,6 @@ public void ClickOnRemoveShouldDeleteRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List() diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs index 67c8dc285..fbf35dadc 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs @@ -22,7 +22,6 @@ public void ModuleDialogTab3ShouldBeRenderedProperly() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -76,7 +74,6 @@ public void ClickOnRemoveShouldDeleteRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs index 5148ac692..a4851d49c 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs @@ -29,7 +29,6 @@ public async Task ModuleDialogTestMustBeRenderedOnShow() var module = new IoTEdgeModule() { ModuleName = moduleName, - Version = "1.0", Status = "running", ImageURI = moduleImageUri, EnvironmentVariables = new List(), @@ -73,7 +72,6 @@ public async Task ClickOnSubmitShouldUpdateModuleValues() var module = new IoTEdgeModule() { ModuleName = moduleName, - Version = moduleVersion, Status = "running", ImageURI = moduleImageUri, EnvironmentVariables = new List(), diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs index d017864c4..3d76b38e4 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs @@ -41,7 +41,6 @@ public async Task ModuleLogsDialogParametersMustBeCorrect() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -84,7 +83,6 @@ public async Task ModuleLogsShouldProcessProblemDetailsExceptionWhenIssueOccursO var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -120,7 +118,6 @@ public async Task ModuleLogsMustCloseOnCLickOnCloseButton() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index 02ef48348..da3601a1f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -180,7 +180,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoError() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index ce0780fc9..57f021c85 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -271,7 +271,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoErrorIsReturned() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs index 3b4a0dca4..b1db5fd4b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs @@ -115,7 +115,6 @@ public void CreateGatewayModuleShouldReturnValue() // Assert Assert.IsNotNull(result); Assert.AreEqual("running", result.Status); - Assert.AreEqual("1.0", result.Version); Assert.AreEqual("image_test", result.ImageURI); Assert.AreEqual(1, result.EnvironmentVariables.Count); } @@ -161,7 +160,6 @@ public void GenerateModulesContentShouldReturnValue() { ModuleName = "ModuleTest", Status = "running", - Version = "1.0", ImageURI = "image", EnvironmentVariables = new List() { diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs index 433833ecf..b47d499cd 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs @@ -1057,7 +1057,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhen200IsReturned() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -1065,7 +1064,7 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhen200IsReturned() var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new @@ -1117,7 +1116,6 @@ public async Task GetEdgeDeviceLogsMustReturnEmptyLogsWhenNot200IsReturned() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -1125,7 +1123,7 @@ public async Task GetEdgeDeviceLogsMustReturnEmptyLogsWhenNot200IsReturned() var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new @@ -1177,7 +1175,6 @@ public async Task GetEdgeDeviceLogsShouldInternalServerErrorExceptionWhenIssueOc var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -1185,7 +1182,7 @@ public async Task GetEdgeDeviceLogsShouldInternalServerErrorExceptionWhenIssueOc var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor index 6a3c23443..3c1675669 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor @@ -54,7 +54,6 @@ public IoTEdgeModule Module { get; set; } private string currentModuleName; - private string currentVersion; private string currentImageUri; private List currentEnvironmentVariables = new(); From bee04c8e5a33f1d9324f9ca1f633b5080e29c4e6 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 16:33:25 +0200 Subject: [PATCH 05/30] add deviceService test --- .../Server/Helpers/DeviceHelperTests.cs | 12 ++++- .../Server/Services/DeviceServiceTests.cs | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs index 84c9e21b0..9957c0934 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs @@ -308,12 +308,20 @@ public void RetrieveModuleListShouldReturnModuleList() LoRaWanNetworkSrvModule = new { version = "1.0", - status = "running" + status = "running", + settings = new + { + image = "image" + } }, LoRaBasicsStationModule = new { runtimeStatus = "running", - version = "1.0" + version = "1.0", + settings = new + { + image = "image" + } } } }); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs index b47d499cd..b9f984aea 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs @@ -1049,6 +1049,60 @@ public async Task ExecuteC2DMethodShouldThrowInternalServerErrorExceptionWhenIss this.mockRepository.VerifyAll(); } + [Test] + public async Task ExecuteCustomCommandC2DMethodShouldReturn200() + { + // Arrange + var service = CreateService(); + var deviceId = Guid.NewGuid().ToString(); + var moduleName = Guid.NewGuid().ToString(); + + var method = new CloudToDeviceMethod(Guid.NewGuid().ToString()); + + _ = this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync( + It.Is(x => x.Equals(deviceId, StringComparison.Ordinal)), + It.Is(x => x.Equals(moduleName, StringComparison.Ordinal)), + It.Is(x => x == method), + It.IsAny())) + .ReturnsAsync(new CloudToDeviceMethodResult + { + Status = 200 + }); + + // Act + var result = await service.ExecuteCustomCommandC2DMethod(deviceId, moduleName, method); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(200, result.Status); + this.mockRepository.VerifyAll(); + } + + [Test] + public async Task ExecuteCustomCommandC2DMethodShouldThrowInternalServerErrorExceptionWhenIssueOccurs() + { + // Arrange + var service = CreateService(); + var deviceId = Guid.NewGuid().ToString(); + var moduleName = Guid.NewGuid().ToString(); + + var method = new CloudToDeviceMethod(Guid.NewGuid().ToString()); + + _ = this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync( + It.Is(x => x.Equals(deviceId, StringComparison.Ordinal)), + It.Is(x => x.Equals(moduleName, StringComparison.Ordinal)), + It.Is(x => x == method), + It.IsAny())) + .ThrowsAsync(new Exception("")); + + // Act + var result = async () => await service.ExecuteCustomCommandC2DMethod(deviceId, moduleName, method); + + // Assert + _ = await result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + [Test] public async Task GetEdgeDeviceLogsMustReturnLogsWhen200IsReturned() { From 230c88e5695dc1a2c0ef661babb472c7ae63a20a Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 17:07:19 +0200 Subject: [PATCH 06/30] add new test edgeDeviceService --- .../Server/Services/EdgeDeviceServiceTest.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index 0dc29822b..63cc21fe1 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -18,6 +18,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using System.Threading.Tasks; using System.Collections.Generic; using AzureIoTHub.Portal.Server.Exceptions; + using FluentAssertions; [TestFixture] public class EdgeDeviceServiceTest @@ -310,6 +311,96 @@ public void WhenEdgeModuleIsNullExecuteMethodShouldThrowArgumentNullException(st _ = Assert.ThrowsAsync(() => edgeDeviceService.ExecuteModuleMethod(null, deviceId, methodName)); } + [TestCase("test")] + public async Task ExecuteModuleCommand(string commandName) + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var edgeModule = new IoTEdgeModule + { + ModuleName = "aaa", + }; + + var deviceId = Guid.NewGuid().ToString(); + + _ = this.mockDeviceService.Setup(c => c.ExecuteCustomCommandC2DMethod( + It.Is(x => x == deviceId), + It.Is(x => x == edgeModule.ModuleName), + It.Is(x => + x.MethodName == commandName + ))) + .ReturnsAsync(new CloudToDeviceMethodResult + { + Status = 200 + }); + + // Act + var result = await edgeDeviceService.ExecuteModuleCommand(deviceId, edgeModule.ModuleName, commandName); + + // Assert + Assert.AreEqual(200, result.Status); + this.mockRepository.VerifyAll(); + } + + [TestCase("test")] + public void WhenDeviceIdIsNullExecuteModuleCommandShouldThrowArgumentNullException(string commandName) + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var edgeModule = new IoTEdgeModule + { + ModuleName = "aaa", + }; + + var deviceId = string.Empty; + + // Act + var result = async () => await edgeDeviceService.ExecuteModuleCommand(deviceId, edgeModule.ModuleName, commandName); + + // Assert + _ = result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + + [TestCase("test")] + public void WhenModuleIsNullExecuteModuleCommandShouldThrowArgumentNullException(string commandName) + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var deviceId = Guid.NewGuid().ToString(); + + // Act + var result = async () => await edgeDeviceService.ExecuteModuleCommand(deviceId, null, commandName); + + // Assert + _ = result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + + [Test] + public void WhenCommandNameIsNullExecuteModuleCommandShouldThrowArgumentNullException() + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var deviceId = string.Empty; + + var edgeModule = new IoTEdgeModule + { + ModuleName = "aaa", + }; + + // Act + var result = async () => await edgeDeviceService.ExecuteModuleCommand(deviceId, edgeModule.ModuleName, null); + + // Assert + _ = result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + [Test] public async Task GetEdgeDeviceCredentialsShouldReturnEnrollmentCredentials() { From ea39e8d42eabe4c2b2ff73b4b161df2086814515 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 09:18:04 +0200 Subject: [PATCH 07/30] add unit test on edgeDeviceClientService --- .../Services/EdgeDeviceClientServiceTests.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index da3601a1f..1e8c9c80b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -207,5 +207,53 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoError() _ = result.Count.Should().Be(1); } + + [Test] + public async Task ExecuteModuleMethodShouldReturn200() + { + // Arrange + var deviceId = Fixture.Create(); + + var edgeModule = new IoTEdgeModule() + { + ModuleName = Guid.NewGuid().ToString() + }; + + var methodName = Fixture.Create(); + var c2Dresult = Fixture.Create(); + + _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}") + .RespondJson(c2Dresult); + + // Act + var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule, methodName); + + // Assert + _ = result.Should().BeEquivalentTo(c2Dresult); + MockHttpClient.VerifyNoOutstandingRequest(); + MockHttpClient.VerifyNoOutstandingExpectation(); + } + + [Test] + public async Task ExecuteModuleCommandShouldReturn200() + { + // Arrange + var deviceId = Fixture.Create(); + var commandName = Fixture.Create(); + var moduleName = Fixture.Create(); + + var c2Dresult = Fixture.Create(); + + _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") + .RespondJson(c2Dresult); + + // Act + var result = await this.edgeDeviceClientService.ExecuteModuleCommand(deviceId, moduleName, commandName); + + // Assert + _ = result.Should().BeEquivalentTo(c2Dresult); + MockHttpClient.VerifyNoOutstandingRequest(); + MockHttpClient.VerifyNoOutstandingExpectation(); + } } } From 54d5c350cb71f58ab2827e86e8cefe1bfba74e7b Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 09:52:47 +0200 Subject: [PATCH 08/30] add unit test on edgeDeviceController --- .../v1.0/EdgeDevicesControllerTests.cs | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index 57f021c85..a454f7f80 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.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.Tests.Unit.Server.Controllers.v1._0 +namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v10 { using System; using System.Collections.Generic; @@ -399,5 +399,30 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName) this.mockRepository.VerifyAll(); } + + [TestCase("test")] + public async Task ExecuteCustomModuleMethodShouldExecuteC2DMethod(string commandName) + { + // Arrange + var edgeDeviceController = CreateEdgeDevicesController(); + + var deviceId = Guid.NewGuid().ToString(); + var moduleName = Guid.NewGuid().ToString(); + + _ = this.mockEdgeDeviceService + .Setup(x => x.ExecuteModuleCommand( + It.Is(c => c.Equals(deviceId, StringComparison.Ordinal)), + It.Is(c => c.Equals(moduleName, StringComparison.Ordinal)), + It.Is(c => c.Equals(commandName, StringComparison.Ordinal)))) + .ReturnsAsync(new C2Dresult()); + + // Act + var result = await edgeDeviceController.ExecuteCustomModuleMethod(deviceId, moduleName, commandName); + + // Assert + Assert.IsNotNull(result); + + this.mockRepository.VerifyAll(); + } } } From ef02b0262621b1cbe51a27252bd77ee8143b65f1 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 10:43:38 +0200 Subject: [PATCH 09/30] add unit test on edgeDeviceDetailPage --- .../EdgeDevices/EdgeDeviceDetailPageTests.cs | 101 +++++++++++++++++- .../EdgeDevices/EdgeDeviceDetailPage.razor | 2 +- 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs index 424982cbb..3b8909951 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs @@ -262,7 +262,7 @@ public void ClickOnRebootShouldDisplaySnackbarIfError() Status = 500 }); - _ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny(), Severity.Error, It.IsAny>())).Returns((Snackbar)null); + _ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny(), Severity.Error, It.IsAny>())).Returns(value: null); var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); @@ -357,6 +357,7 @@ public void ClickOnLogsShouldDisplayLogs() [Test] public void ClickOnConnectShouldDisplayDeviceCredentials() { + // Arrange _ = SetupOnInitialisation(); var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); @@ -376,6 +377,80 @@ public void ClickOnConnectShouldDisplayDeviceCredentials() cut.WaitForAssertion(() => MockRepository.VerifyAll()); } + [Test] + public void ClickOnCommandModuleShouldReturn200() + { + // Arrange + _ = SetupOnInitialisation(); + + var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); + cut.WaitForAssertion(() => cut.Find("#commandTest")); + + var commandButton = cut.Find("#commandTest"); + + _ = this.mockEdgeDeviceClientService + .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .ReturnsAsync(new C2Dresult() { Status = 200 }); + + _ = this.mockSnackbarService + .Setup(c => c.Add(It.IsAny(), Severity.Success, It.IsAny>())) + .Returns(value: null); + + // Act + commandButton.Click(); + + // Assert + cut.WaitForAssertion(() => MockRepository.VerifyAll()); + } + + [Test] + public void ClickOnCommandModuleShouldReturn400() + { + // Arrange + _ = SetupOnInitialisation(); + + var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); + cut.WaitForAssertion(() => cut.Find("#commandTest")); + + var commandButton = cut.Find("#commandTest"); + + _ = this.mockEdgeDeviceClientService + .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .ReturnsAsync(new C2Dresult() { Status = 400 }); + + _ = this.mockSnackbarService + .Setup(c => c.Add(It.IsAny(), Severity.Error, It.IsAny>())) + .Returns(value: null); + + // Act + commandButton.Click(); + + // Assert + cut.WaitForAssertion(() => MockRepository.VerifyAll()); + } + + [Test] + public void ClickOnCommandModuleShouldProcessProblemDetailsExceptionWhenIssueOccurs() + { + // Arrange + _ = SetupOnInitialisation(); + + var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); + cut.WaitForAssertion(() => cut.Find("#commandTest")); + + var commandButton = cut.Find("#commandTest"); + + _ = this.mockEdgeDeviceClientService + .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails())); + + // Act + commandButton.Click(); + + // Assert + cut.WaitForAssertion(() => MockRepository.VerifyAll()); + } + [Test] public void ClickOnDeleteShouldDisplayConfirmationDialogAndReturnIfAborted() { @@ -434,7 +509,15 @@ private IoTEdgeDevice SetupOnInitialisation() DeviceId = this.mockdeviceId, ConnectionState = "Connected", ModelId = Guid.NewGuid().ToString(), - Tags = tags + Tags = tags, + Modules = new List() + { + new IoTEdgeModule() + { + ModuleName = "moduleTest", + ImageURI = Guid.NewGuid().ToString() + } + } }; _ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevice(this.mockdeviceId)) @@ -444,7 +527,19 @@ private IoTEdgeDevice SetupOnInitialisation() .Setup(service => service.GetIoTEdgeModel(It.Is(x => x.Equals(mockIoTEdgeDevice.ModelId, StringComparison.Ordinal)))) .ReturnsAsync(new IoTEdgeModel() { - ModelId = mockIoTEdgeDevice.ModelId + ModelId = mockIoTEdgeDevice.ModelId, + EdgeModules = new List() + { + new IoTEdgeModule() + { + ModuleName = "moduleTest", + ImageURI = Guid.NewGuid().ToString(), + Commands = new List() + { + new Portal.Shared.Models.v10.IoTEdgeModuleCommand(){ Name = "commandTest"} + } + } + } }); _ = this.mockDeviceTagSettingsClientService diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 8a23f603a..2893535dd 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -149,7 +149,7 @@ reboot @foreach (var command in context.Commands) { - @command.Name + @command.Name } From 59b269cf7d00f45a0648d00033e6c8633348aeb6 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 11:16:49 +0200 Subject: [PATCH 10/30] add device image on edge device list --- .../Client/Pages/EdgeDevices/EdgeDeviceListPage.razor | 7 ++++++- src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs | 3 ++- .../Shared/Models/v1.0/IoTEdgeListItem.cs | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor index ba618a630..dc3f2ce9a 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor @@ -48,6 +48,7 @@ + @@ -65,13 +66,17 @@ - ID + + Device ID Allowed Nb devices See details Delete + + + @context.DeviceId diff --git a/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs b/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs index 4e04324e5..33d33fad3 100644 --- a/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs +++ b/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs @@ -24,7 +24,8 @@ public IoTEdgeListItem CreateEdgeDeviceListItem(Twin deviceTwin) { DeviceId = deviceTwin?.DeviceId, Status = deviceTwin?.Status.ToString(), - NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin) + NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin), + ImageUrl = this.deviceModelImageManager.ComputeImageUri(DeviceHelper.RetrieveTagValue(deviceTwin, nameof(IoTEdgeDevice.ModelId))) }; } diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs index b3fa97b46..e8e5561a8 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs @@ -3,6 +3,7 @@ namespace AzureIoTHub.Portal.Models.v10 { + using System; using System.ComponentModel.DataAnnotations; /// @@ -25,5 +26,10 @@ public class IoTEdgeListItem /// The number of devices connected on the IoT Edge. /// public int NbDevices { get; set; } + + /// + /// The device model image Url. + /// + public Uri ImageUrl { get; set; } } } From 76a826bd2d2587f3a383e61876534cdbb9e7baa2 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 11:35:10 +0200 Subject: [PATCH 11/30] fix unit test --- .../Server/Mappers/EdgeDeviceMapperTest.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs index 75826fb03..5dd46cf7f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs @@ -40,7 +40,14 @@ public void CreateEdgeDeviceListItemShouldReturnValue() var edgeDeviceMapper = CreateMapper(); var deviceTwin = new Twin(Guid.NewGuid().ToString()); + var modelId = Guid.NewGuid().ToString(); + deviceTwin.Properties.Reported["clients"] = new object[12]; + deviceTwin.Tags["modelId"] = modelId; + + _ = this.mockDeviceModelImageManager + .Setup(x => x.ComputeImageUri(It.Is(c => c.Equals(modelId, StringComparison.Ordinal)))) + .Returns(new Uri($"http://fake.local/{modelId}")); // Act var result = edgeDeviceMapper.CreateEdgeDeviceListItem(deviceTwin); From 28370d7db7708971a5f1f437db87388bed9bd7ab Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 13:43:06 +0200 Subject: [PATCH 12/30] resolve request change --- .../Client/Services/EdgeDeviceClientServiceTests.cs | 4 ++-- .../Client/Services/EdgeDeviceClientService.cs | 4 ++-- .../Server/Controllers/v1.0/EdgeDevicesController.cs | 4 ++-- src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index 1e8c9c80b..ff839f30c 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -222,7 +222,7 @@ public async Task ExecuteModuleMethodShouldReturn200() var methodName = Fixture.Create(); var c2Dresult = Fixture.Create(); - _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}") + _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}") .RespondJson(c2Dresult); // Act @@ -244,7 +244,7 @@ public async Task ExecuteModuleCommandShouldReturn200() var c2Dresult = Fixture.Create(); - _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") + _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") .RespondJson(c2Dresult); // Act diff --git a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs index 22d237c7c..b3d3cc534 100644 --- a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs @@ -57,14 +57,14 @@ public async Task> GetEdgeDeviceLogs(string deviceId, IoT public async Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName) { - var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}"); + var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}", null); return await response.Content.ReadFromJsonAsync(); } public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) { - var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}"); + var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}", null); return await response.Content.ReadFromJsonAsync(); } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index c7e418f88..1b0ff9b31 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -160,13 +160,13 @@ public async Task> GetCredentials(string dev /// The edge module name. /// The device identifier. /// Name of the method. - [HttpGet("{deviceId}/{moduleName}/{methodName}", Name = "Get Execute module command")] + [HttpPost("{deviceId}/{moduleName}/{methodName}", Name = "POST Execute module command")] public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) { return await this.edgeDevicesService.ExecuteModuleMethod(moduleName, deviceId, methodName); } - [HttpGet("{deviceId}/{moduleName}/custom/{commandName}", Name = "GET Execute custom module command")] + [HttpPost("{deviceId}/{moduleName}/custom/{commandName}", Name = "POST Execute custom module command")] public async Task ExecuteCustomModuleMethod(string deviceId, string moduleName, string commandName) { return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, commandName); diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index 04bd0ddc2..a5228a6e1 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -179,7 +179,7 @@ public async Task ExecuteModuleMethod(string moduleName, string devic ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); var method = new CloudToDeviceMethod(methodName); - var payload = "{}"; + var payload = string.Empty; if (methodName == "RestartModule") { From 9f6d5fb39941859c0c04f15660fd176db80d0dce Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 15:49:53 +0200 Subject: [PATCH 13/30] delete useless endpoint --- .../EdgeDevices/EdgeDeviceDetailPageTests.cs | 12 +++--- .../Services/EdgeDeviceClientServiceTests.cs | 6 +-- .../v1.0/EdgeDevicesControllerTests.cs | 2 +- .../EdgeDevices/EdgeDeviceDetailPage.razor | 39 ++----------------- .../Services/EdgeDeviceClientService.cs | 11 +----- .../Services/IEdgeDeviceClientService.cs | 3 +- .../Controllers/v1.0/EdgeDevicesController.cs | 14 +++---- .../Server/Services/EdgeDevicesService.cs | 14 +++---- .../Server/Services/IEdgeDevicesService.cs | 2 +- 9 files changed, 29 insertions(+), 74 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs index 3b8909951..056049c3d 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs @@ -210,7 +210,7 @@ public void ClickOnRebootShouldRebootModule() _ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List()); - _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule")) + _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule")) .ReturnsAsync(new C2Dresult() { Payload = "ABC", @@ -255,7 +255,7 @@ public void ClickOnRebootShouldDisplaySnackbarIfError() _ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List()); - _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule")) + _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule")) .ReturnsAsync(new C2Dresult() { Payload = "ABC", @@ -301,7 +301,7 @@ public void EdgeDeviceDetailPageShouldProcessProblemDetailsExceptionWhenIssueOcc _ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List()); - _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule")) + _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule")) .ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails())); var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); @@ -389,7 +389,7 @@ public void ClickOnCommandModuleShouldReturn200() var commandButton = cut.Find("#commandTest"); _ = this.mockEdgeDeviceClientService - .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .Setup(x => x.ExecuteModuleMethod(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) .ReturnsAsync(new C2Dresult() { Status = 200 }); _ = this.mockSnackbarService @@ -415,7 +415,7 @@ public void ClickOnCommandModuleShouldReturn400() var commandButton = cut.Find("#commandTest"); _ = this.mockEdgeDeviceClientService - .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .Setup(x => x.ExecuteModuleMethod(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) .ReturnsAsync(new C2Dresult() { Status = 400 }); _ = this.mockSnackbarService @@ -441,7 +441,7 @@ public void ClickOnCommandModuleShouldProcessProblemDetailsExceptionWhenIssueOcc var commandButton = cut.Find("#commandTest"); _ = this.mockEdgeDeviceClientService - .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .Setup(x => x.ExecuteModuleMethod(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) .ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails())); // Act diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index ff839f30c..f4182cb2b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -226,7 +226,7 @@ public async Task ExecuteModuleMethodShouldReturn200() .RespondJson(c2Dresult); // Act - var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule, methodName); + var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule.ModuleName, methodName); // Assert _ = result.Should().BeEquivalentTo(c2Dresult); @@ -244,11 +244,11 @@ public async Task ExecuteModuleCommandShouldReturn200() var c2Dresult = Fixture.Create(); - _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") + _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/{commandName}") .RespondJson(c2Dresult); // Act - var result = await this.edgeDeviceClientService.ExecuteModuleCommand(deviceId, moduleName, commandName); + var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, moduleName, commandName); // Assert _ = result.Should().BeEquivalentTo(c2Dresult); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index a454f7f80..cc85cdc1f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -417,7 +417,7 @@ public async Task ExecuteCustomModuleMethodShouldExecuteC2DMethod(string command .ReturnsAsync(new C2Dresult()); // Act - var result = await edgeDeviceController.ExecuteCustomModuleMethod(deviceId, moduleName, commandName); + var result = await edgeDeviceController.ExecuteModuleMethod(deviceId, moduleName, commandName); // Assert Assert.IsNotNull(result); diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 2893535dd..33d99cc87 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -146,10 +146,10 @@ @context.Status logs - reboot + reboot @foreach (var command in context.Commands) { - @command.Name + @command.Name } @@ -297,44 +297,13 @@ } } - public async Task OnMethod(IoTEdgeModule module, string methodName) + public async Task OnMethod(string moduleName, string methodName) { isProcessing = true; try { - var c2dResult = await EdgeDeviceClientService.ExecuteModuleMethod(edgeDevice.DeviceId, module, methodName); - - if (c2dResult.Status == 200) - { - Snackbar.Add("Command successfully executed.", Severity.Success); - } - else - { - Snackbar.Add($"Error
Status : {c2dResult.Status};
Payload : {c2dResult.Payload};", Severity.Error, - (option) => - { - option.VisibleStateDuration = 10000; - }); - } - } - catch (ProblemDetailsException exception) - { - Error?.ProcessProblemDetails(exception); - } - finally - { - isProcessing = false; - } - } - - public async Task ExecuteCommand(string moduleName, IoTEdgeModuleCommand command) - { - isProcessing = true; - - try - { - var c2dResult = await EdgeDeviceClientService.ExecuteModuleCommand(edgeDevice.DeviceId, moduleName, command.Name); + var c2dResult = await EdgeDeviceClientService.ExecuteModuleMethod(edgeDevice.DeviceId, moduleName, methodName); if (c2dResult.Status == 200) { diff --git a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs index b3d3cc534..8ff7a3349 100644 --- a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs @@ -55,16 +55,9 @@ public async Task> GetEdgeDeviceLogs(string deviceId, IoT return await response.Content.ReadFromJsonAsync>(); } - public async Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName) + public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { - var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}", null); - - return await response.Content.ReadFromJsonAsync(); - } - - public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) - { - var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}", null); + var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{moduleName}/{methodName}", null); return await response.Content.ReadFromJsonAsync(); } diff --git a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs index a096d94cf..4fb17e009 100644 --- a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs @@ -23,8 +23,7 @@ public interface IEdgeDeviceClientService Task> GetEdgeDeviceLogs(string deviceId, IoTEdgeModule edgeModule); - Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName); + Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName); - Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName); } } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index 1b0ff9b31..220b100a4 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -161,15 +161,13 @@ public async Task> GetCredentials(string dev /// The device identifier. /// Name of the method. [HttpPost("{deviceId}/{moduleName}/{methodName}", Name = "POST Execute module command")] - public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) + public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { - return await this.edgeDevicesService.ExecuteModuleMethod(moduleName, deviceId, methodName); - } - - [HttpPost("{deviceId}/{moduleName}/custom/{commandName}", Name = "POST Execute custom module command")] - public async Task ExecuteCustomModuleMethod(string deviceId, string moduleName, string commandName) - { - return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, commandName); + if (methodName.Equals("RestartModule", StringComparison.Ordinal)) + { + return await this.edgeDevicesService.ExecuteModuleMethod(deviceId, moduleName, methodName); + } + return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, methodName); } /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index a5228a6e1..0fc435c9a 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -174,21 +174,17 @@ public async Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice) /// /// /// - public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) + public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); var method = new CloudToDeviceMethod(methodName); - var payload = string.Empty; - if (methodName == "RestartModule") + var payload = JsonConvert.SerializeObject(new { - payload = JsonConvert.SerializeObject(new - { - id = moduleName, - schemaVersion = "1.0" - }); - } + id = moduleName, + schemaVersion = "1.0" + }); _ = method.SetPayloadJson(payload); diff --git a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs index a7af647bf..1e8407bdd 100644 --- a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs @@ -24,7 +24,7 @@ PaginationResult GetEdgeDevicesPage(PaginationResult edge Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice); - Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName); + Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName); Task GetEdgeDeviceCredentials(string edgeDeviceId); From d2846b8cfc88840588d08f92594b4dda0c7c9950 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 16:00:07 +0200 Subject: [PATCH 14/30] fix unit test --- .../Server/Services/EdgeDeviceServiceTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index 63cc21fe1..f08f2de9f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -293,7 +293,7 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string }); // Act - _ = await edgeDeviceService.ExecuteModuleMethod(edgeModule.ModuleName, deviceId, methodName); + _ = await edgeDeviceService.ExecuteModuleMethod(deviceId, edgeModule.ModuleName, methodName); // Assert this.mockRepository.VerifyAll(); @@ -308,7 +308,7 @@ public void WhenEdgeModuleIsNullExecuteMethodShouldThrowArgumentNullException(st var deviceId = Guid.NewGuid().ToString(); // Assert - _ = Assert.ThrowsAsync(() => edgeDeviceService.ExecuteModuleMethod(null, deviceId, methodName)); + _ = Assert.ThrowsAsync(() => edgeDeviceService.ExecuteModuleMethod(deviceId, null, methodName)); } [TestCase("test")] From 31e1e67e8ed29176dee12d1b9c1318d403a3791c Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 16:14:06 +0200 Subject: [PATCH 15/30] fix unit test --- .../Server/Controllers/v1.0/EdgeDevicesControllerTests.cs | 2 +- .../Server/Controllers/v1.0/EdgeDevicesController.cs | 6 +----- .../Server/Services/EdgeDevicesService.cs | 5 +++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index cc85cdc1f..09909638c 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -410,7 +410,7 @@ public async Task ExecuteCustomModuleMethodShouldExecuteC2DMethod(string command var moduleName = Guid.NewGuid().ToString(); _ = this.mockEdgeDeviceService - .Setup(x => x.ExecuteModuleCommand( + .Setup(x => x.ExecuteModuleMethod( It.Is(c => c.Equals(deviceId, StringComparison.Ordinal)), It.Is(c => c.Equals(moduleName, StringComparison.Ordinal)), It.Is(c => c.Equals(commandName, StringComparison.Ordinal)))) diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index 220b100a4..682b4f486 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -163,11 +163,7 @@ public async Task> GetCredentials(string dev [HttpPost("{deviceId}/{moduleName}/{methodName}", Name = "POST Execute module command")] public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { - if (methodName.Equals("RestartModule", StringComparison.Ordinal)) - { - return await this.edgeDevicesService.ExecuteModuleMethod(deviceId, moduleName, methodName); - } - return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, methodName); + return await this.edgeDevicesService.ExecuteModuleMethod(deviceId, moduleName, methodName); } /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index 0fc435c9a..03754a792 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -178,6 +178,11 @@ public async Task ExecuteModuleMethod(string deviceId, string moduleN { ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); + if (!methodName.Equals("RestartModule", StringComparison.Ordinal)) + { + return await ExecuteModuleCommand(deviceId, moduleName, methodName); + } + var method = new CloudToDeviceMethod(methodName); var payload = JsonConvert.SerializeObject(new From a603d5ec8249775fb53651d29d0bbdb71f2c7608 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 14:56:18 +0200 Subject: [PATCH 16/30] resolve issue #1117 --- .../EdgeDevices/EdgeDeviceDetailPage.razor | 52 +++++++++++++++++-- .../Services/EdgeDeviceClientService.cs | 9 +++- .../Services/IEdgeDeviceClientService.cs | 2 + .../Controllers/v1.0/EdgeDevicesController.cs | 14 +++-- .../Server/Helpers/DeviceHelper.cs | 6 +++ .../Server/Services/DeviceService.cs | 20 +++++++ .../Server/Services/EdgeDevicesService.cs | 39 +++++++++++--- .../Server/Services/EdgeModelService.cs | 3 ++ .../Server/Services/IDeviceService.cs | 2 + .../Server/Services/IEdgeDevicesService.cs | 4 +- 10 files changed, 136 insertions(+), 15 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index ee7c18db8..7a9c15412 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -1,5 +1,7 @@ @page "/edge/devices/{deviceId}" + @using AzureIoTHub.Portal.Models.v10 +@using AzureIoTHub.Portal.Shared.Models.v10; @attribute [Authorize] @inject NavigationManager NavigationManager @@ -145,9 +147,13 @@ @context.ModuleName @context.Version @context.Status - + logs reboot + @foreach (var command in context.Commands) + { + @command.Name + } @@ -226,6 +232,17 @@ await LoadModel(edgeDevice.ModelId); + foreach (var edgeModelModule in edgeModel.EdgeModules) + { + foreach (var item in edgeDevice.Modules) + { + if (item.ModuleName.Equals(edgeModelModule.ModuleName, StringComparison.Ordinal)) + { + item.Commands = edgeModelModule.Commands; + } + } + } + TagList = await DeviceTagSettingsClientService.GetDeviceTags(); } catch (ProblemDetailsException exception) @@ -289,8 +306,6 @@ try { - //var result = await Http.PostAsJsonAsync($"api/edge/devices/{edgeDevice.DeviceId}/{module.ModuleName}/{methodName}", module); - var c2dResult = await EdgeDeviceClientService.ExecuteModuleMethod(edgeDevice.DeviceId, module, methodName); if (c2dResult.Status == 200) @@ -316,6 +331,37 @@ } } + public async Task ExecuteCommand(string moduleName, IoTEdgeModuleCommand command) + { + isProcessing = true; + + try + { + var c2dResult = await EdgeDeviceClientService.ExecuteModuleCommand(edgeDevice.DeviceId, moduleName, command.Name); + + if (c2dResult.Status == 200) + { + Snackbar.Add("Command successfully executed.", Severity.Success); + } + else + { + Snackbar.Add($"Error
Status : {c2dResult.Status};
Payload : {c2dResult.Payload};", Severity.Error, + (option) => + { + option.VisibleStateDuration = 10000; + }); + } + } + catch (ProblemDetailsException exception) + { + Error?.ProcessProblemDetails(exception); + } + finally + { + isProcessing = false; + } + } + public async Task ShowEdgeDeviceLogs(IoTEdgeModule module) { var parameter = new DialogParameters diff --git a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs index 0827c35a8..22d237c7c 100644 --- a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs @@ -57,7 +57,14 @@ public async Task> GetEdgeDeviceLogs(string deviceId, IoT public async Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName) { - var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}", edgeModule); + var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}"); + + return await response.Content.ReadFromJsonAsync(); + } + + public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) + { + var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}"); return await response.Content.ReadFromJsonAsync(); } diff --git a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs index ca070d0ef..a096d94cf 100644 --- a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs @@ -24,5 +24,7 @@ public interface IEdgeDeviceClientService Task> GetEdgeDeviceLogs(string deviceId, IoTEdgeModule edgeModule); Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName); + + Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName); } } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index 4c6588c3c..c7e418f88 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -157,13 +157,19 @@ public async Task> GetCredentials(string dev /// /// Executes the module method on the IoT Edge device. /// - /// The edge module. + /// The edge module name. /// The device identifier. /// Name of the method. - [HttpPost("{deviceId}/{moduleId}/{methodName}", Name = "POST Execute module command")] - public async Task ExecuteModuleMethod(IoTEdgeModule edgeModule, string deviceId, string methodName) + [HttpGet("{deviceId}/{moduleName}/{methodName}", Name = "Get Execute module command")] + public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) { - return await this.edgeDevicesService.ExecuteModuleMethod(edgeModule, deviceId, methodName); + return await this.edgeDevicesService.ExecuteModuleMethod(moduleName, deviceId, methodName); + } + + [HttpGet("{deviceId}/{moduleName}/custom/{commandName}", Name = "GET Execute custom module command")] + public async Task ExecuteCustomModuleMethod(string deviceId, string moduleName, string commandName) + { + return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, commandName); } /// diff --git a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs index d76cf9b89..b6bc05b6c 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs @@ -190,6 +190,12 @@ public static IReadOnlyCollection RetrieveModuleList(Twin twin) ModuleName = property.Key }; + if (propertyObject.TryGetValue("settings", out var moduleSettings)) + { + var setting = moduleSettings.ToObject>(); + module.ImageURI = setting["image"]; + } + if (propertyObject.TryGetValue("status", out var status)) { module.Status = status.Value(); diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs index b145d8d4b..88cf262b4 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs @@ -403,6 +403,26 @@ public async Task ExecuteC2DMethod(string deviceId, C } } + /// + /// C2DMethod for custom command. + /// + /// the deviceId. + /// the module name. + /// the C2DMethod. + /// + /// + public async Task ExecuteCustomCommandC2DMethod(string deviceId, string moduleName, CloudToDeviceMethod method) + { + try + { + return await this.serviceClient.InvokeDeviceMethodAsync(deviceId, moduleName, method); + } + catch (Exception e) + { + throw new InternalServerErrorException($"Unable to execute the cloud to device method {method.MethodName} on the device with id {deviceId}", e); + } + } + /// /// Get edge device logs /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index c1bd98583..04bd0ddc2 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -170,23 +170,23 @@ public async Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice) /// /// Executes the module method on the IoT Edge device. /// - /// + /// /// /// /// - public async Task ExecuteModuleMethod(IoTEdgeModule edgeModule, string deviceId, string methodName) + public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) { - ArgumentNullException.ThrowIfNull(edgeModule, nameof(edgeModule)); + ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); var method = new CloudToDeviceMethod(methodName); - var payload = string.Empty; + var payload = "{}"; if (methodName == "RestartModule") { payload = JsonConvert.SerializeObject(new { - id = edgeModule.ModuleName, - schemaVersion = edgeModule.Version + id = moduleName, + schemaVersion = "1.0" }); } @@ -201,6 +201,33 @@ public async Task ExecuteModuleMethod(IoTEdgeModule edgeModule, strin }; } + /// + /// Execute the custom module command. + /// + /// the device identifier. + /// the module name. + /// the command name. + /// + public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) + { + ArgumentNullException.ThrowIfNull(deviceId, nameof(deviceId)); + ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); + ArgumentNullException.ThrowIfNull(commandName, nameof(commandName)); + + var method = new CloudToDeviceMethod(commandName); + var payload = "{}"; + + _ = method.SetPayloadJson(payload); + + var result = await this.devicesService.ExecuteCustomCommandC2DMethod(deviceId,moduleName, method); + + return new C2Dresult() + { + Payload = result.GetPayloadAsJson(), + Status = result.Status + }; + } + /// /// Gets the IoT Edge device enrollement credentials. /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs index 480e1e635..9f5904683 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs @@ -89,10 +89,13 @@ public async Task GetEdgeModel(string modelId) var query = await this.tableClientFactory .GetEdgeDeviceTemplates() .GetEntityAsync(DefaultPartitionKey, modelId); + var modules = await this.configService.GetConfigModuleList(modelId); + var commands = this.tableClientFactory.GetEdgeModuleCommands() .Query(c => c.PartitionKey == modelId) .ToArray(); + return this.edgeDeviceModelMapper.CreateEdgeDeviceModel(query.Value, modules, commands); } catch (RequestFailedException e) diff --git a/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs index 2d748613a..45969d89e 100644 --- a/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/IDeviceService.cs @@ -25,6 +25,8 @@ public interface IDeviceService Task ExecuteC2DMethod(string deviceId, CloudToDeviceMethod method); + Task ExecuteCustomCommandC2DMethod(string deviceId, string moduleName, CloudToDeviceMethod method); + Task DeleteDevice(string deviceId); Task> GetAllDevice( diff --git a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs index 16222e921..a7af647bf 100644 --- a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs @@ -24,8 +24,10 @@ PaginationResult GetEdgeDevicesPage(PaginationResult edge Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice); - Task ExecuteModuleMethod(IoTEdgeModule edgeModule, string deviceId, string methodName); + Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName); Task GetEdgeDeviceCredentials(string edgeDeviceId); + + Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName); } } From edcc0e65bb5ee94aff787a8bfd3a1802ec60e2fa Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 15:15:40 +0200 Subject: [PATCH 17/30] fix unit test --- .../Server/Controllers/v1.0/EdgeDevicesControllerTests.cs | 4 ++-- .../Server/Services/EdgeDeviceServiceTest.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index bf3449017..ce0780fc9 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -387,13 +387,13 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName) var deviceId = Guid.NewGuid().ToString(); _ = this.mockEdgeDeviceService - .Setup(x => x.ExecuteModuleMethod(It.IsAny(), + .Setup(x => x.ExecuteModuleMethod(It.IsAny(), It.Is(c => c.Equals(deviceId, StringComparison.Ordinal)), It.Is(c => c.Equals(methodName, StringComparison.Ordinal)))) .ReturnsAsync(new C2Dresult()); // Act - var result = await edgeDeviceController.ExecuteModuleMethod(module, deviceId, methodName); + var result = await edgeDeviceController.ExecuteModuleMethod(module.ModuleName, deviceId, methodName); // Assert Assert.IsNotNull(result); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index bb45e8259..0dc29822b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -267,7 +267,7 @@ public void WhenEdgeDeviceIsNullUpdateEdgeDeviceShouldThrowArgumentNullException _ = Assert.ThrowsAsync(() => edgeDeviceService.UpdateEdgeDevice(null)); } - [TestCase("RestartModule", /*lang=json,strict*/ "{\"id\":\"aaa\",\"schemaVersion\":null}")] + [TestCase("RestartModule", /*lang=json,strict*/ "{\"id\":\"aaa\",\"schemaVersion\":\"1.0\"}")] public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string expected) { // Arrange @@ -292,7 +292,7 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string }); // Act - _ = await edgeDeviceService.ExecuteModuleMethod(edgeModule, deviceId, methodName); + _ = await edgeDeviceService.ExecuteModuleMethod(edgeModule.ModuleName, deviceId, methodName); // Assert this.mockRepository.VerifyAll(); From 0335bf3ce88b9cca0f7e727d1cb1cba95205a87e Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 15:55:24 +0200 Subject: [PATCH 18/30] delete module version --- .../Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor | 5 +---- .../Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor | 2 -- src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs | 1 - src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs | 5 ----- src/AzureIoTHub.Portal/Server/Services/DeviceService.cs | 2 +- src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs | 5 ----- 6 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 7a9c15412..8a23f603a 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -134,20 +134,17 @@ - Module Name - Version Status @context.ModuleName - @context.Version @context.Status - + logs reboot @foreach (var command in context.Commands) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor index 723aca188..6a3c23443 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor @@ -69,7 +69,6 @@ IsLoading = true; currentModuleName = Module.ModuleName; - currentVersion = Module.Version; currentImageUri = Module.ImageURI; currentEnvironmentVariables = new List(Module.EnvironmentVariables.ToArray()); currentModuleIdentityTwinSettings = new List(Module.ModuleIdentityTwinSettings.ToArray()); @@ -81,7 +80,6 @@ public void Submit() { Module.ModuleName = currentModuleName; - Module.Version = currentVersion; Module.ImageURI = currentImageUri; Module.EnvironmentVariables = new List(currentEnvironmentVariables.ToArray()); Module.ModuleIdentityTwinSettings = new List(currentModuleIdentityTwinSettings.ToArray()); diff --git a/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs index 14225aa04..3e98f83b2 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs @@ -150,7 +150,6 @@ public static IoTEdgeModule CreateGatewayModule(Configuration config, JProperty ModuleName = module.Name, ImageURI = module.Value["settings"]["image"]?.Value(), Status = module.Value["status"]?.Value(), - Version = module.Value["version"]?.Value(), }; foreach (var item in GetEnvironmentVariables(module)) diff --git a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs index b6bc05b6c..fe0634306 100644 --- a/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs +++ b/src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs @@ -201,11 +201,6 @@ public static IReadOnlyCollection RetrieveModuleList(Twin twin) module.Status = status.Value(); } - if (propertyObject.TryGetValue("version", out var version)) - { - module.Version = version.Value(); - } - list.Add(module); } diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs index 88cf262b4..f48bb7e7e 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs @@ -437,7 +437,7 @@ public async Task> GetEdgeDeviceLogs(string device var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs index 1c226709e..084779f93 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeModule.cs @@ -18,11 +18,6 @@ public class IoTEdgeModule [Required(ErrorMessage = "The device model name is required.")] public string ModuleName { get; set; } - /// - /// The module configuration version. - /// - public string Version { get; set; } - /// /// the device image URI. /// From 2352b587ebf86378ec97056013afe11a32129fbb Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 16:01:12 +0200 Subject: [PATCH 19/30] fix unit test --- .../Configurations/EdgeModule/ModuleDialogTab1Tests.cs | 3 --- .../Configurations/EdgeModule/ModuleDialogTab2Tests.cs | 3 --- .../Configurations/EdgeModule/ModuleDialogTab3Tests.cs | 3 --- .../Pages/Configurations/EdgeModule/ModuleDialogTests.cs | 2 -- .../Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs | 3 --- .../Client/Services/EdgeDeviceClientServiceTests.cs | 1 - .../Controllers/v1.0/EdgeDevicesControllerTests.cs | 1 - .../Server/Helpers/ConfigHelperTest.cs | 2 -- .../Server/Services/DeviceServiceTests.cs | 9 +++------ .../Pages/EdgeModels/EdgeModule/ModuleDialog.razor | 1 - 10 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs index 7de7554d4..6a1532171 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab1Tests.cs @@ -22,7 +22,6 @@ public void ModuleDialogTab1ShouldBeRenderedProperly() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List() { @@ -78,7 +76,6 @@ public void ClickOnRemoveShouldDeleteRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List() { diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs index 9d3fc7ea3..9c86a66db 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab2Tests.cs @@ -22,7 +22,6 @@ public void ModuleDialogTab2ShouldBeRenderedProperly() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List() @@ -78,7 +76,6 @@ public void ClickOnRemoveShouldDeleteRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List() diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs index 67c8dc285..fbf35dadc 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTab3Tests.cs @@ -22,7 +22,6 @@ public void ModuleDialogTab3ShouldBeRenderedProperly() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), @@ -76,7 +74,6 @@ public void ClickOnRemoveShouldDeleteRow() var module = new IoTEdgeModule() { ModuleName = Guid.NewGuid().ToString(), - Version = Guid.NewGuid().ToString(), Status = "running", EnvironmentVariables = new List(), ModuleIdentityTwinSettings = new List(), diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs index 5148ac692..a4851d49c 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/Configurations/EdgeModule/ModuleDialogTests.cs @@ -29,7 +29,6 @@ public async Task ModuleDialogTestMustBeRenderedOnShow() var module = new IoTEdgeModule() { ModuleName = moduleName, - Version = "1.0", Status = "running", ImageURI = moduleImageUri, EnvironmentVariables = new List(), @@ -73,7 +72,6 @@ public async Task ClickOnSubmitShouldUpdateModuleValues() var module = new IoTEdgeModule() { ModuleName = moduleName, - Version = moduleVersion, Status = "running", ImageURI = moduleImageUri, EnvironmentVariables = new List(), diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs index d017864c4..3d76b38e4 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/ModuleLogsDialogTests.cs @@ -41,7 +41,6 @@ public async Task ModuleLogsDialogParametersMustBeCorrect() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -84,7 +83,6 @@ public async Task ModuleLogsShouldProcessProblemDetailsExceptionWhenIssueOccursO var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -120,7 +118,6 @@ public async Task ModuleLogsMustCloseOnCLickOnCloseButton() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index 02ef48348..da3601a1f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -180,7 +180,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoError() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index ce0780fc9..57f021c85 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -271,7 +271,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoErrorIsReturned() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs index 3b4a0dca4..b1db5fd4b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/ConfigHelperTest.cs @@ -115,7 +115,6 @@ public void CreateGatewayModuleShouldReturnValue() // Assert Assert.IsNotNull(result); Assert.AreEqual("running", result.Status); - Assert.AreEqual("1.0", result.Version); Assert.AreEqual("image_test", result.ImageURI); Assert.AreEqual(1, result.EnvironmentVariables.Count); } @@ -161,7 +160,6 @@ public void GenerateModulesContentShouldReturnValue() { ModuleName = "ModuleTest", Status = "running", - Version = "1.0", ImageURI = "image", EnvironmentVariables = new List() { diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs index 433833ecf..b47d499cd 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs @@ -1057,7 +1057,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhen200IsReturned() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -1065,7 +1064,7 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhen200IsReturned() var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new @@ -1117,7 +1116,6 @@ public async Task GetEdgeDeviceLogsMustReturnEmptyLogsWhenNot200IsReturned() var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -1125,7 +1123,7 @@ public async Task GetEdgeDeviceLogsMustReturnEmptyLogsWhenNot200IsReturned() var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new @@ -1177,7 +1175,6 @@ public async Task GetEdgeDeviceLogsShouldInternalServerErrorExceptionWhenIssueOc var edgeModule = new IoTEdgeModule { - Version = "1.0", ModuleName = Guid.NewGuid().ToString() }; @@ -1185,7 +1182,7 @@ public async Task GetEdgeDeviceLogsShouldInternalServerErrorExceptionWhenIssueOc var payload = JsonConvert.SerializeObject(new { - schemaVersion = edgeModule.Version, + schemaVersion = "1.0", items = new[] { new diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor index 6a3c23443..3c1675669 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeModels/EdgeModule/ModuleDialog.razor @@ -54,7 +54,6 @@ public IoTEdgeModule Module { get; set; } private string currentModuleName; - private string currentVersion; private string currentImageUri; private List currentEnvironmentVariables = new(); From 519c61f5cf4b88d1c3a943a0819be35bf00624ee Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 16:33:25 +0200 Subject: [PATCH 20/30] add deviceService test --- .../Server/Helpers/DeviceHelperTests.cs | 12 ++++- .../Server/Services/DeviceServiceTests.cs | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs index 84c9e21b0..9957c0934 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Helpers/DeviceHelperTests.cs @@ -308,12 +308,20 @@ public void RetrieveModuleListShouldReturnModuleList() LoRaWanNetworkSrvModule = new { version = "1.0", - status = "running" + status = "running", + settings = new + { + image = "image" + } }, LoRaBasicsStationModule = new { runtimeStatus = "running", - version = "1.0" + version = "1.0", + settings = new + { + image = "image" + } } } }); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs index b47d499cd..b9f984aea 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs @@ -1049,6 +1049,60 @@ public async Task ExecuteC2DMethodShouldThrowInternalServerErrorExceptionWhenIss this.mockRepository.VerifyAll(); } + [Test] + public async Task ExecuteCustomCommandC2DMethodShouldReturn200() + { + // Arrange + var service = CreateService(); + var deviceId = Guid.NewGuid().ToString(); + var moduleName = Guid.NewGuid().ToString(); + + var method = new CloudToDeviceMethod(Guid.NewGuid().ToString()); + + _ = this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync( + It.Is(x => x.Equals(deviceId, StringComparison.Ordinal)), + It.Is(x => x.Equals(moduleName, StringComparison.Ordinal)), + It.Is(x => x == method), + It.IsAny())) + .ReturnsAsync(new CloudToDeviceMethodResult + { + Status = 200 + }); + + // Act + var result = await service.ExecuteCustomCommandC2DMethod(deviceId, moduleName, method); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(200, result.Status); + this.mockRepository.VerifyAll(); + } + + [Test] + public async Task ExecuteCustomCommandC2DMethodShouldThrowInternalServerErrorExceptionWhenIssueOccurs() + { + // Arrange + var service = CreateService(); + var deviceId = Guid.NewGuid().ToString(); + var moduleName = Guid.NewGuid().ToString(); + + var method = new CloudToDeviceMethod(Guid.NewGuid().ToString()); + + _ = this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync( + It.Is(x => x.Equals(deviceId, StringComparison.Ordinal)), + It.Is(x => x.Equals(moduleName, StringComparison.Ordinal)), + It.Is(x => x == method), + It.IsAny())) + .ThrowsAsync(new Exception("")); + + // Act + var result = async () => await service.ExecuteCustomCommandC2DMethod(deviceId, moduleName, method); + + // Assert + _ = await result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + [Test] public async Task GetEdgeDeviceLogsMustReturnLogsWhen200IsReturned() { From d36e8cdac9df67e8aa5a84d35099bea10301f0d5 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 6 Sep 2022 17:07:19 +0200 Subject: [PATCH 21/30] add new test edgeDeviceService --- .../Server/Services/EdgeDeviceServiceTest.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index 0dc29822b..63cc21fe1 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -18,6 +18,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services using System.Threading.Tasks; using System.Collections.Generic; using AzureIoTHub.Portal.Server.Exceptions; + using FluentAssertions; [TestFixture] public class EdgeDeviceServiceTest @@ -310,6 +311,96 @@ public void WhenEdgeModuleIsNullExecuteMethodShouldThrowArgumentNullException(st _ = Assert.ThrowsAsync(() => edgeDeviceService.ExecuteModuleMethod(null, deviceId, methodName)); } + [TestCase("test")] + public async Task ExecuteModuleCommand(string commandName) + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var edgeModule = new IoTEdgeModule + { + ModuleName = "aaa", + }; + + var deviceId = Guid.NewGuid().ToString(); + + _ = this.mockDeviceService.Setup(c => c.ExecuteCustomCommandC2DMethod( + It.Is(x => x == deviceId), + It.Is(x => x == edgeModule.ModuleName), + It.Is(x => + x.MethodName == commandName + ))) + .ReturnsAsync(new CloudToDeviceMethodResult + { + Status = 200 + }); + + // Act + var result = await edgeDeviceService.ExecuteModuleCommand(deviceId, edgeModule.ModuleName, commandName); + + // Assert + Assert.AreEqual(200, result.Status); + this.mockRepository.VerifyAll(); + } + + [TestCase("test")] + public void WhenDeviceIdIsNullExecuteModuleCommandShouldThrowArgumentNullException(string commandName) + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var edgeModule = new IoTEdgeModule + { + ModuleName = "aaa", + }; + + var deviceId = string.Empty; + + // Act + var result = async () => await edgeDeviceService.ExecuteModuleCommand(deviceId, edgeModule.ModuleName, commandName); + + // Assert + _ = result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + + [TestCase("test")] + public void WhenModuleIsNullExecuteModuleCommandShouldThrowArgumentNullException(string commandName) + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var deviceId = Guid.NewGuid().ToString(); + + // Act + var result = async () => await edgeDeviceService.ExecuteModuleCommand(deviceId, null, commandName); + + // Assert + _ = result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + + [Test] + public void WhenCommandNameIsNullExecuteModuleCommandShouldThrowArgumentNullException() + { + // Arrange + var edgeDeviceService = CreateEdgeDeviceService(); + + var deviceId = string.Empty; + + var edgeModule = new IoTEdgeModule + { + ModuleName = "aaa", + }; + + // Act + var result = async () => await edgeDeviceService.ExecuteModuleCommand(deviceId, edgeModule.ModuleName, null); + + // Assert + _ = result.Should().ThrowAsync(); + this.mockRepository.VerifyAll(); + } + [Test] public async Task GetEdgeDeviceCredentialsShouldReturnEnrollmentCredentials() { From 65d3bf308c84f345e96e0212f94fa92cfcf3cae8 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 09:18:04 +0200 Subject: [PATCH 22/30] add unit test on edgeDeviceClientService --- .../Services/EdgeDeviceClientServiceTests.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index da3601a1f..1e8c9c80b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -207,5 +207,53 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoError() _ = result.Count.Should().Be(1); } + + [Test] + public async Task ExecuteModuleMethodShouldReturn200() + { + // Arrange + var deviceId = Fixture.Create(); + + var edgeModule = new IoTEdgeModule() + { + ModuleName = Guid.NewGuid().ToString() + }; + + var methodName = Fixture.Create(); + var c2Dresult = Fixture.Create(); + + _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}") + .RespondJson(c2Dresult); + + // Act + var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule, methodName); + + // Assert + _ = result.Should().BeEquivalentTo(c2Dresult); + MockHttpClient.VerifyNoOutstandingRequest(); + MockHttpClient.VerifyNoOutstandingExpectation(); + } + + [Test] + public async Task ExecuteModuleCommandShouldReturn200() + { + // Arrange + var deviceId = Fixture.Create(); + var commandName = Fixture.Create(); + var moduleName = Fixture.Create(); + + var c2Dresult = Fixture.Create(); + + _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") + .RespondJson(c2Dresult); + + // Act + var result = await this.edgeDeviceClientService.ExecuteModuleCommand(deviceId, moduleName, commandName); + + // Assert + _ = result.Should().BeEquivalentTo(c2Dresult); + MockHttpClient.VerifyNoOutstandingRequest(); + MockHttpClient.VerifyNoOutstandingExpectation(); + } } } From 0da5318fd84aab58998fa0a6e8cd261d3cbd0984 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 09:52:47 +0200 Subject: [PATCH 23/30] add unit test on edgeDeviceController --- .../v1.0/EdgeDevicesControllerTests.cs | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index 57f021c85..a454f7f80 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.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.Tests.Unit.Server.Controllers.v1._0 +namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v10 { using System; using System.Collections.Generic; @@ -399,5 +399,30 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName) this.mockRepository.VerifyAll(); } + + [TestCase("test")] + public async Task ExecuteCustomModuleMethodShouldExecuteC2DMethod(string commandName) + { + // Arrange + var edgeDeviceController = CreateEdgeDevicesController(); + + var deviceId = Guid.NewGuid().ToString(); + var moduleName = Guid.NewGuid().ToString(); + + _ = this.mockEdgeDeviceService + .Setup(x => x.ExecuteModuleCommand( + It.Is(c => c.Equals(deviceId, StringComparison.Ordinal)), + It.Is(c => c.Equals(moduleName, StringComparison.Ordinal)), + It.Is(c => c.Equals(commandName, StringComparison.Ordinal)))) + .ReturnsAsync(new C2Dresult()); + + // Act + var result = await edgeDeviceController.ExecuteCustomModuleMethod(deviceId, moduleName, commandName); + + // Assert + Assert.IsNotNull(result); + + this.mockRepository.VerifyAll(); + } } } From 14004ba2615290d024152ee088207ecdf24a9971 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 10:43:38 +0200 Subject: [PATCH 24/30] add unit test on edgeDeviceDetailPage --- .../EdgeDevices/EdgeDeviceDetailPageTests.cs | 101 +++++++++++++++++- .../EdgeDevices/EdgeDeviceDetailPage.razor | 2 +- 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs index 424982cbb..3b8909951 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs @@ -262,7 +262,7 @@ public void ClickOnRebootShouldDisplaySnackbarIfError() Status = 500 }); - _ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny(), Severity.Error, It.IsAny>())).Returns((Snackbar)null); + _ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny(), Severity.Error, It.IsAny>())).Returns(value: null); var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); @@ -357,6 +357,7 @@ public void ClickOnLogsShouldDisplayLogs() [Test] public void ClickOnConnectShouldDisplayDeviceCredentials() { + // Arrange _ = SetupOnInitialisation(); var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); @@ -376,6 +377,80 @@ public void ClickOnConnectShouldDisplayDeviceCredentials() cut.WaitForAssertion(() => MockRepository.VerifyAll()); } + [Test] + public void ClickOnCommandModuleShouldReturn200() + { + // Arrange + _ = SetupOnInitialisation(); + + var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); + cut.WaitForAssertion(() => cut.Find("#commandTest")); + + var commandButton = cut.Find("#commandTest"); + + _ = this.mockEdgeDeviceClientService + .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .ReturnsAsync(new C2Dresult() { Status = 200 }); + + _ = this.mockSnackbarService + .Setup(c => c.Add(It.IsAny(), Severity.Success, It.IsAny>())) + .Returns(value: null); + + // Act + commandButton.Click(); + + // Assert + cut.WaitForAssertion(() => MockRepository.VerifyAll()); + } + + [Test] + public void ClickOnCommandModuleShouldReturn400() + { + // Arrange + _ = SetupOnInitialisation(); + + var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); + cut.WaitForAssertion(() => cut.Find("#commandTest")); + + var commandButton = cut.Find("#commandTest"); + + _ = this.mockEdgeDeviceClientService + .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .ReturnsAsync(new C2Dresult() { Status = 400 }); + + _ = this.mockSnackbarService + .Setup(c => c.Add(It.IsAny(), Severity.Error, It.IsAny>())) + .Returns(value: null); + + // Act + commandButton.Click(); + + // Assert + cut.WaitForAssertion(() => MockRepository.VerifyAll()); + } + + [Test] + public void ClickOnCommandModuleShouldProcessProblemDetailsExceptionWhenIssueOccurs() + { + // Arrange + _ = SetupOnInitialisation(); + + var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); + cut.WaitForAssertion(() => cut.Find("#commandTest")); + + var commandButton = cut.Find("#commandTest"); + + _ = this.mockEdgeDeviceClientService + .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails())); + + // Act + commandButton.Click(); + + // Assert + cut.WaitForAssertion(() => MockRepository.VerifyAll()); + } + [Test] public void ClickOnDeleteShouldDisplayConfirmationDialogAndReturnIfAborted() { @@ -434,7 +509,15 @@ private IoTEdgeDevice SetupOnInitialisation() DeviceId = this.mockdeviceId, ConnectionState = "Connected", ModelId = Guid.NewGuid().ToString(), - Tags = tags + Tags = tags, + Modules = new List() + { + new IoTEdgeModule() + { + ModuleName = "moduleTest", + ImageURI = Guid.NewGuid().ToString() + } + } }; _ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevice(this.mockdeviceId)) @@ -444,7 +527,19 @@ private IoTEdgeDevice SetupOnInitialisation() .Setup(service => service.GetIoTEdgeModel(It.Is(x => x.Equals(mockIoTEdgeDevice.ModelId, StringComparison.Ordinal)))) .ReturnsAsync(new IoTEdgeModel() { - ModelId = mockIoTEdgeDevice.ModelId + ModelId = mockIoTEdgeDevice.ModelId, + EdgeModules = new List() + { + new IoTEdgeModule() + { + ModuleName = "moduleTest", + ImageURI = Guid.NewGuid().ToString(), + Commands = new List() + { + new Portal.Shared.Models.v10.IoTEdgeModuleCommand(){ Name = "commandTest"} + } + } + } }); _ = this.mockDeviceTagSettingsClientService diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 8a23f603a..2893535dd 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -149,7 +149,7 @@ reboot @foreach (var command in context.Commands) { - @command.Name + @command.Name } From aa5fa255f97b611540a9d5db623ed541e4c74aed Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 11:16:49 +0200 Subject: [PATCH 25/30] add device image on edge device list --- .../Client/Pages/EdgeDevices/EdgeDeviceListPage.razor | 7 ++++++- src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs | 3 ++- .../Shared/Models/v1.0/IoTEdgeListItem.cs | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor index ba618a630..dc3f2ce9a 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceListPage.razor @@ -48,6 +48,7 @@ + @@ -65,13 +66,17 @@ - ID + + Device ID Allowed Nb devices See details Delete + + + @context.DeviceId diff --git a/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs b/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs index 4e04324e5..33d33fad3 100644 --- a/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs +++ b/src/AzureIoTHub.Portal/Server/Mappers/EdgeDeviceMapper.cs @@ -24,7 +24,8 @@ public IoTEdgeListItem CreateEdgeDeviceListItem(Twin deviceTwin) { DeviceId = deviceTwin?.DeviceId, Status = deviceTwin?.Status.ToString(), - NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin) + NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin), + ImageUrl = this.deviceModelImageManager.ComputeImageUri(DeviceHelper.RetrieveTagValue(deviceTwin, nameof(IoTEdgeDevice.ModelId))) }; } diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs index b3fa97b46..e8e5561a8 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/IoTEdgeListItem.cs @@ -3,6 +3,7 @@ namespace AzureIoTHub.Portal.Models.v10 { + using System; using System.ComponentModel.DataAnnotations; /// @@ -25,5 +26,10 @@ public class IoTEdgeListItem /// The number of devices connected on the IoT Edge. /// public int NbDevices { get; set; } + + /// + /// The device model image Url. + /// + public Uri ImageUrl { get; set; } } } From bf2d63e2076537a44593a130919e9d3f83c6b354 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 11:35:10 +0200 Subject: [PATCH 26/30] fix unit test --- .../Server/Mappers/EdgeDeviceMapperTest.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs index 75826fb03..5dd46cf7f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Mappers/EdgeDeviceMapperTest.cs @@ -40,7 +40,14 @@ public void CreateEdgeDeviceListItemShouldReturnValue() var edgeDeviceMapper = CreateMapper(); var deviceTwin = new Twin(Guid.NewGuid().ToString()); + var modelId = Guid.NewGuid().ToString(); + deviceTwin.Properties.Reported["clients"] = new object[12]; + deviceTwin.Tags["modelId"] = modelId; + + _ = this.mockDeviceModelImageManager + .Setup(x => x.ComputeImageUri(It.Is(c => c.Equals(modelId, StringComparison.Ordinal)))) + .Returns(new Uri($"http://fake.local/{modelId}")); // Act var result = edgeDeviceMapper.CreateEdgeDeviceListItem(deviceTwin); From 829d5689fd47dec927c69eba85feac9bd1ec1dc3 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 13:43:06 +0200 Subject: [PATCH 27/30] resolve request change --- .../Client/Services/EdgeDeviceClientServiceTests.cs | 4 ++-- .../Client/Services/EdgeDeviceClientService.cs | 4 ++-- .../Server/Controllers/v1.0/EdgeDevicesController.cs | 4 ++-- src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index 1e8c9c80b..ff839f30c 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -222,7 +222,7 @@ public async Task ExecuteModuleMethodShouldReturn200() var methodName = Fixture.Create(); var c2Dresult = Fixture.Create(); - _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}") + _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}") .RespondJson(c2Dresult); // Act @@ -244,7 +244,7 @@ public async Task ExecuteModuleCommandShouldReturn200() var c2Dresult = Fixture.Create(); - _ = MockHttpClient.When(HttpMethod.Get, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") + _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") .RespondJson(c2Dresult); // Act diff --git a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs index 22d237c7c..b3d3cc534 100644 --- a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs @@ -57,14 +57,14 @@ public async Task> GetEdgeDeviceLogs(string deviceId, IoT public async Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName) { - var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}"); + var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}", null); return await response.Content.ReadFromJsonAsync(); } public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) { - var response = await this.http.GetAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}"); + var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}", null); return await response.Content.ReadFromJsonAsync(); } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index c7e418f88..1b0ff9b31 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -160,13 +160,13 @@ public async Task> GetCredentials(string dev /// The edge module name. /// The device identifier. /// Name of the method. - [HttpGet("{deviceId}/{moduleName}/{methodName}", Name = "Get Execute module command")] + [HttpPost("{deviceId}/{moduleName}/{methodName}", Name = "POST Execute module command")] public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) { return await this.edgeDevicesService.ExecuteModuleMethod(moduleName, deviceId, methodName); } - [HttpGet("{deviceId}/{moduleName}/custom/{commandName}", Name = "GET Execute custom module command")] + [HttpPost("{deviceId}/{moduleName}/custom/{commandName}", Name = "POST Execute custom module command")] public async Task ExecuteCustomModuleMethod(string deviceId, string moduleName, string commandName) { return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, commandName); diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index 04bd0ddc2..a5228a6e1 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -179,7 +179,7 @@ public async Task ExecuteModuleMethod(string moduleName, string devic ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); var method = new CloudToDeviceMethod(methodName); - var payload = "{}"; + var payload = string.Empty; if (methodName == "RestartModule") { From be8cdee4c0008acf264f153d2a0f995aad4a9d85 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 15:49:53 +0200 Subject: [PATCH 28/30] delete useless endpoint --- .../EdgeDevices/EdgeDeviceDetailPageTests.cs | 12 +++--- .../Services/EdgeDeviceClientServiceTests.cs | 6 +-- .../v1.0/EdgeDevicesControllerTests.cs | 2 +- .../EdgeDevices/EdgeDeviceDetailPage.razor | 39 ++----------------- .../Services/EdgeDeviceClientService.cs | 11 +----- .../Services/IEdgeDeviceClientService.cs | 3 +- .../Controllers/v1.0/EdgeDevicesController.cs | 14 +++---- .../Server/Services/EdgeDevicesService.cs | 14 +++---- .../Server/Services/IEdgeDevicesService.cs | 2 +- 9 files changed, 29 insertions(+), 74 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs index 3b8909951..056049c3d 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Pages/EdgeDevices/EdgeDeviceDetailPageTests.cs @@ -210,7 +210,7 @@ public void ClickOnRebootShouldRebootModule() _ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List()); - _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule")) + _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule")) .ReturnsAsync(new C2Dresult() { Payload = "ABC", @@ -255,7 +255,7 @@ public void ClickOnRebootShouldDisplaySnackbarIfError() _ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List()); - _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule")) + _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule")) .ReturnsAsync(new C2Dresult() { Payload = "ABC", @@ -301,7 +301,7 @@ public void EdgeDeviceDetailPageShouldProcessProblemDetailsExceptionWhenIssueOcc _ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List()); - _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule")) + _ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule")) .ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails())); var cut = RenderComponent(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId)); @@ -389,7 +389,7 @@ public void ClickOnCommandModuleShouldReturn200() var commandButton = cut.Find("#commandTest"); _ = this.mockEdgeDeviceClientService - .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .Setup(x => x.ExecuteModuleMethod(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) .ReturnsAsync(new C2Dresult() { Status = 200 }); _ = this.mockSnackbarService @@ -415,7 +415,7 @@ public void ClickOnCommandModuleShouldReturn400() var commandButton = cut.Find("#commandTest"); _ = this.mockEdgeDeviceClientService - .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .Setup(x => x.ExecuteModuleMethod(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) .ReturnsAsync(new C2Dresult() { Status = 400 }); _ = this.mockSnackbarService @@ -441,7 +441,7 @@ public void ClickOnCommandModuleShouldProcessProblemDetailsExceptionWhenIssueOcc var commandButton = cut.Find("#commandTest"); _ = this.mockEdgeDeviceClientService - .Setup(x => x.ExecuteModuleCommand(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) + .Setup(x => x.ExecuteModuleMethod(It.Is(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny(), It.IsAny())) .ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails())); // Act diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs index ff839f30c..f4182cb2b 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Client/Services/EdgeDeviceClientServiceTests.cs @@ -226,7 +226,7 @@ public async Task ExecuteModuleMethodShouldReturn200() .RespondJson(c2Dresult); // Act - var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule, methodName); + var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule.ModuleName, methodName); // Assert _ = result.Should().BeEquivalentTo(c2Dresult); @@ -244,11 +244,11 @@ public async Task ExecuteModuleCommandShouldReturn200() var c2Dresult = Fixture.Create(); - _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}") + _ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/{commandName}") .RespondJson(c2Dresult); // Act - var result = await this.edgeDeviceClientService.ExecuteModuleCommand(deviceId, moduleName, commandName); + var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, moduleName, commandName); // Assert _ = result.Should().BeEquivalentTo(c2Dresult); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index a454f7f80..cc85cdc1f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -417,7 +417,7 @@ public async Task ExecuteCustomModuleMethodShouldExecuteC2DMethod(string command .ReturnsAsync(new C2Dresult()); // Act - var result = await edgeDeviceController.ExecuteCustomModuleMethod(deviceId, moduleName, commandName); + var result = await edgeDeviceController.ExecuteModuleMethod(deviceId, moduleName, commandName); // Assert Assert.IsNotNull(result); diff --git a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor index 2893535dd..33d99cc87 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/EdgeDevices/EdgeDeviceDetailPage.razor @@ -146,10 +146,10 @@ @context.Status logs - reboot + reboot @foreach (var command in context.Commands) { - @command.Name + @command.Name } @@ -297,44 +297,13 @@ } } - public async Task OnMethod(IoTEdgeModule module, string methodName) + public async Task OnMethod(string moduleName, string methodName) { isProcessing = true; try { - var c2dResult = await EdgeDeviceClientService.ExecuteModuleMethod(edgeDevice.DeviceId, module, methodName); - - if (c2dResult.Status == 200) - { - Snackbar.Add("Command successfully executed.", Severity.Success); - } - else - { - Snackbar.Add($"Error
Status : {c2dResult.Status};
Payload : {c2dResult.Payload};", Severity.Error, - (option) => - { - option.VisibleStateDuration = 10000; - }); - } - } - catch (ProblemDetailsException exception) - { - Error?.ProcessProblemDetails(exception); - } - finally - { - isProcessing = false; - } - } - - public async Task ExecuteCommand(string moduleName, IoTEdgeModuleCommand command) - { - isProcessing = true; - - try - { - var c2dResult = await EdgeDeviceClientService.ExecuteModuleCommand(edgeDevice.DeviceId, moduleName, command.Name); + var c2dResult = await EdgeDeviceClientService.ExecuteModuleMethod(edgeDevice.DeviceId, moduleName, methodName); if (c2dResult.Status == 200) { diff --git a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs index b3d3cc534..8ff7a3349 100644 --- a/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/EdgeDeviceClientService.cs @@ -55,16 +55,9 @@ public async Task> GetEdgeDeviceLogs(string deviceId, IoT return await response.Content.ReadFromJsonAsync>(); } - public async Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName) + public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { - var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}", null); - - return await response.Content.ReadFromJsonAsync(); - } - - public async Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName) - { - var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{moduleName}/custom/{commandName}", null); + var response = await this.http.PostAsJsonAsync($"api/edge/devices/{deviceId}/{moduleName}/{methodName}", null); return await response.Content.ReadFromJsonAsync(); } diff --git a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs index a096d94cf..4fb17e009 100644 --- a/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal/Client/Services/IEdgeDeviceClientService.cs @@ -23,8 +23,7 @@ public interface IEdgeDeviceClientService Task> GetEdgeDeviceLogs(string deviceId, IoTEdgeModule edgeModule); - Task ExecuteModuleMethod(string deviceId, IoTEdgeModule edgeModule, string methodName); + Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName); - Task ExecuteModuleCommand(string deviceId, string moduleName, string commandName); } } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index 1b0ff9b31..220b100a4 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -161,15 +161,13 @@ public async Task> GetCredentials(string dev /// The device identifier. /// Name of the method. [HttpPost("{deviceId}/{moduleName}/{methodName}", Name = "POST Execute module command")] - public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) + public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { - return await this.edgeDevicesService.ExecuteModuleMethod(moduleName, deviceId, methodName); - } - - [HttpPost("{deviceId}/{moduleName}/custom/{commandName}", Name = "POST Execute custom module command")] - public async Task ExecuteCustomModuleMethod(string deviceId, string moduleName, string commandName) - { - return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, commandName); + if (methodName.Equals("RestartModule", StringComparison.Ordinal)) + { + return await this.edgeDevicesService.ExecuteModuleMethod(deviceId, moduleName, methodName); + } + return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, methodName); } /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index a5228a6e1..0fc435c9a 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -174,21 +174,17 @@ public async Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice) /// /// /// - public async Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName) + public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); var method = new CloudToDeviceMethod(methodName); - var payload = string.Empty; - if (methodName == "RestartModule") + var payload = JsonConvert.SerializeObject(new { - payload = JsonConvert.SerializeObject(new - { - id = moduleName, - schemaVersion = "1.0" - }); - } + id = moduleName, + schemaVersion = "1.0" + }); _ = method.SetPayloadJson(payload); diff --git a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs index a7af647bf..1e8407bdd 100644 --- a/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/IEdgeDevicesService.cs @@ -24,7 +24,7 @@ PaginationResult GetEdgeDevicesPage(PaginationResult edge Task UpdateEdgeDevice(IoTEdgeDevice edgeDevice); - Task ExecuteModuleMethod(string moduleName, string deviceId, string methodName); + Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName); Task GetEdgeDeviceCredentials(string edgeDeviceId); From ffaaebe5238ec1a0032f36fa5639c5363965d18c Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 16:00:07 +0200 Subject: [PATCH 29/30] fix unit test --- .../Server/Services/EdgeDeviceServiceTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs index 63cc21fe1..f08f2de9f 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/EdgeDeviceServiceTest.cs @@ -293,7 +293,7 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string }); // Act - _ = await edgeDeviceService.ExecuteModuleMethod(edgeModule.ModuleName, deviceId, methodName); + _ = await edgeDeviceService.ExecuteModuleMethod(deviceId, edgeModule.ModuleName, methodName); // Assert this.mockRepository.VerifyAll(); @@ -308,7 +308,7 @@ public void WhenEdgeModuleIsNullExecuteMethodShouldThrowArgumentNullException(st var deviceId = Guid.NewGuid().ToString(); // Assert - _ = Assert.ThrowsAsync(() => edgeDeviceService.ExecuteModuleMethod(null, deviceId, methodName)); + _ = Assert.ThrowsAsync(() => edgeDeviceService.ExecuteModuleMethod(deviceId, null, methodName)); } [TestCase("test")] From 63c5640a5fdac2fa0d032f1d2954b10ad758e9f9 Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 7 Sep 2022 16:14:06 +0200 Subject: [PATCH 30/30] fix unit test --- .../Server/Controllers/v1.0/EdgeDevicesControllerTests.cs | 2 +- .../Server/Controllers/v1.0/EdgeDevicesController.cs | 6 +----- .../Server/Services/EdgeDevicesService.cs | 5 +++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index cc85cdc1f..09909638c 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -410,7 +410,7 @@ public async Task ExecuteCustomModuleMethodShouldExecuteC2DMethod(string command var moduleName = Guid.NewGuid().ToString(); _ = this.mockEdgeDeviceService - .Setup(x => x.ExecuteModuleCommand( + .Setup(x => x.ExecuteModuleMethod( It.Is(c => c.Equals(deviceId, StringComparison.Ordinal)), It.Is(c => c.Equals(moduleName, StringComparison.Ordinal)), It.Is(c => c.Equals(commandName, StringComparison.Ordinal)))) diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs index 220b100a4..682b4f486 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/EdgeDevicesController.cs @@ -163,11 +163,7 @@ public async Task> GetCredentials(string dev [HttpPost("{deviceId}/{moduleName}/{methodName}", Name = "POST Execute module command")] public async Task ExecuteModuleMethod(string deviceId, string moduleName, string methodName) { - if (methodName.Equals("RestartModule", StringComparison.Ordinal)) - { - return await this.edgeDevicesService.ExecuteModuleMethod(deviceId, moduleName, methodName); - } - return await this.edgeDevicesService.ExecuteModuleCommand(deviceId, moduleName, methodName); + return await this.edgeDevicesService.ExecuteModuleMethod(deviceId, moduleName, methodName); } /// diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs index 0fc435c9a..03754a792 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeDevicesService.cs @@ -178,6 +178,11 @@ public async Task ExecuteModuleMethod(string deviceId, string moduleN { ArgumentNullException.ThrowIfNull(moduleName, nameof(moduleName)); + if (!methodName.Equals("RestartModule", StringComparison.Ordinal)) + { + return await ExecuteModuleCommand(deviceId, moduleName, methodName); + } + var method = new CloudToDeviceMethod(methodName); var payload = JsonConvert.SerializeObject(new