1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }