Skip to content

Commit

Permalink
Fix missing delete lorawan device on client side #1592 (#1593)
Browse files Browse the repository at this point in the history
* Fix missing delete lorawan device on client side #1592

* Add unit test on DeleteDevicePage
  • Loading branch information
hocinehacherouf authored Nov 24, 2022
1 parent c741cd8 commit 517fade
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/AzureIoTHub.Portal.Client/Pages/Devices/DeleteDevicePage.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@inject ISnackbar Snackbar
@inject IDeviceClientService DeviceClientService
@inject ILoRaWanDeviceClientService LoRaWanDeviceClientService

<MudDialog>
<DialogContent>
Expand All @@ -16,17 +17,18 @@
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="DeleteDevice">Delete</MudButton>
<MudButton id="delete-device" Color="Color.Primary" OnClick="DeleteDevice">Delete</MudButton>
</DialogActions>
</MudDialog>

@code {
[CascadingParameter]
public Error Error {get; set;}

[CascadingParameter] MudDialogInstance MudDialog { get; set; }
[Parameter] public string deviceID { get; set; }
[Parameter] public string deviceName { get; set; }
[Parameter] public bool IsLoRaWan { get; set; }

void Submit() => MudDialog.Close(DialogResult.Ok(true));
void Cancel() => MudDialog.Cancel();
Expand All @@ -39,7 +41,14 @@
{
try
{
await DeviceClientService.DeleteDevice(deviceID);
if (IsLoRaWan)
{
await LoRaWanDeviceClientService.DeleteDevice(deviceID);
}
else
{
await DeviceClientService.DeleteDevice(deviceID);
}

Snackbar.Add($"Device {deviceName} has been successfully deleted!", Severity.Success);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@
var parameters = new DialogParameters
{
{"deviceID", Device.DeviceID},
{"deviceName", Device.DeviceName}
{"deviceName", Device.DeviceName},
{"IsLoRaWan", IsLoRa}
};
var result = await DialogService.Show<DeleteDevicePage>("Confirm Deletion", parameters).Result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@
var parameters = new DialogParameters();
parameters.Add("deviceID", device.DeviceID);
parameters.Add("deviceName", device.DeviceName);
parameters.Add("IsLoRaWan", device.SupportLoRaFeatures);

var result = await DialogService.Show<DeleteDevicePage>("Confirm Deletion", parameters).Result;

if (result.Cancelled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface ILoRaWanDeviceClientService

Task UpdateDevice(LoRaDeviceDetails device);

Task DeleteDevice(string deviceId);

Task ExecuteCommand(string deviceId, string commandId);

Task<LoRaGatewayIDList> GetGatewayIdList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public Task UpdateDevice(LoRaDeviceDetails device)
return this.http.PutAsJsonAsync("api/lorawan/devices", device);
}

public Task DeleteDevice(string deviceId)
{
return this.http.DeleteAsync($"api/lorawan/devices/{deviceId}");
}

public Task ExecuteCommand(string deviceId, string commandId)
{
return this.http.PostAsJsonAsync($"api/lorawan/devices/{deviceId}/_command/{commandId}", string.Empty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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.Devices
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Client.Pages.Devices;
using AzureIoTHub.Portal.Client.Services;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases;
using Bunit;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MudBlazor;
using NUnit.Framework;

[TestFixture]
public class DeleteDevicePageTests : BlazorUnitTest
{
private Mock<IDeviceClientService> mockDeviceClientService;
private Mock<ILoRaWanDeviceClientService> mockLoRaWanDeviceClientService;

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

this.mockDeviceClientService = MockRepository.Create<IDeviceClientService>();
this.mockLoRaWanDeviceClientService = MockRepository.Create<ILoRaWanDeviceClientService>();

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

[Test]
public async Task DeleteDevice_NonLoRaDevice_DeviceDeleted()
{
// Arrange
var deviceId = Guid.NewGuid().ToString();

_ = this.mockDeviceClientService.Setup(service => service.DeleteDevice(deviceId))
.Returns(Task.CompletedTask);

var cut = RenderComponent<MudDialogProvider>();
var service = Services.GetService<IDialogService>() as DialogService;

var parameters = new DialogParameters
{
{"deviceID", deviceId},
};

// Act
await cut.InvokeAsync(() => service?.Show<DeleteDevicePage>(string.Empty, parameters));
cut.WaitForElement("#delete-device").Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public async Task DeleteDevice_LoRaDevice_LoRaDeviceDeleted()
{
// Arrange
var deviceId = Guid.NewGuid().ToString();

_ = this.mockLoRaWanDeviceClientService.Setup(service => service.DeleteDevice(deviceId))
.Returns(Task.CompletedTask);

var cut = RenderComponent<MudDialogProvider>();
var service = Services.GetService<IDialogService>() as DialogService;

var parameters = new DialogParameters
{
{"deviceID", deviceId},
{"IsLoRaWan", true}
};

// Act
await cut.InvokeAsync(() => service?.Show<DeleteDevicePage>(string.Empty, parameters));
cut.WaitForElement("#delete-device").Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,22 @@ public async Task GetGatewayIdListShouldReturnGatewayIdList()
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}

[Test]
public async Task DeleteDevice_ExistingDevice_DevicDeleted()
{
// Arrange
var deviceId = Fixture.Create<string>();

_ = MockHttpClient.When(HttpMethod.Delete, $"/api/lorawan/devices/{deviceId}")
.Respond(HttpStatusCode.NoContent);

// Act
await this.loRaWanDeviceClientService.DeleteDevice(deviceId);

// Assert
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}
}
}

0 comments on commit 517fade

Please sign in to comment.