Skip to content

Commit

Permalink
Fix #219 - Remove generic catch clause (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand authored Feb 9, 2022
1 parent a4da4b3 commit 6b677ce
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,4 @@
<ProjectReference Include="..\AzureIoTHub.Portal\Server\AzureIoTHub.Portal.Server.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<IConfiguration> mockConfiguration;
private Mock<ILogger<GatewaysController>> mockLogger;
private Mock<RegistryManager> mockRegistryManager;
private Mock<IConnectionStringManager> mockConnectionStringManager;
private Mock<IDeviceService> mockDeviceService;

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

this.mockConfiguration = this.mockRepository.Create<IConfiguration>();
this.mockLogger = this.mockRepository.Create<ILogger<GatewaysController>>();
this.mockRegistryManager = this.mockRepository.Create<RegistryManager>();
this.mockConnectionStringManager = this.mockRepository.Create<IConnectionStringManager>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();
}

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<string>(c => c == twin.DeviceId)))
.ReturnsAsync(twin);

var gatewaysController = this.CreateGatewaysController();

// Act
var result = await gatewaysController.Get();

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkObjectResult>(result);
var okObjectResult = result as ObjectResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<List<GatewayListItem>>(okObjectResult.Value);
var gatewayList = okObjectResult.Value as List<GatewayListItem>;
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<OkObjectResult>(result);
var okObjectResult = result as ObjectResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);
Assert.IsNotNull(okObjectResult.Value);
Assert.IsAssignableFrom<string>(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();
}
}
}
43 changes: 18 additions & 25 deletions src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,30 @@ public GatewaysController(
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<GatewayListItem>))]
public async Task<IActionResult> Get()
{
try
{
// don't contain tags
IEnumerable<Twin> edgeDevices = await this.devicesService.GetAllEdgeDevice();
// don't contain tags
IEnumerable<Twin> edgeDevices = await this.devicesService.GetAllEdgeDevice();

List<GatewayListItem> newGatewayList = new ();
List<GatewayListItem> 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);
}

/// <summary>
Expand Down

0 comments on commit 6b677ce

Please sign in to comment.