From 6b677ce271bd223cdd92f2fc23abe95c09f0e7b0 Mon Sep 17 00:00:00 2001
From: Kevin BEAUGRAND <9513635+kbeaugrand@users.noreply.github.com>
Date: Wed, 9 Feb 2022 09:11:41 +0100
Subject: [PATCH] Fix #219 - Remove generic catch clause (#220)
---
.../AzureIoTHub.Portal.Server.Tests.csproj | 4 -
.../Controllers/GatewaysControllerTests.cs | 146 ++++++++++++++++++
.../Server/Controllers/GatewaysController.cs | 43 +++---
3 files changed, 164 insertions(+), 29 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();
+ }
+ }
+}
diff --git a/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs b/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs
index 457630d27..f82cc808d 100644
--- a/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs
+++ b/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs
@@ -57,37 +57,30 @@ public GatewaysController(
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List))]
public async Task Get()
{
- try
- {
- // don't contain tags
- IEnumerable edgeDevices = await this.devicesService.GetAllEdgeDevice();
+ // don't contain tags
+ IEnumerable edgeDevices = await this.devicesService.GetAllEdgeDevice();
- List newGatewayList = new ();
+ List newGatewayList = new ();
- foreach (Twin deviceTwin in edgeDevices)
- {
- var twin = this.devicesService.GetDeviceTwin(deviceTwin.DeviceId).Result;
+ foreach (Twin deviceTwin in edgeDevices)
+ {
+ var twin = this.devicesService.GetDeviceTwin(deviceTwin.DeviceId).Result;
- if (twin != null)
+ if (twin != null)
+ {
+ GatewayListItem gateway = new ()
{
- GatewayListItem gateway = new ()
- {
- DeviceId = deviceTwin.DeviceId,
- Status = twin.Status?.ToString(),
- Type = DeviceHelper.RetrieveTagValue(twin, "purpose"),
- NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin)
- };
-
- newGatewayList.Add(gateway);
- }
- }
+ DeviceId = deviceTwin.DeviceId,
+ Status = twin.Status?.ToString(),
+ Type = DeviceHelper.RetrieveTagValue(twin, "purpose"),
+ NbDevices = DeviceHelper.RetrieveConnectedDeviceCount(deviceTwin)
+ };
- return this.Ok(newGatewayList);
- }
- catch (Exception e)
- {
- return this.BadRequest(e.Message);
+ newGatewayList.Add(gateway);
+ }
}
+
+ return this.Ok(newGatewayList);
}
///