Skip to content

Commit

Permalink
Refactor DevicesControllerBase #986 (#1172)
Browse files Browse the repository at this point in the history
* Refactor GetCredentials on DevicesControllerBase #986

* Refactor CreateDeviceAsync()

* Delete useless using statements

* Fix formatting in DeviceServiceTests

* Delete useless code in DeviceControllerBase GetItems()

* Add test on GetEnrollmentCredentials()

Co-authored-by: Audrey Serra <audrey.serra@cgi.com>
  • Loading branch information
hocinehacherouf and audserraCGI authored Sep 13, 2022
1 parent 418ba4e commit d5778d2
Show file tree
Hide file tree
Showing 9 changed files with 3,504 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v1._0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Domain.Repositories;
using AzureIoTHub.Portal.Server.Controllers.V10;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using FluentAssertions;
Expand All @@ -37,7 +33,6 @@ public class DevicesControllerTests
{
private MockRepository mockRepository;

private Mock<IDeviceProvisioningServiceManager> mockProvisioningServiceManager;
private Mock<IUrlHelper> mockUrlHelper;
private Mock<ILogger<DevicesController>> mockLogger;
private Mock<IDeviceService> mockDeviceService;
Expand All @@ -52,7 +47,6 @@ public void SetUp()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockProvisioningServiceManager = this.mockRepository.Create<IDeviceProvisioningServiceManager>();
this.mockLogger = this.mockRepository.Create<ILogger<DevicesController>>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
this.mockDevicePropertyService = this.mockRepository.Create<IDevicePropertyService>();
Expand All @@ -69,10 +63,8 @@ private DevicesController CreateDevicesController()
this.mockLogger.Object,
this.mockDeviceService.Object,
this.mockDeviceTagService.Object,
this.mockProvisioningServiceManager.Object,
this.mockDeviceTwinMapper.Object,
this.mockDevicePropertyService.Object,
this.mockTableClientFactory.Object)
this.mockDevicePropertyService.Object)
{
Url = this.mockUrlHelper.Object
};
Expand Down Expand Up @@ -195,8 +187,8 @@ public async Task CreateDeviceAsyncStateUnderTestExpectedBehavior()

Twin twin = null;

_ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny<string>()))
.ReturnsAsync((Device)null);
_ = this.mockDeviceService.Setup(c => c.CreateNewTwinFromDeviceId(It.IsAny<string>()))
.ReturnsAsync(new Twin { DeviceId = device.DeviceID });

