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

Feature/907 alway send confirmed property on command #1037

Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,41 @@

namespace AzureIoTHub.Portal.Server.Tests.Unit.Managers
{
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Server.Managers;
using FluentAssertions;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using Microsoft.Extensions.DependencyInjection;
using RichardSzalay.MockHttp;

[TestFixture]
public class LoraDeviceMethodManagerTests : IDisposable
public class LoraDeviceMethodManagerTests : BackendUnitTest
{
#pragma warning disable CA2213 // Disposable fields should be disposed
private HttpClient mockHttpClient;
#pragma warning restore CA2213 // Disposable fields should be disposed
private MockRepository mockRepository;
private ILoraDeviceMethodManager loraDeviceMethodManager;

private Mock<HttpMessageHandler> httpMessageHandlerMock;

[SetUp]
public void SetUp()
public override void Setup()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);
base.Setup();

this.httpMessageHandlerMock = this.mockRepository.Create<HttpMessageHandler>();
this.mockHttpClient = new HttpClient(this.httpMessageHandlerMock.Object)
{
BaseAddress = new Uri("http://fake.local")
};
_ = ServiceCollection.AddSingleton<ILoraDeviceMethodManager, LoraDeviceMethodManager>();

Services = ServiceCollection.BuildServiceProvider();

this.loraDeviceMethodManager = Services.GetRequiredService<ILoraDeviceMethodManager>();
}

[Test]
public async Task ExecuteLoRaDeviceMessageThrowsArgumentNullExceptionWhenDeviceIdIsNull()
{
// Arrange
var loraDeviceMethodManager = new LoraDeviceMethodManager(this.mockHttpClient);

// Act
var act = () => loraDeviceMethodManager.ExecuteLoRaDeviceMessage(null, null);
var act = () => this.loraDeviceMethodManager.ExecuteLoRaDeviceMessage(null, null);

// Assert
_ = await act.Should().ThrowAsync<ArgumentNullException>();
Expand All @@ -54,11 +47,10 @@ public async Task ExecuteLoRaDeviceMessageThrowsArgumentNullExceptionWhenDeviceI
public async Task ExecuteLoRaDeviceMessageThrowsArgumentNullExceptionWhenCommandIsNull()
{
// Arrange
var loraDeviceMethodManager = new LoraDeviceMethodManager(this.mockHttpClient);
var deviceId = Guid.NewGuid().ToString();
var deviceId = Fixture.Create<string>();

// Act
var act = () => loraDeviceMethodManager.ExecuteLoRaDeviceMessage(deviceId, null);
var act = () => this.loraDeviceMethodManager.ExecuteLoRaDeviceMessage(deviceId, null);

// Assert
_ = await act.Should().ThrowAsync<ArgumentNullException>();
Expand All @@ -68,39 +60,38 @@ public async Task ExecuteLoRaDeviceMessageThrowsArgumentNullExceptionWhenCommand
public async Task ExecuteLoRaDeviceMessageMustBeSuccessfullWhenParametersAreProvided()
{
// Arrange
var loraDeviceMethodManager = new LoraDeviceMethodManager(this.mockHttpClient);
var deviceId = Guid.NewGuid().ToString();
var deviceId = Fixture.Create<string>();
var command = new DeviceModelCommand
{
Frame = Guid.NewGuid().ToString()
Frame = Fixture.Create<string>(),
Confirmed = Fixture.Create<bool>(),
Port = Fixture.Create<int>()
};

using var deviceResponseMock = new HttpResponseMessage();

deviceResponseMock.Content = new StringContent("{}", Encoding.UTF8, "application/json");

this.httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.Is<HttpRequestMessage>(req => req.RequestUri.LocalPath.Equals($"/api/cloudtodevicemessage/{deviceId}", StringComparison.OrdinalIgnoreCase) && req.Method == HttpMethod.Post), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync((HttpRequestMessage _, CancellationToken _) => deviceResponseMock)
.Verifiable();
var expectedRawPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(command.Frame));

_ = 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();

Check warning

Code scanning / CodeQL

Dereferenced variable may be null

Variable [loRaCloudToDeviceMessage](1) may be null here as suggested by [this](2) null check. Variable [loRaCloudToDeviceMessage](1) may be null here as suggested by [this](3) null check. Variable [loRaCloudToDeviceMessage](1) may be null here as suggested by [this](4) null check.
_ = 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 loraDeviceMethodManager.ExecuteLoRaDeviceMessage(deviceId, command);
var result = await this.loraDeviceMethodManager.ExecuteLoRaDeviceMessage(deviceId, command);

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

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace AzureIoTHub.Portal.Server.Managers
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Server.Extensions;

public class LoraDeviceMethodManager : ILoraDeviceMethodManager
{
Expand All @@ -26,7 +26,12 @@ public async Task<HttpResponseMessage> ExecuteLoRaDeviceMessage(string deviceId,
ArgumentNullException.ThrowIfNull(deviceId, nameof(deviceId));
ArgumentNullException.ThrowIfNull(command, nameof(command));

var body = command.ToDynamic();
var body = new LoRaCloudToDeviceMessage
{
RawPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(command.Frame)),
Fport = command.Port,
Confirmed = command.Confirmed
};

using var commandContent = JsonContent.Create(body);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Models.v10.LoRaWAN
{
using System.Text.Json.Serialization;

public class LoRaCloudToDeviceMessage
{
[JsonPropertyName("rawPayload")]
public string RawPayload { get; set; }

[JsonPropertyName("fport")]
public int Fport { get; set; }

[JsonPropertyName("confirmed")]
public bool Confirmed { get; set; }
}
}