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

Create enrollment groups for device models #346

Merged
merged 9 commits into from
Feb 28, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.Azure.Devices.Shared;
using AzureIoTHub.Portal.Shared.Models.V10.DeviceModel;
using AzureIoTHub.Portal.Server.Controllers.V10;
using Microsoft.Azure.Devices.Provisioning.Service;

namespace AzureIoTHub.Portal.Server.Tests.Controllers.V10
{
Expand All @@ -35,6 +36,7 @@ public class DeviceModelsControllerTests
private Mock<IDeviceModelMapper<DeviceModel, DeviceModel>> mockDeviceModelMapper;
private Mock<IDeviceService> mockDeviceService;
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<IDeviceProvisioningServiceManager> mockDeviceProvisioningServiceManager;
private Mock<TableClient> mockDeviceTemplatesTableClient;
private Mock<TableClient> mockCommandsTableClient;

Expand All @@ -46,6 +48,7 @@ public void SetUp()
this.mockLogger = this.mockRepository.Create<ILogger<DeviceModelsController>>();
this.mockDeviceModelImageManager = this.mockRepository.Create<IDeviceModelImageManager>();
this.mockDeviceModelCommandMapper = this.mockRepository.Create<IDeviceModelCommandMapper>();
this.mockDeviceProvisioningServiceManager = this.mockRepository.Create<IDeviceProvisioningServiceManager>();
this.mockDeviceModelMapper = this.mockRepository.Create<IDeviceModelMapper<DeviceModel, DeviceModel>>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
Expand All @@ -60,7 +63,8 @@ private DeviceModelsController CreateDeviceModelsController()
this.mockDeviceModelImageManager.Object,
this.mockDeviceModelMapper.Object,
this.mockDeviceService.Object,
this.mockTableClientFactory.Object);
this.mockTableClientFactory.Object,
mockDeviceProvisioningServiceManager.Object);

return result;
}
Expand Down Expand Up @@ -318,8 +322,10 @@ public async Task Post_Should_Create_A_New_Entity()

var requestModel = new DeviceModel
{
Name = Guid.NewGuid().ToString(),
ModelId = Guid.NewGuid().ToString()
};
var mockEnrollmentGroup = this.mockRepository.Create<EnrollmentGroup>(string.Empty, new SymmetricKeyAttestation(string.Empty, string.Empty));

var mockResponse = this.mockRepository.Create<Response>();

Expand All @@ -333,6 +339,12 @@ public async Task Post_Should_Create_A_New_Entity()
It.Is<TableEntity>(x => x.RowKey == requestModel.ModelId && x.PartitionKey == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.IsAny<DeviceModel>()));

this.mockDeviceProvisioningServiceManager.Setup(c => c.CreateEnrollmentGroupFormModelAsync(
It.IsAny<string>(),
It.Is<string>(x => x == requestModel.Name),
It.IsAny<TwinCollection>()))
.ReturnsAsync(mockEnrollmentGroup.Object);

// Act
var result = await deviceModelsController.Post(requestModel);

Expand All @@ -355,10 +367,12 @@ public async Task WhenEmptyModelId_Post_Should_Create_A_New_Entity()

var requestModel = new DeviceModel
{
ModelId = String.Empty
ModelId = String.Empty,
Name = Guid.NewGuid().ToString(),
};

var mockResponse = this.mockRepository.Create<Response>();
var mockEnrollmentGroup = this.mockRepository.Create<EnrollmentGroup>(string.Empty, new SymmetricKeyAttestation(string.Empty, string.Empty));

