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 commands manager tests #216

Merged
merged 2 commits into from
Feb 8, 2022
Merged
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,132 @@
using Azure;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Server.Factories;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Shared.Models;
using AzureIoTHub.Portal.Shared.Models.Device;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;

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

private Mock<ITableClientFactory> mockTableClientFactory;

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

this.mockTableClientFactory = this.mockRepository.Create<ITableClientFactory>();
}

private DeviceModelCommandsManager CreateManager()
{
return new DeviceModelCommandsManager(
this.mockTableClientFactory.Object);
}

[Test]
public void RetrieveCommands_StateUnderTest_ExpectedBehavior()
{
// Arrange
var manager = this.CreateManager();
string deviceModel = "aaa";
bool resultReturned = false;

var entityMock = new TableEntity(deviceModel, "bbbb");
entityMock.TryAdd(nameof(Command.Frame), "ADETRDTHDFG");

var mockTable = this.mockRepository.Create<TableClient>();
var mockTableResponse = this.mockRepository.Create<Pageable<TableEntity>>();
var mockEnumerator = this.mockRepository.Create<IEnumerator<TableEntity>>();
mockEnumerator.Setup(x => x.Dispose()).Callback(() => { });
mockEnumerator.Setup(x => x.MoveNext()).Returns(() =>
{
resultReturned = !resultReturned;
return resultReturned;
});

mockEnumerator.Setup(x => x.Current).Returns(entityMock);

mockTableResponse.Setup(x => x.GetEnumerator())
.Returns(mockEnumerator.Object);

mockTable.Setup(x => x.Query<TableEntity>(It.Is<string>(x => x.StartsWith($"PartitionKey eq '{deviceModel}'")),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(mockTableResponse.Object);

this.mockTableClientFactory.Setup(x => x.GetDeviceCommands())
.Returns(mockTable.Object);

// Act
var result = manager.RetrieveCommands(deviceModel);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Assert.AreEqual("bbbb", result[0].CommandId);
Assert.AreEqual("ADETRDTHDFG", result[0].Frame);

this.mockRepository.VerifyAll();
}

[Test]
public void RetrieveDeviceModelCommands_StateUnderTest_ExpectedBehavior()
{
// Arrange
var manager = this.CreateManager();
string deviceModel = "aaa";
bool resultReturned = false;

var entityMock = new TableEntity(deviceModel, "bbbb");
entityMock.TryAdd(nameof(DeviceModelCommand.Frame), "ADETRDTHDFG");
entityMock.TryAdd(nameof(DeviceModelCommand.Port), 125);

var mockTable = this.mockRepository.Create<TableClient>();
var mockTableResponse = this.mockRepository.Create<Pageable<TableEntity>>();
var mockEnumerator = this.mockRepository.Create<IEnumerator<TableEntity>>();
mockEnumerator.Setup(x => x.Dispose()).Callback(() => { });
mockEnumerator.Setup(x => x.MoveNext()).Returns(() =>
{
resultReturned = !resultReturned;
return resultReturned;
});

mockEnumerator.Setup(x => x.Current).Returns(entityMock);

mockTableResponse.Setup(x => x.GetEnumerator())
.Returns(mockEnumerator.Object);

mockTable.Setup(x => x.Query<TableEntity>(It.Is<string>(x => x.StartsWith($"PartitionKey eq '{deviceModel}'")),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(mockTableResponse.Object);

this.mockTableClientFactory.Setup(x => x.GetDeviceCommands())
.Returns(mockTable.Object);

// Act
var result = manager.RetrieveDeviceModelCommands(deviceModel);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Assert.AreEqual("bbbb", result[0].Name);
Assert.AreEqual(125, result[0].Port);
Assert.AreEqual("ADETRDTHDFG", result[0].Frame);

this.mockRepository.VerifyAll();
}
}
}