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

Commit 803ce0b

Browse files
committed
feat: its now sending a file to mesh!
1 parent dabee55 commit 803ce0b

File tree

3 files changed

+91
-26
lines changed

3 files changed

+91
-26
lines changed

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

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
using Microsoft.Data.SqlClient;
66
using Moq;
77

8+
using System;
9+
using System.Net.Http;
10+
using System.Net.Http.Headers;
11+
using System.Threading.Tasks;
12+
using ServiceLayer.Data;
13+
814
namespace ServiceLayer.Mesh.Tests.Integration;
915

1016
public class IntegrationTests
@@ -13,22 +19,39 @@ public class IntegrationTests
1319

1420
public IntegrationTests()
1521
{
16-
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
22+
23+
}
24+
25+
private async Task SetupEnvironment()
26+
{
27+
var environment = "development";
1728
if (environment == null)
1829
{
1930
throw new InvalidOperationException("ASPNETCORE_ENVIRONMENT environment variable is not set of is empty.");
2031
}
2132
if (environment == "development")
2233
{
23-
RunCommand("podman compose up");
34+
RunCommand("podman compose up -d");
2435
}
2536
if (environment == "production")
2637
{
27-
RunCommand("docker compose up");
38+
RunCommand("docker compose up -d");
2839
}
2940

30-
// Wait for SQL Server to be reachable
31-
//await WaitForSqlServerAsync();
41+
bool environmentIsUp = false;
42+
43+
while (environmentIsUp == false)
44+
{
45+
var responce = await HttpHelper.SendHttpRequestAsync(HttpMethod.Get, "http://localhost:7072/api/health");
46+
if (responce.IsSuccessStatusCode)
47+
{
48+
environmentIsUp = true;
49+
}
50+
else
51+
{
52+
await Task.Delay(1000);
53+
}
54+
}
3255
}
3356

3457
public void Teardown()
@@ -38,13 +61,32 @@ public void Teardown()
3861
}
3962

4063
[Fact]
41-
public async Task ShouldWriteToDatabase()
64+
public async Task EndToEndTest()
4265
{
43-
using var sql = new SqlConnection(ConnectionString);
44-
await sql.OpenAsync();
66+
await SetupEnvironment();
67+
68+
byte[] binaryData = await File.ReadAllBytesAsync("TestData/KMK_20250212095121_APPT_87.dat");
69+
var content = new ByteArrayContent(binaryData);
70+
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
71+
72+
var response = await HttpHelper.SendHttpRequestAsync(
73+
HttpMethod.Post,
74+
"http://localhost:8700/messageexchange/X26ABC1/outbox",
75+
content,
76+
headers =>
77+
{
78+
headers.Add("Authorization", "NHSMESH X26ABC1:a42f77b9-58de-4b45-b599-2d5bf320b44d:0:202407291437:e3005627136e01706efabcfe72269bc8da3192e90a840ab344ab7f82a39bb5c6");
79+
headers.Add("Mex-Filename", "KMK_20250212095121_APPT_87.dat");
80+
headers.Add("Mex-From", "X26ABC1");
81+
headers.Add("Mex-To", "X26ABC1");
82+
headers.Add("Mex-Workflowid", "API-DOCS-TEST");
83+
headers.Add("User-Agent", "HTTPie");
84+
}
85+
);
4586

46-
// var count = await sql.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM SomeTable");
47-
// Assert.IsTrue(count > 0, "Expected at least one row in SomeTable.");
87+
await Task.Delay(5000);
88+
89+
Teardown();
4890
}
4991

5092
private void RunCommand(string command)
@@ -65,24 +107,38 @@ private void RunCommand(string command)
65107
throw new Exception($"Command failed: {command}\n{process.StandardError.ReadToEnd()}");
66108
}
67109
}
110+
}
111+
68112

