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

Feature/1118_-_Select the edge model at device creation #1125

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void ClickOnAddPropertyShouldAddNewProperty()
service.SetDeviceModelModelProperties(It.IsAny<string>(), It.Is<List<DeviceProperty>>(properties => properties.Count.Equals(1))))
.Returns(Task.CompletedTask);

_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Success, null)).Returns((Snackbar)null);
_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Success, null)).Returns(value: null);

// Act
var cut = RenderComponent<DeviceModelDetailPage>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.EdgeDevices
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Client.Exceptions;
using AzureIoTHub.Portal.Client.Models;
using AzureIoTHub.Portal.Client.Pages.EdgeDevices;
using AzureIoTHub.Portal.Client.Services;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases;
using Bunit;
using Bunit.TestDoubles;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MudBlazor;
using NUnit.Framework;

[TestFixture]
public class CreateEdgeDevicePageTest : BlazorUnitTest
{
private Mock<ISnackbar> mockSnackbarService;
private Mock<IEdgeDeviceClientService> mockEdgeDeviceClientService;
private Mock<IEdgeModelClientService> mockIEdgeModelClientService;
private Mock<IDeviceTagSettingsClientService> mockDeviceTagSettingsClientService;

private FakeNavigationManager mockNavigationManager;

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

this.mockSnackbarService = MockRepository.Create<ISnackbar>();
this.mockEdgeDeviceClientService = MockRepository.Create<IEdgeDeviceClientService>();
this.mockIEdgeModelClientService = MockRepository.Create<IEdgeModelClientService>();
this.mockDeviceTagSettingsClientService = MockRepository.Create<IDeviceTagSettingsClientService>();

_ = Services.AddSingleton(this.mockEdgeDeviceClientService.Object);
_ = Services.AddSingleton(this.mockIEdgeModelClientService.Object);
_ = Services.AddSingleton(this.mockDeviceTagSettingsClientService.Object);
_ = Services.AddSingleton(this.mockSnackbarService.Object);

this.mockNavigationManager = Services.GetRequiredService<FakeNavigationManager>();
}

[Test]
public async Task SaveShouldCreateEdgeDeviceAndRedirectToEdgeDeviceList()
{
// Arrange
var edgeModel = new IoTEdgeModelListItem(){ Name = "model01", ModelId = "model01"};

var edgeDevice = new IoTEdgeDevice()
{
DeviceId = Guid.NewGuid().ToString()
};

_ = this.mockIEdgeModelClientService.Setup(x => x.GetIoTEdgeModelList())
.ReturnsAsync(new List<IoTEdgeModelListItem>() { edgeModel });

_ = this.mockDeviceTagSettingsClientService.Setup(x => x.GetDeviceTags())
.ReturnsAsync(new List<DeviceTag>()
{
new DeviceTag(){ Name = "tag01", Required = true}
});

_ = this.mockEdgeDeviceClientService
.Setup(x => x.CreateDevice(It.Is<IoTEdgeDevice>(c => c.DeviceId.Equals(edgeDevice.DeviceId, StringComparison.Ordinal))))
.Returns(Task.CompletedTask);

_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Success, null)).Returns(value: null);

var cut = RenderComponent<CreateEdgeDevicePage>();
var saveButton = cut.WaitForElement("#SaveButton");

cut.WaitForElement($"#{nameof(IoTEdgeDevice.DeviceName)}").Change("testName");
cut.WaitForElement($"#{nameof(IoTEdgeDevice.DeviceId)}").Change(edgeDevice.DeviceId);
cut.WaitForElement($"#tag01").Change("testTag01");
await cut.Instance.ChangeModel(edgeModel);

saveButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
cut.WaitForAssertion(() => this.mockNavigationManager.Uri.Should().EndWith("/edge/devices"));
}

[Test]
public async Task CreateEdgeDevicePageSaveShouldProcessProblemDetailsExceptionWhenIssueOccurs()
{
// Arrange
var edgeModel = new IoTEdgeModelListItem(){ Name = "model01", ModelId = "model01"};

var edgeDevice = new IoTEdgeDevice()
{
DeviceId = Guid.NewGuid().ToString()
};

_ = this.mockIEdgeModelClientService.Setup(x => x.GetIoTEdgeModelList())
.ReturnsAsync(new List<IoTEdgeModelListItem>() { edgeModel });

_ = this.mockDeviceTagSettingsClientService.Setup(x => x.GetDeviceTags())
.ReturnsAsync(new List<DeviceTag>()
{
new DeviceTag(){ Name = "tag01", Required = true}
});

_ = this.mockEdgeDeviceClientService
.Setup(x => x.CreateDevice(It.Is<IoTEdgeDevice>(c => c.DeviceId.Equals(edgeDevice.DeviceId, StringComparison.Ordinal))))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

var cut = RenderComponent<CreateEdgeDevicePage>();
var saveButton = cut.WaitForElement("#SaveButton");

cut.WaitForElement($"#{nameof(IoTEdgeDevice.DeviceName)}").Change("testName");
cut.WaitForElement($"#{nameof(IoTEdgeDevice.DeviceId)}").Change(edgeDevice.DeviceId);
cut.WaitForElement($"#tag01").Change("testTag01");
await cut.Instance.ChangeModel(edgeModel);

saveButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
//cut.WaitForAssertion(() => this.mockNavigationManager.Uri.Should().EndWith("/edge/devices"));
}

[Test]
public void WhenAnErrorOccurInOnInitializedAsyncShouldProcessProblemDetailsException()
{
// Arrange
_ = this.mockIEdgeModelClientService.Setup(x => x.GetIoTEdgeModelList())
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

// Act
var cut = RenderComponent<CreateEdgeDevicePage>();

// Assert
cut.WaitForAssertion(() => cut.Markup.Should().NotBeNullOrEmpty());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Loading