1   /*
2    * Copyright 2005-2011 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  
17  package org.springframework.ws.soap.server.endpoint.interceptor;
18  
19  import org.springframework.ws.MockWebServiceMessage;
20  import org.springframework.ws.MockWebServiceMessageFactory;
21  import org.springframework.ws.context.DefaultMessageContext;
22  import org.springframework.ws.context.MessageContext;
23  import org.springframework.ws.server.EndpointInterceptor;
24  import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  public class PayloadRootSmartSoapEndpointInterceptorTest {
33  
34      private EndpointInterceptor delegate;
35  
36      private String namespaceUri;
37  
38      private String localPart;
39  
40      private MessageContext messageContext;
41  
42      @Before
43      public void setUp() {
44          delegate = new EndpointInterceptorAdapter();
45  
46          namespaceUri = "http://springframework.org/spring-ws";
47          localPart = "element";
48  
49          MockWebServiceMessage request = new MockWebServiceMessage("<" + localPart + " xmlns=\"" + namespaceUri + "\" />");
50          messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
51      }
52  
53      @Test(expected = IllegalArgumentException.class)
54      public void neitherNamespaceNorLocalPart() {
55          new PayloadRootSmartSoapEndpointInterceptor(delegate, null, null);
56      }
57  
58      @Test
59      public void shouldInterceptFullMatch() throws Exception {
60          PayloadRootSmartSoapEndpointInterceptor interceptor =
61                  new PayloadRootSmartSoapEndpointInterceptor(delegate, namespaceUri, localPart);
62  
63          boolean result = interceptor.shouldIntercept(messageContext, null);
64          assertTrue("Interceptor should apply", result);
65      }
66  
67      @Test
68      public void shouldInterceptFullNonMatch() throws Exception {
69          PayloadRootSmartSoapEndpointInterceptor interceptor =
70                  new PayloadRootSmartSoapEndpointInterceptor(delegate, "http://springframework.org/other", localPart);
71  
72          boolean result = interceptor.shouldIntercept(messageContext, null);
73          assertFalse("Interceptor should not apply", result);
74      }
75  
76      @Test
77      public void shouldInterceptNamespaceUriMatch() throws Exception {
78          PayloadRootSmartSoapEndpointInterceptor interceptor =
79                  new PayloadRootSmartSoapEndpointInterceptor(delegate, namespaceUri, null);
80  
81          boolean result = interceptor.shouldIntercept(messageContext, null);
82          assertTrue("Interceptor should apply", result);
83      }
84      
85      @Test
86      public void shouldInterceptNamespaceUriNonMatch() throws Exception {
87          PayloadRootSmartSoapEndpointInterceptor interceptor =
88                  new PayloadRootSmartSoapEndpointInterceptor(delegate, "http://springframework.org/other", null);
89  
90          boolean result = interceptor.shouldIntercept(messageContext, null);
91          assertFalse("Interceptor should not apply", result);
92      }
93  
94  }