Skip to content

Commit

Permalink
Feature/#1117_-_Execute commands defined in edge model (#1164)
Browse files Browse the repository at this point in the history
* resolve issue #1117

* fix unit test

* delete module version

* fix unit test

* add deviceService test

* add new test edgeDeviceService

* add unit test on edgeDeviceClientService

* add unit test on edgeDeviceController

* add unit test on edgeDeviceDetailPage

* add device image on edge device list

* fix unit test

* resolve request change

* delete useless endpoint

* fix unit test

* fix unit test

* resolve issue #1117

* fix unit test

* delete module version

* fix unit test

* add deviceService test

* add new test edgeDeviceService

* add unit test on edgeDeviceClientService

* add unit test on edgeDeviceController

* add unit test on edgeDeviceDetailPage

* add device image on edge device list

* fix unit test

* resolve request change

* delete useless endpoint

* fix unit test

* fix unit test

Co-authored-by: Kevin BEAUGRAND <9513635+kbeaugrand@users.noreply.github.com>
  • Loading branch information
Sben65 and kbeaugrand authored Sep 7, 2022
1 parent 9774208 commit fa9aa17
Show file tree
Hide file tree
Showing 29 changed files with 461 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void ModuleDialogTab1ShouldBeRenderedProperly()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(),
Expand All @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>()
{
Expand Down Expand Up @@ -78,7 +76,6 @@ public void ClickOnRemoveShouldDeleteRow()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void ModuleDialogTab2ShouldBeRenderedProperly()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(),
Expand All @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>()
Expand Down Expand Up @@ -78,7 +76,6 @@ public void ClickOnRemoveShouldDeleteRow()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void ModuleDialogTab3ShouldBeRenderedProperly()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(),
Expand All @@ -43,7 +42,6 @@ public void ClickOnAddShouldAddRow()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(),
Expand Down Expand Up @@ -76,7 +74,6 @@ public void ClickOnRemoveShouldDeleteRow()
var module = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString(),
Version = Guid.NewGuid().ToString(),
Status = "running",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
ModuleIdentityTwinSettings = new List<IoTEdgeModuleTwinSetting>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public async Task ModuleDialogTestMustBeRenderedOnShow()
var module = new IoTEdgeModule()
{
ModuleName = moduleName,
Version = "1.0",
Status = "running",
ImageURI = moduleImageUri,
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
Expand Down Expand Up @@ -73,7 +72,6 @@ public async Task ClickOnSubmitShouldUpdateModuleValues()
var module = new IoTEdgeModule()
{
ModuleName = moduleName,
Version = moduleVersion,
Status = "running",
ImageURI = moduleImageUri,
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void ClickOnRebootShouldRebootModule()

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List<DeviceTag>());

_ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is<IoTEdgeModule>(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule"))
_ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is<string>(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule"))
.ReturnsAsync(new C2Dresult()
{
Payload = "ABC",
Expand Down Expand Up @@ -255,14 +255,14 @@ public void ClickOnRebootShouldDisplaySnackbarIfError()

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List<DeviceTag>());

_ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is<IoTEdgeModule>(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule"))
_ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is<string>(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule"))
.ReturnsAsync(new C2Dresult()
{
Payload = "ABC",
Status = 500
});

_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Error, It.IsAny<Action<SnackbarOptions>>())).Returns((Snackbar)null);
_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Error, It.IsAny<Action<SnackbarOptions>>())).Returns(value: null);

var cut = RenderComponent<EdgeDeviceDetailPage>(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId));

Expand Down Expand Up @@ -301,7 +301,7 @@ public void EdgeDeviceDetailPageShouldProcessProblemDetailsExceptionWhenIssueOcc

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags()).ReturnsAsync(new List<DeviceTag>());

_ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is<IoTEdgeModule>(module => mockIoTEdgeModule.ModuleName.Equals(module.ModuleName, StringComparison.Ordinal)), "RestartModule"))
_ = this.mockEdgeDeviceClientService.Setup(service => service.ExecuteModuleMethod(this.mockdeviceId, It.Is<string>(module => mockIoTEdgeModule.ModuleName.Equals(module, StringComparison.Ordinal)), "RestartModule"))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

var cut = RenderComponent<EdgeDeviceDetailPage>(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId));
Expand Down Expand Up @@ -357,6 +357,7 @@ public void ClickOnLogsShouldDisplayLogs()
[Test]
public void ClickOnConnectShouldDisplayDeviceCredentials()
{
// Arrange
_ = SetupOnInitialisation();

var cut = RenderComponent<EdgeDeviceDetailPage>(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId));
Expand All @@ -376,6 +377,80 @@ public void ClickOnConnectShouldDisplayDeviceCredentials()
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickOnCommandModuleShouldReturn200()
{
// Arrange
_ = SetupOnInitialisation();

var cut = RenderComponent<EdgeDeviceDetailPage>(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId));
cut.WaitForAssertion(() => cut.Find("#commandTest"));

var commandButton = cut.Find("#commandTest");