_ = this.mockDeviceTwinMapper.Setup(c => c.UpdateTwin(It.Is<Twin>(x => x.DeviceId == device.DeviceID), It.Is<DeviceDetails>(x => x == device)))
.Callback<Twin, DeviceDetails>((t, _) => twin = t);
Expand Down Expand Up @@ -309,41 +301,17 @@ public async Task GetEnrollmentCredentialsShouldReturnEnrollmentCredentials()
{
// Arrange
var devicesController = CreateDevicesController();


var mockRegistrationCredentials = new EnrollmentCredentials
{
RegistrationID = "aaa",
SymmetricKey = "dfhjkfdgh"
};

var mockTwin = new Twin("aaa");
mockTwin.Tags["modelId"] = "bbb";

var mockTableClient = this.mockRepository.Create<TableClient>();
var mockDeviceModelEntity = new TableEntity
{
[nameof(DeviceModel.Name)] = "ccc"
};
var mockResponse = this.mockRepository.Create<Response<TableEntity>>();

_ = mockResponse.SetupGet(c => c.Value)
.Returns(mockDeviceModelEntity);

_ = mockTableClient.Setup(c => c.GetEntityAsync<TableEntity>(
It.Is<string>(x => x == "0"),
It.Is<string>(x => x == "bbb"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

_ = this.mockProvisioningServiceManager.Setup(c => c.GetEnrollmentCredentialsAsync("aaa", "ccc"))
_ = this.mockDeviceService.Setup(c => c.GetEnrollmentCredentials("aaa"))
.ReturnsAsync(mockRegistrationCredentials);

_ = this.mockDeviceService.Setup(c => c.GetDeviceTwin("aaa"))
.ReturnsAsync(mockTwin);

_ = this.mockTableClientFactory.Setup(c => c.GetDeviceTemplates())
.Returns(mockTableClient.Object);

// Act
var response = await devicesController.GetCredentials("aaa");

Expand All @@ -358,54 +326,14 @@ public async Task GetEnrollmentCredentialsShouldReturnEnrollmentCredentials()
this.mockRepository.VerifyAll();
}

[Test]
public async Task WhenDeviceTypePropertyNotExistGetEnrollmentCredentialsShouldReturnBadRequest()
{
// Arrange
var devicesController = CreateDevicesController();

var mockTwin = new Twin("aaa");

_ = this.mockDeviceService.Setup(c => c.GetDeviceTwin("aaa"))
.ReturnsAsync(mockTwin);

// Act
var response = await devicesController.GetCredentials("aaa");

// Assert
Assert.IsNotNull(response);
Assert.IsAssignableFrom<BadRequestObjectResult>(response.Result);

this.mockRepository.VerifyAll();
}

[Test]
public async Task WhenDeviceNotExistGetEnrollmentCredentialsShouldReturnNotFound()
{
// Arrange
var devicesController = CreateDevicesController();

_ = this.mockDeviceService.Setup(c => c.GetDeviceTwin("aaa"))
.ReturnsAsync((Twin)null);

// Act
var response = await devicesController.GetCredentials("aaa");

// Assert
Assert.IsNotNull(response);
Assert.IsAssignableFrom<NotFoundObjectResult>(response.Result);

this.mockRepository.VerifyAll();
}

[Test]
public async Task CreateDeviceAsyncDuplicatedDeviceIdShouldThrowInternalServerErrorException()
{
// Arrange
var devicesController = CreateDevicesController();

_ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny<string>()))
.ReturnsAsync(new Device());
_ = this.mockDeviceService.Setup(c => c.CreateNewTwinFromDeviceId(It.IsAny<string>()))
.ThrowsAsync(new InternalServerErrorException("test"));

// Act
var result = async () => await devicesController.CreateDeviceAsync(new DeviceDetails());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v1._0.LoRaWAN
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Server.Controllers.V10;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using FluentAssertions;
Expand All @@ -29,33 +26,21 @@ public class LoRaWANDevicesControllerTests
{
private MockRepository mockRepository;

private Mock<IDeviceProvisioningServiceManager> mockProvisioningServiceManager;
private Mock<ILogger<LoRaWANDevicesController>> mockLogger;
private Mock<IDeviceService> mockDeviceService;
private Mock<IDeviceTagService> mockDeviceTagService;
private Mock<IDeviceTwinMapper<DeviceListItem, LoRaDeviceDetails>> mockDeviceTwinMapper;
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<ILoraDeviceMethodManager> mockLoraDeviceMethodManager;
private Mock<IDeviceModelCommandMapper> mockDeviceModelCommandMapper;
private Mock<IUrlHelper> mockUrlHelper;
private Mock<TableClient> mockCommandsTableClient;
private Mock<ILoRaWANDeviceService> mockLoRaWANDeviceService;

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

this.mockProvisioningServiceManager = this.mockRepository.Create<IDeviceProvisioningServiceManager>();
this.mockLogger = this.mockRepository.Create<ILogger<LoRaWANDevicesController>>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
this.mockDeviceTagService = this.mockRepository.Create<IDeviceTagService>();
this.mockDeviceTwinMapper = this.mockRepository.Create<IDeviceTwinMapper<DeviceListItem, LoRaDeviceDetails>>();
this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
this.mockLoraDeviceMethodManager = this.mockRepository.Create<ILoraDeviceMethodManager>();
this.mockDeviceModelCommandMapper = this.mockRepository.Create<IDeviceModelCommandMapper>();
this.mockCommandsTableClient = this.mockRepository.Create<TableClient>();
this.mockUrlHelper = this.mockRepository.Create<IUrlHelper>();
this.mockLoRaWANDeviceService = this.mockRepository.Create<ILoRaWANDeviceService>();
}

Expand All @@ -66,9 +51,7 @@ private LoRaWANDevicesController CreateLoRaWANDevicesController()
this.mockDeviceService.Object,
this.mockDeviceTagService.Object,
this.mockDeviceTwinMapper.Object,
this.mockTableClientFactory.Object,
this.mockLoRaWANDeviceService.Object,
this.mockProvisioningServiceManager.Object);
this.mockLoRaWANDeviceService.Object);
}

[Test]
Expand Down Expand Up @@ -182,8 +165,8 @@ public async Task CreateDeviceAsyncStateUnderTestExpectedBehavior()

Twin twin = null;

_ = this.mockDeviceService.Setup(c => c.GetDevice(It.IsAny<string>()))
.ReturnsAsync((Device)null);
_ = this.mockDeviceService.Setup(c => c.CreateNewTwinFromDeviceId(It.IsAny<string>()))
.ReturnsAsync(new Twin { DeviceId = device.DeviceID });

_ = this.mockDeviceTwinMapper.Setup(c => c.UpdateTwin(It.Is<Twin>(x => x.DeviceId == device.DeviceID), It.Is<LoRaDeviceDetails>(x => x == device)))
.Callback<Twin, LoRaDeviceDetails>((t, _) => twin = t);
Expand Down
Loading

0 comments on commit d5778d2

Please sign in to comment.