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

Introduce LoRa Concentrator Management #240

Merged
merged 36 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
80b68c5
add of concentrator page list
Sben65 Feb 5, 2022
9082295
concentrator controller
Sben65 Feb 5, 2022
a1bcbd0
add of default properties
Sben65 Feb 6, 2022
795589d
add of create function in concentratorsController
Sben65 Feb 7, 2022
9e1b81c
change the folder name
Sben65 Feb 7, 2022
1da0799
change the old links
Sben65 Feb 7, 2022
1ab9d5a
add create concentrator page with required properties
Sben65 Feb 7, 2022
abc0936
functional update, delete and detail
Sben65 Feb 9, 2022
a93ad0e
update desired properties of concentrator
Sben65 Feb 9, 2022
546aa51
add of test
Sben65 Feb 9, 2022
4dd4ad2
Add mocked httpClient
kbeaugrand Feb 9, 2022
76250a4
add of concentrator page list
Sben65 Feb 5, 2022
e80cea7
concentrator controller
Sben65 Feb 5, 2022
a85fabf
add of default properties
Sben65 Feb 6, 2022
8d93cf8
add of create function in concentratorsController
Sben65 Feb 7, 2022
d3f17e8
change the folder name
Sben65 Feb 7, 2022
4559bc0
change the old links
Sben65 Feb 7, 2022
d33579c
add create concentrator page with required properties
Sben65 Feb 7, 2022
260ff0e
functional update, delete and detail
Sben65 Feb 9, 2022
4373a2b
update desired properties of concentrator
Sben65 Feb 9, 2022
ac6bb24
add of test
Sben65 Feb 9, 2022
c9cbdfc
add of RouterConfigManager
Sben65 Feb 9, 2022
633adbf
Add mocked httpClient
kbeaugrand Feb 9, 2022
d9cacdc
concentratorsControllerTest + concentratorsTwinMapperTest
Sben65 Feb 10, 2022
e9a71bc
fix #192
Sben65 Feb 10, 2022
3b2ae5c
resolve bug
Sben65 Feb 10, 2022
3130a2d
fix #192
Sben65 Feb 10, 2022
b6069aa
...
Sben65 Feb 10, 2022
e7641f4
fix error on devicesControllerTest-GetList_StateUnderTest_ExpectedBeh…
Sben65 Feb 10, 2022
15d89e8
Merge branch 'main' into issue_#192_add_LoRa_concentrator
kbeaugrand Feb 10, 2022
e523c96
Fix unit testing on concentrator twin mapper
kbeaugrand Feb 10, 2022
18eab84
Change on GetRouterConfig unit testing
kbeaugrand Feb 10, 2022
d164b87
Update src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/ModuleLogsDia…
kbeaugrand Feb 10, 2022
fb877a7
Change menu organization + Fix labels
kbeaugrand Feb 10, 2022
454bf50
Fix #241 - Add setting to deploy template
kbeaugrand Feb 10, 2022
f3dab7a
Fix Code QL issues
kbeaugrand Feb 10, 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
@@ -0,0 +1,238 @@
using AzureIoTHub.Portal.Server.Controllers;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.Concentrator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Controllers
{
[TestFixture]
public class ConcentratorsControllerTests
{
private MockRepository mockRepository;

private Mock<IRouterConfigManager> mockRouterManager;
private Mock<IConcentratorTwinMapper> mockConcentratorTwinMapper;
private Mock<IDeviceService> mockDeviceService;
private Mock<ILogger<ConcentratorsController>> mockLogger;

[SetUp]
public void SetUp()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockLogger = this.mockRepository.Create<ILogger<ConcentratorsController>>();
this.mockRouterManager = this.mockRepository.Create<IRouterConfigManager>();
this.mockConcentratorTwinMapper = this.mockRepository.Create<IConcentratorTwinMapper>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
}

private ConcentratorsController CreateController()
{
return new ConcentratorsController(
this.mockLogger.Object,
this.mockDeviceService.Object,
this.mockRouterManager.Object,
this.mockConcentratorTwinMapper.Object);
}

[Test]
public async Task GetAllDeviceConcentrator_WithNoArgument_ReturnConcentratorList()
{
// Arrange
var twin = new Twin("aaa");
twin.Tags["deviceType"] = "LoRa Concentrator";
var concentrator = new Concentrator
{
DeviceId = twin.DeviceId,
DeviceType = "LoRa Concentrator"
};

this.mockDeviceService.Setup(x => x.GetAllDevice())
.ReturnsAsync(new[]
{
twin
});

this.mockConcentratorTwinMapper.Setup(x => x.CreateDeviceDetails(It.Is<Twin>(c => c.DeviceId == twin.DeviceId)))
.Returns(concentrator);

var concentratorController = this.CreateController();

// Act
var result = await concentratorController.GetAllDeviceConcentrator();

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkObjectResult>(result);
var okObjectResult = result as ObjectResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<List<Concentrator>>(okObjectResult.Value);
var deviceList = okObjectResult.Value as List<Concentrator>;
Assert.IsNotNull(deviceList);
Assert.AreEqual(1, deviceList.Count);
var device = deviceList[0];
Assert.IsNotNull(device);
Assert.AreEqual(twin.DeviceId, device.DeviceId);
Assert.AreEqual("LoRa Concentrator", device.DeviceType);

this.mockRepository.VerifyAll();
}

[Test]
public async Task GetDeviceConcentrator_With_Valid_Argument_Should_Return_Concentrator()
{
// Arrange
var twin = new Twin("aaa");
twin.Tags["deviceType"] = "LoRa Concentrator";
var concentrator = new Concentrator
{
DeviceId = twin.DeviceId,
DeviceType = "LoRa Concentrator"
};

this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is<string>(c => c == twin.DeviceId)))
.ReturnsAsync(twin);

