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

Manage LoRa and non-LoRa Devices #247

Merged
merged 7 commits into from
Feb 17, 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
3 changes: 3 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

# Default severity for all analyzer diagnostics
dotnet_analyzer_diagnostic.severity = suggestion

# CS0649: Field 'CreateDeviceModelPage.form' is never assigned to, and will always have its default value null
dotnet_diagnostic.CS0649.severity = suggestion
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AzureIoTHub.Portal.Server.Controllers.V10;
using AzureIoTHub.Portal.Server.Controllers.V10.LoRaWAN;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.V10.Concentrator;
using AzureIoTHub.Portal.Shared.Models.V10.LoRaWAN.Concentrator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
Expand All @@ -14,7 +14,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Controllers
namespace AzureIoTHub.Portal.Server.Tests.Controllers.V10
{
[TestFixture]
public class ConcentratorsControllerTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Controllers
namespace AzureIoTHub.Portal.Server.Tests.Controllers.V10
{
[TestFixture]
public class DevicesControllerTests
Expand All @@ -23,7 +23,7 @@ public class DevicesControllerTests
private Mock<ILogger<DevicesController>> mockLogger;
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<IDeviceService> mockDeviceService;
private Mock<IDeviceTwinMapper> mockDeviceTwinMapper;
private Mock<IDeviceTwinMapper<DeviceListItem, DeviceDetails>> mockDeviceTwinMapper;
private Mock<ILoraDeviceMethodManager> mockLoraDeviceMethodManager;
private Mock<IDeviceModelCommandMapper> mockDeviceModelCommandMapper;

Expand All @@ -35,7 +35,7 @@ public void SetUp()
this.mockLogger = this.mockRepository.Create<ILogger<DevicesController>>();
this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
this.mockDeviceTwinMapper = this.mockRepository.Create<IDeviceTwinMapper>();
this.mockDeviceTwinMapper = this.mockRepository.Create<IDeviceTwinMapper<DeviceListItem, DeviceDetails>>();
this.mockLoraDeviceMethodManager = this.mockRepository.Create<ILoraDeviceMethodManager>();
this.mockDeviceModelCommandMapper = this.mockRepository.Create<IDeviceModelCommandMapper>();
}
Expand All @@ -44,11 +44,8 @@ private DevicesController CreateDevicesController()
{
return new DevicesController(
this.mockLogger.Object,
this.mockTableClientFactory.Object,
this.mockDeviceService.Object,
this.mockDeviceTwinMapper.Object,
this.mockLoraDeviceMethodManager.Object,
this.mockDeviceModelCommandMapper.Object);
this.mockDeviceTwinMapper.Object);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Controllers
namespace AzureIoTHub.Portal.Server.Tests.Controllers.V10
{
[TestFixture]
public class GatewaysControllerTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Azure;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Server.Controllers.V10;
using AzureIoTHub.Portal.Server.Controllers.V10.LoRaWAN;
using AzureIoTHub.Portal.Server.Factories;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Shared.Models.V10;
using AzureIoTHub.Portal.Shared.Models.V10.LoRaWAN.LoRaDeviceModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand All @@ -25,23 +25,23 @@ public class DeviceModelCommandsControllerTests
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<TableClient> mockDeviceTemplatesTableClient;
private Mock<TableClient> mockCommandsTableClient;
private Mock<ILogger<DeviceModelCommandsController>> mockLogger;
private Mock<ILogger<LoRaWANCommandsController>> mockLogger;

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

this.mockLogger = this.mockRepository.Create<ILogger<DeviceModelCommandsController>>();
this.mockLogger = this.mockRepository.Create<ILogger<LoRaWANCommandsController>>();
this.mockDeviceModelCommandMapper = this.mockRepository.Create<IDeviceModelCommandMapper>();
this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
this.mockDeviceTemplatesTableClient = this.mockRepository.Create<TableClient>();
this.mockCommandsTableClient = this.mockRepository.Create<TableClient>();
}

private DeviceModelCommandsController CreateDeviceModelCommandsController()
private LoRaWANCommandsController CreateDeviceModelCommandsController()
{
return new DeviceModelCommandsController(
return new LoRaWANCommandsController(
this.mockLogger.Object,
this.mockDeviceModelCommandMapper.Object,
this.mockTableClientFactory.Object);
Expand Down Expand Up @@ -70,24 +70,44 @@ public async Task Post_Should_Create_Command()
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

this.mockCommandsTableClient.Setup(c => c.Query<TableEntity>(
It.Is<string>(x => x == $"PartitionKey eq '{ entity.RowKey }'"),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(Pageable<TableEntity>.FromPages(new[]
{
Page<TableEntity>.FromValues(new[]
{
new TableEntity(entity.RowKey, Guid.NewGuid().ToString())
}, null, mockResponse.Object)
}));

this.mockCommandsTableClient.Setup(c => c.DeleteEntityAsync(
It.Is<string>(x => x == entity.RowKey),
It.IsAny<string>(),
It.IsAny<ETag>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

this.mockTableClientFactory.Setup(c => c.GetDeviceCommands())
.Returns(mockCommandsTableClient.Object);

// Act
var result = await deviceModelCommandsController.Post(entity.RowKey, command);
var result = await deviceModelCommandsController.Post(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.GetEntity<TableEntity>(
It.Is<string>(p => p == DeviceModelsController.DefaultPartitionKey),
It.Is<string>(p => p == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.Is<string>(k => k == entity.RowKey),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()), Times.Once);

this.mockTableClientFactory.Verify(c => c.GetDeviceCommands(), Times.Once());
this.mockTableClientFactory.Verify(c => c.GetDeviceCommands());
this.mockDeviceModelCommandMapper.VerifyAll();
this.mockCommandsTableClient.Verify(c => c.AddEntityAsync(
It.Is<TableEntity>(x => x.RowKey == command.Name && x.PartitionKey == entity.RowKey),
Expand All @@ -103,7 +123,7 @@ public async Task When_Model_NotExists_Post_Should_Return_404()
SetupNotFoundDeviceModel();

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

// Assert
Assert.IsNotNull(result);
Expand All @@ -113,60 +133,19 @@ public async Task When_Model_NotExists_Post_Should_Return_404()
}

[Test]
public async Task Delete_Should_Delete_Command()
{
// Arrange
var deviceModelCommandsController = this.CreateDeviceModelCommandsController();
var entity = this.SetupMockDeviceModel();
string commandId = Guid.NewGuid().ToString();
var mockResponse = this.mockRepository.Create<Response>();

this.mockCommandsTableClient.Setup(c => c.DeleteEntityAsync(
It.Is<string>(x => x == entity.RowKey),
It.Is<string>(x => x == commandId),
It.IsAny<ETag>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

this.mockTableClientFactory
.Setup(c => c.GetDeviceCommands())
.Returns(mockCommandsTableClient.Object);

// Act
var result = await deviceModelCommandsController.Delete(entity.RowKey, commandId);

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

this.mockTableClientFactory.Verify(c => c.GetDeviceTemplates(), Times.Once());
this.mockDeviceTemplatesTableClient.Verify(c => c.GetEntity<TableEntity>(
It.Is<string>(p => p == DeviceModelsController.DefaultPartitionKey),
It.Is<string>(k => k == entity.RowKey),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()), Times.Once);

this.mockCommandsTableClient.Verify(c => c.DeleteEntityAsync(
It.Is<string>(x => x == entity.RowKey),
It.Is<string>(x => x == commandId),
It.IsAny<ETag>(),
It.IsAny<CancellationToken>()), Times.Once);
}

[Test]
public async Task When_Model_NotExists_Delete_Should_Return_404()
public void When_Model_NotExists_Get_Should_Return_404()
{
// Arrange
var deviceModelCommandsController = this.CreateDeviceModelCommandsController();

SetupNotFoundDeviceModel();

// Act
var result = await deviceModelCommandsController.Delete(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
var result = deviceModelCommandsController.Get(Guid.NewGuid().ToString());

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<NotFoundResult>(result);
Assert.IsAssignableFrom<NotFoundResult>(result.Result);

this.mockRepository.VerifyAll();
}
Expand All @@ -175,13 +154,13 @@ private TableEntity SetupMockDeviceModel()
{
var mockResponse = this.mockRepository.Create<Response<TableEntity>>();
var modelId = Guid.NewGuid().ToString();
var entity = new TableEntity(DeviceModelsController.DefaultPartitionKey, modelId);
var entity = new TableEntity(LoRaWANDeviceModelsController.DefaultPartitionKey, modelId);

mockResponse.Setup(c => c.Value)
.Returns(entity);

this.mockDeviceTemplatesTableClient.Setup(c => c.GetEntity<TableEntity>(
It.Is<string>(p => p == DeviceModelsController.DefaultPartitionKey),
It.Is<string>(p => p == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.Is<string>(k => k == modelId),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
Expand All @@ -196,7 +175,7 @@ private TableEntity SetupMockDeviceModel()
private void SetupNotFoundDeviceModel()
{
this.mockDeviceTemplatesTableClient.Setup(c => c.GetEntity<TableEntity>(
It.Is<string>(p => p == DeviceModelsController.DefaultPartitionKey),
It.Is<string>(p => p == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.IsAny<string>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
Expand Down
Loading