this.mockDeviceTemplatesTableClient.Setup(c => c.UpsertEntityAsync(
It.Is<TableEntity>(x => x.RowKey != requestModel.ModelId && x.PartitionKey == LoRaWANDeviceModelsController.DefaultPartitionKey),
Expand All @@ -373,6 +387,12 @@ public async Task WhenEmptyModelId_Post_Should_Create_A_New_Entity()
this.mockTableClientFactory.Setup(c => c.GetDeviceTemplates())
.Returns(mockDeviceTemplatesTableClient.Object);

this.mockDeviceProvisioningServiceManager.Setup(c => c.CreateEnrollmentGroupFormModelAsync(
It.IsAny<string>(),
It.Is<string>(x => x == requestModel.Name),
It.IsAny<TwinCollection>()))
.ReturnsAsync(mockEnrollmentGroup.Object);

// Act
var result = await deviceModelsController.Post(requestModel);

Expand Down Expand Up @@ -417,9 +437,12 @@ public async Task Put_Should_Update_The_Device_Model()

var requestModel = new DeviceModel
{
Name = Guid.NewGuid().ToString(),
ModelId = deviceModel.RowKey
};

var mockEnrollmentGroup = this.mockRepository.Create<EnrollmentGroup>(string.Empty, new SymmetricKeyAttestation(string.Empty, string.Empty));

this.mockDeviceTemplatesTableClient.Setup(c => c.UpsertEntityAsync(
It.Is<TableEntity>(x => x.RowKey == deviceModel.RowKey && x.PartitionKey == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.IsAny<TableUpdateMode>(),
Expand All @@ -433,6 +456,12 @@ public async Task Put_Should_Update_The_Device_Model()
this.mockTableClientFactory.Setup(c => c.GetDeviceTemplates())
.Returns(mockDeviceTemplatesTableClient.Object);

this.mockDeviceProvisioningServiceManager.Setup(c => c.CreateEnrollmentGroupFormModelAsync(
It.IsAny<string>(),
It.Is<string>(x => x == requestModel.Name),
It.IsAny<TwinCollection>()))
.ReturnsAsync(mockEnrollmentGroup.Object);

// Act
var result = await deviceModelsController.Put(requestModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.V10;
using AzureIoTHub.Portal.Shared.Models.V10.Device;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Devices;
Expand All @@ -20,31 +21,28 @@ public class DevicesControllerTests
{
private MockRepository mockRepository;

private Mock<IDeviceProvisioningServiceManager> mockProvisioningServiceManager;
private Mock<ILogger<DevicesController>> mockLogger;
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<IDeviceService> mockDeviceService;
private Mock<IDeviceTwinMapper<DeviceListItem, DeviceDetails>> mockDeviceTwinMapper;
private Mock<ILoraDeviceMethodManager> mockLoraDeviceMethodManager;
private Mock<IDeviceModelCommandMapper> mockDeviceModelCommandMapper;

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

this.mockProvisioningServiceManager = this.mockRepository.Create<IDeviceProvisioningServiceManager>();
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<DeviceListItem, DeviceDetails>>();
this.mockLoraDeviceMethodManager = this.mockRepository.Create<ILoraDeviceMethodManager>();
this.mockDeviceModelCommandMapper = this.mockRepository.Create<IDeviceModelCommandMapper>();
}

private DevicesController CreateDevicesController()
{
return new DevicesController(
this.mockLogger.Object,
this.mockDeviceService.Object,
this.mockProvisioningServiceManager.Object,
this.mockDeviceTwinMapper.Object);
}

Expand Down Expand Up @@ -143,7 +141,6 @@ public async Task CreateDeviceAsync_ModelNotValid_ShouldReturnBadRequest()
DeviceID = "aaa",
};


devicesController.ModelState.AddModelError("Key", "Device model is invalid");

// Act
Expand Down Expand Up @@ -226,5 +223,80 @@ public async Task Delete_StateUnderTest_ExpectedBehavior()
Assert.IsAssignableFrom<OkResult>(result);
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetEnrollmentCredentials_Should_Return_Enrollment_Credentials()
{
// Arrange
var devicesController = this.CreateDevicesController();
var mockRegistrationCredentials = new EnrollmentCredentials
{
RegistrationID = "aaa",
SymmetricKey = "dfhjkfdgh"
};

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

this.mockProvisioningServiceManager.Setup(c => c.GetEnrollmentCredentialsAsync("aaa", "bbb"))
.ReturnsAsync(mockRegistrationCredentials);

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

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

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

var okObjectResult = (OkObjectResult)response.Result;
Assert.IsNotNull(okObjectResult);
Assert.AreEqual(mockRegistrationCredentials, okObjectResult.Value);

this.mockRepository.VerifyAll();
}

[Test]
public async Task When_DeviceType_Property_Not_Exist_GetEnrollmentCredentials_Should_Return_BadRequest()
{
// Arrange
var devicesController = this.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 When_Device_Not_Exist_GetEnrollmentCredentials_Should_Return_NotFound()
{
// Arrange
var devicesController = this.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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ public class EdgeDevicesControllerTests
{
private MockRepository mockRepository;

private Mock<IDeviceProvisioningServiceManager> mockProvisioningServiceManager;
private Mock<IConfiguration> mockConfiguration;
private Mock<ILogger<EdgeDevicesController>> mockLogger;
private Mock<RegistryManager> mockRegistryManager;
private Mock<IConnectionStringManager> mockConnectionStringManager;
private Mock<IDeviceService> mockDeviceService;

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

this.mockProvisioningServiceManager = this.mockRepository.Create<IDeviceProvisioningServiceManager>();
this.mockConfiguration = this.mockRepository.Create<IConfiguration>();
this.mockLogger = this.mockRepository.Create<ILogger<EdgeDevicesController>>();
this.mockRegistryManager = this.mockRepository.Create<RegistryManager>();
this.mockConnectionStringManager = this.mockRepository.Create<IConnectionStringManager>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
}

Expand All @@ -45,8 +45,8 @@ private EdgeDevicesController CreateEdgeDevicesController()
this.mockConfiguration.Object,
this.mockLogger.Object,
this.mockRegistryManager.Object,
this.mockConnectionStringManager.Object,
this.mockDeviceService.Object);
this.mockDeviceService.Object,
this.mockProvisioningServiceManager.Object);
}

[Test]
Expand Down Expand Up @@ -108,9 +108,6 @@ public async Task When_Specifying_Id_Get_Should_Return_The_Edge_Device()
this.mockDeviceService.Setup(c => c.GetDeviceTwinWithModule(It.Is<string>(x => x == deviceId)))
.ReturnsAsync(twin);

this.mockConfiguration.Setup(c => c[It.Is<string>(x => x == "IoTDPS:ServiceEndpoint")])
.Returns("fake.local");

var edgeHubTwin = new Twin("edgeHub");
edgeHubTwin.Properties.Reported["clients"] = new[]
{
Expand Down Expand Up @@ -163,26 +160,54 @@ public async Task When_Not_Found_Should_Return_Not_Found()
}

[Test]
public async Task GetSymmetricKey_StateUnderTest_ExpectedBehavior()
public async Task GetEnrollmentCredentials_Should_Return_Enrollment_Credentials()
{
// Arrange
var edgeDevicesController = this.CreateEdgeDevicesController();
this.mockConnectionStringManager.Setup(c => c.GetSymmetricKey("aaa", "bbb"))
.ReturnsAsync("dfhjkfdgh");
var mockRegistrationCredentials = new EnrollmentCredentials
{
RegistrationID = "aaa",
SymmetricKey = "dfhjkfdgh"
};

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

this.mockProvisioningServiceManager.Setup(c => c.GetEnrollmentCredentialsAsync("aaa", "bbb"))
.ReturnsAsync(mockRegistrationCredentials);

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

// Act
var result = await edgeDevicesController.GetSymmetricKey("aaa", "bbb");
var response = await edgeDevicesController.GetCredentials("aaa");

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

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<string>(okObjectResult.Value);
Assert.AreEqual("dfhjkfdgh", okObjectResult.Value);
var okObjectResult = (OkObjectResult)response.Result;
Assert.IsNotNull(okObjectResult);
Assert.AreEqual(mockRegistrationCredentials, okObjectResult.Value);

this.mockRepository.VerifyAll();
}

[Test]
public async Task When_Device_Not_Exist_GetEnrollmentCredentials_Should_Return_NotFound()
{
// Arrange
var devicesController = this.CreateEdgeDevicesController();

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();
}
Expand Down
Loading