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

Issue#333 bug when lora is disabled #353

Merged
merged 11 commits into from
Feb 28, 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 @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using AzureIoTHub.Portal.Client.Pages.Devices;
using AzureIoTHub.Portal.Server.Tests.Unit.Helpers;
using AzureIoTHub.Portal.Shared.Models.V10.Device;
using Bunit;
using Bunit.TestDoubles;
using FluentAssertions.Extensions;
Expand All @@ -33,6 +34,7 @@ public class DevicesListPageTests
private MockHttpMessageHandler mockHttpClient;

private string apiBaseUrl = "/api/Devices";
private string apiSettingsBaseUrl = "/api/settings/lora";

[SetUp]
public void SetUp()
Expand Down Expand Up @@ -71,6 +73,10 @@ public void DeviceListPageRendersCorrectly()
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new object[0]);

this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

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

Expand Down Expand Up @@ -101,6 +107,10 @@ public void When_Devices_Not_Loaded_DeviceListPage_Should_Render_ProgressBar()
.When(HttpMethod.Get, apiBaseUrl)
.RespondJsonAsync(task);

this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

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

Expand All @@ -119,6 +129,10 @@ public async Task When_ResetFilterButton_Click_Should_Clear_Filters()
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new object[0]);

this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

var cut = RenderComponent<DeviceListPage>();

cut.Find("#searchID").NodeValue = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -148,6 +162,10 @@ public async Task When_AddNewDevice_Click_Should_Navigate_To_New_Device_Page(str
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new object[0]);

this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

var cut = RenderComponent<DeviceListPage>();
await Task.Delay(100);

Expand All @@ -169,6 +187,10 @@ public async Task When_Refresh_Click_Should_Reload_From_Api()
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new object[0]);

this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

var cut = RenderComponent<DeviceListPage>();
cut.WaitForAssertion(() => cut.Find($"#tableRefreshButton"), 1.Seconds());

Expand All @@ -185,5 +207,61 @@ public async Task When_Refresh_Click_Should_Reload_From_Api()
Assert.AreEqual(4, matchCount);
this.mockRepository.VerifyAll();
}

[Test]
public void When_Lora_Feature_disable_device_detail_link_Should_not_contain_lora()
{
// Arrange
string deviceId = Guid.NewGuid().ToString();

this.mockHttpClient
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new DeviceListItem[] { new DeviceListItem { DeviceID = deviceId } });

_ = this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(false);

var cut = RenderComponent<DeviceListPage>();
_ = cut.WaitForElements(".detail-link");

// Act
var link = cut.FindAll("a.detail-link");

// Assert
Assert.IsNotNull(link);
foreach (var item in link)
{
Assert.AreEqual($"devices/{deviceId}", item.GetAttribute("href"));
}
}

[Test]
public void When_Lora_Feature_enable_device_detail_link_Should_contain_lora()
{
// Arrange
string deviceId = Guid.NewGuid().ToString();

this.mockHttpClient
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new DeviceListItem[] { new DeviceListItem { DeviceID = deviceId, SupportLoRaFeatures = true } });

_ = this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

var cut = RenderComponent<DeviceListPage>();
_ = cut.WaitForElements(".detail-link");

// Act
var link = cut.FindAll("a.detail-link");

