Skip to content

Commit

Permalink
Increase retrieved edge device logs #591 (#629)
Browse files Browse the repository at this point in the history
* Increase retrieved edge device logs #591

* Add and update unit tests on edge device logs

* Add unit tests on EdgeDeviceClientService

* Add ut on ModuleLogsDialog
  • Loading branch information
hocinehacherouf authored Apr 28, 2022
1 parent 344f181 commit aa6e384
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Controllers.V10
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using FluentAssertions;

[TestFixture]
public class EdgeDevicesControllerTests
Expand Down Expand Up @@ -373,7 +375,6 @@ public async Task DeleteDeviceAsyncStateUnderTestExpectedBehavior()
}

[TestCase("RestartModule", /*lang=json,strict*/ "{\"id\":\"aaa\",\"schemaVersion\":null}")]
[TestCase("GetModuleLogs", /*lang=json,strict*/ "{\"schemaVersion\":null,\"items\":[{\"id\":\"aaa\",\"filter\":{\"tail\":10}}],\"encoding\":\"none\",\"contentType\":\"json\"}")]
public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string expected)
{
// Arrange
Expand Down Expand Up @@ -404,13 +405,69 @@ public async Task ExecuteMethodShouldExecuteC2DMethod(string methodName, string
});

// Act
_ = await edgeDevicesController.ExecuteModuleMethod(
_ = await edgeDevicesController.ExecuteModuleMethod(
module,
deviceId,
methodName);

// Assert
this.mockRepository.VerifyAll();
}

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

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

_ = this.mockLogger.Setup(c => c.Log(
It.Is<LogLevel>(x => x == LogLevel.Information),
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception, string>>()));

_ = this.mockDeviceService.Setup(c => c.GetEdgeDeviceLogs(
It.Is<string>(x => x == deviceId),
It.Is<IoTEdgeModule>(x => x == edgeModule)))
.ReturnsAsync(new List<IoTEdgeDeviceLog>
{
new IoTEdgeDeviceLog
{
Id = deviceId,
Text = Guid.NewGuid().ToString(),
LogLevel = 1,
TimeStamp = DateTime.UtcNow
}
});

// Act
var result = await edgeDevicesController.GetEdgeDeviceLogs(deviceId, edgeModule);

// Assert
_ = result.Should().NotBeNull();
_ = result.Count().Should().Be(1);
}

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

// Act
var act = () => edgeDevicesController.GetEdgeDeviceLogs(deviceId, null);

