1   /*
2    * Copyright 2005-2010 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.addressing.client;
18  
19  import java.net.URI;
20  import javax.xml.namespace.QName;
21  import javax.xml.soap.SOAPBody;
22  import javax.xml.soap.SOAPBodyElement;
23  import javax.xml.soap.SOAPElement;
24  import javax.xml.soap.SOAPException;
25  import javax.xml.soap.SOAPMessage;
26  
27  import org.springframework.ws.soap.addressing.AbstractWsAddressingTestCase;
28  import org.springframework.ws.soap.addressing.core.EndpointReference;
29  import org.springframework.ws.soap.addressing.messageid.MessageIdStrategy;
30  import org.springframework.ws.soap.addressing.version.AddressingVersion;
31  import org.springframework.ws.soap.saaj.SaajSoapMessage;
32  import org.springframework.ws.transport.WebServiceConnection;
33  import org.springframework.ws.transport.context.DefaultTransportContext;
34  import org.springframework.ws.transport.context.TransportContext;
35  import org.springframework.ws.transport.context.TransportContextHolder;
36  
37  import org.junit.After;
38  import org.junit.Before;
39  import org.junit.Test;
40  
41  import static org.easymock.EasyMock.*;
42  
43  public abstract class AbstractActionCallbackTestCase extends AbstractWsAddressingTestCase {
44  
45      private ActionCallback callback;
46  
47      private MessageIdStrategy strategyMock;
48  
49      private WebServiceConnection connectionMock;
50  
51      @Before
52      public void createMocks() throws Exception {
53          strategyMock = createMock(MessageIdStrategy.class);
54  
55          connectionMock = createMock(WebServiceConnection.class);
56  
57          TransportContext transportContext = new DefaultTransportContext(connectionMock);
58          TransportContextHolder.setTransportContext(transportContext);
59      }
60  
61      @After
62      public void clearContext() throws Exception {
63          TransportContextHolder.setTransportContext(null);
64      }
65  
66      @Test
67      public void testValid() throws Exception {
68          URI action = new URI("http://example.com/fabrikam/mail/Delete");
69          URI to = new URI("mailto:fabrikam@example.com");
70          callback = new ActionCallback(action, getVersion(), to);
71          callback.setMessageIdStrategy(strategyMock);
72          SaajSoapMessage message = createDeleteMessage();
73          expect(strategyMock.newMessageId(message)).andReturn(new URI("http://example.com/someuniquestring"));
74          callback.setReplyTo(new EndpointReference(new URI("http://example.com/business/client1")));
75  
76          replay(strategyMock, connectionMock);
77  
78          callback.doWithMessage(message);
79  
80          SaajSoapMessage expected = loadSaajMessage(getTestPath() + "/valid.xml");
81          assertXMLEqual("Invalid message", expected, message);
82  
83          verify(strategyMock, connectionMock);
84      }
85  
86      @Test
87      public void testDefaults() throws Exception {
88          URI action = new URI("http://example.com/fabrikam/mail/Delete");
89          URI connectionUri = new URI("mailto:fabrikam@example.com");
90          callback = new ActionCallback(action, getVersion());
91          callback.setMessageIdStrategy(strategyMock);
92          expect(connectionMock.getUri()).andReturn(connectionUri);
93  
94          SaajSoapMessage message = createDeleteMessage();
95          expect(strategyMock.newMessageId(message)).andReturn(new URI("http://example.com/someuniquestring"));
96          callback.setReplyTo(new EndpointReference(new URI("http://example.com/business/client1")));
97  
98          replay(strategyMock, connectionMock);
99  
100         callback.doWithMessage(message);
101 
102         SaajSoapMessage expected = loadSaajMessage(getTestPath() + "/valid.xml");
103         assertXMLEqual("Invalid message", expected, message);
104         verify(strategyMock, connectionMock);
105     }
106 
107     private SaajSoapMessage createDeleteMessage() throws SOAPException {
108         SOAPMessage saajMessage = messageFactory.createMessage();
109         SOAPBody saajBody = saajMessage.getSOAPBody();
110         SOAPBodyElement delete = saajBody.addBodyElement(new QName("http://example.com/fabrikam", "Delete"));
111         SOAPElement maxCount = delete.addChildElement(new QName("maxCount"));
112         maxCount.setTextContent("42");
113         return new SaajSoapMessage(saajMessage);
114     }
115 
116     protected abstract AddressingVersion getVersion();
117 
118     protected abstract String getTestPath();
119 
120 }