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

Add device model properties support #385

Merged
merged 6 commits into from
Mar 8, 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,234 @@
using AutoMapper;
using Azure;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Server.Controllers.V10;
using AzureIoTHub.Portal.Server.Entities;
using AzureIoTHub.Portal.Server.Factories;
using AzureIoTHub.Portal.Shared.Models.V10;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

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

private Mock<ILogger<DeviceModelPropertiesController>> mockLogger;
private Mock<IMapper> mockMapper;
private Mock<ITableClientFactory> mockTableClientFactory;
private Mock<TableClient> mockDeviceTemplatesTableClient;
private Mock<TableClient> mockDeviceModelPropertiesTableClient;

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

this.mockLogger = this.mockRepository.Create<ILogger<DeviceModelPropertiesController>>();
this.mockMapper = this.mockRepository.Create<IMapper>();
this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
this.mockDeviceTemplatesTableClient = this.mockRepository.Create<TableClient>();
this.mockDeviceModelPropertiesTableClient = this.mockRepository.Create<TableClient>();

}

private DeviceModelPropertiesController CreateDeviceModelPropertiesController()
{
return new DeviceModelPropertiesController(
this.mockLogger.Object,
this.mockMapper.Object,
this.mockTableClientFactory.Object);
}

[Test]
public async Task GetProperties_StateUnderTest_ExpectedBehavior()
{
// Arrange
var deviceModelPropertiesController = this.CreateDeviceModelPropertiesController();
var entity = SetupMockEntity();
var mockResponse = this.mockRepository.Create<Response>();

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

this.mockTableClientFactory.Setup(c => c.GetDeviceTemplateProperties())
.Returns(this.mockDeviceModelPropertiesTableClient.Object);

this.mockMapper.Setup(c => c.Map<DeviceProperty>(It.Is<DeviceModelProperty>(x => x.PartitionKey == entity.RowKey)))
.Returns((DeviceModelProperty x) => new DeviceProperty
{
Name = x.Name,
});

// Act
var response = await deviceModelPropertiesController.GetProperties(entity.RowKey);

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

Assert.NotNull(okObjectResult);
Assert.IsAssignableFrom<List<DeviceProperty>>(okObjectResult.Value);
var result = (List<DeviceProperty>)okObjectResult.Value;

Assert.NotNull(result);
Assert.AreEqual(1, result.Count());


this.mockRepository.VerifyAll();
}

[Test]
public async Task When_DeviceModel_NotExists_GetProperties_Should_Return_Http404()
{
// Arrange
var deviceModelPropertiesController = this.CreateDeviceModelPropertiesController();
SetupNotFoundEntity();

// Act
var response = await deviceModelPropertiesController.GetProperties(Guid.NewGuid().ToString());

// Assert
Assert.IsAssignableFrom<NotFoundResult>(response.Result);
this.mockRepository.VerifyAll();
}

[Test]
public async Task SetProperties_StateUnderTest_ExpectedBehavior()
{
// Arrange
var deviceModelPropertiesController = this.CreateDeviceModelPropertiesController();
var entity = SetupMockEntity();
var mockResponse = this.mockRepository.Create<Response>();
var existingProperty = Guid.NewGuid().ToString();

var properties = new[]
{
new DeviceProperty
{
DisplayName =Guid.NewGuid().ToString(),
Name = Guid.NewGuid().ToString()
}
};

this.mockDeviceModelPropertiesTableClient.Setup(c => c.QueryAsync<DeviceModelProperty>(
It.Is<string>(x => x == $"PartitionKey eq '{ entity.RowKey }'"),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(AsyncPageable<DeviceModelProperty>.FromPages(new[]
{
Page<DeviceModelProperty>.FromValues(new DeviceModelProperty[]
{
new DeviceModelProperty
{
RowKey = existingProperty,
PartitionKey = entity.RowKey,
}
}, null, mockResponse.Object)
}));

this.mockDeviceModelPropertiesTableClient.Setup(c => c.AddEntityAsync(
It.Is<DeviceModelProperty>(x => x.PartitionKey == entity.RowKey),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

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

this.mockMapper.Setup(c => c.Map(
It.IsAny<DeviceProperty>(),
It.IsAny<Action<IMappingOperationOptions<object, DeviceModelProperty>>>()))
.Returns((DeviceProperty x, Action<IMappingOperationOptions<object, DeviceModelProperty>> a) => new DeviceModelProperty
{
Name = x.Name,
PartitionKey = entity.RowKey
});

this.mockTableClientFactory.Setup(c => c.GetDeviceTemplateProperties())
.Returns(this.mockDeviceModelPropertiesTableClient.Object);

// Act
var result = await deviceModelPropertiesController.SetProperties(entity.RowKey, properties);

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

[Test]
public async Task When_DeviceModel_NotExists_SetProperties_Should_Return_Http404()
{
// Arrange
var deviceModelPropertiesController = this.CreateDeviceModelPropertiesController();
SetupNotFoundEntity();

// Act
var result = await deviceModelPropertiesController.SetProperties(Guid.NewGuid().ToString(), null);

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

private TableEntity SetupMockEntity()
{
var mockResponse = this.mockRepository.Create<Response<TableEntity>>();
var modelId = Guid.NewGuid().ToString();
var entity = new TableEntity("0", modelId);

this.mockDeviceTemplatesTableClient.Setup(c => c.GetEntityAsync<TableEntity>(
It.Is<string>(p => p == "0"),
It.Is<string>(k => k == modelId),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

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

return entity;
}

private void SetupNotFoundEntity()
{
this.mockDeviceTemplatesTableClient.Setup(c => c.GetEntityAsync<TableEntity>(
It.Is<string>(p => p == "0"),
It.IsAny<string>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Throws(new RequestFailedException(StatusCodes.Status404NotFound, "Not Found"));

this.mockTableClientFactory.Setup(c => c.GetDeviceTemplates())
.Returns(mockDeviceTemplatesTableClient.Object);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static MockedRequest RespondJson<T>(this MockedRequest request, T content

return request;
}

public static MockedRequest RespondJsonAsync<T>(this MockedRequest request, Task<T> content)
{
request.Respond(req =>
Expand All @@ -47,6 +48,19 @@ public static MockedRequest RespondJsonAsync<T>(this MockedRequest request, Task
return request;
}

public static MockedRequest RespondText(this MockedRequest request, string content)
{
request.Respond(req =>
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(content);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return response;
});

return request;
}

public static MockedRequest RespondJson<T>(this MockedRequest request, Func<T> contentProvider)
{
request.Respond(req =>
Expand All @@ -59,5 +73,12 @@ public static MockedRequest RespondJson<T>(this MockedRequest request, Func<T> c

return request;
}

public static IMockedRequest AddRequestExpectation(this IMockedRequest request, MockHttpMessageHandler mockHttpClient)
{
mockHttpClient.AddRequestExpectation(request);

return request;
}
}
}
Loading