View Javadoc
1   /*
2    * Copyright 2009 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.commons.ihe.ws.utils;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNull;
22  import static org.openehealth.ipf.commons.ihe.ws.utils.SoapUtils.extractSoapBody;
23  
24  public class TestSoapUtils {
25      
26      private final String contents =
27          "<ns2:ImportantQuestion>" +
28          "Camel rules as cigarettes as well.  Do Apache Indians smoke it?" +
29          "</ns2:ImportantQuestion>";
30  
31      private final String envelopeWithNamespacePrefixes =
32          "<soap:Envelope><soap:Header>" +
33          "    <thirdns:header value=\"12345\">some text</thirdns:header></soap:Header>" +
34          "    <soap:Body>" +
35          contents +
36          "</soap:Body></soap:Envelope>";
37  
38      private final String envelopeWithoutNamespacePrefixes =
39          "<Envelope><Header>" +
40          "    <thirdns:header value=\"12345\">some text</thirdns:header></Header>" +
41          "    <Body>" +
42          contents +
43          "</Body></Envelope>";
44  
45      private final String emptyEnvelopeWithNamespacePrefixes =
46          "<soap:Envelope><soap:Header>" +
47          "    <thirdns:header value=\"12345\">some text</thirdns:header></soap:Header>" +
48          "    <soap:Body xmlns:prefix=\"uri\"></soap:Body></soap:Envelope>";
49  
50      private final String emptyEnvelopeWithoutNamespacePrefixes =
51          "<Envelope><Header>" +
52          "    <thirdns:header value=\"12345\">some text</thirdns:header></Header>" +
53          "    <Body></Body></Envelope>";
54  
55      private final String emptyEnvelopeWithNamespacePrefixesShort =
56          "<soap:Envelope><soap:Header>" +
57          "    <thirdns:header value=\"12345\">some text</thirdns:header></soap:Header>" +
58          "    <soap:Body attrib=\"value\" /></soap:Envelope>";
59  
60      private final String emptyEnvelopeWithoutNamespacePrefixesShort =
61          "<Envelope><Header>" +
62          "    <thirdns:header value=\"12345\">some text</thirdns:header></Header>" +
63          "    <Body /></Envelope>";
64  
65      private final String totallyBad = "12345";
66  
67  
68      @Test
69      public void testExctractSoapBody() {
70          assertEquals(contents, extractSoapBody(envelopeWithNamespacePrefixes));
71          assertEquals(contents, extractSoapBody(envelopeWithoutNamespacePrefixes));
72  
73          assertEquals("", extractSoapBody(emptyEnvelopeWithNamespacePrefixes));
74          assertEquals("", extractSoapBody(emptyEnvelopeWithoutNamespacePrefixes));
75          assertEquals("", extractSoapBody(emptyEnvelopeWithNamespacePrefixesShort));
76          assertEquals("", extractSoapBody(emptyEnvelopeWithoutNamespacePrefixesShort));
77          
78          assertNull(extractSoapBody(null));
79          assertEquals(totallyBad, extractSoapBody(totallyBad));
80      }
81  
82  }