Skip to content

Commit

Permalink
Feature/#1282_-_Edge listing should use database (#1335)
Browse files Browse the repository at this point in the history
* resolve #1282

* fix unit test

* fix

* fix test

* fix error on search type

* fix test

* add unit test

* add more test

* update test

* fix test

* add new line on test

* update

* add device predicate

* fix test
  • Loading branch information
Sben65 authored Oct 14, 2022
1 parent 0c886b2 commit 18aaf95
Show file tree
Hide file tree
Showing 12 changed files with 825 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Setup()
public void EdgeDeviceListPageShouldShowEdgeDevices()
{
// Arrange
var expectedUrl = "api/edge/devices?pageSize=10&searchText=&searchStatus=&searchType=";
var expectedUrl = "api/edge/devices?pageNumber=0&pageSize=10&searchText=&searchStatus=&orderBy=";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(expectedUrl))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>
{
Expand All @@ -63,7 +63,7 @@ public void EdgeDeviceListPageShouldShowEdgeDevices()
public void EdgeDeviceListPageShouldShowNoContentWhenNoEdgeDevices()
{
// Arrange
var expectedUrl = "api/edge/devices?pageSize=10&searchText=&searchStatus=&searchType=";
var expectedUrl = "api/edge/devices?pageNumber=0&pageSize=10&searchText=&searchStatus=&orderBy=";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(expectedUrl))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>());

Expand All @@ -81,7 +81,7 @@ public void EdgeDeviceListPageShouldShowNoContentWhenNoEdgeDevices()
public void EdgeDeviceListPageShouldShowNoContentWhenProblemDetailsExceptionOccurs()
{
// Arrange
var expectedUrl = "api/edge/devices?pageSize=10&searchText=&searchStatus=&searchType=";
var expectedUrl = "api/edge/devices?pageNumber=0&pageSize=10&searchText=&searchStatus=&orderBy=";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(expectedUrl))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

Expand All @@ -99,7 +99,7 @@ public void EdgeDeviceListPageShouldShowNoContentWhenProblemDetailsExceptionOccu
public void EdgeDeviceListPageShouldResetOnClickOnReset()
{
// Arrange
var expectedUrl = "api/edge/devices?pageSize=10&searchText=&searchStatus=&searchType=";
var expectedUrl = "api/edge/devices?pageNumber=0&pageSize=10&searchText=&searchStatus=&orderBy=";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(expectedUrl))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>
{
Expand Down Expand Up @@ -129,7 +129,7 @@ public void ClickToItemShouldRedirectToEdgeDetailsPage()
// Arrange
var deviceId = Guid.NewGuid().ToString();

var expectedUrl = "api/edge/devices?pageSize=10&searchText=&searchStatus=&searchType=";
var expectedUrl = "api/edge/devices?pageNumber=0&pageSize=10&searchText=&searchStatus=&orderBy=";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(expectedUrl))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>
{
Expand Down Expand Up @@ -158,8 +158,8 @@ public void ClickToItemShouldRedirectToEdgeDetailsPage()
public void ClickOnRefreshShouldReloadEdgeDevices()
{
// Arrange
var expectedUrl = "api/edge/devices?pageSize=10&searchText=&searchStatus=&searchType=";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(expectedUrl))

_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(It.IsAny<string>()))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>
{
Items = Array.Empty<IoTEdgeListItem>()
Expand All @@ -168,6 +168,8 @@ public void ClickOnRefreshShouldReloadEdgeDevices()
var cut = RenderComponent<EdgeDeviceListPage>();

// Act
cut.WaitForElement("#sortDeviceId").Click();
cut.WaitForElement("#sortDeviceId").Click();
cut.WaitForElement("#tableRefreshButton").Click();

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v10
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Server.Controllers.V10;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Devices;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Azure.Devices.Common.Exceptions;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Models.v10;
using Moq;
Expand Down Expand Up @@ -58,49 +58,33 @@ public async Task GetAllDeviceShouldReturnOkResult()
// Arrange
var edgeDeviceController = CreateEdgeDevicesController();

var deviceTwinListPage = new PaginationResult<Twin>();

_ = this.mockDeviceService
.Setup(x => x.GetAllEdgeDevice(It.Is<string>(c => c.Equals("aaa", StringComparison.Ordinal)),
It.Is<string>(c => c.Equals("bbb", StringComparison.Ordinal)),
It.Is<bool>(c => c.Equals(true)), It.IsAny<string>(), It.IsAny<int>()))
.ReturnsAsync(deviceTwinListPage);
var expectedPaginedEdgeDevice = new PaginatedResult<IoTEdgeListItem>()
{
Data = Enumerable.Range(0, 10).Select(x => new IoTEdgeListItem()
{
DeviceId = FormattableString.Invariant($"{x}"),
}).ToList(),
TotalCount = 100,
PageSize = 10,
TotalPages = 10,
CurrentPage = 0
};

_ = this.mockEdgeDeviceService
.Setup(x => x.GetEdgeDevicesPage(deviceTwinListPage, It.IsAny<IUrlHelper>(),
It.Is<string>(c => c.Equals("bbb", StringComparison.Ordinal)), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<int>()))
.Returns(new PaginationResult<IoTEdgeListItem>()
{
Items = Enumerable.Range(0, 10).Select(x => new IoTEdgeListItem()
{
DeviceId = FormattableString.Invariant($"{x}"),
}),
NextPage = Guid.NewGuid().ToString(),
TotalItems = 100
});
.Setup(x => x.GetEdgeDevicesPage(
It.IsAny<string>(),
It.IsAny<bool?>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<string[]>()))
.ReturnsAsync(expectedPaginedEdgeDevice);

// Act
var result = await edgeDeviceController.Get(continuationToken: "aaa",
searchText: "bbb",
searchStatus: true,
pageSize: 2);
var result = await edgeDeviceController.Get();

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

var okObjectResult = result as ObjectResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);

