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

Refactor LoraWanCommandsController #996

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,141 @@
// 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.Server.Tests.Unit.Controllers.V10.LoRaWAN
{
using System;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Server.Controllers.V10.LoRaWAN;
using AzureIoTHub.Portal.Server.Services;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

[TestFixture]
public class LoRaWANCommandsControllerTests
{
private MockRepository mockRepository;

private Mock<ILoRaWANCommandService> mockLoRaWANCommandService ;

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

this.mockLoRaWANCommandService = this.mockRepository.Create<ILoRaWANCommandService>();
}

[Test]
public async Task PostShouldCreateCommand()
{
// Arrange
var command = new DeviceModelCommand
{
Name = Guid.NewGuid().ToString()
};

var deviceModelCommandsController = CreateDeviceModelCommandsController();

_ = this.mockLoRaWANCommandService.Setup(c => c.PostDeviceModelCommands(It.IsAny<string>(), new[] { command }))
.Returns(Task.CompletedTask);

// Act
var result = await deviceModelCommandsController.Post(Guid.NewGuid().ToString(), new[] { command });

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkResult>(result);
this.mockRepository.VerifyAll();
}

[Test]
public async Task PostNullModelIdShouldThrowArgumentNullException()
{
//Arrange
var command = new DeviceModelCommand
{
Name = Guid.NewGuid().ToString()
};

var deviceModelCommandsController = CreateDeviceModelCommandsController();

// Act
var act = () => deviceModelCommandsController.Post(null, new[] { command });

// Assert
_ = await act.Should().ThrowAsync<ArgumentNullException>();
this.mockRepository.VerifyAll();
}

[Test]
public async Task PostNullCommandsShouldThrowArgumentNullException()
{
// Assert
var deviceModelCommandsController = CreateDeviceModelCommandsController();

// Act
var act = () => deviceModelCommandsController.Post(Guid.NewGuid().ToString(), null);

// Assert
_ = await act.Should().ThrowAsync<ArgumentNullException>();
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetShouldReturnDeviceModelCommands()
{
// Arrange
var command = new DeviceModelCommand
{
Name = Guid.NewGuid().ToString()
};

var deviceModelCommandsController = CreateDeviceModelCommandsController();

_ = this.mockLoRaWANCommandService.Setup(c => c.GetDeviceModelCommandsFromModel(It.IsAny<string>()))
.ReturnsAsync(new[] { command });


// Act
var response = await deviceModelCommandsController.Get(Guid.NewGuid().ToString());

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

var okResult = (OkObjectResult)response.Result;

Assert.IsNotNull(okResult);
Assert.IsAssignableFrom<DeviceModelCommand[]>(okResult.Value);

var result = (DeviceModelCommand[])okResult.Value;
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Length);

this.mockRepository.VerifyAll();
}

[Test]
public async Task GetNullModelIdShouldThrowArgumentNullException()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();

// Act
var act = () => deviceModelCommandsController.Get(null);

// Assert
_ = await act.Should().ThrowAsync<ArgumentNullException>();
this.mockRepository.VerifyAll();
}

