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

Add save and duplicate device #616 #997

Merged
merged 8 commits into from
Jul 29, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public override void Setup()
_ = Services.AddSingleton(this.mockDeviceClientService.Object);
_ = Services.AddSingleton(this.mockLoRaWanDeviceClientService.Object);

_ = Services.AddSingleton<IDeviceLayoutService, DeviceLayoutService>();

Services.Add(new ServiceDescriptor(typeof(IResizeObserver), new MockResizeObserver()));

this.mockNavigationManager = Services.GetRequiredService<FakeNavigationManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public override void Setup()
_ = Services.AddSingleton(this.mockDeviceClientService.Object);
_ = Services.AddSingleton(this.mockLoRaWanDeviceClientService.Object);

_ = Services.AddSingleton<IDeviceLayoutService, DeviceLayoutService>();

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

Services.Add(new ServiceDescriptor(typeof(IResizeObserver), new MockResizeObserver()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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.Server.Tests.Unit.Pages.Devices
{
using System.Collections.Generic;
using AngleSharp.Dom;
using AutoFixture;
using AzureIoTHub.Portal.Client.Services;
using Bunit;
using Client.Exceptions;
using Client.Models;
using Client.Pages.Devices;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Models.v10;
using Moq;
using MudBlazor;
using NUnit.Framework;

[TestFixture]
public class DeviceToDuplicateSelectorTests : BlazorUnitTest
{
private Mock<IDeviceClientService> mockDeviceClientService;
private Mock<ILoRaWanDeviceClientService> mockLoRaWanDeviceClientService;
private Mock<IDeviceModelsClientService> mockDeviceModelsClientService;
private Mock<ILoRaWanDeviceModelsClientService> mockLoRaWanDeviceModelsClientService;

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

this.mockDeviceClientService = MockRepository.Create<IDeviceClientService>();
this.mockLoRaWanDeviceClientService = MockRepository.Create<ILoRaWanDeviceClientService>();
this.mockDeviceModelsClientService = MockRepository.Create<IDeviceModelsClientService>();
this.mockLoRaWanDeviceModelsClientService = MockRepository.Create<ILoRaWanDeviceModelsClientService>();

_ = Services.AddSingleton(this.mockDeviceClientService.Object);
_ = Services.AddSingleton(this.mockLoRaWanDeviceClientService.Object);
_ = Services.AddSingleton(this.mockDeviceModelsClientService.Object);
_ = Services.AddSingleton(this.mockLoRaWanDeviceModelsClientService.Object);

_ = Services.AddSingleton<IDeviceLayoutService, DeviceLayoutService>();
}

[Test]
public void DeviceToDuplicateSelectorShouldRenderCorrectly()
{
// Act
var cut = RenderComponent<DeviceToDuplicateSelector>();

// Assert
cut.WaitForAssertion(() => cut.FindAll("#search-device").Count.Should().Be(1));
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void TypingOnMudAutocompleteShouldTriggerSearch()
{
// Arrange
var query = Fixture.Create<string>();

var url = $"api/devices?pageSize=10&searchText={query}";
_ = this.mockDeviceClientService.Setup(service => service.GetDevices(url))
.ReturnsAsync(new PaginationResult<DeviceListItem>()
{
Items = new List<DeviceListItem>
{
new()
{
DeviceID = Fixture.Create<string>()
}
}
});

var cut = RenderComponent<DeviceToDuplicateSelector>();
var autocompleteComponent = cut.FindComponent<MudAutocomplete<DeviceListItem>>();

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

// Assert
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeTrue());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void TypingOnMudAutocompleteShouldProcessProblemDetailsExceptionWhenTriggerSearch()
{
// Arrange
var query = Fixture.Create<string>();

var url = $"api/devices?pageSize=10&searchText={query}";
_ = this.mockDeviceClientService.Setup(service => service.GetDevices(url))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

var cut = RenderComponent<DeviceToDuplicateSelector>();
var autocompleteComponent = cut.FindComponent<MudAutocomplete<DeviceListItem>>();

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

// Assert
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeTrue());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// 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.Server.Tests.Unit.Services
{
using System.Collections.Generic;
using System.Linq;
using AutoFixture;
using Client.Services;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Models.v10;
using Models.v10.LoRaWAN;
using NUnit.Framework;

[TestFixture]
public class DeviceLayoutServiceTests : BlazorUnitTest
{
private IDeviceLayoutService deviceLayoutService;

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

_ = Services.AddSingleton<IDeviceLayoutService, DeviceLayoutService>();

this.deviceLayoutService = Services.GetRequiredService<IDeviceLayoutService>();
}

[Test]
public void RefreshDeviceShouldRaiseRefreshDeviceOccurredEvent()
{
// Arrange
var receivedEvents = new List<string>();
this.deviceLayoutService.RefreshDeviceOccurred += (sender, _) =>
{
receivedEvents.Add(sender?.GetType().ToString());
};

// Act
this.deviceLayoutService.RefreshDevice();

// Assert
_ = receivedEvents.Count.Should().Be(1);
_ = receivedEvents.First().Should().Be(typeof(DeviceLayoutService).ToString());
}

[Test]
public void DuplicateSharedDeviceShouldReturnDuplicatedDevice()
{
// Arrange
var deviceId = Fixture.Create<string>();
var deviceName = Fixture.Create<string>();

// Act
var result = this.deviceLayoutService.DuplicateSharedDevice(new DeviceDetails
{
DeviceID = deviceId,
DeviceName = deviceName
});

// Assert
_ = result.DeviceID.Should().BeEmpty();
_ = result.DeviceName.Should().Be($"{deviceName} - copy");
}

[Test]
public void DuplicateSharedDeviceShouldReturnDuplicatedLoraWanDevice()
{
// Arrange
var deviceId = Fixture.Create<string>();
var deviceName = Fixture.Create<string>();
var appKey = Fixture.Create<string>();

// Act
var result = this.deviceLayoutService.DuplicateSharedDevice(new LoRaDeviceDetails
{
DeviceID = deviceId,
DeviceName = deviceName,
AppKey = appKey
});

// Assert
var loraWanDevice = (LoRaDeviceDetails) result;

_ = loraWanDevice.DeviceID.Should().BeEmpty();
_ = loraWanDevice.DeviceName.Should().Be($"{deviceName} - copy");
_ = loraWanDevice.AppKey.Should().BeEmpty();
}

[Test]
public void DuplicateSharedDeviceModelShouldReturnDuplicatedDeviceModel()
{
// Arrange
var deviceModel = Fixture.Create<DeviceModel>();

// Act
var result = this.deviceLayoutService.DuplicateSharedDeviceModel(deviceModel);

// Assert
_ = result.Should().BeEquivalentTo(deviceModel);
}

[Test]
public void ResetSharedDeviceShouldReturnNewDevice()
{
// Arrange
var expectedDevice = new DeviceDetails();

// Act
var result = this.deviceLayoutService.ResetSharedDevice();

// Assert
_ = result.Should().BeEquivalentTo(expectedDevice);
}

[Test]
public void ResetSharedDeviceShouldReturnNewDeviceWithExpectedTags()
{
// Arrange
var expectedTags = Fixture.CreateMany<DeviceTag>(2).ToList();

var expectedDevice = new DeviceDetails();

foreach (var tag in expectedTags)
{
_ = expectedDevice.Tags.TryAdd(tag.Name, string.Empty);
}

// Act
var result = this.deviceLayoutService.ResetSharedDevice(expectedTags);

// Assert
_ = result.Should().BeEquivalentTo(expectedDevice);
}

[Test]
public void ResetSharedDeviceModelShouldReturnNewDeviceModel()
{
// Arrange
var expectedDeviceModel = new DeviceModel();

// Act
var result = this.deviceLayoutService.ResetSharedDeviceModel();

// Assert
_ = result.Should().BeEquivalentTo(expectedDeviceModel);
}

[Test]
public void GetSharedDeviceShouldReturnDevice()
{
// Arrange
var expectedDevice = new DeviceDetails();

// Act
var result = this.deviceLayoutService.GetSharedDevice();

// Assert
_ = result.Should().BeEquivalentTo(expectedDevice);
}

[Test]
public void GetSharedDeviceModelShouldReturnDeviceModel()
{
// Arrange
var expectedDeviceModel = new DeviceModel();

// Act
var result = this.deviceLayoutService.GetSharedDeviceModel();

// Assert
_ = result.Should().BeEquivalentTo(expectedDeviceModel);
}
}
}
13 changes: 13 additions & 0 deletions src/AzureIoTHub.Portal/Client/Enums/DeviceSaveAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.Client.Enums
{
public enum DeviceSaveAction
{
Save,
SaveAndAddNew,
SaveAndDuplicate,
Duplicate
}
}
Loading