Assert.IsAssignableFrom<PaginationResult<IoTEdgeListItem>>(okObjectResult.Value);

var paginationResult = okObjectResult.Value as PaginationResult<IoTEdgeListItem>;

Assert.IsNotNull(paginationResult);
Assert.AreEqual(10, paginationResult.Items.Count());
Assert.AreEqual(expectedPaginedEdgeDevice.Data.Count, result.Items.Count());

this.mockRepository.VerifyAll();
}
Expand Down Expand Up @@ -174,7 +158,7 @@ public async Task CreateEdgeDeviceAsyncShouldReturnOk()

_ = this.mockEdgeDeviceService
.Setup(x => x.CreateEdgeDevice(It.Is<IoTEdgeDevice>(c => c.DeviceId.Equals(edgeDevice.DeviceId, StringComparison.Ordinal))))
.ReturnsAsync(new BulkRegistryOperationResult() { IsSuccessful = true });
.ReturnsAsync(edgeDevice);

// Act
var result = await edgeDeviceController.CreateEdgeDeviceAsync(edgeDevice);
Expand All @@ -189,11 +173,11 @@ public async Task CreateEdgeDeviceAsyncShouldReturnOk()
Assert.AreEqual(200, okObjectResult.StatusCode);

Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<BulkRegistryOperationResult>(okObjectResult.Value);
Assert.IsAssignableFrom<IoTEdgeDevice>(okObjectResult.Value);

var bulkOperationResult = okObjectResult.Value as BulkRegistryOperationResult;
Assert.IsNotNull(bulkOperationResult);
Assert.AreEqual(true, bulkOperationResult.IsSuccessful);
var edgeDeviceObject = okObjectResult.Value as IoTEdgeDevice;
Assert.IsNotNull(edgeDeviceObject);
Assert.AreEqual(edgeDevice.DeviceId, edgeDeviceObject.DeviceId);

this.mockRepository.VerifyAll();
}
Expand All @@ -209,28 +193,15 @@ public async Task UpdateDeviceAsyncShouldReturnOkResult()
DeviceId = Guid.NewGuid().ToString(),
};

var deviceTwin = new Twin(edgeDevice.DeviceId);

_ = this.mockEdgeDeviceService
.Setup(x => x.UpdateEdgeDevice(It.Is<IoTEdgeDevice>(c => c.DeviceId.Equals(edgeDevice.DeviceId, StringComparison.Ordinal))))
.ReturnsAsync(deviceTwin);
.ReturnsAsync(edgeDevice);

// Act
var result = await edgeDeviceController.UpdateDeviceAsync(edgeDevice);

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

var okObjectResult = result as ObjectResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);

Assert.IsNotNull(okObjectResult.Value);

Assert.IsAssignableFrom<Twin>(okObjectResult.Value);
Assert.AreEqual(deviceTwin, okObjectResult.Value);

this.mockRepository.VerifyAll();
}
Expand All @@ -242,7 +213,7 @@ public async Task DeleteDeviceAsyncStateUnderTestExpectedBehavior()
var edgeDevicesController = CreateEdgeDevicesController();
var deviceId = Guid.NewGuid().ToString();

_ = this.mockDeviceService.Setup(c => c.DeleteDevice(It.Is<string>(x => x == deviceId)))
_ = this.mockEdgeDeviceService.Setup(c => c.DeleteEdgeDeviceAsync(It.Is<string>(x => x == deviceId)))
.Returns(Task.CompletedTask);

_ = this.mockLogger.Setup(c => c.Log(
Expand Down Expand Up @@ -325,7 +296,7 @@ public async Task GetEnrollmentCredentialsShouldReturnEnrollmentCredentials()

var deviceId = Guid.NewGuid().ToString();

_ = this.mockEdgeDeviceService
_ = this.mockDeviceService
.Setup(x => x.GetEdgeDeviceCredentials(It.Is<string>(c => c.Equals(deviceId, StringComparison.Ordinal))))
.ReturnsAsync(new EnrollmentCredentials());

Expand Down Expand Up @@ -354,7 +325,7 @@ public async Task WhenDeviceDoesNotExistGetEnrollmentCredentialsShouldThrowAnExc

var deviceId = Guid.NewGuid().ToString();

_ = this.mockEdgeDeviceService
_ = this.mockDeviceService
.Setup(x => x.GetEdgeDeviceCredentials(It.Is<string>(c => c.Equals(deviceId, StringComparison.Ordinal))))
.ThrowsAsync(new ResourceNotFoundException(""));

Expand Down
Loading

0 comments on commit 18aaf95

Please sign in to comment.