Skip to content

Commit 736a1d6

Browse files
test: added further basic unit tests around the MeshPoller and shared Caas subscribe code
1 parent 6ae4fdd commit 736a1d6

5 files changed

Lines changed: 183 additions & 1 deletion

File tree

tests/UnitTests/DemographicServicesTests/ManageCaasSubscriptionTests/ManageCaasSubscriptionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,22 @@ public void Config_MissingMailboxes_FailsValidation()
269269
Assert.IsTrue(results.Any(r => r.MemberNames.Contains("CaasToMailbox")));
270270
Assert.IsTrue(results.Any(r => r.MemberNames.Contains("CaasFromMailbox")));
271271
}
272+
273+
[TestMethod]
274+
public void Config_MissingMeshApiBaseUrl_FailsValidation()
275+
{
276+
var cfg = new ManageCaasSubscriptionConfig
277+
{
278+
CaasToMailbox = "TEST_TO",
279+
CaasFromMailbox = "TEST_FROM",
280+
MeshCaasSharedKey = "dummy"
281+
};
282+
283+
var context = new ValidationContext(cfg);
284+
var results = new System.Collections.Generic.List<ValidationResult>();
285+
var isValid = Validator.TryValidateObject(cfg, context, results, validateAllProperties: true);
286+
287+
Assert.IsFalse(isValid);
288+
Assert.IsTrue(results.Any(r => r.MemberNames.Contains("MeshApiBaseUrl")));
289+
}
272290
}