private LoRaWANCommandsController CreateDeviceModelCommandsController()
{
return new LoRaWANCommandsController(
this.mockLoRaWANCommandService.Object);
}
}
}
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.Server.Tests.Unit.Controllers.V10
namespace AzureIoTHub.Portal.Server.Tests.Unit.Services
{
using System;
using System.Collections.Generic;
Expand All @@ -11,33 +11,33 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Controllers.V10
using Azure.Data.Tables;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Server.Controllers.V10.LoRaWAN;
using AzureIoTHub.Portal.Server.Exceptions;
using AzureIoTHub.Portal.Server.Factories;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Server.Exceptions;

[TestFixture]
public class DeviceModelCommandsControllerTests
public class LoRaWANCommandServiceTests
{
private MockRepository mockRepository;

private Mock<IDeviceModelCommandMapper> mockDeviceModelCommandMapper;
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<TableClient> mockDeviceTemplatesTableClient;
private Mock<TableClient> mockCommandsTableClient;
private Mock<ILogger<LoRaWANCommandsController>> mockLogger;
private Mock<ILogger<LoRaWANCommandService>> mockLogger;

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

this.mockLogger = this.mockRepository.Create<ILogger<LoRaWANCommandsController>>();
this.mockLogger = this.mockRepository.Create<ILogger<LoRaWANCommandService>>();
this.mockDeviceModelCommandMapper = this.mockRepository.Create<IDeviceModelCommandMapper>();
this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
this.mockDeviceTemplatesTableClient = this.mockRepository.Create<TableClient>();
Expand All @@ -48,7 +48,7 @@ public void SetUp()
public async Task PostShouldCreateCommand()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();
var entity = SetupMockDeviceModel();

var command = new DeviceModelCommand
Expand Down Expand Up @@ -91,12 +91,9 @@ public async Task PostShouldCreateCommand()
.Returns(this.mockCommandsTableClient.Object);

// Act
var result = await deviceModelCommandsController.Post(entity.RowKey, new[] { command });
await deviceModelCommandsService.PostDeviceModelCommands(entity.RowKey, new[] { command });

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkResult>(result);

this.mockTableClientFactory.Verify(c => c.GetDeviceTemplates(), Times.Once());
this.mockDeviceTemplatesTableClient.Verify(c => c.GetEntityAsync<TableEntity>(
It.Is<string>(p => p == LoRaWANDeviceModelsController.DefaultPartitionKey),
Expand All @@ -112,10 +109,10 @@ public async Task PostShouldCreateCommand()
}

[Test]
public async Task PostShouldInternalServerErrorExceptionWhenQueryingExistingCommands()
public async Task PostShouldThrowInternalServerErrorExceptionWhenFailQueryingExistingCommands()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();

var mockResponse = this.mockRepository.Create<Response<TableEntity>>();
var modelId = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -147,18 +144,18 @@ public async Task PostShouldInternalServerErrorExceptionWhenQueryingExistingComm
.Returns(this.mockCommandsTableClient.Object);

// Act
var act = () => deviceModelCommandsController.Post(entity.RowKey, new[] { command });
var act = () => deviceModelCommandsService.PostDeviceModelCommands(entity.RowKey, new[] { command });

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
this.mockRepository.VerifyAll();
}

[Test]
public async Task PostShouldInternalServerErrorExceptionWhenDeletingExistingCommands()
public async Task PostShouldThrowInternalServerErrorExceptionWhenFailDeletingExistingCommands()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();

var mockResponseTableEntity = this.mockRepository.Create<Response<TableEntity>>();
var mockResponse = this.mockRepository.Create<Response>();
Expand Down Expand Up @@ -204,18 +201,18 @@ public async Task PostShouldInternalServerErrorExceptionWhenDeletingExistingComm
.Returns(this.mockCommandsTableClient.Object);

// Act
var act = () => deviceModelCommandsController.Post(entity.RowKey, new[] { command });
var act = () => deviceModelCommandsService.PostDeviceModelCommands(entity.RowKey, new[] { command });

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
this.mockRepository.VerifyAll();
}

[Test]
public async Task PostShouldInternalServerErrorExceptionWhenAddingNewCommands()
public async Task PostShouldThrowInternalServerErrorExceptionWhenFailAddingNewCommands()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();

var mockResponseTableEntity = this.mockRepository.Create<Response<TableEntity>>();
var mockResponse = this.mockRepository.Create<Response>();
Expand Down Expand Up @@ -270,45 +267,43 @@ public async Task PostShouldInternalServerErrorExceptionWhenAddingNewCommands()
.Returns(this.mockCommandsTableClient.Object);

// Act
var act = () => deviceModelCommandsController.Post(entity.RowKey, new[] { command });
var act = () => deviceModelCommandsService.PostDeviceModelCommands(entity.RowKey, new[] { command });

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
this.mockRepository.VerifyAll();
}

