Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 26cddd0

Browse files
committed
introduce app configuration
1 parent e70e497 commit 26cddd0

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ AZURE_WEB_JOBS_STORAGE=UseDevelopmentStorage=true
77
DatabaseConnectionString=Server=${DATABASE_HOST};Database=${DATABASE_NAME};User Id=${DATABASE_USER};Password=${DATABASE_PASSWORD};TrustServerCertificate=True
88
AzureWebJobsStorage=UseDevelopmentStorage=true
99
FUNCTIONS_WORKER_RUNTIME=dotnet-isolated
10-
MailboxId=X26ABC1
1110
MeshSharedKey=TestKey
1211
MeshPassword=password
13-
NBSSMailBoxId=X26ABC1
12+
NbssMailboxId=X26ABC1
1413
MeshApiBaseUrl=http://localhost:8700/messageexchange
1514
ASPNETCORE_ENVIRONMENT=Development
1615
FileDiscoveryTimerExpression=*/5 * * * *
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public class AppConfiguration : IFileDiscoveryFunctionConfiguration
4+
{
5+
public string NbssMeshMailboxId => GetRequired("NbssMailboxId");
6+
7+
private static string GetRequired(string key)
8+
{
9+
var value = Environment.GetEnvironmentVariable(key);
10+
11+
if (string.IsNullOrEmpty(value))
12+
{
13+
throw new InvalidOperationException($"Environment variable '{key}' is not set or is empty.");
14+
}
15+
16+
return value;
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public interface IFileDiscoveryFunctionConfiguration
4+
{
5+
string NbssMeshMailboxId { get; }
6+
}

src/ServiceLayer.Mesh/Functions/FileDiscoveryFunction.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.Extensions.Logging;
44
using NHS.MESH.Client.Contracts.Services;
5+
using ServiceLayer.Mesh.Configuration;
56
using ServiceLayer.Mesh.Data;
67
using ServiceLayer.Mesh.Messaging;
78
using ServiceLayer.Mesh.Models;
@@ -10,6 +11,7 @@ namespace ServiceLayer.Mesh.Functions
1011
{
1112
public class FileDiscoveryFunction(
1213
ILogger<FileDiscoveryFunction> logger,
14+
IFileDiscoveryFunctionConfiguration configuration,
1315
IMeshInboxService meshInboxService,
1416
ServiceLayerDbContext serviceLayerDbContext,
1517
IFileExtractQueueClient fileExtractQueueClient)
@@ -19,11 +21,7 @@ public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo
1921
{
2022
logger.LogInformation($"DiscoveryFunction started at: {DateTime.Now}");
2123

22-
// TODO - abstract this out into an injectable configuration interface for testing purposes
23-
var mailboxId = Environment.GetEnvironmentVariable("NBSSMailBoxId")
24-
?? throw new InvalidOperationException($"Environment variable 'NBSSMailBoxId' is not set or is empty.");
25-
26-
var response = await meshInboxService.GetMessagesAsync(mailboxId);
24+
var response = await meshInboxService.GetMessagesAsync(configuration.NbssMeshMailboxId);
2725

2826
foreach (var messageId in response.Response.Messages)
2927
{
@@ -38,7 +36,7 @@ public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo
3836
{
3937
FileId = messageId,
4038
FileType = MeshFileType.NbssAppointmentEvents,
41-
MailboxId = mailboxId,
39+
MailboxId = configuration.NbssMeshMailboxId,
4240
Status = MeshFileStatus.Discovered,
4341
FirstSeenUtc = DateTime.UtcNow,
4442
LastUpdatedUtc = DateTime.UtcNow

src/ServiceLayer.Mesh/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using NHS.MESH.Client;
88
using ServiceLayer.Mesh.Data;
99
using Azure.Storage.Blobs;
10+
using ServiceLayer.Mesh.Configuration;
1011
using ServiceLayer.Mesh.Messaging;
1112

1213
var host = new HostBuilder()
@@ -19,7 +20,7 @@
1920
// MESH Client config
2021
services
2122
.AddMeshClient(_ => _.MeshApiBaseUrl = Environment.GetEnvironmentVariable("MeshApiBaseUrl"))
22-
.AddMailbox(Environment.GetEnvironmentVariable("NBSSMailBoxId"), new NHS.MESH.Client.Configuration.MailboxConfiguration
23+
.AddMailbox(Environment.GetEnvironmentVariable("NbssMailboxId"), new NHS.MESH.Client.Configuration.MailboxConfiguration
2324
{
2425
Password = Environment.GetEnvironmentVariable("MeshPassword"),
2526
SharedKey = Environment.GetEnvironmentVariable("MeshSharedKey"),
@@ -57,6 +58,8 @@
5758
Environment.GetEnvironmentVariable("AzureWebJobsStorage"),
5859
Environment.GetEnvironmentVariable("BlobContainerName"));
5960
});
61+
62+
services.AddTransient<IFileDiscoveryFunctionConfiguration, AppConfiguration>();
6063
});
6164

6265

tests/ServiceLayer.Mesh.Tests/Functions/FileDiscoveryFunctionTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Moq;
44
using NHS.MESH.Client.Contracts.Services;
55
using NHS.MESH.Client.Models;
6+
using ServiceLayer.Mesh.Configuration;
67
using ServiceLayer.Mesh.Data;
78
using ServiceLayer.Mesh.Functions;
89
using ServiceLayer.Mesh.Messaging;
@@ -32,11 +33,12 @@ public FileDiscoveryFunctionTests()
3233

3334
_dbContext = new ServiceLayerDbContext(options);
3435

35-
Environment.SetEnvironmentVariable("NBSSMailBoxId", "test-mailbox");
36-
Environment.SetEnvironmentVariable("QueueUrl", "https://fakestorageaccount.queue.core.windows.net/testqueue");
36+
var functionConfiguration = new Mock<IFileDiscoveryFunctionConfiguration>();
37+
functionConfiguration.Setup(c => c.NbssMeshMailboxId).Returns("test-mailbox");
3738

3839
_function = new FileDiscoveryFunction(
3940
_loggerMock.Object,
41+
functionConfiguration.Object,
4042
_meshInboxServiceMock.Object,
4143
_dbContext,
4244
_queueClientMock.Object

0 commit comments

Comments
 (0)