tests/UnitTests/DemographicServicesTests/ManageCaasSubscriptionTests/ManageCaasSubscriptionTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<ProjectReference Include="..\..\..\TestUtils\TestUtils.csproj" />
2222
<ProjectReference Include="..\..\..\..\application\CohortManager\src\Functions\Shared\Common\Common.csproj" />
2323
<ProjectReference Include="..\..\..\..\application\CohortManager\src\Functions\DemographicServices\ManageCaasSubscription\ManageCaasSubscription.csproj" />
24+
<ProjectReference Include="..\..\..\..\application\CohortManager\src\Functions\Shared\dotnet-mesh-client\application\DotNetMeshClient\NHS.Mesh.Client\NHS.Mesh.Client.csproj" />
2425
</ItemGroup>
2526
</Project>
26-
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace NHS.CohortManager.Tests.UnitTests.DemographicServicesTests;
2+
3+
using System.Security.Cryptography.X509Certificates;
4+
using System.IO;
5+
using System;
6+
using Common;
7+
8+
[TestClass]
9+
public class MeshMailboxExtensionTests
10+
{
11+
[TestMethod]
12+
public async Task GetCACertificates_FromFilePath_ReturnsCollection()
13+
{
14+
var path = FindInParents("nems_certificate.pem");
15+
Assert.IsTrue(File.Exists(path), $"Test certificate not found at {path}");
16+
var certs = await MeshMailboxExtension.GetCACertificates(path, null);
17+
Assert.IsNotNull(certs);
18+
Assert.IsInstanceOfType(certs, typeof(X509Certificate2Collection));
19+
Assert.IsTrue(certs!.Count > 0);
20+
}
21+
22+
[TestMethod]
23+
public async Task GetCACertificates_NoInputs_ReturnsNull()
24+
{
25+
var certs = await MeshMailboxExtension.GetCACertificates(null, null);
26+
Assert.IsNull(certs);
27+
}
28+
private static string FindInParents(string fileName)
29+
{
30+
var dir = new DirectoryInfo(AppContext.BaseDirectory);
31+
while (dir != null)
32+
{
33+
var candidate = Path.Combine(dir.FullName, fileName);
34+
if (File.Exists(candidate)) return candidate;
35+
dir = dir.Parent;
36+
}
37+
return fileName; // will fail later if not found
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace NHS.CohortManager.Tests.UnitTests.DemographicServicesTests;
2+
3+
using System.Threading.Tasks;
4+
using Common;
5+
using Microsoft.Extensions.Logging;
6+
using Moq;
7+
using NHS.MESH.Client.Contracts.Services;
8+
using NHS.MESH.Client.Models;
9+
10+
[TestClass]
11+
public class MeshPollerTests
12+
{
13+
private readonly Mock<ILogger<MeshPoller>> _logger = new();
14+
private readonly Mock<IMeshOperationService> _meshOps = new();
15+
16+
[TestMethod]
17+
public async Task ExecuteHandshake_Success_ReturnsTrue()
18+
{
19+
_meshOps
20+
.Setup(m => m.MeshHandshakeAsync(It.IsAny<string>()))
21+
.ReturnsAsync(new MeshResponse<HandshakeResponse> { IsSuccessful = true, Response = new HandshakeResponse { MailboxId = "MAILBOX" } });
22+
23+
var sut = new MeshPoller(_logger.Object, _meshOps.Object);
24+
var result = await sut.ExecuteHandshake("MAILBOX");
25+
Assert.IsTrue(result);
26+
}
27+
28+
[TestMethod]
29+
public async Task ExecuteHandshake_Failure_ReturnsFalse()
30+
{
31+
_meshOps
32+
.Setup(m => m.MeshHandshakeAsync(It.IsAny<string>()))
33+
.ReturnsAsync(new MeshResponse<HandshakeResponse> { IsSuccessful = false, Error = new APIErrorResponse { ErrorCode = "500", ErrorDescription = "err" } });
34+
35+
var sut = new MeshPoller(_logger.Object, _meshOps.Object);
36+
var result = await sut.ExecuteHandshake("MAILBOX");
37+
Assert.IsFalse(result);
38+
}
39+
40+
[TestMethod]
41+
public async Task ShouldExecuteHandshake_NotImplemented_Throws()
42+
{
43+
var sut = new MeshPoller(_logger.Object, _meshOps.Object);
44+
await Assert.ThrowsExceptionAsync<NotImplementedException>(() => sut.ShouldExecuteHandshake("MAILBOX", "config.json"));
45+
}
46+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace NHS.CohortManager.Tests.UnitTests.DemographicServicesTests;
2+
3+
using System.Threading.Tasks;
4+
using Common;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Options;
7+
using Moq;
8+
using NHS.MESH.Client.Contracts.Services;
9+
using NHS.MESH.Client.Models;
10+
11+
[TestClass]
12+
public class MeshSendCaasSubscribeTests
13+
{
14+
private readonly Mock<ILogger<MeshSendCaasSubscribe>> _logger = new();
15+
private readonly Mock<IMeshOutboxService> _meshOutbox = new();
16+
17+
private MeshSendCaasSubscribe CreateSut(string workflowId = "WF-CAAS-SUB")
18+
{
19+
var cfg = Options.Create(new MeshSendCaasSubscribeConfig { SendCaasWorkflowId = workflowId });
20+
return new MeshSendCaasSubscribe(_logger.Object, _meshOutbox.Object, cfg);
21+
}
22+
23+
[TestMethod]
24+
public async Task SendSubscriptionRequest_Success_SendsExpectedAttachment_AndReturnsMessageId()
25+
{
26+
// Arrange
27+
string? capturedFrom = null, capturedTo = null, capturedWorkflow = null;
28+
FileAttachment? capturedFile = null;
29+
_meshOutbox
30+
.Setup(m => m.SendCompressedMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<FileAttachment>(), null, null, false))
31+
.Callback((string from, string to, string workflow, FileAttachment file, string? _, string? __, bool ___) =>
32+
{
33+
capturedFrom = from; capturedTo = to; capturedWorkflow = workflow; capturedFile = file;
34+
})
35+
.ReturnsAsync(new MeshResponse<SendMessageResponse>
36+
{
37+
IsSuccessful = true,
38+
Response = new SendMessageResponse { MessageId = "MSG123" }
39+
});
40+
41+
var sut = CreateSut();
42+
43+
// Act
44+
var result = await sut.SendSubscriptionRequest(9000000009L, "TO_BOX", "FROM_BOX");
45+
46+
// Assert
47+
Assert.AreEqual("MSG123", result);
48+
Assert.AreEqual("FROM_BOX", capturedFrom);
49+
Assert.AreEqual("TO_BOX", capturedTo);
50+
Assert.AreEqual("WF-CAAS-SUB", capturedWorkflow);
51+
Assert.IsNotNull(capturedFile);
52+
Assert.AreEqual("CaaSSubscribe.parquet", capturedFile!.FileName);
53+
Assert.AreEqual("application/octet-stream", capturedFile.ContentType);
54+
Assert.IsNotNull(capturedFile.Content);
55+
Assert.IsTrue(capturedFile.Content.Length > 0);
56+
}
57+
58+
[TestMethod]
59+
public async Task SendSubscriptionRequest_Failure_ReturnsNull()
60+
{
61+
// Arrange
62+
_meshOutbox
63+
.Setup(m => m.SendCompressedMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<FileAttachment>(), null, null, false))
64+
.ReturnsAsync(new MeshResponse<SendMessageResponse>
65+
{
66+
IsSuccessful = false,
67+
Error = new APIErrorResponse { ErrorCode = "500", ErrorDescription = "boom" }
68+
});
69+
70+
var sut = CreateSut();
71+
72+
// Act
73+
var result = await sut.SendSubscriptionRequest(9000000009L, "TO_BOX", "FROM_BOX");
74+
75+
// Assert
76+
Assert.IsNull(result);
77+
}
78+
}
79+

0 commit comments

Comments
 (0)