this.mockConcentratorTwinMapper.Setup(x => x.CreateDeviceDetails(It.Is<Twin>(c => c.DeviceId == twin.DeviceId)))
.Returns(concentrator);

var concentratorController = this.CreateController();

// Act
var result = await concentratorController.GetDeviceConcentrator(twin.DeviceId);

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkObjectResult>(result);
var okObjectResult = result as ObjectResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<Concentrator>(okObjectResult.Value);
var device = okObjectResult.Value as Concentrator;
Assert.IsNotNull(device);
Assert.AreEqual(twin.DeviceId, device.DeviceId);
Assert.AreEqual("LoRa Concentrator", device.DeviceType);

this.mockRepository.VerifyAll();
}

[Test]
public async Task CreateDeviceAsync_With_Valid_Argument_Should_Return_OkResult()
{
// Arrange
var concentratorController = this.CreateController();
var concentrator = new Concentrator
{
DeviceId = "4512457896451156",
LoraRegion = Guid.NewGuid().ToString() ,
IsEnabled = true
};

var routerConfig = new RouterConfig();
var mockResult = new BulkRegistryOperationResult
{
IsSuccessful = true
};

var twin = new Twin
{
DeviceId = concentrator.DeviceId,
};

this.mockDeviceService.Setup(c => c.CreateDeviceWithTwin(
It.Is<string>(x => x == twin.DeviceId),
It.Is<bool>(x => !x),
It.Is<Twin>(x => x.DeviceId == twin.DeviceId),
It.Is<DeviceStatus>(x => x == DeviceStatus.Enabled)))
.ReturnsAsync(mockResult);

this.mockRouterManager.Setup(x => x.GetRouterConfig(It.Is<string>(c => c == concentrator.LoraRegion)))
.ReturnsAsync(routerConfig);

this.mockConcentratorTwinMapper.Setup(x => x.UpdateTwin(It.Is<Twin>(c => c.DeviceId == twin.DeviceId), It.Is<Concentrator>(c => c.DeviceId == concentrator.DeviceId)));
this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));

// Act
var result = await concentratorController.CreateDeviceAsync(concentrator);

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkObjectResult>(result);
var okObjectResult = result as ObjectResult;
Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<BulkRegistryOperationResult>(okObjectResult.Value);
Assert.AreEqual(mockResult, okObjectResult.Value);
}

