Skip to content

Commit

Permalink
Move http calls from UI layer to dedicated client services (#898)
Browse files Browse the repository at this point in the history
* Move http calls from DeleteDeviceConfiguration #858


Move http calls from DeleteDeviceConfiguration #858

* Move http calls from DeviceConfigurationListPage #858

* Move http calls from Create and Detail device config pages #858

* Update unit tests on DeviceConfigurationsClientServiceTests

* Add GetDeviceConfigurationMetricsShouldReturnDeviceConfigurationMetrics unit test

* Add DeviceTagSettingsClientServiceTests

* Add DeviceModelsClientServiceTests

* Move http calls from DeviceTagsSetting page #858

* Move http calls from Concentrators listing page #858

* Move http calls from Concentrator Delete page #858

* Move http calls from Concentrator Create page #858

* Move http calls from Concentrator Details page #858

* Move http calls from Devices pages #858

* Add unit tests on client services

* Move http calls from Device Models pages #858

* Move http calls from Edge Devices pages #858

* Add unit tests on EdgeDeviceClientServiceTests

* Move http calls from Edge Devices pages #858
  • Loading branch information
hocinehacherouf authored Jul 7, 2022
1 parent 526137f commit 13e48be
Show file tree
Hide file tree
Showing 82 changed files with 3,221 additions and 1,994 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="Blazored.LocalStorage.TestExtensions" Version="4.2.0" />
<PackageReference Include="bunit" Version="1.9.8" />
<PackageReference Include="bunit.core" Version="1.9.8" />
Expand Down
2 changes: 2 additions & 0 deletions src/AzureIoTHub.Portal.Server.Tests.Unit/BlazorUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public abstract class BlazorUnitTest : TestContextWrapper, IDisposable

protected virtual MockHttpMessageHandler MockHttpClient { get; set; }

protected virtual AutoFixture.Fixture Fixture { get; } = new();

[SetUp]
public virtual void Setup()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages.Configurations
{
using System;
using System.Net.Http;
using System.Threading;
using Models.v10;
using Helpers;
using Bunit;
using Bunit.TestDoubles;
using Client.Exceptions;
Expand All @@ -17,56 +14,57 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages.Configurations
using Moq;
using MudBlazor;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using AzureIoTHub.Portal.Client.Pages.EdgeModels;
using Client.Services;

[TestFixture]
public class ConfigDetailTests : BlazorUnitTest
{
private Mock<IDialogService> mockDialogService;
private Mock<IEdgeDeviceConfigurationsClientService> mockEdgeDeviceConfigurationsClientService;

private readonly string mockConfigurationId = Guid.NewGuid().ToString();

public override void Setup()
{
base.Setup();

this.mockDialogService = MockRepository.Create<IDialogService>();
this.mockEdgeDeviceConfigurationsClientService = MockRepository.Create<IEdgeDeviceConfigurationsClientService>();

_ = Services.AddSingleton(this.mockDialogService.Object);
_ = Services.AddSingleton(this.mockEdgeDeviceConfigurationsClientService.Object);
}

[TestCase]
[Test]
public void ReturnButtonMustNavigateToPreviousPage()
{
// Arrange
_ = MockHttpClient
.When(HttpMethod.Get, $"/api/edge/configurations/{this.mockConfigurationId}")
.RespondJson(new ConfigListItem());
_ = this.mockEdgeDeviceConfigurationsClientService
.Setup(service => service.GetDeviceConfiguration(this.mockConfigurationId))
.ReturnsAsync(new ConfigListItem());

_ = Services.AddSingleton(new PortalSettings { IsLoRaSupported = false });

var cut = RenderComponent<ConfigDetail>(ComponentParameter.CreateParameter("ConfigurationID", this.mockConfigurationId));
Thread.Sleep(500);

var returnButton = cut.WaitForElement("#returnButton");

var mockNavigationManager = Services.GetRequiredService<FakeNavigationManager>();

// Act
returnButton.Click();

// Assert
cut.WaitForState(() => mockNavigationManager.Uri.EndsWith("/edge/configurations", StringComparison.OrdinalIgnoreCase));
MockHttpClient.VerifyNoOutstandingExpectation();
cut.WaitForAssertion(() => Services.GetRequiredService<FakeNavigationManager>().Uri.Should().EndWith("/edge/configurations"));
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[TestCase]
[Test]
public void ConfigDetailShouldProcessProblemDetailsExceptionWhenIssueOccursOnGettingConfiguration()
{
// Arrange
_ = MockHttpClient
.When(HttpMethod.Get, $"/api/edge/configurations/{this.mockConfigurationId}")
.Throw(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));
_ = this.mockEdgeDeviceConfigurationsClientService
.Setup(service => service.GetDeviceConfiguration(this.mockConfigurationId))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

_ = Services.AddSingleton(new PortalSettings { IsLoRaSupported = false });

Expand All @@ -76,7 +74,7 @@ public void ConfigDetailShouldProcessProblemDetailsExceptionWhenIssueOccursOnGet

// Assert
_ = cut.Markup.Should().NotBeEmpty();
MockHttpClient.VerifyNoOutstandingExpectation();
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages.Configurations
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using AzureIoTHub.Portal.Client.Pages.EdgeModels;
using Bunit;
using Bunit.TestDoubles;
using Client.Exceptions;
using Client.Models;
using Client.Services;
using FluentAssertions;
using Helpers;
using Microsoft.Extensions.DependencyInjection;
using Models.v10;
using Moq;
using NUnit.Framework;
using RichardSzalay.MockHttp;

[TestFixture]
public class ConfigsTests : BlazorUnitTest
{
private Mock<IEdgeDeviceConfigurationsClientService> mockEdgeDeviceConfigurationsClientService;

public override void Setup()
{
base.Setup();

this.mockEdgeDeviceConfigurationsClientService = MockRepository.Create<IEdgeDeviceConfigurationsClientService>();


_ = Services.AddSingleton(this.mockEdgeDeviceConfigurationsClientService.Object);
_ = Services.AddSingleton(new PortalSettings { IsLoRaSupported = false });
}

[TestCase]
[Test]
public void ConfigsPageMustLoadConfigurations()
{
// Arrange
Expand All @@ -38,34 +43,32 @@ public void ConfigsPageMustLoadConfigurations()
new()
};

_ = MockHttpClient
.When(HttpMethod.Get, "/api/edge/configurations")
.RespondJson(configurations);
_ = this.mockEdgeDeviceConfigurationsClientService
.Setup(service => service.GetDeviceConfigurations())
.ReturnsAsync(configurations);

// Act
var cut = RenderComponent<Configs>();
cut.WaitForAssertion(() => cut.FindAll("tr").Count.Should().Be(3));
cut.WaitForAssertion(() => cut.FindAll("table tbody tr").Count.Should().Be(2));

// Assert
cut.WaitForAssertion(() => MockHttpClient.VerifyNoOutstandingRequest());
cut.WaitForAssertion(() => MockHttpClient.VerifyNoOutstandingExpectation());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[TestCase]
[Test]
public void ConfigsPageShouldProcessProblemDetailsExceptionWhenIssueOccursOnGettingConfigurations()
{
// Arrange
_ = MockHttpClient
.When(HttpMethod.Get, "/api/edge/configurations")
.Throw(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));
_ = this.mockEdgeDeviceConfigurationsClientService
.Setup(service => service.GetDeviceConfigurations())
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

// Act
var cut = RenderComponent<Configs>();
cut.WaitForAssertion(() => cut.FindAll("tr").Count.Should().Be(2));

// Assert
cut.WaitForAssertion(() => MockHttpClient.VerifyNoOutstandingRequest());
cut.WaitForAssertion(() => MockHttpClient.VerifyNoOutstandingExpectation());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
Expand All @@ -76,15 +79,15 @@ public void ClickToItemShouldRedirectToConfigurationDetailsPage()

var configurations = new List<ConfigListItem>
{
new ConfigListItem
new()
{
ConfigurationID = configurationId
}
};

_ = MockHttpClient
.When(HttpMethod.Get, "/api/edge/configurations")
.RespondJson(configurations);
_ = this.mockEdgeDeviceConfigurationsClientService
.Setup(service => service.GetDeviceConfigurations())
.ReturnsAsync(configurations);

var cut = RenderComponent<Configs>();
cut.WaitForAssertion(() => cut.Markup.Should().NotContain("Loading..."));
Expand All @@ -93,7 +96,8 @@ public void ClickToItemShouldRedirectToConfigurationDetailsPage()
cut.WaitForAssertion(() => cut.Find("table tbody tr").Click());

// Assert
cut.WaitForAssertion(() => this.TestContext.Services.GetService<FakeNavigationManager>().Uri.Should().EndWith($"/edge/configurations/{configurationId}"));
cut.WaitForAssertion(() => Services.GetService<FakeNavigationManager>()?.Uri.Should().EndWith($"/edge/configurations/{configurationId}"));
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Loading

0 comments on commit 13e48be

Please sign in to comment.