1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.hl7v2ws.pcd01;
17
18 import ca.uhn.hl7v2.HL7Exception;
19 import ca.uhn.hl7v2.HapiContext;
20 import ca.uhn.hl7v2.model.Message;
21 import org.apache.camel.Endpoint;
22 import org.apache.camel.Exchange;
23 import org.apache.camel.Processor;
24 import org.apache.camel.impl.DefaultExchange;
25 import org.apache.camel.util.CastUtils;
26 import org.apache.cxf.transport.servlet.CXFServlet;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.openehealth.ipf.commons.ihe.hl7v2.Hl7v2AcceptanceException;
31 import org.openehealth.ipf.commons.ihe.hl7v2.definitions.HapiContextFactory;
32 import org.openehealth.ipf.gazelle.validation.profile.pcd.PcdTransactions;
33 import org.openehealth.ipf.platform.camel.core.util.Exchanges;
34 import org.openehealth.ipf.platform.camel.ihe.core.Interceptor;
35 import org.openehealth.ipf.platform.camel.ihe.core.Interceptor2ProducerAdapter;
36 import org.openehealth.ipf.platform.camel.ihe.ws.StandardTestContainer;
37
38 import javax.management.MBeanServer;
39 import javax.management.ObjectName;
40 import java.util.Scanner;
41 import java.util.Set;
42
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
45 import static org.junit.Assert.assertTrue;
46
47
48
49
50
51 public class Pcd01Test extends StandardTestContainer {
52 public static final String CONTEXT_DESCRIPTOR = "pcd-01.xml";
53
54 public static final String PCD_01_SPEC_REQUEST = load(
55 HapiContextFactory.createHapiContext(PcdTransactions.PCD1),
56 "pcd01/pcd01-request.hl7").toString();
57
58 public static final String PCD_01_SPEC_RESPONSE = load(
59 HapiContextFactory.createHapiContext(PcdTransactions.PCD1),
60 "pcd01/pcd01-response.hl7").toString();
61
62 public static void main(String... args) {
63 startServer(new CXFServlet(), CONTEXT_DESCRIPTOR, false, DEMO_APP_PORT);
64 }
65
66 @BeforeClass
67 public static void setUpClass() {
68 startServer(new CXFServlet(), CONTEXT_DESCRIPTOR);
69 }
70
71 @Before
72 public void setUp() {
73 MyRejectionHandlingStrategy.resetCounter();
74 }
75
76 @Test
77 public void testHappyCase() throws Exception {
78 String uri = "pcd-pcd01://localhost:" + getPort() + "/devicedata";
79 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
80 assertResponseEquals(PCD_01_SPEC_RESPONSE, response);
81 assertEquals(0, MyRejectionHandlingStrategy.getCount());
82 }
83
84 @Test
85 public void testHappyCaseInboundValidation() throws Exception {
86 String uri = "pcd-pcd01://localhost:" + getPort() + "/route_inbound_validation";
87 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
88 assertResponseEquals(PCD_01_SPEC_RESPONSE, response);
89 assertEquals(0, MyRejectionHandlingStrategy.getCount());
90 }
91
92 @Test
93 public void testHappyCaseInboundAndOutboundValidation() throws Exception {
94 String uri = "pcd-pcd01://localhost:" + getPort() + "/route_inbound_and_outbound_validation";
95 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
96 assertResponseEquals(PCD_01_SPEC_RESPONSE, response);
97 assertEquals(0, MyRejectionHandlingStrategy.getCount());
98 }
99
100 @Test(expected = Hl7v2AcceptanceException.class)
101 public void testInacceptableRequestOnProducer() throws Exception {
102 String uri = "pcd-pcd01://localhost:" + getPort() + "/devicedata";
103 requestBody(uri, PCD_01_SPEC_REQUEST.replace("|2.6|", "|2.5|"));
104 assertEquals(0, MyRejectionHandlingStrategy.getCount());
105 }
106
107 @Test
108 public void testInacceptableRequestOnConsumer() throws Exception {
109 String uri = "pcd-pcd01://localhost:" + getPort() + "/devicedata";
110 Endpoint endpoint = getCamelContext().getEndpoint(uri);
111 Processor processor = endpoint.createProducer();
112 processor = ((Interceptor2ProducerAdapter) processor).getProcessor();
113 while (processor instanceof Interceptor) {
114 processor = ((Interceptor) processor).getWrappedProcessor();
115 }
116 Exchange exchange = new DefaultExchange(getCamelContext());
117 exchange.getIn().setBody(PCD_01_SPEC_REQUEST.replace("|2.6|", "|2.5|"));
118 processor.process(exchange);
119 assertEquals(1, MyRejectionHandlingStrategy.getCount());
120 }
121
122 @Test
123 public void testApplicationError() throws Exception {
124 String uri = "pcd-pcd01://localhost:" + getPort() + "/route_throws_exception";
125 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
126 assertTrue(response.startsWith("MSH|^~\\&|"));
127 assertTrue("The response message must contain the cause", response.contains("java.lang.RuntimeException"));
128 assertTrue("On application error the request message id must be returned.", response.contains("MSA|AE|MSGID1234"));
129 assertEquals(0, MyRejectionHandlingStrategy.getCount());
130 }
131
132 @Test
133 public void testInboundValidation() throws Exception {
134 String uri = "pcd-pcd01://localhost:" + getPort() + "/route_inbound_validation";
135 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
136 assertTrue(response.startsWith("MSH|^~\\&|"));
137 assertResponseEquals(PCD_01_SPEC_RESPONSE, response);
138 assertEquals(0, MyRejectionHandlingStrategy.getCount());
139 }
140
141 @Test
142 public void testInboundValidationError() throws Exception {
143 String uri = "pcd-pcd01://localhost:" + getPort() + "/route_inbound_validation";
144
145 String invalidMSG = PCD_01_SPEC_REQUEST.replace("|1.0.1|", "||");
146 String response = requestBody(uri, invalidMSG);
147 assertTrue(response.startsWith("MSH|^~\\&|"));
148 assertTrue(response.contains("MSA|AE"));
149 assertTrue(response.contains("Observation Sub-ID"));
150 assertEquals(0, MyRejectionHandlingStrategy.getCount());
151 }
152
153 @Test
154 public void testInboundAndOutboundValidationError() throws Exception {
155 String uri = "pcd-pcd01://localhost:" + getPort() + "/route_inbound_and_outbound_validation";
156
157 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
158 assertTrue(response.startsWith("MSH|^~\\&|"));
159 assertResponseEquals(PCD_01_SPEC_RESPONSE, response);
160 assertEquals(0, MyRejectionHandlingStrategy.getCount());
161 }
162
163 @Test
164 public void testDefaultAcceptedResponse() throws Exception {
165 String uri = "pcd-pcd01://localhost:" + getPort()
166 + "/route_unacceptable_response";
167 String response = requestBody(uri, PCD_01_SPEC_REQUEST);
168 assertTrue(response.startsWith("MSH|^~\\&|"));
169 assertTrue(response.contains("|ACK^R01^ACK|"));
170 assertTrue(response.contains("MSA|AR|MSGID1234"));
171 assertTrue(response.contains("ERR|||203^Unsupported version id^HL70357"));
172 assertEquals(1, MyRejectionHandlingStrategy.getCount());
173 }
174
175 @Test
176 public void testJmxAttribute() throws Exception {
177 MBeanServer mbsc = getCamelContext().getManagementStrategy().getManagementAgent()
178 .getMBeanServer();
179 Set<ObjectName> s = CastUtils.cast(mbsc.queryNames(new ObjectName(
180 "org.apache.camel:*,type=endpoints,name=\"pcd-pcd01://devicedata\\?rejectionHandlingStrategy=%23rejectionHandlingStrategy\""), null));
181 assertEquals(1, s.size());
182 ObjectName object = (ObjectName) s.toArray()[0];
183 assertNotNull(object);
184 assertTrue((Boolean) mbsc.getAttribute(object, "Addressing"));
185 }
186
187 private String requestBody(String uri, String msg) {
188 Object response = send(uri, msg);
189 return Exchanges.resultMessage((Exchange) response).getBody(String.class);
190 }
191
192 private void assertResponseEquals(String expected, String response) {
193
194
195 assertEquals(expected, response);
196 }
197
198 private static <T extends Message> T load(HapiContext context, String fileName) {
199 try {
200 return (T) context.getPipeParser().parse(
201 new Scanner(Pcd01RouteBuilder.class.getResourceAsStream("/" + fileName)).useDelimiter("\\A").next());
202 } catch (HL7Exception e) {
203 return null;
204 }
205 }
206
207 }