[Test]
public async Task WhenModelNotExistsPostShouldReturn404()
public async Task PostShouldThrowInternalServerErrorExceptionWhenModelDoesNotExist()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();

SetupNotFoundDeviceModel();

// Act
var result = await deviceModelCommandsController.Post(Guid.NewGuid().ToString(), new[] { new DeviceModelCommand() });
var act = () => deviceModelCommandsService.PostDeviceModelCommands(Guid.NewGuid().ToString(), new[] { new DeviceModelCommand() });

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<NotFoundResult>(result);
_ = await act.Should().ThrowAsync<InternalServerErrorException>();

this.mockRepository.VerifyAll();
}

[Test]
public async Task WhenModelNotExistsGetShouldReturn404()
public async Task GetShouldThrowInternalServerErrorExceptionWhenModelDoesNotExist()
{
// Arrange
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();

SetupNotFoundDeviceModel();

// Act
var result = await deviceModelCommandsController.Get(Guid.NewGuid().ToString());
var act = () => deviceModelCommandsService.GetDeviceModelCommandsFromModel(Guid.NewGuid().ToString());

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<NotFoundResult>(result.Result);
_ = await act.Should().ThrowAsync<InternalServerErrorException>();

this.mockRepository.VerifyAll();
}
Expand All @@ -319,7 +314,7 @@ public async Task GetShouldReturnDeviceModelCommands()
// Arrange
var deviceModel = SetupMockDeviceModel();
var deviceModelId = deviceModel.RowKey;
var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();
var mockResponse = this.mockRepository.Create<Response>();

_ = this.mockCommandsTableClient.Setup(c => c.Query<TableEntity>(
Expand All @@ -343,18 +338,10 @@ public async Task GetShouldReturnDeviceModelCommands()
.Returns(new DeviceModelCommand());

// Act
var response = await deviceModelCommandsController.Get(deviceModelId);
var result = await deviceModelCommandsService.GetDeviceModelCommandsFromModel(deviceModelId);

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

var okResult = (OkObjectResult)response.Result;

Assert.IsNotNull(okResult);
Assert.IsAssignableFrom<DeviceModelCommand[]>(okResult.Value);

var result = (DeviceModelCommand[])okResult.Value;
Assert.IsAssignableFrom<DeviceModelCommand[]>(result);
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Length);
}
Expand All @@ -376,7 +363,7 @@ public async Task GetShouldThrowRequestFailedExceptionWhenIssueOccursWhenQueryin
_ = this.mockTableClientFactory.Setup(c => c.GetDeviceTemplates())
.Returns(this.mockDeviceTemplatesTableClient.Object);

var deviceModelCommandsController = CreateDeviceModelCommandsController();
var deviceModelCommandsService = CreateDeviceModelCommandsService();

_ = this.mockCommandsTableClient.Setup(c => c.Query<TableEntity>(
It.Is<string>(x => x == $"PartitionKey eq '{deviceModelId}'"),
Expand All @@ -389,7 +376,7 @@ public async Task GetShouldThrowRequestFailedExceptionWhenIssueOccursWhenQueryin
.Returns(this.mockCommandsTableClient.Object);

// Act
var act = () => deviceModelCommandsController.Get(deviceModelId);
var act = () => deviceModelCommandsService.GetDeviceModelCommandsFromModel(deviceModelId);

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
Expand Down Expand Up @@ -431,9 +418,9 @@ private void SetupNotFoundDeviceModel()
.Returns(this.mockDeviceTemplatesTableClient.Object);
}

private LoRaWANCommandsController CreateDeviceModelCommandsController()
private LoRaWANCommandService CreateDeviceModelCommandsService()
{
return new LoRaWANCommandsController(
return new LoRaWANCommandService(
this.mockLogger.Object,
this.mockDeviceModelCommandMapper.Object,
this.mockTableClientFactory.Object);
Expand Down
Loading