Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit b8b02fa

Browse files
optimizing test case processing (#433)
1 parent d01fc6e commit b8b02fa

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

src/test/java/com/linuxforhealth/connect/builder/EtlRouteTest.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@
1212
import com.linuxforhealth.connect.support.etl.PractitionerCsvFormat;
1313
import com.linuxforhealth.connect.support.etl.PractitionerCsvTransform;
1414

15+
import org.apache.camel.CamelContext;
16+
import org.apache.camel.CamelException;
1517
import org.apache.camel.RoutesBuilder;
1618
import org.apache.camel.component.mock.MockEndpoint;
1719
import org.junit.jupiter.api.BeforeEach;
1820
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.TestInstance;
22+
import org.junit.jupiter.api.TestInstance.Lifecycle;
23+
import org.junit.jupiter.api.extension.ExtensionContext;
1924

2025

2126
/**
2227
* Tests {@link EtlRouteBuilder}
2328
*/
29+
@TestInstance(Lifecycle.PER_CLASS)
2430
public class EtlRouteTest extends RouteTestSupport{
2531

2632
private MockEndpoint mockError;
@@ -53,26 +59,29 @@ protected RoutesBuilder[] createRouteBuilders() throws Exception {
5359
};
5460
}
5561

56-
/**
57-
* Configures mock endppoints for the ETLRoute and the supporting LinuxForHealthRoute error handling route.
58-
* @throws Exception if an error occurs applying advice
59-
*/
60-
@BeforeEach
6162
@Override
62-
protected void configureContext() throws Exception {
63+
protected CamelContext createCamelContext() throws Exception {
64+
CamelContext context = super.createCamelContext();
6365
context.getRegistry().bind("LFHKafkaConsumer", new LFHKafkaConsumer());
6466
context.getRegistry().bind("practitionercsvformat", new PractitionerCsvFormat());
6567
context.getRegistry().bind("practitionercsvtransform", new PractitionerCsvTransform());
68+
return context;
69+
}
6670

67-
mockError = mockProducerEndpointById(LinuxForHealthRouteBuilder.ERROR_ROUTE_ID,
68-
LinuxForHealthRouteBuilder.ERROR_PRODUCER_ID,
69-
"mock:error");
70-
71-
mockPersist = mockProducerEndpointById(EtlRouteBuilder.ROUTE_ID,
72-
EtlRouteBuilder.DATA_PERSIST_PRODUCER_ID,
73-
"mock:dataPersist");
74-
75-
super.configureContext();
71+
@Override
72+
public void beforeTestExecution(ExtensionContext context) throws Exception {
73+
super.beforeTestExecution(context);
74+
if (mockError == null) {
75+
mockError = mockProducerEndpointById(LinuxForHealthRouteBuilder.ERROR_ROUTE_ID,
76+
LinuxForHealthRouteBuilder.ERROR_PRODUCER_ID,
77+
"mock:error");
78+
}
79+
80+
if (mockPersist == null) {
81+
mockPersist = mockProducerEndpointById(EtlRouteBuilder.ROUTE_ID,
82+
EtlRouteBuilder.DATA_PERSIST_PRODUCER_ID,
83+
"mock:dataPersist");
84+
}
7685
}
7786

7887
/**

src/test/java/com/linuxforhealth/connect/builder/Hl7v2RouteTest.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
public class Hl7v2RouteTest extends RouteTestSupport {
2727

28-
private MockEndpoint mockResult;
28+
private MockEndpoint mockStoreAndNotify;
2929

3030
@Override
3131
protected RoutesBuilder createRouteBuilder() throws Exception {
@@ -45,15 +45,11 @@ protected void configureContext() throws Exception {
4545
context.getRegistry().bind("hl7encoder", hl7encoder);
4646
context.getRegistry().bind("hl7decoder", hl7decoder);
4747

48-
mockProducerEndpointById(
49-
Hl7v2RouteBuilder.ROUTE_ID,
50-
Hl7v2RouteBuilder.ROUTE_PRODUCER_ID,
51-
"mock:result"
52-
);
48+
mockStoreAndNotify = mockProducerEndpoint(Hl7v2RouteBuilder.ROUTE_ID,
49+
LinuxForHealthRouteBuilder.STORE_AND_NOTIFY_CONSUMER_URI,
50+
"mock:storeAndNotify");
5351

5452
super.configureContext();
55-
56-
mockResult = MockEndpoint.resolve(context, "mock:result");
5753
}
5854

5955
@Test
@@ -65,23 +61,24 @@ void testRoute() throws Exception {
6561

6662
String expectedMessage = Base64.getEncoder().encodeToString(testMessage.getBytes(StandardCharsets.UTF_8));
6763

68-
mockResult.expectedMessageCount(1);
64+
mockStoreAndNotify.expectedMessageCount(1);
6965
// the camel hl7 data format removes trailing delimiters from segments and fields
7066
// test files do not include trailing delimiters to simplify test assertions
7167
// the data format will include a terminating carriage return, \r, which is translated above from a new line \n
72-
mockResult.expectedBodiesReceived(expectedMessage);
73-
mockResult.expectedPropertyReceived("dataStoreUri", "kafka:HL7-V2_ADT?brokers=localhost:9094");
74-
mockResult.expectedPropertyReceived("dataFormat", "HL7-V2");
75-
mockResult.expectedPropertyReceived("messageType", "ADT");
76-
mockResult.expectedPropertyReceived("routeId", "hl7-v2");
68+
mockStoreAndNotify.expectedBodiesReceived(expectedMessage);
69+
mockStoreAndNotify
70+
.expectedPropertyReceived("dataStoreUri", "kafka:HL7-V2_ADT?brokers=localhost:9094");
71+
mockStoreAndNotify.expectedPropertyReceived("dataFormat", "HL7-V2");
72+
mockStoreAndNotify.expectedPropertyReceived("messageType", "ADT");
73+
mockStoreAndNotify.expectedPropertyReceived("routeId", "hl7-v2");
7774

7875
fluentTemplate.to("netty:tcp://localhost:2576?sync=true&encoders=#hl7encoder&decoders=#hl7decoder")
7976
.withBody(testMessage)
8077
.send();
8178

82-
mockResult.assertIsSatisfied();
79+
mockStoreAndNotify.assertIsSatisfied();
8380

84-
Exchange mockExchange = mockResult.getExchanges().get(0);
81+
Exchange mockExchange = mockStoreAndNotify.getExchanges().get(0);
8582
String expectedRouteUri = "netty://tcp://0.0.0.0:2576?sync=true&encoders=#hl7encoder&decoders=#hl7decoder";
8683
String actualRouteUri = mockExchange.getProperty("routeUri", String.class);
8784
LinuxForHealthAssertions.assertEndpointUriSame(expectedRouteUri, actualRouteUri);

src/test/java/com/linuxforhealth/connect/builder/RouteTestSupport.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import java.io.IOException;
99
import java.util.Properties;
1010

11+
import org.apache.camel.CamelContext;
1112
import org.apache.camel.Processor;
1213
import org.apache.camel.builder.AdviceWithRouteBuilder;
1314
import org.apache.camel.component.mock.MockEndpoint;
1415
import org.apache.camel.model.RouteDefinition;
1516
import org.apache.camel.model.language.ConstantExpression;
1617
import org.apache.camel.test.junit5.CamelTestSupport;
18+
import org.junit.jupiter.api.AfterAll;
1719
import org.junit.jupiter.api.AfterEach;
1820
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.BeforeAll;
1922
import org.junit.jupiter.api.BeforeEach;
2023

2124
import com.linuxforhealth.connect.support.TestUtils;
@@ -229,12 +232,18 @@ protected void addLast(String routeId, String mockUri) throws Exception {
229232
*/
230233
@BeforeEach
231234
protected void configureContext() throws Exception {
235+
if (isCreateCamelContextPerClass() && context.isStarted()) {
236+
return;
237+
}
232238
context.start();
233239
fluentTemplate.start();
234240
}
235241

236242
@AfterEach
237243
protected void stopContext() {
244+
if (isCreateCamelContextPerClass()) {
245+
return;
246+
}
238247
fluentTemplate.stop();
239248
context.stop();
240249
}

0 commit comments

Comments
 (0)