1   /*
2    * Copyright 2006 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.security.xwss;
18  
19  import javax.xml.soap.MessageFactory;
20  import javax.xml.soap.SOAPMessage;
21  
22  import junit.framework.TestCase;
23  import org.springframework.ws.context.DefaultMessageContext;
24  import org.springframework.ws.context.MessageContext;
25  import org.springframework.ws.soap.SoapMessage;
26  import org.springframework.ws.soap.saaj.SaajSoapMessage;
27  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
28  import org.springframework.ws.soap.security.WsSecurityValidationException;
29  
30  public class XwsSecurityInterceptorTest extends TestCase {
31  
32      private MessageFactory messageFactory;
33  
34      protected void setUp() throws Exception {
35          messageFactory = MessageFactory.newInstance();
36      }
37  
38      public void testhandleRequest() throws Exception {
39          final SOAPMessage request = messageFactory.createMessage();
40          final SOAPMessage validatedRequest = messageFactory.createMessage();
41          XwsSecurityInterceptor interceptor = new XwsSecurityInterceptor() {
42  
43              protected void secureMessage(SoapMessage soapMessage) throws XwsSecuritySecurementException {
44                  fail("secure not expected");
45              }
46  
47              protected void validateMessage(SoapMessage message) throws WsSecurityValidationException {
48                  SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
49                  assertEquals("Invalid message", request, saajSoapMessage.getSaajMessage());
50                  saajSoapMessage.setSaajMessage(validatedRequest);
51              }
52  
53          };
54          MessageContext context =
55                  new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
56          interceptor.handleRequest(context, null);
57          assertEquals("Invalid request", validatedRequest, ((SaajSoapMessage) context.getRequest()).getSaajMessage());
58      }
59  
60      public void testhandleResponse() throws Exception {
61          final SOAPMessage securedResponse = messageFactory.createMessage();
62          XwsSecurityInterceptor interceptor = new XwsSecurityInterceptor() {
63  
64              protected void secureMessage(SoapMessage message) throws XwsSecuritySecurementException {
65                  SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
66                  saajSoapMessage.setSaajMessage(securedResponse);
67              }
68  
69              protected void validateMessage(SoapMessage soapMessage) throws WsSecurityValidationException {
70                  fail("validate not expected");
71              }
72  
73          };
74          SOAPMessage request = messageFactory.createMessage();
75          MessageContext context =
76                  new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
77          context.getResponse();
78          interceptor.handleResponse(context, null);
79          assertEquals("Invalid response", securedResponse, ((SaajSoapMessage) context.getResponse()).getSaajMessage());
80      }
81  
82  }