Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#1117_-_Execute commands defined in edge model #1164

Merged
merged 32 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cf4448c
resolve issue #1117
Sben65 Sep 6, 2022
f1ec8df
fix unit test
Sben65 Sep 6, 2022
178acc0
delete module version
Sben65 Sep 6, 2022
e624cfc
fix unit test
Sben65 Sep 6, 2022
bee04c8
add deviceService test
Sben65 Sep 6, 2022
230c88e
add new test edgeDeviceService
Sben65 Sep 6, 2022
ea39e8d
add unit test on edgeDeviceClientService
Sben65 Sep 7, 2022
54d5c35
add unit test on edgeDeviceController
Sben65 Sep 7, 2022
ef02b02
add unit test on edgeDeviceDetailPage
Sben65 Sep 7, 2022
59b269c
add device image on edge device list
Sben65 Sep 7, 2022
76a826b
fix unit test
Sben65 Sep 7, 2022
28370d7
resolve request change
Sben65 Sep 7, 2022
9f6d5fb
delete useless endpoint
Sben65 Sep 7, 2022
d2846b8
fix unit test
Sben65 Sep 7, 2022
31e1e67
fix unit test
Sben65 Sep 7, 2022
a603d5e
resolve issue #1117
Sben65 Sep 6, 2022
edcc0e6
fix unit test
Sben65 Sep 6, 2022
0335bf3
delete module version
Sben65 Sep 6, 2022
2352b58
fix unit test
Sben65 Sep 6, 2022
519c61f
add deviceService test
Sben65 Sep 6, 2022
d36e8cd
add new test edgeDeviceService
Sben65 Sep 6, 2022
65d3bf3
add unit test on edgeDeviceClientService
Sben65 Sep 7, 2022
0da5318
add unit test on edgeDeviceController
Sben65 Sep 7, 2022
14004ba
add unit test on edgeDeviceDetailPage
Sben65 Sep 7, 2022
aa5fa25
add device image on edge device list
Sben65 Sep 7, 2022
bf2d63e
fix unit test
Sben65 Sep 7, 2022
829d568
resolve request change
Sben65 Sep 7, 2022
be8cdee
delete useless endpoint
Sben65 Sep 7, 2022
ffaaebe
fix unit test
Sben65 Sep 7, 2022
63c5640
fix unit test
Sben65 Sep 7, 2022
6b2bd2b
Merge branch 'Feature/1117_Execute_commands_defined_in_edge_model' of…
Sben65 Sep 7, 2022
a730c10
Merge branch 'main' into Feature/1117_Execute_commands_defined_in_edg…
kbeaugrand Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -262,7 +262,7 @@ public void ClickOnRebootShouldDisplaySnackbarIfError()
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 @@ -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.ExecuteModuleCommand(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.ExecuteModuleCommand(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.ExecuteModuleCommand(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, 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}/custom/{commandName}")
.RespondJson(c2Dresult);

// Act
var result = await this.edgeDeviceClientService.ExecuteModuleCommand(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.ExecuteModuleCommand(
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.ExecuteCustomModuleMethod(deviceId, moduleName, commandName);

// Assert
Assert.IsNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -161,7 +160,6 @@ public void GenerateModulesContentShouldReturnValue()
{
ModuleName = "ModuleTest",
Status = "running",
Version = "1.0",
ImageURI = "image",
EnvironmentVariables = new List<IoTEdgeModuleEnvironmentVariable>()
{
Expand Down
Loading