69-
private async Task WaitForSqlServerAsync(int timeoutSeconds = 60)
113+
114+
public static class HttpHelper
115+
{
116+
private static readonly HttpClient _client = new HttpClient();
117+
118+
public static async Task<HttpResponseMessage> SendHttpRequestAsync(
119+
HttpMethod method,
120+
string url,
121+
HttpContent? content = null,
122+
Action<HttpRequestHeaders>? configureHeaders = null)
70123
{
71-
var start = DateTime.UtcNow;
72-
while ((DateTime.UtcNow - start).TotalSeconds < timeoutSeconds)
124+
var request = new HttpRequestMessage(method, url)
73125
{
74-
try
75-
{
76-
using var sql = new SqlConnection(ConnectionString);
77-
await sql.OpenAsync();
78-
return; // Success
79-
}
80-
catch
81-
{
82-
await Task.Delay(1000);
83-
}
84-
}
126+
Content = content
127+
};
128+
129+
// Customize headers if provided
130+
configureHeaders?.Invoke(request.Headers);
85131

86-
throw new TimeoutException("SQL Server did not become available in time.");
132+
try
133+
{
134+
var response = await _client.SendAsync(request);
135+
response.EnsureSuccessStatusCode(); // Throw if not a success status
136+
return response;
137+
}
138+
catch (HttpRequestException ex)
139+
{
140+
Console.WriteLine($"HTTP Request failed: {ex.Message}");
141+
throw;
142+
}
87143
}
88144
}

tests/ServiceLayer.Mesh.Tests/ServiceLayer.Mesh.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
<ItemGroup>
2020
<ProjectReference Include="../../src/ServiceLayer.Mesh/ServiceLayer.Mesh.csproj" />
21+
<ProjectReference Include="../../src/ServiceLayer.Common/ServiceLayer.Common.csproj" />
2122
<ProjectReference Include="..\ServiceLayer.TestUtilities\ServiceLayer.TestUtilities.csproj" />
2223
</ItemGroup>
2324

2425
<ItemGroup>
2526
<Using Include="Xunit" />
2627
</ItemGroup>
2728
<ItemGroup>
28-
<None Update="TestData\**\*.*">
29+
<None Update="TestData\*.*">
2930
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3031
</None>
3132
</ItemGroup>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"NBSSAPPT_HDR"|"00000087"|"20250212"|"095121"|"000005"
2+
"NBSSAPPT_FLDS"|"Sequence"|"BSO"|"Action"|"Clinic Code"|"Holding Clinic"|"Status"|"Attended Not Scr"|"Appointment ID"|"NHS Num"|"Epsiode Type"|"Episode Start"|"BatchID"|"Screen or Asses"|"Screen Appt num"|"Booked By"|"Cancelled By"|"Appt Date"|"Appt Time"|"Location"|"Clinic Name"|"Clinic Name (Let)"|"Clinic Address 1"|"Clinic Address 2"|"Clinic Address 3"|"Clinic Address 4"|"Clinic Address 5"|"Postcode"|"Action Timestamp"
3+
"NBSSAPPT_DATA"|"000001"|"KMK"|"U"|"BU011"|"N"|"D"|""|"BU011-67246-RA1-DN-T1316-1"|"9619663241"|"G"|"2025-02-10"|"KMKG00581"|"S"|"1"|"H"|""|"20250210"|"1316"|"BU"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250212-094011"
4+
"NBSSAPPT_DATA"|"000002"|"KMK"|"U"|"BU011"|"N"|"D"|""|"BU011-67246-RA1-DN-T1345-1"|"9900798635"|"G"|"2025-02-10"|"KMKG00581"|"S"|"1"|"H"|""|"20250210"|"1345"|"BU"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250212-094153"
5+
"NBSSAPPT_DATA"|"000003"|"KMK"|"U"|"BU011"|"N"|"D"|""|"BU011-67246-RA1-DN-T1400-1"|"9612996474"|"G"|"2025-02-10"|"KMKG00581"|"S"|"1"|"H"|""|"20250210"|"1400"|"BU"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250212-094408"
6+
"NBSSAPPT_DATA"|"000004"|"KMK"|"U"|"BU011"|"N"|"D"|""|"BU011-67246-RA1-DN-T1416-1"|"9630806428"|"F"|"2025-01-31"|"KMK001329"|"S"|"1"|"H"|""|"20250210"|"1416"|"BU"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250212-094418"
7+
"NBSSAPPT_DATA"|"000005"|"KMK"|"B"|"DU101"|"Y"|"B"|""|"DU101-67248-RA1-DN-T0930-1"|"9900798961"|"G"|"2025-02-12"|"KMKG00581"|"S"|"1"|"H"|""|"20250212"|"0930"|"DU"|"DUMMY CLINIC"|"HOLDING CLINIC"|"SO USE ONLY, COVID 19"|""|""|""|""|""|"20250212-095013"
8+
"NBSSAPPT_END"|"00000087"|"20250212"|"095122"|"000005"

0 commit comments

Comments
 (0)