Skip to content

Commit

Permalink
Fix - Failed to execute LoRaWAN Command (#2887)
Browse files Browse the repository at this point in the history
* Fix command issue

Fix #1458

* Fix Command frame encoding

* fix-command/update unit test

* fix-command/add KeyManagementApiVersion

* fix-command/Delete Http issue

* fix-command/change-public to internal

* fix-command/add a new test for the payload

* Change test to make it use the real Service
  • Loading branch information
lucas-mrq authored Apr 16, 2024
1 parent 02901dc commit ff1b10a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ public async Task<HttpResponseMessage> ExecuteLoRaDeviceMessage(string deviceId,
ArgumentNullException.ThrowIfNull(deviceId, nameof(deviceId));
ArgumentNullException.ThrowIfNull(commandDto, nameof(commandDto));

// Convert the hex frame to a byte array
var hexFrame = Enumerable.Range(0, commandDto.Frame.Length / 2)
.Select(x => Convert.ToByte(commandDto.Frame.Substring(x * 2, 2), 16))
.ToArray();

// Convert the byte array to a base64 string
var rawPayload = Convert.ToBase64String(hexFrame);

var body = new LoRaCloudToDeviceMessage
{
RawPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(commandDto.Frame)),
RawPayload = rawPayload,
Fport = commandDto.Port,
Confirmed = commandDto.Confirmed
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace IoTHub.Portal.Tests.Unit.Infrastructure.Services
using NUnit.Framework;
using RichardSzalay.MockHttp;
using UnitTests.Bases;
using System.Linq;
using Fare;

[TestFixture]
public class LoRaWanManagementServiceTests : BackendUnitTest
Expand Down Expand Up @@ -63,14 +65,60 @@ public async Task ExecuteLoRaDeviceMessageMustBeSuccessfullWhenParametersAreProv
{
// Arrange
var deviceId = Fixture.Create<string>();

string regex = "[0-9A-F]{8,15}";

Xeger xeger = new Xeger(regex, new Random(0)); // Note zero in Random constructor

var command = new DeviceModelCommandDto
{
Frame = xeger.Generate(),
Confirmed = Fixture.Create<bool>(),
Port = Fixture.Create<int>()
};

var expectedRawPayload = Convert.ToBase64String(Enumerable.Range(0, command.Frame.Length / 2).Select(x => Convert.ToByte(command.Frame.Substring(x * 2, 2), 16)).ToArray());

_ = MockHttpClient.When(HttpMethod.Post, $"/api/cloudtodevicemessage/{deviceId}")
.With(m =>
{
_ = m.Content.Should().BeAssignableTo<JsonContent>();
var body = (JsonContent) m.Content;
var loRaCloudToDeviceMessage = (LoRaCloudToDeviceMessage)body?.Value;
_ = loRaCloudToDeviceMessage?.Should().NotBeNull();
_ = loRaCloudToDeviceMessage?.Fport.Should().Be(command.Port);
_ = loRaCloudToDeviceMessage?.Confirmed.Should().Be(command.Confirmed);
_ = loRaCloudToDeviceMessage?.RawPayload.Should().Be(expectedRawPayload);
return true;
})
.Respond(HttpStatusCode.Created);

// Act
var result = await this.loRaWanManagementService.ExecuteLoRaDeviceMessage(deviceId, command);

// Assert
_ = result.Should().NotBeNull();
_ = result.IsSuccessStatusCode.Should().BeTrue();
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}

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

var commandHex = "0113007801680100640064";

var command = new DeviceModelCommandDto
{
Frame = Fixture.Create<string>(),
Frame = commandHex,
Confirmed = Fixture.Create<bool>(),
Port = Fixture.Create<int>()
};

var expectedRawPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(command.Frame));
var expectedRawPayload = "ARMAeAFoAQBkAGQ=";

_ = MockHttpClient.When(HttpMethod.Post, $"/api/cloudtodevicemessage/{deviceId}")
.With(m =>
Expand Down

0 comments on commit ff1b10a

Please sign in to comment.