_ = this.mockEdgeDeviceClientService
.Setup(x => x.ExecuteModuleMethod(It.Is<string>(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new C2Dresult() { Status = 200 });

_ = this.mockSnackbarService
.Setup(c => c.Add(It.IsAny<string>(), Severity.Success, It.IsAny<Action<SnackbarOptions>>()))
.Returns(value: null);

// Act
commandButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickOnCommandModuleShouldReturn400()
{
// Arrange
_ = SetupOnInitialisation();

var cut = RenderComponent<EdgeDeviceDetailPage>(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId));
cut.WaitForAssertion(() => cut.Find("#commandTest"));

var commandButton = cut.Find("#commandTest");

_ = this.mockEdgeDeviceClientService
.Setup(x => x.ExecuteModuleMethod(It.Is<string>(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new C2Dresult() { Status = 400 });

_ = this.mockSnackbarService
.Setup(c => c.Add(It.IsAny<string>(), Severity.Error, It.IsAny<Action<SnackbarOptions>>()))
.Returns(value: null);

// Act
commandButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickOnCommandModuleShouldProcessProblemDetailsExceptionWhenIssueOccurs()
{
// Arrange
_ = SetupOnInitialisation();

var cut = RenderComponent<EdgeDeviceDetailPage>(ComponentParameter.CreateParameter("deviceId", this.mockdeviceId));
cut.WaitForAssertion(() => cut.Find("#commandTest"));

var commandButton = cut.Find("#commandTest");

_ = this.mockEdgeDeviceClientService
.Setup(x => x.ExecuteModuleMethod(It.Is<string>(c => c.Equals(this.mockdeviceId, StringComparison.Ordinal)), It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

// Act
commandButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickOnDeleteShouldDisplayConfirmationDialogAndReturnIfAborted()
{
Expand Down Expand Up @@ -434,7 +509,15 @@ private IoTEdgeDevice SetupOnInitialisation()
DeviceId = this.mockdeviceId,
ConnectionState = "Connected",
ModelId = Guid.NewGuid().ToString(),
Tags = tags
Tags = tags,
Modules = new List<IoTEdgeModule>()
{
new IoTEdgeModule()
{
ModuleName = "moduleTest",
ImageURI = Guid.NewGuid().ToString()
}
}
};

_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevice(this.mockdeviceId))
Expand All @@ -444,7 +527,19 @@ private IoTEdgeDevice SetupOnInitialisation()
.Setup(service => service.GetIoTEdgeModel(It.Is<string>(x => x.Equals(mockIoTEdgeDevice.ModelId, StringComparison.Ordinal))))
.ReturnsAsync(new IoTEdgeModel()
{
ModelId = mockIoTEdgeDevice.ModelId
ModelId = mockIoTEdgeDevice.ModelId,
EdgeModules = new List<IoTEdgeModule>()
{
new IoTEdgeModule()
{
ModuleName = "moduleTest",
ImageURI = Guid.NewGuid().ToString(),
Commands = new List<Portal.Shared.Models.v10.IoTEdgeModuleCommand>()
{
new Portal.Shared.Models.v10.IoTEdgeModuleCommand(){ Name = "commandTest"}
}
}
}
});

_ = this.mockDeviceTagSettingsClientService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public async Task ModuleLogsDialogParametersMustBeCorrect()

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

Expand Down Expand Up @@ -84,7 +83,6 @@ public async Task ModuleLogsShouldProcessProblemDetailsExceptionWhenIssueOccursO

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

Expand Down Expand Up @@ -120,7 +118,6 @@ public async Task ModuleLogsMustCloseOnCLickOnCloseButton()

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoError()

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

Expand Down Expand Up @@ -208,5 +207,53 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoError()

_ = result.Count.Should().Be(1);
}

[Test]
public async Task ExecuteModuleMethodShouldReturn200()
{
// Arrange
var deviceId = Fixture.Create<string>();

var edgeModule = new IoTEdgeModule()
{
ModuleName = Guid.NewGuid().ToString()
};

var methodName = Fixture.Create<string>();
var c2Dresult = Fixture.Create<C2Dresult>();

_ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{edgeModule.ModuleName}/{methodName}")
.RespondJson(c2Dresult);

// Act
var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, edgeModule.ModuleName, methodName);

// Assert
_ = result.Should().BeEquivalentTo(c2Dresult);
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}

[Test]
public async Task ExecuteModuleCommandShouldReturn200()
{
// Arrange
var deviceId = Fixture.Create<string>();
var commandName = Fixture.Create<string>();
var moduleName = Fixture.Create<string>();

var c2Dresult = Fixture.Create<C2Dresult>();

_ = MockHttpClient.When(HttpMethod.Post, $"/api/edge/devices/{deviceId}/{moduleName}/{commandName}")
.RespondJson(c2Dresult);

// Act
var result = await this.edgeDeviceClientService.ExecuteModuleMethod(deviceId, moduleName, commandName);

// Assert
_ = result.Should().BeEquivalentTo(c2Dresult);
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -271,7 +271,6 @@ public async Task GetEdgeDeviceLogsMustReturnLogsWhenNoErrorIsReturned()

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

Expand Down Expand Up @@ -387,13 +386,38 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName)
var deviceId = Guid.NewGuid().ToString();

_ = this.mockEdgeDeviceService
.Setup(x => x.ExecuteModuleMethod(It.IsAny<IoTEdgeModule>(),
.Setup(x => x.ExecuteModuleMethod(It.IsAny<string>(),
It.Is<string>(c => c.Equals(deviceId, StringComparison.Ordinal)),
It.Is<string>(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);

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.ExecuteModuleMethod(
It.Is<string>(c => c.Equals(deviceId, StringComparison.Ordinal)),
It.Is<string>(c => c.Equals(moduleName, StringComparison.Ordinal)),
It.Is<string>(c => c.Equals(commandName, StringComparison.Ordinal))))
.ReturnsAsync(new C2Dresult());

// Act
var result = await edgeDeviceController.ExecuteModuleMethod(deviceId, moduleName, commandName);

// Assert
Assert.IsNotNull(result);
Expand Down
Loading

0 comments on commit fa9aa17

Please sign in to comment.