View Javadoc
1   /*
2    * Copyright 2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.ihe.hl7v2ws.pcd01;
17  
18  import static org.junit.Assert.assertTrue;
19  
20  import java.net.URL;
21  
22  import org.apache.camel.Exchange;
23  import org.apache.camel.ExchangePattern;
24  import org.apache.camel.builder.RouteBuilder;
25  import org.apache.camel.impl.DefaultExchange;
26  import org.apache.camel.spi.TransactedPolicy;
27  import org.apache.camel.spring.spi.SpringTransactionPolicy;
28  import org.apache.cxf.transport.servlet.CXFServlet;
29  import org.easymock.EasyMock;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.openehealth.ipf.commons.ihe.hl7v2.Hl7v2AcceptanceException;
33  import org.openehealth.ipf.platform.camel.ihe.ws.StandardTestContainer;
34  import org.springframework.transaction.PlatformTransactionManager;
35  import org.springframework.transaction.support.SimpleTransactionStatus;
36  
37  /**
38   * Test for handling of HL7v2 exceptions from within transactional contexts.
39   *
40   * @author Stefan Albrecht
41   */
42  public class TransactedRouteTest extends StandardTestContainer {
43      
44      private static final String WAN_REQUEST = "MSH|^~\\&|AcmeInc^ACDE48234567ABCD^EUI-64||||20090713090030+0500||ORU^R01^ORU_R01|MSGID1234|P|2.6|||NE|AL|||||IHE PCD ORU-R01 2006^HL7^2.16.840.1.113883.9.n.m^HL7\n"
45  + "PID|||789567^^^Imaginary Hospital^PI||Doe^John^Joseph^^^^L^A|||M\n"
46  + "OBR|1|AB12345^AcmeAHDInc^ACDE48234567ABCD^EUI-64|CD12345^AcmeAHDInc^ACDE48234567ABCD^EUI-64|528391^MDC_DEV_SPEC_PROFILE_BP^MDC|||20090813095715+0500\n"
47  + "OBX|1||528391^MDC_DEV_SPEC_PROFILE_BP^MDC|1|||||||R|||||||0123456789ABCDEF^EUI-64\n"
48  + "OBX|2||150020^MDC_PRESS_BLD_NONINV^MDC|1.0.1|||||||R|||20090813095715+0500\n"
49  + "OBX|3|NM|150021^MDC_PRESS_BLD_NONINV_SYS^MDC|1.0.1.1|120|266016^MDC_DIM_MMHG^MDC|||||R\n"
50  + "OBX|4|NM|150022^MDC_PRESS_BLD_NONINV_DIA^MDC|1.0.1.2|80|266016^MDC_DIM_MMHG^MDC|||||R\n"
51  + "OBX|5|NM|150023^MDC_PRESS_BLD_NONINV_MEAN^MDC|1.0.1.3|100|266016^MDC_DIM_MMHG^MDC|||||R\n";
52      
53      private static final SpringTransactionPolicy transactionPolicy = new SpringTransactionPolicy();
54      private static PlatformTransactionManager txManager;
55  
56      
57      @BeforeClass
58      public static void setUpClass () throws Exception {
59          txManager = EasyMock.createMock(PlatformTransactionManager.class);
60          transactionPolicy.setPropagationBehaviorName("PROPAGATION_REQUIRES_NEW");
61          transactionPolicy.setTransactionManager(txManager);
62          final URL resource = ClassLoader.getSystemResource("transacted-test-context.xml");
63          startServer(new CXFServlet(), "transacted-test-context.xml");
64          getCamelContext().addRoutes(new TestRoutes(transactionPolicy));
65      }
66      
67      @Test
68      public void testNonTransactedRoute () throws Exception {
69          final String response = sendRequest(
70                  "pcd-pcd01://localhost:" + getPort() + "/communicateLabData/notransaction", WAN_REQUEST);
71          assertTrue(response.contains("testexception"));        
72          assertTrue(response.contains("MSA|AR"));        
73      }
74      
75      @Test
76      public void testTransactedRoute () throws Exception {
77          // setup the transaction mock        
78          EasyMock.reset(txManager);
79          txManager.getTransaction(EasyMock.anyObject());
80          EasyMock.expectLastCall().andReturn(new SimpleTransactionStatus()).atLeastOnce();
81          txManager.rollback(EasyMock.anyObject());
82          EasyMock.expectLastCall().asStub();
83                  
84          EasyMock.replay(txManager);
85  
86          final String response = sendRequest(
87                  "pcd-pcd01://localhost:" + getPort() + "/communicateLabData/transacted", WAN_REQUEST);
88          EasyMock.verify(txManager);
89          assertTrue(response.contains("testexception"));        
90          assertTrue(response.contains("MSA|AR"));
91      }
92      
93      public String sendRequest(final String url, final String body) {
94          final Exchange exchange = new DefaultExchange(
95                  getCamelContext(), ExchangePattern.InOut);
96          exchange.getIn().setBody(body);
97          final Exchange response = getProducerTemplate().send(url, exchange);
98          return response.getOut().getBody(String.class);
99      }
100     
101     private static class TestRoutes extends RouteBuilder {
102         
103         private TransactedPolicy txPolicy;
104 
105         private TestRoutes (final TransactedPolicy policy) {
106             txPolicy = policy;
107         }
108 
109         @Override
110         public void configure() throws Exception {
111             from("pcd-pcd01://communicateLabData/notransaction")
112                 .throwException(new Hl7v2AcceptanceException("testexception"));
113                 
114             from("pcd-pcd01://communicateLabData/transacted")
115                 .policy(txPolicy)
116                 .throwException(new Hl7v2AcceptanceException("testexception"));
117         }
118     }
119 }