1- using System ;
21using System . Diagnostics ;
3- using System . IO ;
4- using System . Threading . Tasks ;
5- using Microsoft . Data . SqlClient ;
6- using Moq ;
7-
8- using System ;
9- using System . Net . Http ;
2+ using System . Net ;
103using System . Net . Http . Headers ;
11- using System . Threading . Tasks ;
12- using ServiceLayer . Data ;
134
14- namespace ServiceLayer . Mesh . Tests . Integration ;
5+ namespace ServiceLayer . Mesh . Tests ;
156
16- public class IntegrationTests
7+ [ CollectionDefinition ( "DockerComposeCollection" ) ]
8+ public class DockerComposeCollection : ICollectionFixture < DockerComposeFixture >
179{
18- private const string ConnectionString = "Server=localhost,1433;User Id=sa;Password=YourPassword123;TrustServerCertificate=True;" ;
19-
20- public IntegrationTests ( )
21- {
22-
23- }
10+ }
2411
25- private async Task SetupEnvironment ( )
12+ [ Collection ( "DockerComposeCollection" ) ]
13+ public class IntegrationTests
14+ {
15+ private static async Task WaitForHealthyService ( )
2616 {
27- var environment = "development" ;
28- if ( environment == null )
29- {
30- throw new InvalidOperationException ( "ASPNETCORE_ENVIRONMENT environment variable is not set of is empty." ) ;
31- }
32- if ( environment == "development" )
33- {
34- RunCommand ( "podman compose up -d" ) ;
35- }
36- if ( environment == "production" )
37- {
38- RunCommand ( "docker compose up -d" ) ;
39- }
40-
4117 bool environmentIsUp = false ;
4218
4319 while ( environmentIsUp == false )
4420 {
45- var responce = await HttpHelper . SendHttpRequestAsync ( HttpMethod . Get , "http://localhost:7072/api/health" ) ;
46- if ( responce . IsSuccessStatusCode )
21+ var response = await HttpHelper . SendHttpRequestAsync ( HttpMethod . Get , "http://localhost:7072/api/health" ) ;
22+ if ( response . IsSuccessStatusCode )
4723 {
4824 environmentIsUp = true ;
4925 }
@@ -54,18 +30,20 @@ private async Task SetupEnvironment()
5430 }
5531 }
5632
57- public void Teardown ( )
58- {
59- // Stop containers
60- RunCommand ( "docker compose down" ) ;
61- }
62-
6333 [ Fact ]
6434 public async Task EndToEndTest ( )
6535 {
66- await SetupEnvironment ( ) ;
36+ // Arrange
37+ await WaitForHealthyService ( ) ;
38+
39+ await SendFileToMeshInbox ( "KMK_20250212095121_APPT_87.dat" ) ;
40+
41+ await Task . Delay ( 5000 ) ;
42+ }
6743
68- byte [ ] binaryData = await File . ReadAllBytesAsync ( "TestData/KMK_20250212095121_APPT_87.dat" ) ;
44+ private static async Task SendFileToMeshInbox ( string fileName )
45+ {
46+ byte [ ] binaryData = await File . ReadAllBytesAsync ( $ "TestData/{ fileName } ") ;
6947 var content = new ByteArrayContent ( binaryData ) ;
7048 content . Headers . ContentType = new MediaTypeHeaderValue ( "application/octet-stream" ) ;
7149
@@ -76,41 +54,15 @@ public async Task EndToEndTest()
7654 headers =>
7755 {
7856 headers . Add ( "Authorization" , "NHSMESH X26ABC1:a42f77b9-58de-4b45-b599-2d5bf320b44d:0:202407291437:e3005627136e01706efabcfe72269bc8da3192e90a840ab344ab7f82a39bb5c6" ) ;
79- headers . Add ( "Mex-Filename" , "KMK_20250212095121_APPT_87.dat" ) ;
57+ headers . Add ( "Mex-Filename" , fileName ) ;
8058 headers . Add ( "Mex-From" , "X26ABC1" ) ;
8159 headers . Add ( "Mex-To" , "X26ABC1" ) ;
8260 headers . Add ( "Mex-Workflowid" , "API-DOCS-TEST" ) ;
83- headers . Add ( "User-Agent" , "HTTPie" ) ;
8461 }
8562 ) ;
86-
87- await Task . Delay ( 5000 ) ;
88-
89- Teardown ( ) ;
90- }
91-
92- private void RunCommand ( string command )
93- {
94- var psi = new ProcessStartInfo ( "cmd" , $ "/c { command } ")
95- {
96- RedirectStandardOutput = true ,
97- RedirectStandardError = true ,
98- UseShellExecute = false ,
99- CreateNoWindow = true
100- } ;
101-
102- using var process = Process . Start ( psi ) ;
103- process . WaitForExit ( ) ;
104-
105- if ( process . ExitCode != 0 )
106- {
107- throw new Exception ( $ "Command failed: { command } \n { process . StandardError . ReadToEnd ( ) } ") ;
108- }
10963 }
11064}
11165
112-
113-
11466public static class HttpHelper
11567{
11668 private static readonly HttpClient _client = new HttpClient ( ) ;
@@ -138,7 +90,49 @@ public static async Task<HttpResponseMessage> SendHttpRequestAsync(
13890 catch ( HttpRequestException ex )
13991 {
14092 Console . WriteLine ( $ "HTTP Request failed: { ex . Message } ") ;
141- throw ;
93+ return new HttpResponseMessage ( HttpStatusCode . ServiceUnavailable ) ;
94+ }
95+ }
96+ }
97+
98+ public class DockerComposeFixture : IAsyncLifetime
99+ {
100+ public async Task InitializeAsync ( )
101+ {
102+ // Start Docker Compose
103+ var startInfo = new ProcessStartInfo
104+ {
105+ FileName = "docker" ,
106+ Arguments = "compose up -d mesh-ingest azurite db db-migrations" ,
107+ RedirectStandardOutput = true ,
108+ RedirectStandardError = true ,
109+ UseShellExecute = false ,
110+ CreateNoWindow = true
111+ } ;
112+
113+ using var process = Process . Start ( startInfo ) ;
114+ await process . WaitForExitAsync ( ) ;
115+
116+ if ( process . ExitCode != 0 )
117+ {
118+ throw new Exception ( $ "docker compose up failed, error: { process . StandardError . ReadToEnd ( ) } ") ;
142119 }
143120 }
121+
122+ public async Task DisposeAsync ( )
123+ {
124+ // Stop Docker Compose
125+ var stopInfo = new ProcessStartInfo
126+ {
127+ FileName = "docker" ,
128+ Arguments = "compose down" ,
129+ RedirectStandardOutput = true ,
130+ RedirectStandardError = true ,
131+ UseShellExecute = false ,
132+ CreateNoWindow = true
133+ } ;
134+
135+ using var process = Process . Start ( stopInfo ) ;
136+ await process . WaitForExitAsync ( ) ;
137+ }
144138}
0 commit comments