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.server.endpoint.mapping;
18  
19  import java.lang.reflect.Method;
20  import java.util.Collections;
21  import javax.xml.namespace.QName;
22  import javax.xml.soap.MessageFactory;
23  import javax.xml.soap.SOAPMessage;
24  import javax.xml.transform.Source;
25  
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.context.ApplicationContext;
28  import org.springframework.test.context.ContextConfiguration;
29  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30  import org.springframework.ws.context.DefaultMessageContext;
31  import org.springframework.ws.context.MessageContext;
32  import org.springframework.ws.server.EndpointAdapter;
33  import org.springframework.ws.server.EndpointMapping;
34  import org.springframework.ws.server.MessageDispatcher;
35  import org.springframework.ws.server.endpoint.MethodEndpoint;
36  import org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter;
37  import org.springframework.ws.server.endpoint.annotation.Endpoint;
38  import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
39  import org.springframework.ws.server.endpoint.annotation.RequestPayload;
40  import org.springframework.ws.soap.saaj.SaajSoapMessage;
41  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
42  import org.springframework.ws.soap.server.SoapMessageDispatcher;
43  
44  import org.apache.commons.logging.LogFactory;
45  import org.junit.Test;
46  import org.junit.runner.RunWith;
47  
48  import static org.junit.Assert.*;
49  
50  @RunWith(SpringJUnit4ClassRunner.class)
51  @ContextConfiguration("payloadRootAnnotationMethodEndpointMapping.xml")
52  public class PayloadRootAnnotationMethodEndpointMappingTest  {
53  
54      @Autowired
55      private PayloadRootAnnotationMethodEndpointMapping mapping;
56  
57      @Autowired
58      private ApplicationContext applicationContext;
59  
60      @Test
61      public void registration() throws NoSuchMethodException {
62          MethodEndpoint endpoint = mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Request"));
63          assertNotNull("MethodEndpoint not registered", endpoint);
64          Method doIt = MyEndpoint.class.getMethod("doIt", Source.class);
65          MethodEndpoint expected = new MethodEndpoint("endpoint", applicationContext, doIt);
66          assertEquals("Invalid endpoint registered", expected, endpoint);
67  
68          assertNull("Invalid endpoint registered",
69                  mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Request2")));
70      }
71  
72      @Test
73      public void invoke() throws Exception {
74  
75          MessageFactory messageFactory = MessageFactory.newInstance();
76          SOAPMessage request = messageFactory.createMessage();
77          request.getSOAPBody().addBodyElement(QName.valueOf("{http://springframework.org/spring-ws}Request"));
78          MessageContext messageContext =
79                  new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
80          DefaultMethodEndpointAdapter adapter = new DefaultMethodEndpointAdapter();
81          adapter.afterPropertiesSet();
82  
83          MessageDispatcher messageDispatcher = new SoapMessageDispatcher();
84          messageDispatcher.setApplicationContext(applicationContext);
85          messageDispatcher.setEndpointMappings(Collections.<EndpointMapping>singletonList(mapping));
86          messageDispatcher.setEndpointAdapters(Collections.<EndpointAdapter>singletonList(adapter));
87  
88          messageDispatcher.receive(messageContext);
89  
90          MyEndpoint endpoint = applicationContext.getBean("endpoint", MyEndpoint.class);
91          assertTrue("doIt() not invoked on endpoint", endpoint.isDoItInvoked());
92  
93          LogAspect aspect = (LogAspect) applicationContext.getBean("logAspect");
94          assertTrue("log() not invoked on aspect", aspect.isLogInvoked());
95      }
96  
97      @Endpoint
98      public static class MyEndpoint {
99  
100         private static final org.apache.commons.logging.Log logger = LogFactory.getLog(MyEndpoint.class);
101 
102         private boolean doItInvoked = false;
103 
104         public boolean isDoItInvoked() {
105             return doItInvoked;
106         }
107 
108         @PayloadRoot(localPart = "Request", namespace = "http://springframework.org/spring-ws")
109         @Log
110         public void doIt(@RequestPayload Source payload) {
111             doItInvoked = true;
112             logger.info("In doIt()");
113         }
114 
115     }
116 
117     static class OtherBean {
118 
119         @PayloadRoot(localPart = "Request2", namespace = "http://springframework.org/spring-ws")
120         public void doIt() {
121 
122         }
123 
124     }
125 }