Skip to content

Commit

Permalink
rework exceptions on edge configuration (#721)
Browse files Browse the repository at this point in the history
* rework exceptions on edge configuration.controller

* add unitest on GetIoTEdgeConfigurations #720

* rework exception EdgeConfigurationsController.get(configurationid) #720

* add test on controller edge configuration

Co-authored-by: crib <[email protected]>
  • Loading branch information
ChristopheRib63 and crib authored May 18, 2022
1 parent 641f94b commit 71b32b4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace AzureIoTHub.Portal.Server.Tests.Unit.Services
{
using AzureIoTHub.Portal.Server.Exceptions;
using AzureIoTHub.Portal.Server.Services;
using FluentAssertions;
using Microsoft.Azure.Devices;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -58,6 +60,26 @@ public async Task GetIoTEdgeConfigsShouldReturnModulesConfigurations()
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetIoTEdgeConfigurationsShouldThrowInternalServerErrorExceptionWhenIssueOccurs()
{
// Arrange
var configsServices = CreateConfigsServices();
var iotEdgeConfiguration = new Configuration("bbb");

iotEdgeConfiguration.Content.ModulesContent.Add("test", new Dictionary<string, object>());
_ = this.mockRegistryManager.Setup(c => c.GetConfigurationsAsync(It.Is<int>(x => x == 0)))
.ThrowsAsync(new Exception("test"));

// Act
var act = () => configsServices.GetIoTEdgeConfigurations();

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();

this.mockRepository.VerifyAll();
}

[Test]
public async Task GetDevicesConfigsShouldReturnDeviceTwinConfigurations()
{
Expand Down Expand Up @@ -102,6 +124,23 @@ public async Task GetConfigItemStateUnderTestExpectedBehavior()
this.mockRepository.VerifyAll();
}

[Test]
public async Task GetConfigItemShouldThrowInternalServerErrorExceptionWhenIssueOccurs()
{
// Arrange
var configsServices = CreateConfigsServices();
const string id = "aaa";
_ = this.mockRegistryManager.Setup(c => c.GetConfigurationAsync(It.Is<string>(x => x == id)))
.ThrowsAsync(new Exception("test"));

// Act
var act = () => configsServices.GetConfigItem(id);

// Assert
_ = await act.Should().ThrowAsync<InternalServerErrorException>();
this.mockRepository.VerifyAll();
}

[TestCase("aaa", "aaa")]
[TestCase("AAA", "aaa")]
[TestCase("AAA AAA", "aaa-aaa")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<ConfigListItem> Get(string configurationID)
// Converts the object to a JObject to access its properties more easily
if (edgeAgentDesiredProperties is not JObject modObject)
{
throw new InvalidOperationException("Could not parse properties.desired.");
throw new InvalidOperationException($"Could not parse properties.desired for the configuration id {configurationID}");
}

// Adds regular modules to the list of modules
Expand Down
24 changes: 19 additions & 5 deletions src/AzureIoTHub.Portal/Server/Services/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace AzureIoTHub.Portal.Server.Services
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Server.Exceptions;
using Extensions;
using Microsoft.Azure.Devices;

Expand All @@ -24,9 +25,15 @@ public ConfigService(

public async Task<IEnumerable<Configuration>> GetIoTEdgeConfigurations()
{
var configurations = await this.registryManager.GetConfigurationsAsync(0);

return configurations.Where(c => c.Content.ModulesContent.Count > 0);
try
{
var configurations = await this.registryManager.GetConfigurationsAsync(0);
return configurations.Where(c => c.Content.ModulesContent.Count > 0);
}
catch (Exception ex)
{
throw new InternalServerErrorException("Unable to get IOT Edge configurations", ex);
}
}

public async Task<IEnumerable<Configuration>> GetDevicesConfigurations()
Expand All @@ -37,9 +44,16 @@ public async Task<IEnumerable<Configuration>> GetDevicesConfigurations()
.Where(c => c.Priority > 0 && c.Content.ModulesContent.Count == 0);
}

public Task<Configuration> GetConfigItem(string id)
public async Task<Configuration> GetConfigItem(string id)
{
return this.registryManager.GetConfigurationAsync(id);
try
{
return await this.registryManager.GetConfigurationAsync(id);
}
catch (Exception ex)
{
throw new InternalServerErrorException($"Unable to get the configuration for id {id}", ex);
}
}

public async Task DeleteConfiguration(string configId)
Expand Down

0 comments on commit 71b32b4

Please sign in to comment.