Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change GatewayID field to list in device creation/edition pages #1376

Merged
merged 15 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.Devices
{
using System;
using System.Collections.Generic;
using AngleSharp.Dom;
using AutoFixture;
using AzureIoTHub.Portal.Client.Exceptions;
using AzureIoTHub.Portal.Client.Models;
using AzureIoTHub.Portal.Client.Pages.Devices.LoRaWAN;
using AzureIoTHub.Portal.Client.Services;
using AzureIoTHub.Portal.Client.Validators;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using Bunit;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MudBlazor;
using MudBlazor.Services;
using NUnit.Framework;
using UnitTests.Bases;
Expand All @@ -17,10 +27,14 @@ namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.Devices
[TestFixture]
public class CreateLoraDeviceTests : BlazorUnitTest
{
private Mock<ILoRaWanDeviceClientService> mockLoRaWanDeviceClientService;

public override void Setup()
{
base.Setup();
this.mockLoRaWanDeviceClientService = MockRepository.Create<ILoRaWanDeviceClientService>();

_ = Services.AddSingleton(this.mockLoRaWanDeviceClientService.Object);

Services.Add(new ServiceDescriptor(typeof(IResizeObserver), new MockResizeObserver()));
}
Expand All @@ -34,16 +48,12 @@ public void WhenUseOTAAShouldDisplayOTAATextboxes()
UseOTAA = true
};

var deviceDetails = new LoRaDeviceDetails()
{
DeviceID = Guid.NewGuid().ToString(),
ModelId = mockLoRaModel.ModelId,
AppEUI = Guid.NewGuid().ToString(),
AppKey = Guid.NewGuid().ToString()
};

var deviceDetails = new LoRaDeviceDetails();
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act

var cut = RenderComponent<CreateLoraDevice>(
Expand All @@ -66,16 +76,12 @@ public void WhenNotUseOTAAShouldDisplayABPTextboxes()
UseOTAA = false
};

var deviceDetails= new LoRaDeviceDetails()
{
DeviceID = Guid.NewGuid().ToString(),
ModelId = mockLoRaModel.ModelId,
AppSKey = Guid.NewGuid().ToString(),
NwkSKey = Guid.NewGuid().ToString(),
DevAddr = Guid.NewGuid().ToString(),
};
var deviceDetails= new LoRaDeviceDetails();
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<CreateLoraDevice>(
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoRaDevice), deviceDetails),
Expand All @@ -84,10 +90,139 @@ public void WhenNotUseOTAAShouldDisplayABPTextboxes()


// Assert

cut.WaitForAssertion(() => Assert.AreEqual(deviceDetails.AppSKey, cut.WaitForElement($"#{nameof(LoRaDeviceDetails.AppSKey)}").GetAttribute("value")));
cut.WaitForAssertion(() => Assert.AreEqual(deviceDetails.NwkSKey, cut.WaitForElement($"#{nameof(LoRaDeviceDetails.NwkSKey)}").GetAttribute("value")));
cut.WaitForAssertion(() => Assert.AreEqual(deviceDetails.DevAddr, cut.WaitForElement($"#{nameof(LoRaDeviceDetails.DevAddr)}").GetAttribute("value")));
}

[Test]
public void CreateLoRaDevicePageShouldBeRenderedProperly()
{
// Arrange
var mockLoRaModel = new LoRaDeviceModelDto
{
ModelId = Guid.NewGuid().ToString(),
UseOTAA = false
};

var deviceDetails= new LoRaDeviceDetails();
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(Fixture.Create<LoRaGatewayIDList>);

// Act
var cut = RenderComponent<CreateLoraDevice>(
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoRaDevice), deviceDetails),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraModelDto), mockLoRaModel),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraValidator), validator));

// Assert
cut.WaitForAssertion(() => cut.FindAll(".mud-expand-panel").Count.Should().Be(1));
Assert.AreEqual(3, cut.Instance.GatewayIdList.Count);
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void OnInitializedAsyncShouldProcessProblemDetailsExceptionWhenIssueOccursOnGettingGatewayIDList()
{
var mockLoRaModel = new LoRaDeviceModelDto
{
ModelId = Guid.NewGuid().ToString(),
UseOTAA = false
};

var deviceDetails= new LoRaDeviceDetails();
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

// Act
var cut = RenderComponent<CreateLoraDevice>(
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoRaDevice), deviceDetails),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraModelDto), mockLoRaModel),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraValidator), validator));

// Assert
Assert.AreEqual(0, cut.Instance.GatewayIdList.Count);
cut.WaitForAssertion(() => cut.Markup.Should().NotBeNullOrEmpty());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickingOnMudAutocompleteShouldDisplaySearch()
{
// Arrange
var mockLoRaModel = new LoRaDeviceModelDto
{
ModelId = Guid.NewGuid().ToString(),
UseOTAA = false
};

var deviceDetails= new LoRaDeviceDetails();
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(Fixture.Create<LoRaGatewayIDList>);

// Act
var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<CreateLoraDevice>(
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoRaDevice), deviceDetails),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraModelDto), mockLoRaModel),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraValidator), validator));
var autocompleteComponent = cut.FindComponent<MudAutocomplete<string>>();

// Act
autocompleteComponent.Find(TagNames.Input).Click();

// Assert
popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(3));
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeTrue());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void TypingOnMudAutocompleteShouldTriggerSearch()
{
// Arrange
var query = "Gateway";

var mockLoRaModel = new LoRaDeviceModelDto
{
ModelId = Guid.NewGuid().ToString(),
UseOTAA = false
};

var deviceDetails= new LoRaDeviceDetails();
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList()
{
GatewayIds = new List<string>()
{
"GatewayID01", "GatewayID02", "TestValue"
}
});

// Act
var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<CreateLoraDevice>(
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoRaDevice), deviceDetails),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraModelDto), mockLoRaModel),
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoraValidator), validator));
var autocompleteComponent = cut.FindComponent<MudAutocomplete<string>>();

// Act
autocompleteComponent.Find(TagNames.Input).Click();
autocompleteComponent.Find(TagNames.Input).Input(query);

// Assert
popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(2));
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeTrue());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Loading