From a58404ae54b6d6298a202381f2a740e19c7a836e Mon Sep 17 00:00:00 2001 From: "Beaugrand, Kevin" Date: Tue, 8 Feb 2022 19:03:10 +0100 Subject: [PATCH] Add unit tests --- .../AzureIoTHub.Portal.Server.Tests.csproj | 4 - .../Controllers/GatewaysControllerTests.cs | 146 ++++++++++++++++++ 2 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 src/AzureIoTHub.Portal.Server.Tests/Controllers/GatewaysControllerTests.cs diff --git a/src/AzureIoTHub.Portal.Server.Tests/AzureIoTHub.Portal.Server.Tests.csproj b/src/AzureIoTHub.Portal.Server.Tests/AzureIoTHub.Portal.Server.Tests.csproj index f44b9778c..504a9d254 100644 --- a/src/AzureIoTHub.Portal.Server.Tests/AzureIoTHub.Portal.Server.Tests.csproj +++ b/src/AzureIoTHub.Portal.Server.Tests/AzureIoTHub.Portal.Server.Tests.csproj @@ -22,8 +22,4 @@ - - - - diff --git a/src/AzureIoTHub.Portal.Server.Tests/Controllers/GatewaysControllerTests.cs b/src/AzureIoTHub.Portal.Server.Tests/Controllers/GatewaysControllerTests.cs new file mode 100644 index 000000000..034a4892e --- /dev/null +++ b/src/AzureIoTHub.Portal.Server.Tests/Controllers/GatewaysControllerTests.cs @@ -0,0 +1,146 @@ +using AzureIoTHub.Portal.Server.Controllers; +using AzureIoTHub.Portal.Server.Managers; +using AzureIoTHub.Portal.Server.Services; +using AzureIoTHub.Portal.Shared.Models; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Devices; +using Microsoft.Azure.Devices.Shared; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Moq; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace AzureIoTHub.Portal.Server.Tests.Controllers +{ + [TestFixture] + public class GatewaysControllerTests + { + private MockRepository mockRepository; + + private Mock mockConfiguration; + private Mock> mockLogger; + private Mock mockRegistryManager; + private Mock mockConnectionStringManager; + private Mock mockDeviceService; + + [SetUp] + public void SetUp() + { + this.mockRepository = new MockRepository(MockBehavior.Strict); + + this.mockConfiguration = this.mockRepository.Create(); + this.mockLogger = this.mockRepository.Create>(); + this.mockRegistryManager = this.mockRepository.Create(); + this.mockConnectionStringManager = this.mockRepository.Create(); + this.mockDeviceService = this.mockRepository.Create(); + } + + private GatewaysController CreateGatewaysController() + { + return new GatewaysController( + this.mockConfiguration.Object, + this.mockLogger.Object, + this.mockRegistryManager.Object, + this.mockConnectionStringManager.Object, + this.mockDeviceService.Object); + } + + [Test] + public async Task Get_StateUnderTest_ExpectedBehavior() + { + // Arrange + var twin = new Twin("aaa"); + twin.Tags["purpose"] = "test"; + + this.mockDeviceService.Setup(x => x.GetAllEdgeDevice()) + .ReturnsAsync(new[] + { + twin + }); + + this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is(c => c == twin.DeviceId))) + .ReturnsAsync(twin); + + var gatewaysController = this.CreateGatewaysController(); + + // Act + var result = await gatewaysController.Get(); + + // Assert + Assert.IsNotNull(result); + Assert.IsAssignableFrom(result); + var okObjectResult = result as ObjectResult; + + Assert.IsNotNull(okObjectResult); + Assert.AreEqual(200, okObjectResult.StatusCode); + Assert.IsNotNull(okObjectResult.Value); + Assert.IsAssignableFrom>(okObjectResult.Value); + var gatewayList = okObjectResult.Value as List; + Assert.IsNotNull(gatewayList); + Assert.AreEqual(1, gatewayList.Count); + var gateway = gatewayList[0]; + Assert.IsNotNull(gateway); + Assert.AreEqual(twin.DeviceId, gateway.DeviceId); + Assert.AreEqual("test", gateway.Type); + Assert.AreEqual(0, gateway.NbDevices); + + this.mockRepository.VerifyAll(); + } + + [Test] + public async Task GetSymmetricKey_StateUnderTest_ExpectedBehavior() + { + // Arrange + var gatewaysController = this.CreateGatewaysController(); + this.mockConnectionStringManager.Setup(c => c.GetSymmetricKey("aaa", "bbb")) + .ReturnsAsync("dfhjkfdgh"); + + // Act + var result = await gatewaysController.GetSymmetricKey("aaa", "bbb"); + + // Assert + Assert.IsNotNull(result); + Assert.IsAssignableFrom(result); + var okObjectResult = result as ObjectResult; + + Assert.IsNotNull(okObjectResult); + Assert.AreEqual(200, okObjectResult.StatusCode); + Assert.IsNotNull(okObjectResult.Value); + Assert.IsAssignableFrom(okObjectResult.Value); + Assert.AreEqual("dfhjkfdgh", okObjectResult.Value); + + this.mockRepository.VerifyAll(); + } + + [Test] + public async Task CreateGatewayAsync_StateUnderTest_ExpectedBehavior() + { + await Task.CompletedTask; + Assert.Inconclusive(); + } + + [Test] + public async Task UpdateDeviceAsync_StateUnderTest_ExpectedBehavior() + { + await Task.CompletedTask; + Assert.Inconclusive(); + } + + [Test] + public async Task DeleteDeviceAsync_StateUnderTest_ExpectedBehavior() + { + await Task.CompletedTask; + Assert.Inconclusive(); + } + + [Test] + public async Task ExecuteMethode_StateUnderTest_ExpectedBehavior() + { + await Task.CompletedTask; + Assert.Inconclusive(); + } + } +}