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

Fix - Failed to execute LoRaWAN Command #2887

Merged
merged 8 commits into from
Apr 16, 2024
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 @@ -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
kbeaugrand marked this conversation as resolved.
Show resolved Hide resolved
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
Loading