This repository was archived by the owner on Jul 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMeshHandshakeFunctionTests.cs
More file actions
124 lines (109 loc) · 4.43 KB
/
MeshHandshakeFunctionTests.cs
File metadata and controls
124 lines (109 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Moq;
using NHS.MESH.Client.Contracts.Services;
using NHS.MESH.Client.Models;
using ServiceLayer.Mesh.Configuration;
using ServiceLayer.Mesh.Functions;
namespace ServiceLayer.Mesh.Tests.Functions;
public class MeshHandshakeFunctionTests
{
private readonly Mock<ILogger<MeshHandshakeFunction>> _loggerMock;
private readonly Mock<IMeshOperationService> _meshOperationServiceMock;
private readonly Mock<IMeshHandshakeFunctionConfiguration> _configurationMock;
private readonly MeshHandshakeFunction _function;
private readonly TimerInfo _timerInfo;
private const string TestMailboxId = "test-mailbox-123";
public MeshHandshakeFunctionTests()
{
_loggerMock = new Mock<ILogger<MeshHandshakeFunction>>();
_meshOperationServiceMock = new Mock<IMeshOperationService>();
_configurationMock = new Mock<IMeshHandshakeFunctionConfiguration>();
_timerInfo = new TimerInfo();
_configurationMock.Setup(c => c.NbssMeshMailboxId).Returns(TestMailboxId);
_function = new MeshHandshakeFunction(
_loggerMock.Object,
_meshOperationServiceMock.Object,
_configurationMock.Object
);
}
[Fact]
public async Task Run_SuccessfulHandshake_LogsSuccessAndCompletion()
{
// Arrange
var successfulResponse = new MeshResponse<HandshakeResponse>
{
IsSuccessful = true,
Response = new HandshakeResponse { MailboxId = TestMailboxId }
};
_meshOperationServiceMock
.Setup(s => s.MeshHandshakeAsync(TestMailboxId))
.ReturnsAsync(successfulResponse);
// Act
await _function.Run(_timerInfo);
// Assert
_meshOperationServiceMock.Verify(s => s.MeshHandshakeAsync(TestMailboxId), Times.Once());
VerifyLogMessage(LogLevel.Information, "MeshHandshakeFunction started at");
VerifyLogMessage(LogLevel.Information, "Mesh handshake completed successfully");
VerifyLogMessage(LogLevel.Information, "MeshHandshakeFunction completed at");
}
[Fact]
public async Task Run_FailedHandshake_LogsWarningAndCompletion()
{
// Arrange
var failedResponse = new MeshResponse<HandshakeResponse>
{
IsSuccessful = false,
Error = new APIErrorResponse
{
ErrorDescription = "Authentication failed"
}
};
_meshOperationServiceMock
.Setup(s => s.MeshHandshakeAsync(TestMailboxId))
.ReturnsAsync(failedResponse);
// Act
await _function.Run(_timerInfo);
// Assert
_meshOperationServiceMock.Verify(s => s.MeshHandshakeAsync(TestMailboxId), Times.Once());
VerifyLogMessage(LogLevel.Information, "MeshHandshakeFunction started at");
VerifyLogMessage(LogLevel.Warning, "Mesh handshake failed");
VerifyLogMessage(LogLevel.Information, "MeshHandshakeFunction completed at");
}
[Fact]
public async Task Run_ExceptionThrown_LogsErrorAndCompletion()
{
// Arrange
_meshOperationServiceMock.Reset();
_loggerMock.Reset();
var expectedException = new InvalidOperationException("Connection failed");
_meshOperationServiceMock
.Setup(s => s.MeshHandshakeAsync(TestMailboxId))
.ThrowsAsync(expectedException);
// Act
await _function.Run(_timerInfo);
// Assert
_meshOperationServiceMock.Verify(s => s.MeshHandshakeAsync(TestMailboxId), Times.Once());
VerifyLogMessage(LogLevel.Information, "MeshHandshakeFunction started at");
VerifyLogMessage(LogLevel.Error, "An error occurred during mesh handshake");
_loggerMock.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
expectedException,
It.IsAny<Func<It.IsAnyType, Exception, string>>()),
Times.Once);
}
private void VerifyLogMessage(LogLevel level, string expectedMessage)
{
_loggerMock.Verify(
x => x.Log(
level,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString().Contains(expectedMessage)),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception, string>>()),
Times.Once);
}
}