This repository was archived by the owner on Jul 28, 2025. It is now read-only.
generated from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: MESH-Handshake #13
Merged
Merged
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
75d78ec
feat: migrations. docker, program and dbContext, csproj updated for …
Warren-Pitterson 6779b27
feat: db-setup no longer needed in compose
Warren-Pitterson d75f4dc
feat: connectionString no longer hardcoded
Warren-Pitterson de14772
chore: variable casing
Warren-Pitterson 2fd2c48
feat: logging added
Warren-Pitterson 42b2525
chore: tidy up
Warren-Pitterson a9729ae
chore: explicit directories
Warren-Pitterson 1ac6c1a
Merge branch 'main' into feat/Database-migration
Warren-Pitterson 6d6c015
feat: change targer from API to Mesh project
Warren-Pitterson fad274a
feat: refactor to target mesh, builds database with migrations,
Warren-Pitterson a1ef903
refactor: revert unnecessary API changes
Warren-Pitterson 12bb95d
Merge branch 'main' into feat/DTOSS-8740-MESH-Handshake
Warren-Pitterson 3acca93
feat: MeshHandshake function and tests
Warren-Pitterson 023db4a
chore: removed comment
Warren-Pitterson 547f6aa
chore: removed unnecessary usings
Warren-Pitterson 320b4f3
fix: removed package wrongfully added to incorrect project
Warren-Pitterson 2e40b7d
feat: IMeshHandshakeFunctionConfiguration added to program.cs
Warren-Pitterson e3170a6
feat: update .env example
Warren-Pitterson 547fa8f
feat: removed duplicate logging and updated tests
Warren-Pitterson 0bfbc8b
feat: removed unnecessary DI registration
Warren-Pitterson 2ca3fc0
test: sonarQube issue fix
Warren-Pitterson d38495b
refactor: remove redundant jump
Warren-Pitterson fe3ddbd
refactor: remove time from log statements
Warren-Pitterson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
src/ServiceLayer.Mesh/Configuration/IMeshHandshakeFunctionConfiguration.cs
This file contains hidden or 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,7 @@ | ||
| namespace ServiceLayer.Mesh.Configuration | ||
| { | ||
| public interface IMeshHandshakeFunctionConfiguration | ||
| { | ||
| string NbssMeshMailboxId { get; } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,43 @@ | ||
| namespace ServiceLayer.Mesh.Functions; | ||
| using Azure; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Extensions.Logging; | ||
| using NHS.MESH.Client.Contracts.Services; | ||
| using ServiceLayer.Mesh.Configuration; | ||
|
|
||
| public class MeshHandshakeFunction | ||
| namespace ServiceLayer.Mesh.Functions | ||
| { | ||
| public class MeshHandshakeFunction( | ||
| ILogger<MeshHandshakeFunction> logger, | ||
| IMeshOperationService meshOperationService, | ||
| IMeshHandshakeFunctionConfiguration configuration) | ||
| { | ||
| [Function("MeshHandshakeFunction")] | ||
| public async Task Run([TimerTrigger("%MeshHandshakeTimerExpression%")] TimerInfo myTimer) | ||
| { | ||
| logger.LogInformation("{FunctionName} started at: {Time}", nameof(MeshHandshakeFunction), DateTime.UtcNow); | ||
|
|
||
| try | ||
| { | ||
| var response = await meshOperationService.MeshHandshakeAsync(configuration.NbssMeshMailboxId); | ||
|
|
||
| if (response.IsSuccessful) | ||
| { | ||
| logger.LogInformation("Mesh handshake completed successfully for mailbox {MailboxId} at {Time}. Status: {Status}", | ||
| response.Response.MailboxId, DateTime.UtcNow, response.IsSuccessful); | ||
| } | ||
| else | ||
| { | ||
| logger.LogWarning("Mesh handshake failed for mailbox {MailboxId} at {Time}. Error: {Error}", | ||
| configuration.NbssMeshMailboxId, DateTime.UtcNow, response.Error); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "An error occurred during mesh handshake for mailbox {MailboxId} at {Time}", configuration.NbssMeshMailboxId, DateTime.UtcNow); | ||
| return; | ||
| } | ||
|
|
||
| logger.LogInformation("{FunctionName} completed at: {Time}", nameof(MeshHandshakeFunction), DateTime.UtcNow); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
124 changes: 124 additions & 0 deletions
124
tests/ServiceLayer.Mesh.Tests/Functions/MeshHandshakeFunctionTests.cs
This file contains hidden or 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,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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.