From c863c93a16857e27fcf0a9a9cd7b5e59b7fe6c6f Mon Sep 17 00:00:00 2001 From: Kevin BEAUGRAND <9513635+kbeaugrand@users.noreply.github.com> Date: Tue, 8 Feb 2022 12:06:35 +0100 Subject: [PATCH] Add Device model commands manager tests (#216) --- .../DeviceModelCommandsManagerTests.cs | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/AzureIoTHub.Portal.Server.Tests/Managers/DeviceModelCommandsManagerTests.cs diff --git a/src/AzureIoTHub.Portal.Server.Tests/Managers/DeviceModelCommandsManagerTests.cs b/src/AzureIoTHub.Portal.Server.Tests/Managers/DeviceModelCommandsManagerTests.cs new file mode 100644 index 000000000..ad2c075c4 --- /dev/null +++ b/src/AzureIoTHub.Portal.Server.Tests/Managers/DeviceModelCommandsManagerTests.cs @@ -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 mockTableClientFactory; + + [SetUp] + public void SetUp() + { + this.mockRepository = new MockRepository(MockBehavior.Strict); + + this.mockTableClientFactory = this.mockRepository.Create(); + } + + 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(); + var mockTableResponse = this.mockRepository.Create>(); + var mockEnumerator = this.mockRepository.Create>(); + 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(It.Is(x => x.StartsWith($"PartitionKey eq '{deviceModel}'")), + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .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(); + var mockTableResponse = this.mockRepository.Create>(); + var mockEnumerator = this.mockRepository.Create>(); + 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(It.Is(x => x.StartsWith($"PartitionKey eq '{deviceModel}'")), + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .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(); + } + } +}