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.adapter;
17  
18  import org.apache.camel.EndpointInject;
19  import org.apache.camel.Exchange;
20  import org.apache.camel.ExchangePattern;
21  import org.apache.camel.component.mock.MockEndpoint;
22  import org.apache.commons.io.IOUtils;
23  import org.junit.After;
24  import org.junit.Test;
25  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
26  import org.openehealth.ipf.commons.xml.SchematronValidationException;
27  import org.openehealth.ipf.platform.camel.core.AbstractRouteTest;
28  
29  import java.io.IOException;
30  import java.nio.charset.Charset;
31  
32  import static org.junit.Assert.assertEquals;
33  
34  /**
35   * @author Martin Krasser
36   * @author Christian Ohr
37   */
38  public class ValidatorRouteTest extends AbstractRouteTest {
39  
40      @EndpointInject(uri = "mock:error")
41      protected MockEndpoint error;
42  
43      @After
44      public void tearDownError() throws Exception {
45          error.reset();
46      }
47  
48      @Test
49      public void testValidator1() throws InterruptedException {
50          String result = (String) producerTemplate.sendBody(
51                  "direct:validator-test", ExchangePattern.InOut, "correct");
52          assertEquals("correct", result);
53      }
54  
55      @Test
56      public void testValidator2() throws InterruptedException {
57          error.expectedMessageCount(1);
58          Exchange exchange = producerTemplate.send("direct:validator-test",
59                  ExchangePattern.InOut, exchange1 -> exchange1.getIn().setBody("incorrect"));
60          assertEquals(ValidationException.class, exchange.getException().getClass());
61          error.assertIsSatisfied(2000);
62      }
63  
64      @Test
65      public void testValidator3() throws InterruptedException, IOException {
66          final String xml = IOUtils.toString(getClass().getResourceAsStream("/xsd/test.xml"), Charset.defaultCharset());
67          String response = (String)producerTemplate.sendBody(
68          		"direct:validator-xml-test", ExchangePattern.InOut, xml);
69          assertEquals("passed", response);
70       }
71  
72      @Test
73      public void testValidator4() throws InterruptedException, IOException {
74          final String xml = IOUtils.toString(getClass().getResourceAsStream("/xsd/invalidtest.xml"), Charset.defaultCharset());
75          error.expectedMessageCount(1);
76          Exchange exchange = producerTemplate.send("direct:validator-xml-test",
77                  ExchangePattern.InOut, exchange1 -> exchange1.getIn().setBody(xml));
78          assertEquals(ValidationException.class, exchange.getException()
79                  .getClass());
80          ValidationException e = (ValidationException) exchange.getException();
81          assertEquals(5, e.getCauses().length);
82          error.assertIsSatisfied(2000);
83      }
84  
85      @Test
86      public void testValidator5() throws InterruptedException, IOException {
87          final String xml = IOUtils.toString(getClass().getResourceAsStream("/schematron/schematron-test.xml"), Charset.defaultCharset());
88          String response = (String)producerTemplate.sendBody(
89          		"direct:validator-schematron-test", ExchangePattern.InOut, xml);
90          assertEquals("passed", response);
91       }
92  
93      @Test
94      public void testValidator6() throws InterruptedException, IOException {
95          final String xml = IOUtils.toString(getClass().getResourceAsStream("/schematron/schematron-test-fail.xml"), Charset.defaultCharset());
96          error.expectedMessageCount(1);
97          Exchange exchange = producerTemplate.send("direct:validator-schematron-test",
98                  ExchangePattern.InOut, exchange1 -> exchange1.getIn().setBody(xml));
99          assertEquals(SchematronValidationException.class, exchange.getException().getClass());
100         ValidationException e = (ValidationException) exchange.getException();
101         assertEquals(3, e.getCauses().length);
102         error.assertIsSatisfied(2000);
103 
104     }    
105 }