[Test]
public async Task UpdateDeviceAsync_With_Valid_Argument_Should_Return_OkResult()
{
// Arrange
var concentratorController = this.CreateController();
var concentrator = new Concentrator
{
DeviceId = "4512457896451156",
LoraRegion = Guid.NewGuid().ToString(),
IsEnabled = true,
RouterConfig = new RouterConfig()
};

var twin = new Twin
{
DeviceId = concentrator.DeviceId,
};

var device = new Device(concentrator.DeviceId);

this.mockRouterManager.Setup(x => x.GetRouterConfig(It.Is<string>(c => c == concentrator.LoraRegion)))
.ReturnsAsync(concentrator.RouterConfig);
this.mockConcentratorTwinMapper.Setup(x => x.UpdateTwin(It.Is<Twin>(c => c.DeviceId == twin.DeviceId), It.Is<Concentrator>(c => c.DeviceId == concentrator.DeviceId)));

this.mockDeviceService.Setup(x => x.GetDevice(It.Is<string>(c => c == concentrator.DeviceId)))
.ReturnsAsync(device);
this.mockDeviceService.Setup(x => x.UpdateDevice(It.Is<Device>(c => c.Id == concentrator.DeviceId)))
.ReturnsAsync(device);
this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is<string>(c => c == concentrator.DeviceId)))
.ReturnsAsync(twin);
this.mockDeviceService.Setup(x => x.UpdateDeviceTwin(It.Is<string>(c => c == concentrator.DeviceId), It.Is<Twin>(c => c.DeviceId == concentrator.DeviceId)))
.ReturnsAsync(twin);

// Act
var result = await concentratorController.UpdateDeviceAsync(concentrator);

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkObjectResult>(result);
var okObjectResult = result as ObjectResult;
Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
}

[Test]
public async Task DeleteDeviceAsync()
{
await Task.CompletedTask;
Assert.Inconclusive();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ public async Task GetList_StateUnderTest_ExpectedBehavior()
// Arrange
var devicesController = this.CreateDevicesController();
int count = 100;
TwinCollection twinCollection = new TwinCollection();
twinCollection["deviceType"] = "test";

this.mockDeviceService.Setup(c => c.GetAllDevice())
.ReturnsAsync(Enumerable.Range(0, 100).Select(x => new Twin
{
DeviceId = x.ToString()
DeviceId = x.ToString(),
Tags = twinCollection
}));

this.mockDeviceTwinMapper.Setup(c => c.CreateDeviceListItem(It.IsAny<Twin>()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using AzureIoTHub.Portal.Server.Managers;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Managers
{
[TestFixture]
public class RouterConfigManagerTests
{
private MockRepository mockRepository;

private HttpClient mockHttpClient;
private Mock<HttpMessageHandler> httpMessageHandlerMock;

[SetUp]
public void SetUp()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.httpMessageHandlerMock = this.mockRepository.Create<HttpMessageHandler>();
this.mockHttpClient = new HttpClient(this.httpMessageHandlerMock.Object)
{
BaseAddress = new Uri("http://fake.local")
};
}

private RouterConfigManager CreateManager()
{
return new RouterConfigManager(this.mockHttpClient);
}

[Test]
public async Task GetRouterConfig_StateUnderTest_ExpectedBehavior()
{
// Arrange
var routerConfig = CreateManager();
var loraRegion = Guid.NewGuid().ToString();

using var deviceResponseMock = new HttpResponseMessage();

deviceResponseMock.Content = new StringContent("{}", Encoding.UTF8, "application/json");

this.httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.Is<HttpRequestMessage>(req => req.RequestUri.LocalPath.Equals($"/{loraRegion}.json", StringComparison.OrdinalIgnoreCase)), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync((HttpRequestMessage req, CancellationToken token) => deviceResponseMock)
.Verifiable();

// Act
var result = await routerConfig.GetRouterConfig(loraRegion);

// Assert
Assert.IsNotNull(result);
}
}
}
Loading