-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add save and duplicate device #616 Add save and duplicate device #616 Add save and duplicate device #616 Add save and duplicate device #616 * Fix unit tests on create/detail device * Add unit tests on DeviceLayoutService * Add unit test on DeviceToDuplicateSelector * Add unit test TypingOnMudAutocompleteShouldTriggerSearch * Fix DuplicateSharedDevice when device is lorawan * Add unit test TypingOnMudAutocompleteShouldProcessProblemDetailsExceptionWhenTriggerSearch * Rename method SearchDevicesToClone to SearchDevicesToDuplicate
- Loading branch information
1 parent
526b04e
commit 9d6efa0
Showing
12 changed files
with
678 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/AzureIoTHub.Portal.Server.Tests.Unit/Pages/Devices/DeviceToDuplicateSelectorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
176 changes: 176 additions & 0 deletions
176
src/AzureIoTHub.Portal.Server.Tests.Unit/Services/DeviceLayoutServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.