From 7ec9a026a2e0bd5b5fada819c8eb4b3a7416a4f2 Mon Sep 17 00:00:00 2001 From: Kevin BEAUGRAND <9513635+kbeaugrand@users.noreply.github.com> Date: Sun, 13 Mar 2022 14:10:26 +0100 Subject: [PATCH] Test LoRaWan save Action --- .../Helpers/MockHttpHelper.cs | 88 +++++++++++-------- .../CreateDeviceModelPageTests.cs | 37 ++++++++ .../DeviceModels/CreateDeviceModelPage.razor | 8 +- .../LoRaWAN/CreateLoraDeviceModel.razor | 39 ++++---- .../Validators/LoRaDeviceModelValidator.cs | 8 +- 5 files changed, 114 insertions(+), 66 deletions(-) diff --git a/src/AzureIoTHub.Portal.Server.Tests.Unit/Helpers/MockHttpHelper.cs b/src/AzureIoTHub.Portal.Server.Tests.Unit/Helpers/MockHttpHelper.cs index e29f749c1..7eae46f52 100644 --- a/src/AzureIoTHub.Portal.Server.Tests.Unit/Helpers/MockHttpHelper.cs +++ b/src/AzureIoTHub.Portal.Server.Tests.Unit/Helpers/MockHttpHelper.cs @@ -1,15 +1,18 @@ -using Bunit; -using Microsoft.Extensions.DependencyInjection; -using RichardSzalay.MockHttp; -using System; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Text.Json; -using System.Threading.Tasks; +// 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.Helpers { + using Bunit; + using Microsoft.Extensions.DependencyInjection; + using RichardSzalay.MockHttp; + using System; + using System.Net; + using System.Net.Http; + using System.Net.Http.Headers; + using System.Text.Json; + using System.Threading.Tasks; + public static class MockHttpHelper { public static MockHttpMessageHandler AddMockHttpClient(this TestServiceProvider services) @@ -17,59 +20,68 @@ public static MockHttpMessageHandler AddMockHttpClient(this TestServiceProvider var mockHttpHandler = new MockHttpMessageHandler(); var httpClient = mockHttpHandler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://fake.local"); - services.AddSingleton(httpClient); + + _ = services.AddSingleton(httpClient); return mockHttpHandler; } public static MockedRequest RespondJson(this MockedRequest request, T content) { - request.Respond(req => - { - var response = new HttpResponseMessage(HttpStatusCode.OK); - response.Content = new StringContent(JsonSerializer.Serialize(content)); - response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - return response; - }); + _ = request.Respond(req => + { + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(JsonSerializer.Serialize(content)) + }; + response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + return response; + }); return request; } public static MockedRequest RespondJsonAsync(this MockedRequest request, Task content) { - request.Respond(req => - { - var response = new HttpResponseMessage(HttpStatusCode.OK); - response.Content = new StringContent(JsonSerializer.Serialize(content)); - response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - return response; - }); + _ = request.Respond(req => + { + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(JsonSerializer.Serialize(content)) + }; + response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + return response; + }); return request; } public static MockedRequest RespondText(this MockedRequest request, string content) { - request.Respond(req => - { - var response = new HttpResponseMessage(HttpStatusCode.OK); - response.Content = new StringContent(content); - response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - return response; - }); + _ = request.Respond(req => + { + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(content) + }; + response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + return response; + }); return request; } public static MockedRequest RespondJson(this MockedRequest request, Func contentProvider) { - request.Respond(req => - { - var response = new HttpResponseMessage(HttpStatusCode.OK); - response.Content = new StringContent(JsonSerializer.Serialize(contentProvider())); - response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - return response; - }); + _ = request.Respond(req => + { + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(JsonSerializer.Serialize(contentProvider())) + }; + response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + return response; + }); return request; } diff --git a/src/AzureIoTHub.Portal.Server.Tests.Unit/Pages/DevicesModels/CreateDeviceModelPageTests.cs b/src/AzureIoTHub.Portal.Server.Tests.Unit/Pages/DevicesModels/CreateDeviceModelPageTests.cs index 34788a0d9..c93988192 100644 --- a/src/AzureIoTHub.Portal.Server.Tests.Unit/Pages/DevicesModels/CreateDeviceModelPageTests.cs +++ b/src/AzureIoTHub.Portal.Server.Tests.Unit/Pages/DevicesModels/CreateDeviceModelPageTests.cs @@ -8,6 +8,7 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages using System.Linq; using System.Net.Http; using System.Net.Http.Json; + using System.Threading.Tasks; using AzureIoTHub.Portal.Client.Pages.DeviceModels; using AzureIoTHub.Portal.Server.Tests.Unit.Helpers; using AzureIoTHub.Portal.Shared.Models; @@ -35,6 +36,7 @@ public class CreateDeviceModelPageTests : IDisposable private MockHttpMessageHandler mockHttpClient; private static string apiBaseUrl => $"/api/models"; + private static string lorawanApiUrl => $"/api/lorawan/models"; [SetUp] public void SetUp() @@ -282,6 +284,41 @@ public void WhenLoraFeatureIsEnabledModelDetailsShouldDisplayLoRaWANTab() this.mockHttpClient.VerifyNoOutstandingExpectation(); } + [Test] + public void WhenLoraDeviceModelDetailsShouldCallLoRaAPIs() + { + // Arrange + _ = testContext.Services.AddSingleton(new PortalSettings { IsLoRaSupported = true }); + + var cut = RenderComponent(); + _ = cut.WaitForElement("#form"); + + cut.Find($"#{nameof(DeviceModel.Name)}").Change(Guid.NewGuid().ToString()); + cut.Find($"#{nameof(DeviceModel.Description)}").Change(Guid.NewGuid().ToString()); + + cut.WaitForElement("#SupportLoRaFeatures") + .Change(true); + + _ = this.mockHttpClient.When(HttpMethod.Post, $"{lorawanApiUrl}") + .RespondText(string.Empty); + + _ = this.mockHttpClient.When(HttpMethod.Post, $"{ lorawanApiUrl }/*/commands") + .RespondText(string.Empty); + + _ = this.mockHttpClient.When(HttpMethod.Post, $"{ lorawanApiUrl }/*/avatar") + .RespondText(string.Empty); + + var saveButton = cut.WaitForElement("#SaveButton"); + + // Act + saveButton.Click(); + cut.WaitForState(() => testContext.Services.GetRequiredService().Uri.EndsWith("/device-models", StringComparison.OrdinalIgnoreCase)); + + // Assert + this.mockHttpClient.VerifyNoOutstandingExpectation(); + } + + public void Dispose() { GC.SuppressFinalize(this); diff --git a/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor b/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor index 7d5846ce9..23bb1126b 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor @@ -25,7 +25,7 @@ Device Model - + @@ -143,13 +143,12 @@ @if (IsLoRa) { - + - } @@ -217,9 +216,6 @@ private MultipartFormDataContent content; private string imageDataUrl; - // Used to switch active tab - private MudTabs tabs; - private void DeleteAvatar() { content = null; diff --git a/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/LoRaWAN/CreateLoraDeviceModel.razor b/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/LoRaWAN/CreateLoraDeviceModel.razor index de8e6a4fe..67eaa2f39 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/LoRaWAN/CreateLoraDeviceModel.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/LoRaWAN/CreateLoraDeviceModel.razor @@ -13,11 +13,14 @@ - @(LoRaDeviceModel.UseOTAA ? "OTAA setting enable" : "APB setting enable") - - - + @(LoRaDeviceModel.UseOTAA ? "OTAA setting enable" : "APB setting enable") + @if (!LoRaDeviceModel.UseOTAA) + { + + + + } - + @foreach (DeduplicationMode item in Enum.GetValues(typeof(DeduplicationMode))) { @item @@ -46,18 +49,18 @@ @if (LoRaDeviceModel.UseOTAA) { - + } - + - + @@ -71,21 +74,21 @@ Receive Windows - + @if (LoRaDeviceModel.Downlink.HasValue && LoRaDeviceModel.Downlink.Value) { - + - + - + - + } @@ -95,18 +98,18 @@ Frame Counters - + - + @if (LoRaDeviceModel.Downlink.HasValue && LoRaDeviceModel.Downlink.Value) { - + - + } @@ -157,7 +160,7 @@ } - Save Changes + Save Changes diff --git a/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceModelValidator.cs b/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceModelValidator.cs index dd90c18b3..59c36b52d 100644 --- a/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceModelValidator.cs +++ b/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceModelValidator.cs @@ -10,10 +10,10 @@ public class LoRaDeviceModelValidator : AbstractValidator { public LoRaDeviceModelValidator() { - _ = RuleFor(x => x.AppEUI) - .NotEmpty() - .Length(1, 100) - .When(x => x.UseOTAA); + //_ = RuleFor(x => x.AppEUI) + // .NotEmpty() + // .Length(1, 100) + // .When(x => x.UseOTAA); } } }