View Javadoc
1   /*
2    * Copyright 2010 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.tutorials.config.base.route;
17  
18  import static org.junit.Assert.*;
19  
20  import org.apache.camel.EndpointInject;
21  import org.apache.camel.ProducerTemplate;
22  import org.apache.camel.http.common.HttpOperationFailedException;
23  import org.apache.camel.component.mock.MockEndpoint;
24  import org.apache.commons.io.IOUtils;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.test.context.ContextConfiguration;
30  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31  
32  import java.nio.charset.Charset;
33  
34  /**
35   * @author Boris Stanojevic
36   */
37  @RunWith(SpringJUnit4ClassRunner.class)
38  @ContextConfiguration(locations = { "/base-context.xml",
39          "/extender-context.xml",
40          "/extension-context.xml",
41          "/mock-interceptor-context.xml"})
42  public class SampleRouteBuilderTest {
43  
44      @Autowired
45      ProducerTemplate producerTemplate;
46      
47      @EndpointInject(uri = "mock:file")
48      MockEndpoint mockFile;
49      
50      private final String JETTY_URI = "http4://0.0.0.0:8800";
51      
52      @Before
53      public void setUp(){
54          mockFile.reset();
55      }
56      
57      @Test
58      public void testReverse() throws Exception {
59          mockFile.expectedMessageCount(1);
60          String request = "BLAH";
61          String response = producerTemplate.requestBody(JETTY_URI + "/reverse", request, String.class);
62          assertEquals("reversed response: HALB", response);
63          mockFile.assertIsSatisfied();
64          assertEquals(request, mockFile.getExchanges().get(0).getIn().getBody(String.class));
65      }
66  
67      @Test
68      public void testMap() throws Exception {
69          mockFile.expectedMessageCount(1);        
70          String hl7 = IOUtils.toString(this.getClass().getResourceAsStream("/message.hl7"), Charset.defaultCharset());
71          String response = producerTemplate.requestBody(JETTY_URI + "/map", hl7, String.class);
72          assertTrue(response.contains("Nachname||W|||Blahplatz"));
73          mockFile.assertIsSatisfied();
74      }
75      
76      @Test
77      public void testMapError() {
78          try {
79              producerTemplate.requestBody(JETTY_URI + "/map", "BLAH");
80          } catch (Exception e) {
81              assertTrue(e.getCause() instanceof HttpOperationFailedException);
82              String response = ((HttpOperationFailedException)e.getCause()).getResponseBody();
83              assertTrue(response.startsWith("Message encoding is not recognized"));
84              assertEquals(400, ((HttpOperationFailedException)e.getCause()).getStatusCode());            
85          }
86      }
87  }