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

Filter devices at iot hub level #270

Merged
merged 3 commits into from
Feb 15, 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
Expand Up @@ -11,6 +11,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Controllers
Expand Down Expand Up @@ -57,7 +58,9 @@ public async Task GetAllDeviceConcentrator_WithNoArgument_ReturnConcentratorList
DeviceType = "LoRa Concentrator"
};

this.mockDeviceService.Setup(x => x.GetAllDevice())
this.mockDeviceService.Setup(x => x.GetAllDevice(
It.Is<string>(x => x == "LoRa Concentrator"),
It.Is<string>(x => string.IsNullOrEmpty(x))))
.ReturnsAsync(new[]
{
twin
Expand All @@ -79,11 +82,10 @@ public async Task GetAllDeviceConcentrator_WithNoArgument_ReturnConcentratorList
Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<List<Concentrator>>(okObjectResult.Value);
var deviceList = okObjectResult.Value as List<Concentrator>;
var deviceList = okObjectResult.Value as IEnumerable<Concentrator>;
Assert.IsNotNull(deviceList);
Assert.AreEqual(1, deviceList.Count);
var device = deviceList[0];
Assert.AreEqual(1, deviceList.Count());
var device = deviceList.First();
Assert.IsNotNull(device);
Assert.AreEqual(twin.DeviceId, device.DeviceId);
Assert.AreEqual("LoRa Concentrator", device.DeviceType);
Expand Down Expand Up @@ -136,10 +138,10 @@ public async Task CreateDeviceAsync_With_Valid_Argument_Should_Return_OkResult()
{
// Arrange
var concentratorController = this.CreateController();
var concentrator = new Concentrator
var concentrator = new Concentrator
{
DeviceId = "4512457896451156",
LoraRegion = Guid.NewGuid().ToString() ,
LoraRegion = Guid.NewGuid().ToString(),
IsEnabled = true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public async Task GetList_StateUnderTest_ExpectedBehavior()
TwinCollection twinCollection = new TwinCollection();
twinCollection["deviceType"] = "test";

this.mockDeviceService.Setup(c => c.GetAllDevice())
this.mockDeviceService.Setup(c => c.GetAllDevice(
It.Is<string>(x => string.IsNullOrEmpty(x)),
It.Is<string>(x => x == "LoRa Concentrator")))
.ReturnsAsync(Enumerable.Range(0, 100).Select(x => new Twin
{
DeviceId = x.ToString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ public async Task Delete_Should_Remove_The_Entity_Commands_And_Avatar()
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

this.mockDeviceService.Setup(c => c.GetAllDevice())
this.mockDeviceService.Setup(c => c.GetAllDevice(
It.Is<string>(x => string.IsNullOrEmpty(x)),
It.Is<string>(x => string.IsNullOrEmpty(x))))
.ReturnsAsync(new List<Twin>());

this.mockDeviceModelImageManager.Setup(c => c.DeleteDeviceModelImageAsync(It.Is<string>(x => x == id)))
Expand Down
297 changes: 297 additions & 0 deletions src/AzureIoTHub.Portal.Server.Tests/Services/DeviceServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
using AzureIoTHub.Portal.Server.Services;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Moq;
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace AzureIoTHub.Portal.Server.Tests.Services
{
[TestFixture]
public class DeviceServiceTests
{
private MockRepository mockRepository;

private Mock<RegistryManager> mockRegistryManager;
private Mock<ServiceClient> mockServiceClient;

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

this.mockRegistryManager = this.mockRepository.Create<RegistryManager>();
this.mockServiceClient = this.mockRepository.Create<ServiceClient>();
}

private DeviceService CreateService()
{
return new DeviceService(
this.mockRegistryManager.Object,
this.mockServiceClient.Object);
}

[Test]
public async Task GetAllEdgeDevice_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
var mockQuery = this.mockRepository.Create<IQuery>();

var resultReturned = false;

mockQuery.SetupGet(c => c.HasMoreResults)
.Returns(!resultReturned);

mockQuery.Setup(c => c.GetNextAsTwinAsync())
.ReturnsAsync(new Twin[]
{
new Twin(Guid.NewGuid().ToString())
});

this.mockRegistryManager.Setup(c => c.CreateQuery(
It.Is<string>(x => x == "SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeHub' GROUP BY deviceId"),
It.Is<int>(x => x == 10)))
.Returns(mockQuery.Object);

// Act
var result = await service.GetAllEdgeDevice();

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count());
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetAllDevice_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
var mockQuery = this.mockRepository.Create<IQuery>();

var resultReturned = false;

mockQuery.SetupGet(c => c.HasMoreResults)
.Returns(!resultReturned);

mockQuery.Setup(c => c.GetNextAsTwinAsync())
.ReturnsAsync(new Twin[]
{
new Twin(Guid.NewGuid().ToString())
});

this.mockRegistryManager.Setup(c => c.CreateQuery(
It.Is<string>(x => x == "SELECT * FROM devices WHERE devices.capabilities.iotEdge = false")))
.Returns(mockQuery.Object);

// Act
var result = await service.GetAllDevice();

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count());
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetDevice_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
string deviceId = Guid.NewGuid().ToString();
var expected = new Device();

this.mockRegistryManager.Setup(c => c.GetDeviceAsync(It.Is<string>(x => x == deviceId)))
.ReturnsAsync(expected);

// Act
var result = await service.GetDevice(deviceId);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expected, result);
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetDeviceTwin_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
string deviceId = Guid.NewGuid().ToString();
var expected = new Twin();

this.mockRegistryManager.Setup(c => c.GetTwinAsync(It.Is<string>(x => x == deviceId)))
.ReturnsAsync(expected);

// Act
var result = await service.GetDeviceTwin(deviceId);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expected, result);
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetDeviceTwinWithModule_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
string deviceId = Guid.NewGuid().ToString();

var mockQuery = this.mockRepository.Create<IQuery>();
var resultReturned = false;

mockQuery.SetupGet(c => c.HasMoreResults)
.Returns(!resultReturned);

mockQuery.Setup(c => c.GetNextAsTwinAsync())
.ReturnsAsync(new Twin[]
{
new Twin(Guid.NewGuid().ToString())
});

this.mockRegistryManager.Setup(c => c.CreateQuery(
It.Is<string>(x => x == $"SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeAgent' AND deviceId in ['{deviceId}']")))
.Returns(mockQuery.Object);

// Act
var result = await service.GetDeviceTwinWithModule(deviceId);

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

[TestCase(false, DeviceStatus.Enabled)]
[TestCase(true, DeviceStatus.Enabled)]
[TestCase(false, DeviceStatus.Disabled)]
[TestCase(true, DeviceStatus.Disabled)]
public async Task CreateDeviceWithTwin_StateUnderTest_ExpectedBehavior(bool isEdge, DeviceStatus isEnabled)
{
// Arrange
var service = this.CreateService();
string deviceId = Guid.NewGuid().ToString();
Twin twin = new Twin();

this.mockRegistryManager
.Setup(c => c.AddDeviceWithTwinAsync(It.Is<Device>(x => x.Id == deviceId
&& x.Capabilities.IotEdge == isEdge
&& x.Status == isEnabled),
It.Is<Twin>(x => x == twin)))
.ReturnsAsync(new BulkRegistryOperationResult
{
IsSuccessful = true
});

// Act
var result = await service.CreateDeviceWithTwin(
deviceId,
isEdge,
twin,
isEnabled);

// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.IsSuccessful);
this.mockRepository.VerifyAll();
}

[Test]
public async Task DeleteDevice_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
string deviceId = Guid.NewGuid().ToString();

this.mockRegistryManager.Setup(c => c.RemoveDeviceAsync(It.Is<string>(x => x == deviceId)))
.Returns(Task.CompletedTask);

// Act
await service.DeleteDevice(deviceId);

// Assert
this.mockRepository.VerifyAll();
}

[Test]
public async Task UpdateDevice_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
var device = new Device();

this.mockRegistryManager.Setup(c => c.UpdateDeviceAsync(It.Is<Device>(x => x == device)))
.ReturnsAsync(new Device());

// Act
var result = await service.UpdateDevice(device);

// Assert
Assert.IsNotNull(result);
Assert.AreNotEqual(result, device);
this.mockRepository.VerifyAll();
}

[Test]
public async Task UpdateDeviceTwin_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
var deviceId = Guid.NewGuid().ToString();

var twin = new Twin()
{
ETag = Guid.NewGuid().ToString(),
};

this.mockRegistryManager.Setup(c => c.UpdateTwinAsync(
It.Is<string>(x => x == deviceId),
It.Is<Twin>(x => x == twin),
It.Is<string>(x => x == twin.ETag)))
.ReturnsAsync(new Twin());

// Act
var result = await service.UpdateDeviceTwin(deviceId, twin);

// Assert
Assert.IsNotNull(result);
Assert.AreNotEqual(result, twin);
this.mockRepository.VerifyAll();
}

[Test]
public async Task ExecuteC2DMethod_StateUnderTest_ExpectedBehavior()
{
// Arrange
var service = this.CreateService();
var deviceId = Guid.NewGuid().ToString();

CloudToDeviceMethod method = new CloudToDeviceMethod(Guid.NewGuid().ToString());

this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync(
It.Is<string>(x => x == deviceId),
It.Is<string>(x => x == "$edgeAgent"),
It.Is<CloudToDeviceMethod>(x => x == method),
It.IsAny<CancellationToken>()))
.ReturnsAsync(new CloudToDeviceMethodResult
{
Status = 200
});

// Act
var result = await service.ExecuteC2DMethod(deviceId, method);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(200, result.Status);
this.mockRepository.VerifyAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,9 @@ public DevicesController(
[HttpGet]
public async Task<IEnumerable<DeviceListItem>> Get()
{
// Gets all the twins from this devices
var items = await this.devicesService.GetAllDevice();
List<Twin> listTwin = new List<Twin>();
var itemFilter = items.Where(item => item.Tags.Contains("deviceType") && item.Tags["deviceType"] != "LoRa Concentrator");
var items = await this.devicesService.GetAllDevice(excludeDeviceType: "LoRa Concentrator");

foreach (var item in itemFilter)
{
listTwin.Add(item);
}

return listTwin.Select(this.deviceTwinMapper.CreateDeviceListItem);
return items.Select(this.deviceTwinMapper.CreateDeviceListItem);
}

/// <summary>
Expand Down
Loading