// Assert
Assert.IsNotNull(link);
foreach (var item in link)
{
Assert.AreEqual($"devices/{deviceId}?isLora=true", item.GetAttribute("href"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Client.Pages.DeviceModels;
using AzureIoTHub.Portal.Client.Pages.Devices;
using AzureIoTHub.Portal.Server.Tests.Unit.Helpers;
using AzureIoTHub.Portal.Shared.Models.V10.Device;
using AzureIoTHub.Portal.Shared.Models.V10.DeviceModel;
using Bunit;
using Bunit.TestDoubles;
using FluentAssertions.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Moq.Protected;
using MudBlazor;
using MudBlazor.Interop;
using MudBlazor.Services;
using NUnit.Framework;
using RichardSzalay.MockHttp;

namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages.DevicesModels
{
[TestFixture]
public class DeviceModelListPageTests
{
private Bunit.TestContext testContext;

private MockRepository mockRepository;
private Mock<IDialogService> mockDialogService;
private FakeNavigationManager mockNavigationManager;
private MockHttpMessageHandler mockHttpClient;

private string apiBaseUrl = "/api/models";
private string apiSettingsBaseUrl = "/api/settings/lora";

[SetUp]
public void SetUp()
{
this.testContext = new Bunit.TestContext();

this.mockRepository = new MockRepository(MockBehavior.Strict);
this.mockDialogService = this.mockRepository.Create<IDialogService>();

this.mockHttpClient = testContext.Services.AddMockHttpClient();

testContext.Services.AddSingleton(this.mockDialogService.Object);

testContext.Services.AddMudServices();

testContext.JSInterop.SetupVoid("mudKeyInterceptor.connect", _ => true);
testContext.JSInterop.SetupVoid("mudPopover.connect", _ => true);
testContext.JSInterop.Setup<BoundingClientRect>("mudElementRef.getBoundingClientRect", _ => true);

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

private IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters)
where TComponent : IComponent
{
return this.testContext.RenderComponent<TComponent>(parameters);
}

[Test]
public void When_Lora_Feature_enable_device_detail_link_Should_contain_lora()
{
// Arrange
string deviceId = Guid.NewGuid().ToString();

this.mockHttpClient
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new DeviceModel[] { new DeviceModel { ModelId = deviceId, SupportLoRaFeatures = true } });

_ = this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);

var cut = RenderComponent<DeviceModelListPage>();
_ = cut.WaitForElements(".detail-link");

// Act
var link = cut.FindAll("a.detail-link");

// Assert
Assert.IsNotNull(link);
foreach (var item in link)
{
Assert.AreEqual($"device-models/{deviceId}?isLora=true", item.GetAttribute("href"));
}
}

[Test]
public void When_Lora_Feature_disable_device_detail_link_Should_not_contain_lora()
{
// Arrange
string deviceId = Guid.NewGuid().ToString();

this.mockHttpClient
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new DeviceModel[] { new DeviceModel { ModelId = deviceId, SupportLoRaFeatures = true } });

_ = this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(false);

var cut = RenderComponent<DeviceModelListPage>();
_ = cut.WaitForElements(".detail-link");

// Act
var link = cut.FindAll("a.detail-link");

// Assert
Assert.IsNotNull(link);
foreach (var item in link)
{
Assert.AreEqual($"device-models/{deviceId}", item.GetAttribute("href"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
<img height="25" src="@context.ImageUrl" />
</MudTd>
<MudTd DataLabel="Name" Style="word-break: break-all;">
<a href=@($"/device-models/{@context.ModelId}{(@context.SupportLoRaFeatures ? "?isLora=true": "")}")>@context.Name</a>
<a class="detail-link" href=@($"device-models/{@context.ModelId}{((@context.SupportLoRaFeatures && isLoraEnable) ? "?isLora=true": "")}")>@context.Name</a>
</MudTd>
<MudTd DataLabel="Description" Style="word-break: break-all; ">
@context.Description
</MudTd>
<MudTd DataLabel="Details" Style="text-align: center">
<a href="@($"/device-models/{@context.ModelId}{(@context.SupportLoRaFeatures ? "?isLora=true": "")}")"><MudIconButton Icon="@Icons.Filled.Visibility" Color="Color.Default" /></a>
<a class="detail-link" href="@($"device-models/{@context.ModelId}{((@context.SupportLoRaFeatures && isLoraEnable) ? "?isLora=true": "")}")"><MudIconButton Icon="@Icons.Filled.Visibility" Color="Color.Default" /></a>
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudIconButton Color="Color.Default" Disabled="context.IsBuiltin" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" OnClick="@(e => DeleteDeviceModel(context))"></MudIconButton>
Expand All @@ -78,9 +78,11 @@
@code {
private DeviceModel[] result;
private int[] pageSizeOptions = new int[] { 2, 5, 10 };
private bool isLoraEnable;

protected override async Task OnInitializedAsync()
{
isLoraEnable = await this.httpClient.GetFromJsonAsync<bool>("api/settings/lora");
await LoadDeviceModels();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<img height="25" src="@context.ImageUrl" />
</MudTd>
<MudTd DataLabel="Device" Style="word-break: break-all;">
<a href="@($"devices/{@context.DeviceID}{(@context.SupportLoRaFeatures ? "?isLora=true": "")}")">
<a class="detail-link" href="@($"devices/{@context.DeviceID}{((@context.SupportLoRaFeatures && isLoraEnable) ? "?isLora=true": "")}")">
@(string.IsNullOrEmpty(context.DeviceName) ? context.DeviceID : context.DeviceName)
</a>
</MudTd>
Expand Down Expand Up @@ -132,7 +132,7 @@
</MudTd>
<MudTd DataLabel="LSU" Style="text-align: center">@context.StatusUpdatedTime</MudTd>
<MudTd DataLabel="Details" Style="text-align: center">
<a href="@($"devices/{@context.DeviceID}{(@context.SupportLoRaFeatures ? "?isLora=true": "")}")">
<a class="detail-link" href="@($"devices/{@context.DeviceID}{((@context.SupportLoRaFeatures && isLoraEnable) ? "?isLora=true": "")}")">
<MudIconButton Icon="@Icons.Filled.Visibility" Color="Color.Default" />
</a>
</MudTd>
Expand All @@ -158,11 +158,13 @@
private string searchID = "";
private string searchStatus = "";
private string searchState = "";
private bool isLoraEnable;

private int[] pageSizeOptions = new int[] { 2, 5, 10 };

protected override async Task OnInitializedAsync()
{
isLoraEnable = await this.Http.GetFromJsonAsync<bool>("api/settings/lora");
await LoadDevices();
}

Expand Down