Skip to content

Commit

Permalink
Bug fix: Cannot send a LoRaWAN command with the same name on several …
Browse files Browse the repository at this point in the history
…models (#433)
  • Loading branch information
audserraCGI authored Mar 11, 2022
1 parent fa76786 commit 16337df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) CGI France. All rights reserved.
// 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.Controllers.V10.LoRaWAN
Expand Down Expand Up @@ -77,12 +77,13 @@ public async Task ExecuteCommandStateUnderTestExpectedBehavior()
{
// Arrange
var loRaWANDevicesController = this.CreateLoRaWANDevicesController();
var modelId = Guid.NewGuid().ToString();
var deviceId = Guid.NewGuid().ToString();
var commandId = Guid.NewGuid().ToString();

var mockResponse = this.mockRepository.Create<Response>();
_ = this.mockDeviceModelCommandMapper
.Setup(c => c.GetDeviceModelCommand(It.Is<TableEntity>(x => x.RowKey == commandId)))
.Setup(c => c.GetDeviceModelCommand(It.Is<TableEntity>(x => x.RowKey == commandId && x.PartitionKey == modelId)))
.Returns(new DeviceModelCommand
{
Name = commandId,
Expand All @@ -91,15 +92,15 @@ public async Task ExecuteCommandStateUnderTestExpectedBehavior()
});

_ = this.mockCommandsTableClient.Setup(c => c.Query<TableEntity>(
It.Is<string>(x => x == $"RowKey eq '{ commandId }'"),
It.Is<string>(x => x == $"RowKey eq '{commandId}' and PartitionKey eq '{modelId}'"),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(Pageable<TableEntity>.FromPages(new[]
{
Page<TableEntity>.FromValues(new[]
{
new TableEntity(Guid.NewGuid().ToString(), commandId)
new TableEntity(modelId, commandId)
}, null, mockResponse.Object)
}));

Expand All @@ -119,7 +120,7 @@ public async Task ExecuteCommandStateUnderTestExpectedBehavior()
It.IsAny<Func<It.IsAnyType, Exception?, string>>()));

// Act
var result = await loRaWANDevicesController.ExecuteCommand(deviceId, commandId);
var result = await loRaWANDevicesController.ExecuteCommand(modelId, deviceId, commandId);

// Assert
Assert.IsNotNull(result);
Expand All @@ -133,12 +134,13 @@ public async Task ExecuteCommandFailedShouldReturnHttp400()
{
// Arrange
var loRaWANDevicesController = this.CreateLoRaWANDevicesController();
var modelId = Guid.NewGuid().ToString();
var deviceId = Guid.NewGuid().ToString();
var commandId = Guid.NewGuid().ToString();

var mockResponse = this.mockRepository.Create<Response>();
_ = this.mockDeviceModelCommandMapper
.Setup(c => c.GetDeviceModelCommand(It.Is<TableEntity>(x => x.RowKey == commandId)))
.Setup(c => c.GetDeviceModelCommand(It.Is<TableEntity>(x => x.RowKey == commandId && x.PartitionKey == modelId)))
.Returns(new DeviceModelCommand
{
Name = commandId,
Expand All @@ -147,15 +149,15 @@ public async Task ExecuteCommandFailedShouldReturnHttp400()
});

_ = this.mockCommandsTableClient.Setup(c => c.Query<TableEntity>(
It.Is<string>(x => x == $"RowKey eq '{ commandId }'"),
It.Is<string>(x => x == $"RowKey eq '{commandId}' and PartitionKey eq '{modelId}'"),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(Pageable<TableEntity>.FromPages(new[]
{
Page<TableEntity>.FromValues(new[]
{
new TableEntity(Guid.NewGuid().ToString(), commandId)
new TableEntity(modelId, commandId)
}, null, mockResponse.Object)
}));

Expand All @@ -175,7 +177,7 @@ public async Task ExecuteCommandFailedShouldReturnHttp400()
It.IsAny<Func<It.IsAnyType, Exception?, string>>()));

// Act
var result = await loRaWANDevicesController.ExecuteCommand(deviceId, commandId);
var result = await loRaWANDevicesController.ExecuteCommand(modelId, deviceId, commandId);

// Assert
Assert.IsNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@

private async Task ExecuteMethod(DeviceModelCommand method)
{
var result = await Http.PostAsJsonAsync($"api/lorawan/devices/{LoRaDevice.DeviceID}/_command/{method.Name}", method);
var result = await Http.PostAsJsonAsync($"api/lorawan/devices/{LoRaDevice.ModelId}/{LoRaDevice.DeviceID}/_command/{method.Name}", method);

if (result.IsSuccessStatusCode)
{
Snackbar.Add($"{method.Name} has been successfully executed!",
Expand All @@ -197,4 +198,4 @@
Snackbar.Add(await result.Content.ReadAsStringAsync(), Severity.Error, (option) => { option.VisibleStateDuration = 1000; });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ public override Task<IActionResult> Delete(string deviceID)
/// <summary>
/// Executes the command on the device..
/// </summary>
/// <param name="modelId">The model identifier.</param>
/// <param name="deviceId">The device identifier.</param>
/// <param name="commandId">The command identifier.</param>
/// <returns></returns>
/// <exception cref="System.FormatException">Incorrect port or invalid DevEui Format.</exception>
[HttpPost("{deviceId}/_command/{commandId}", Name = "POST Execute LoRaWAN command")]
public async Task<IActionResult> ExecuteCommand(string deviceId, string commandId)
[HttpPost("{modelId}/{deviceId}/_command/{commandId}", Name = "POST Execute LoRaWAN command")]
public async Task<IActionResult> ExecuteCommand(string modelId, string deviceId, string commandId)
{
var commandEntity = this.tableClientFactory
.GetDeviceCommands()
.Query<TableEntity>(filter: $"RowKey eq '{commandId}'")
.Query<TableEntity>(filter: $"RowKey eq '{commandId}' and PartitionKey eq '{modelId}'")
.Single();

var deviceModelCommand = this.deviceModelCommandMapper.GetDeviceModelCommand(commandEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) CGI France. All rights reserved.
// 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.Managers
Expand Down Expand Up @@ -40,7 +40,7 @@ public List<Command> RetrieveCommands(string deviceModel)
.GetDeviceCommands()
.Query<TableEntity>(filter: $"PartitionKey eq '{deviceModel}'");

foreach (TableEntity qEntity in queryResultsFilter)
foreach (var qEntity in queryResultsFilter)
{
commands.Add(
new Command()
Expand Down

0 comments on commit 16337df

Please sign in to comment.