// Assert
_ = await act.Should().ThrowAsync<ArgumentNullException>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.Pages.Edge_Devices
{
using System;
using System.Collections.Generic;
using AzureIoTHub.Portal.Client.Pages.Edge_Devices;
using AzureIoTHub.Portal.Client.Services;
using AzureIoTHub.Portal.Models.v10;
using Bunit;
using FluentAssertions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MudBlazor;
using MudBlazor.Interop;
using MudBlazor.Services;
using NUnit.Framework;

[TestFixture]
public class ModuleLogsDialogTests
{

#pragma warning disable CA2213 // Disposable fields should be disposed
private Bunit.TestContext testContext;
#pragma warning restore CA2213 // Disposable fields should be disposed

private MockRepository mockRepository;
private Mock<IEdgeDeviceClientService> edgeDeviceClientServiceMock;
private Mock<IDialogService> mockDialogService;

[SetUp]
public void SetUp()
{
this.testContext = new Bunit.TestContext();

this.mockRepository = new MockRepository(MockBehavior.Strict);
this.edgeDeviceClientServiceMock = this.mockRepository.Create<IEdgeDeviceClientService>();
this.mockDialogService = this.mockRepository.Create<IDialogService>();

_ = this.testContext.Services.AddMudServices();

_ = this.testContext.Services.AddSingleton(this.edgeDeviceClientServiceMock.Object);

_ = this.testContext.Services.AddSingleton(this.mockDialogService.Object);

_ = this.testContext.JSInterop.Setup<BoundingClientRect>("mudElementRef.getBoundingClientRect", _ => true);
_ = this.testContext.JSInterop.SetupVoid("mudPopover.connect", _ => true);
}



private IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters)
where TComponent : IComponent
{
return this.testContext.RenderComponent<TComponent>(parameters);
}

[Test]
public void ModuleLogsDialogParametersMustBeCorrect()
{
// Arrange
var deviceId = Guid.NewGuid().ToString();

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

var expectedLog = new IoTEdgeDeviceLog
{
Id = deviceId,
Text = Guid.NewGuid().ToString(),
LogLevel = 1,
TimeStamp = DateTime.UtcNow
};

var expectedLogs = new List<IoTEdgeDeviceLog>()
{
expectedLog
};

_ = edgeDeviceClientServiceMock.Setup(c => c.GetEdgeDeviceLogs(It.Is<string>(x => x.Equals(deviceId, StringComparison.Ordinal)), It.Is<IoTEdgeModule>(x => x.Equals(edgeModule))))
.ReturnsAsync(expectedLogs);

// Act
var cut = RenderComponent<ModuleLogsDialog>(ComponentParameter.CreateParameter("deviceId", deviceId), ComponentParameter.CreateParameter("edgeModule", edgeModule));

// Assert
_ = cut.Instance.deviceId.Should().Be(deviceId);
_ = cut.Instance.edgeModule.Should().Be(edgeModule);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Services
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Constants;
using FluentAssertions;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;

[TestFixture]
Expand Down Expand Up @@ -755,5 +759,125 @@ public async Task ExecuteC2DMethodStateUnderTestExpectedBehavior()
Assert.AreEqual(200, result.Status);
this.mockRepository.VerifyAll();
}

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

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

var method = new CloudToDeviceMethod(CloudToDeviceMethods.GetModuleLogs);

var payload = JsonConvert.SerializeObject(new
{
schemaVersion = edgeModule.Version,
items = new[]
{
new
{
id = edgeModule.ModuleName,
filter = new
{
tail = 300
}
}
},
encoding = "none",
contentType = "json"
});

_ = method.SetPayloadJson(payload);


var logger = Mock.Of<ILogger<DeviceService>>();

_ = this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync(
It.Is<string>(x => x == deviceId),
It.Is<string>(x => x == "$edgeAgent"),
It.Is<CloudToDeviceMethod>(x => x.MethodName == method.MethodName && x.GetPayloadAsJson() == payload),
It.IsAny<CancellationToken>()))
.ReturnsAsync(new CloudToDeviceMethodResult
{
Status = 200
});

var deviceService = new DeviceService(
logger,
this.mockRegistryManager.Object,
this.mockServiceClient.Object);

// Act
var result = await deviceService.GetEdgeDeviceLogs(deviceId, edgeModule);

// Assert
_ = result.Should().NotBeNull();
_ = result.Count().Should().Be(0);
}

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

var edgeModule = new IoTEdgeModule
{
Version = "1.0",
ModuleName = Guid.NewGuid().ToString()
};

var method = new CloudToDeviceMethod(CloudToDeviceMethods.GetModuleLogs);

var payload = JsonConvert.SerializeObject(new
{
schemaVersion = edgeModule.Version,
items = new[]
{
new
{
id = edgeModule.ModuleName,
filter = new
{
tail = 300
}
}
},
encoding = "none",
contentType = "json"
});

_ = method.SetPayloadJson(payload);


var logger = Mock.Of<ILogger<DeviceService>>();

_ = this.mockServiceClient.Setup(c => c.InvokeDeviceMethodAsync(
It.Is<string>(x => x == deviceId),
It.Is<string>(x => x == "$edgeAgent"),
It.Is<CloudToDeviceMethod>(x => x.MethodName == method.MethodName && x.GetPayloadAsJson() == payload),
It.IsAny<CancellationToken>()))
.ReturnsAsync(new CloudToDeviceMethodResult
{
Status = 500
});

var deviceService = new DeviceService(
logger,
this.mockRegistryManager.Object,
this.mockServiceClient.Object);

// Act
var result = await deviceService.GetEdgeDeviceLogs(deviceId, edgeModule);

// Assert
_ = result.Should().NotBeNull();
_ = result.Count().Should().Be(0);
}
}
}
Loading

0 comments on commit aa6e384

Please sign in to comment.