1   /*
2    * Copyright ${YEAR} 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.easymock.MockControl;
28  
29  import org.springframework.ws.soap.addressing.AbstractWsAddressingTestCase;
30  import org.springframework.ws.soap.addressing.core.EndpointReference;
31  import org.springframework.ws.soap.addressing.messageid.MessageIdStrategy;
32  import org.springframework.ws.soap.addressing.version.AddressingVersion;
33  import org.springframework.ws.soap.saaj.SaajSoapMessage;
34  import org.springframework.ws.transport.WebServiceConnection;
35  import org.springframework.ws.transport.context.DefaultTransportContext;
36  import org.springframework.ws.transport.context.TransportContext;
37  import org.springframework.ws.transport.context.TransportContextHolder;
38  
39  public abstract class AbstractActionCallbackTestCase extends AbstractWsAddressingTestCase {
40  
41      private ActionCallback callback;
42  
43      private MockControl strategyControl;
44  
45      private MessageIdStrategy strategyMock;
46  
47      private MockControl connectionControl;
48  
49      private WebServiceConnection connectionMock;
50  
51      protected final void onSetUp() throws Exception {
52          strategyControl = MockControl.createControl(MessageIdStrategy.class);
53          strategyMock = (MessageIdStrategy) strategyControl.getMock();
54  
55          connectionControl = MockControl.createControl(WebServiceConnection.class);
56          connectionMock = (WebServiceConnection) connectionControl.getMock();
57          TransportContext transportContext = new DefaultTransportContext(connectionMock);
58          TransportContextHolder.setTransportContext(transportContext);
59      }
60  
61      protected void tearDown() throws Exception {
62          TransportContextHolder.setTransportContext(null);
63      }
64  
65      public void testValid() throws Exception {
66          URI action = new URI("http://example.com/fabrikam/mail/Delete");
67          URI to = new URI("mailto:[email protected]");
68          callback = new ActionCallback(action, getVersion(), to);
69          callback.setMessageIdStrategy(strategyMock);
70          SaajSoapMessage message = createDeleteMessage();
71          strategyControl
72                  .expectAndReturn(strategyMock.newMessageId(message), new URI("http://example.com/someuniquestring"));
73          callback.setReplyTo(new EndpointReference(new URI("http://example.com/business/client1")));
74          strategyControl.replay();
75          connectionControl.replay();
76  
77          callback.doWithMessage(message);
78  
79          SaajSoapMessage expected = loadSaajMessage(getTestPath() + "/valid.xml");
80          assertXMLEqual("Invalid message", expected, message);
81          strategyControl.verify();
82          connectionControl.verify();
83      }
84  
85      public void testDefaults() throws Exception {
86          URI action = new URI("http://example.com/fabrikam/mail/Delete");
87          URI connectionUri = new URI("mailto:[email protected]");
88          callback = new ActionCallback(action, getVersion());
89          callback.setMessageIdStrategy(strategyMock);
90          connectionControl.expectAndReturn(connectionMock.getUri(), connectionUri);
91          connectionControl.replay();
92  
93          SaajSoapMessage message = createDeleteMessage();
94          strategyControl
95                  .expectAndReturn(strategyMock.newMessageId(message), new URI("http://example.com/someuniquestring"));
96          callback.setReplyTo(new EndpointReference(new URI("http://example.com/business/client1")));
97          strategyControl.replay();
98  
99          callback.doWithMessage(message);
100 
101         SaajSoapMessage expected = loadSaajMessage(getTestPath() + "/valid.xml");
102         assertXMLEqual("Invalid message", expected, message);
103         strategyControl.verify();
104         connectionControl.verify();
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 }