-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
611 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ub.Portal.Server.Tests.Unit/ServicesHealthCheck/LoRaManagementKeyFacadeHealthCheckTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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.ServicesHealthCheck | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AzureIoTHub.Portal.Server.Managers; | ||
using AzureIoTHub.Portal.Server.ServicesHealthCheck; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class LoRaManagementKeyFacadeHealthCheckTest | ||
{ | ||
private MockRepository mockRepository; | ||
private Mock<ILoraDeviceMethodManager> mockoraDeviceMethodManager; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
this.mockRepository = new MockRepository(MockBehavior.Strict); | ||
|
||
this.mockoraDeviceMethodManager = this.mockRepository.Create<ILoraDeviceMethodManager>(); | ||
} | ||
|
||
private LoRaManagementKeyFacadeHealthCheck CreateService() | ||
{ | ||
return new LoRaManagementKeyFacadeHealthCheck(this.mockoraDeviceMethodManager.Object); | ||
} | ||
|
||
[Test] | ||
public async Task CheckHealthAsyncStateUnderTestReturnHealthy() | ||
{ | ||
// Arrange | ||
var healthService = CreateService(); | ||
|
||
var healthCheckContext = new HealthCheckContext(); | ||
var token = new CancellationToken(canceled:false); | ||
|
||
var mockHttpMessage = this.mockRepository.Create<HttpResponseMessage>(System.Net.HttpStatusCode.OK); | ||
|
||
_ = this.mockoraDeviceMethodManager | ||
.Setup(c => c.CheckAzureFunctionReturn(It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(mockHttpMessage.Object); | ||
|
||
// Act | ||
var result = await healthService.CheckHealthAsync(healthCheckContext, token); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.AreEqual(HealthStatus.Healthy, result.Status); | ||
} | ||
|
||
[Test] | ||
public async Task CheckHealthAsyncStateUnderTestReturnUnhealthy() | ||
{ | ||
// Arrange | ||
var healthService = CreateService(); | ||
|
||
var healthRegistration = new HealthCheckRegistration(Guid.NewGuid().ToString(), healthService, HealthStatus.Unhealthy, new List<string>()); | ||
var healthCheckContext = new HealthCheckContext() | ||
{ | ||
Registration = healthRegistration | ||
}; | ||
var token = new CancellationToken(canceled:false); | ||
|
||
var mockHttpMessage = this.mockRepository.Create<HttpResponseMessage>(System.Net.HttpStatusCode.BadRequest); | ||
|
||
_ = this.mockoraDeviceMethodManager | ||
.Setup(c => c.CheckAzureFunctionReturn(It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(mockHttpMessage.Object); | ||
|
||
// Act | ||
var result = await healthService.CheckHealthAsync(healthCheckContext, token); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.AreEqual(HealthStatus.Unhealthy, result.Status); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
....Portal.Server.Tests.Unit/ServicesHealthCheck/ProvisioningServiceClientHealthCheckTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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.ServicesHealthCheck | ||
{ | ||
using System; | ||
using System.Security.Cryptography; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AzureIoTHub.Portal.Server.ServicesHealthCheck; | ||
using AzureIoTHub.Portal.Server.Wrappers; | ||
using Microsoft.Azure.Devices.Provisioning.Service; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class ProvisioningServiceClientHealthCheckTest | ||
{ | ||
private MockRepository mockRepository; | ||
private Mock<IProvisioningServiceClient> mockDps; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
this.mockRepository = new MockRepository(MockBehavior.Strict); | ||
|
||
this.mockDps = this.mockRepository.Create<IProvisioningServiceClient>(); | ||
} | ||
|
||
private ProvisioningServiceClientHealthCheck CreateHealthService() | ||
{ | ||
return new ProvisioningServiceClientHealthCheck(this.mockDps.Object); | ||
} | ||
|
||
[Test] | ||
public async Task CheckHealthAsyncStateUnderTestReturnHealthy() | ||
{ | ||
// Arrange | ||
var healthService = CreateHealthService(); | ||
|
||
var healthCheckContext = new HealthCheckContext(); | ||
var token = new CancellationToken(canceled:false); | ||
|
||
var attestation = new SymmetricKeyAttestation(GenerateKey(), GenerateKey()); | ||
var enrollmentGroup = new EnrollmentGroup("enrollmentId", attestation); | ||
|
||
_ = this.mockDps | ||
.Setup(c => c.CreateOrUpdateEnrollmentGroupAsync(It.IsAny<EnrollmentGroup>())) | ||
.ReturnsAsync(enrollmentGroup); | ||
|
||
_ = this.mockDps | ||
.Setup(c => c.GetEnrollmentGroupAsync(It.IsAny<string>())) | ||
.ReturnsAsync(enrollmentGroup); | ||
|
||
_ = this.mockDps | ||
.Setup(c => c.DeleteEnrollmentGroupAsync(It.IsAny<EnrollmentGroup>(), It.IsAny<CancellationToken>())); | ||
|
||
// Act | ||
var result = await healthService.CheckHealthAsync(healthCheckContext, token); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.AreEqual(HealthStatus.Healthy, result.Status); | ||
} | ||
|
||
private static string GenerateKey() | ||
{ | ||
const int length = 48; | ||
var rnd = RandomNumberGenerator.GetBytes(length); | ||
|
||
return Convert.ToBase64String(rnd); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.