View Javadoc
1   /*
2    * Copyright 2008 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.core.extend;
17  
18  import org.apache.camel.Exchange;
19  import org.apache.commons.io.IOUtils;
20  import org.junit.Test;
21  import org.springframework.test.context.ContextConfiguration;
22  import org.xml.sax.SAXException;
23  
24  import javax.xml.transform.dom.DOMResult;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.nio.charset.Charset;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertFalse;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * @author Martin Krasser
36   * @author Stefan Ivanov
37   */
38  @ContextConfiguration(locations = { "/context-core-extend-transmogrifier.xml" })
39  public class TransmogrifierExtensionTest extends AbstractExtensionTest {
40  
41      @Test
42      public void testReply() {
43          Exchange exchange = producerTemplate.request("direct:reply", exchange1 -> exchange1.getIn().setBody("abc"));
44          assertEquals("abc", exchange.getIn().getBody());
45          assertEquals("abcxyz", exchange.getOut().getBody());
46      }
47  
48      @Test
49      public void testClosureOneParam() throws InterruptedException {
50          mockOutput.expectedBodiesReceived("bulb");
51          producerTemplate.sendBody("direct:input1", "blub");
52          mockOutput.assertIsSatisfied();
53      }
54  
55      @Test
56      public void testClosureTwoParams() throws InterruptedException {
57          mockOutput.expectedBodiesReceived("blah-blub");
58          producerTemplate.sendBodyAndHeader("direct:input2", "blah", "foo",
59                  "blub");
60          mockOutput.assertIsSatisfied();
61      }
62  
63      @Test
64      public void testCustomInputExpression() throws InterruptedException {
65          mockOutput.expectedBodiesReceived("knio");
66          producerTemplate.sendBodyAndHeader("direct:input3", "blah", "foo",
67                  "oink");
68          mockOutput.assertIsSatisfied();
69      }
70  
71      @Test
72      public void testCustomInputClosure() throws InterruptedException {
73          mockOutput.expectedBodiesReceived("knioknio");
74          producerTemplate.sendBodyAndHeader("direct:input4", "blah", "foo",
75                  "oink");
76          mockOutput.assertIsSatisfied();
77      }
78  
79      @Test
80      public void testCustomParamsExpression() throws InterruptedException {
81          mockOutput.expectedBodiesReceived("dcba");
82          producerTemplate.sendBodyAndHeader("direct:input5", "blah", "foo",
83                  "abcd");
84          mockOutput.assertIsSatisfied();
85      }
86  
87      @Test
88      public void testCustomParamsClosure() throws InterruptedException {
89          mockOutput.expectedBodiesReceived("dcba");
90          producerTemplate.sendBodyAndHeader("direct:input6", "blah", "foo",
91                  "abcd");
92          mockOutput.assertIsSatisfied();
93      }
94  
95      @Test
96      public void testCustomParamsStatic() throws InterruptedException {
97          mockOutput.expectedBodiesReceived("blahabc");
98          producerTemplate.sendBody("direct:input7", "blah");
99          mockOutput.assertIsSatisfied();
100     }
101 
102     @Test
103     public void testTransmogrifierObject() throws InterruptedException {
104         mockOutput.expectedBodiesReceived("b");
105         producerTemplate.sendBody("direct:input8", "a");
106         mockOutput.assertIsSatisfied();
107     }
108 
109     @Test
110     public void testTransmogrifierBean() throws InterruptedException {
111         mockOutput.expectedBodiesReceived("g");
112         producerTemplate.sendBody("direct:input9a", "f");
113         mockOutput.assertIsSatisfied();
114     }
115 
116     @Test
117     public void testTransmogrifierBeanFromContextProperty()
118             throws InterruptedException {
119         mockOutput.expectedBodiesReceived("g");
120         producerTemplate.sendBody("direct:input9b", "f");
121         mockOutput.assertIsSatisfied();
122     }
123 
124     @Test
125     public void testCustomInputOrdering() throws InterruptedException {
126         mockOutput.expectedBodiesReceived("kniooink");
127         producerTemplate.sendBodyAndHeader("direct:input10", "blah", "foo",
128                 "oink");
129         mockOutput.assertIsSatisfied();
130     }
131 
132     @Test
133     public void testXmlClosureBuilderParams() throws InterruptedException {
134         mockOutput.expectedBodiesReceived("<result>very</result>");
135         producerTemplate.sendBody("direct:input11", "very");
136         mockOutput.assertIsSatisfied();
137     }
138 
139     @Test
140     public void testXmlClosureHeadersAndBuilderParams()
141             throws InterruptedException {
142         mockOutput.expectedBodiesReceived("<result>magic</result>");
143         producerTemplate
144                 .sendBodyAndHeader("direct:input12", "ma", "foo", "gic");
145         mockOutput.assertIsSatisfied();
146     }
147 
148     @Test
149     public void testXmlBeanBuilderParams() throws InterruptedException {
150         mockOutput.expectedBodiesReceived("<result>bean</result>");
151         producerTemplate.sendBody("direct:input13", "bean");
152         mockOutput.assertIsSatisfied();
153     }
154 
155     @Test
156     public void testXmlObjectBuilderParams() throws InterruptedException {
157         mockOutput.expectedBodiesReceived("<result>object</result>");
158         producerTemplate.sendBody("direct:input14", "object");
159         mockOutput.assertIsSatisfied();
160     }
161 
162     @Test
163     public void testDedicatedXsltTransmogrifier() throws InterruptedException,
164             IOException {
165         mockOutput.expectedMessageCount(1);
166         producerTemplate.sendBody("direct:input15", xsltInput());
167         assertXsltOutput("P", "T");
168     }
169 
170     @Test
171     public void testXsltTransmogrifier() throws InterruptedException,
172             IOException {
173         mockOutput.expectedMessageCount(1);
174         producerTemplate.sendBody("direct:input16", xsltInput());
175         assertXsltOutput("P", "T");
176     }
177 
178     @Test
179     public void testXsltTransmogrifierReturningInputStream()
180             throws InterruptedException, IOException {
181         mockOutput.expectedMessageCount(1);
182         producerTemplate.sendBody("direct:input17", xsltInput());
183         mockOutput.assertIsSatisfied();
184         InputStream result = (InputStream) mockOutput.getExchanges().get(0)
185                 .getIn().getBody();
186         assertNotNull(result);
187     }
188 
189     @Test
190     public void testXsltTransmogrifierWithXsltParams()
191             throws InterruptedException, IOException {
192         mockOutput.expectedMessageCount(1);
193         producerTemplate.sendBody("direct:input18", xsltInput());
194         assertXsltOutput("D", "I");
195     }
196 
197     @Test
198     public void testXsltTransmogrifierWithStylesheetInParams()
199             throws InterruptedException, IOException {
200         mockOutput.expectedMessageCount(1);
201         producerTemplate.sendBody("direct:input19", xsltInput());
202         assertXsltOutput("D", "I");
203     }
204 
205     @Test
206     public void testXsltTransmogrifierWithStylesheetInHeader()
207             throws InterruptedException, IOException {
208         mockOutput.expectedMessageCount(1);
209         producerTemplate.sendBody("direct:input20", xsltInput());
210         assertXsltOutput("P", "T");
211     }
212 
213     @Test
214     public void testXsltTransmogrifierWithStylesheetAndParamsInHeaders()
215             throws InterruptedException, IOException {
216         mockOutput.expectedMessageCount(1);
217         producerTemplate.sendBody("direct:input21", xsltInput());
218         assertXsltOutput("D", "I");
219     }
220 
221     @Test
222     public void testSchematronTransmogrifier() throws InterruptedException,
223             IOException {
224         mockOutput.expectedMessageCount(1);
225         producerTemplate.sendBody("direct:input23", content("/schematron/schematron-test.xml"));
226         mockOutput.assertIsSatisfied();
227         String result = (String)mockOutput.getExchanges().get(0).getIn().getBody();
228         assertFalse(result.contains("svrl:failed-assert"));
229     }
230     
231     @Test
232     public void testInvalidSchematronTransmogrifier() throws InterruptedException,
233             IOException {
234         mockOutput.expectedMessageCount(1);
235         producerTemplate.sendBody("direct:input23", content("/schematron/schematron-test-fail.xml"));
236         mockOutput.assertIsSatisfied();
237         String result = (String)mockOutput.getExchanges().get(0).getIn().getBody();
238         assertTrue(result.contains("svrl:failed-assert"));
239     }
240     
241     @Test
242     public void testXqueryDedicatedTransmogrifier()
243             throws InterruptedException,
244             IOException, SAXException {
245         mockOutput.expectedMessageCount(1);
246         producerTemplate.sendBody("direct:input24", content("/xquery/labreport.xml"));
247         assertXqueryOutput("someid");
248     }
249 
250     @Test
251     public void testXqueryTransmogrifier() throws InterruptedException,
252             IOException, SAXException {
253         mockOutput.expectedMessageCount(1);
254         producerTemplate.sendBody("direct:input25", content("/xquery/labreport.xml"));
255         assertXqueryOutput("headerId");
256     }
257 
258     @Test
259     public void testXqueryTransmogrifierReturningInputStream()
260             throws InterruptedException, IOException, SAXException {
261         mockOutput.expectedMessageCount(1);
262         producerTemplate.sendBody("direct:input26", content("/xquery/labreport.xml"));
263         mockOutput.assertIsSatisfied();
264         InputStream result = mockOutput.getExchanges().get(0).getIn()
265                 .getBody(InputStream.class);
266         assertNotNull(result);
267     }
268     
269     @Test
270     public void testXqueryTransmogrifierReturningDOMResult()
271             throws InterruptedException, IOException {
272         mockOutput.expectedMessageCount(1);
273         producerTemplate.sendBody("direct:input27", content("/xquery/labreport.xml"));
274         mockOutput.assertIsSatisfied();
275         DOMResult result = mockOutput.getExchanges().get(0).getIn()
276                 .getBody(DOMResult.class);
277         assertNotNull(result);
278     }
279 
280     @Test
281     public void testXqueryTransmogrifierExternalRessource()
282             throws InterruptedException, IOException, SAXException {
283         mockOutput.expectedMessageCount(1);
284         producerTemplate.sendBody("direct:input28", content("/xquery/labreport.xml"));
285         assertXqueryOutput("mapid");
286     }
287 
288     @Test
289     public void testXqueryTransmogrifierExternalSourceParam()
290             throws InterruptedException, IOException, SAXException {
291         mockOutput.expectedMessageCount(1);
292         producerTemplate.sendBody("direct:input29", content("/xquery/labreport.xml"));
293         assertXqueryOutput("externalid");
294     }
295 
296     @Test
297     public void testXqueryWithHeaderParams()
298             throws InterruptedException, IOException, SAXException {
299         mockOutput.expectedMessageCount(1);
300         producerTemplate.sendBody("direct:input30", content("/xquery/labreport.xml"));
301         mockOutput.assertIsSatisfied();
302     }
303 
304     
305     private String xsltInput() throws IOException {
306         return IOUtils.toString(getClass().getResourceAsStream("/xslt/createPatient.xml"), Charset.defaultCharset());
307     }
308 
309     private void assertXqueryOutput(String resultXml) throws InterruptedException {
310         mockOutput.assertIsSatisfied();
311         String result = mockOutput.getExchanges().get(0).getIn()
312                 .getBody(String.class);
313         assertNotNull(result);
314         assertTrue(result.contains("<item name=\"pid\">" + resultXml + "</item>"));
315     }
316 
317     private void assertXsltOutput(String processingCode, String processingMode) throws InterruptedException {
318         mockOutput.assertIsSatisfied();
319         String result = (String)mockOutput.getExchanges().get(0).getIn().getBody();
320         assertNotNull(result);
321         assertTrue(result.contains("<processingCode code=\"" + processingCode + "\""));
322         assertTrue(result.contains("<processingModeCode code=\"" + processingMode + "\""));
323     }
324 
325     private String content(String path) throws IOException {
326         return IOUtils.toString(getClass().getResourceAsStream(path), Charset.defaultCharset());
327     }
328 
329 }