View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.ksb.messaging;
17  
18  import org.apache.cxf.aegis.databinding.AegisDatabinding;
19  import org.apache.cxf.binding.soap.SoapFault;
20  import org.apache.cxf.endpoint.Client;
21  import org.apache.cxf.frontend.ClientProxyFactoryBean;
22  import org.apache.cxf.interceptor.LoggingInInterceptor;
23  import org.apache.cxf.interceptor.LoggingOutInterceptor;
24  import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
25  import org.junit.Test;
26  import org.kuali.rice.core.api.config.property.ConfigContext;
27  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
28  import org.kuali.rice.ksb.messaging.remotedservices.EchoService;
29  import org.kuali.rice.ksb.messaging.remotedservices.JaxWsEchoService;
30  import org.kuali.rice.ksb.messaging.remotedservices.SOAPService;
31  import org.kuali.rice.ksb.service.KSBServiceLocator;
32  import org.kuali.rice.ksb.test.KSBTestCase;
33  
34  import javax.xml.namespace.QName;
35  import java.net.URI;
36  
37  import static org.junit.Assert.*;
38  
39  /**
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class SOAPServiceTest extends KSBTestCase {
44  	
45  	public boolean startClient1() {
46  		return true;
47  	}
48  	
49  	private String getEndpointUrl() {
50  		return "http://localhost:" + 
51  			getClient1Port() +
52  			"/TestClient1/remoting/secure/soap-echoServiceSecure";
53  	}
54  	
55  	private String getWsdlUrl() {
56  		return "http://localhost:" +
57  			getClient1Port() +
58  			"/TestClient1/remoting/soap-echoService?wsdl";
59  	}
60  	
61  	private String getClient1Port() {
62  		return ConfigContext.getCurrentContextConfig().getProperty("ksb.client1.port");
63  	}
64  
65  	
66  	@Test
67      public void testSimpleSOAPService() throws Exception{
68  		EchoService echoService = GlobalResourceLoader.getService(new QName("TestCl1", "soap-echoService"));
69  		String result = echoService.trueEcho("Yo yo yo");
70  		assertNotNull(result);
71  
72  		QName serviceName = new QName("testNameSpace", "soap-repeatTopic");
73  		SOAPService soapService = GlobalResourceLoader.getService(serviceName);
74  		soapService.doTheThing("hello");
75  	}
76  
77  	@Test
78  	public void testJaxWsSOAPService(){
79  		JaxWsEchoService jaxwsEchoService = GlobalResourceLoader.getService(new QName("TestCl1", "jaxwsEchoService"));
80  		String result = jaxwsEchoService.doEcho("Fi Fi Fo Fum");
81  		assertTrue(("Fi Fi Fo Fum").equals(result));
82  	}
83  
84  	@Test
85  	public void testBusSecureSOAPService() throws Exception{
86  		//Create non-secure client to access secure service
87  		ClientProxyFactoryBean clientFactory;		
88  		clientFactory = new ClientProxyFactoryBean();
89  
90  		clientFactory.setBus(KSBServiceLocator.getCXFBus());
91  		clientFactory.getServiceFactory().setDataBinding(new AegisDatabinding());	
92  		clientFactory.setServiceClass(EchoService.class);
93  		clientFactory.setServiceName(new QName("urn:TestCl1", "soap-echoServiceSecure"));
94  		clientFactory.setAddress(new URI(getEndpointUrl()).toString());
95  		clientFactory.getInInterceptors().add(new LoggingInInterceptor());
96  		clientFactory.getOutInterceptors().add(new LoggingOutInterceptor());
97  		EchoService echoService = (EchoService)clientFactory.create();
98  		
99  		try{
100 			echoService.echo("I can't echo");
101 			fail("Expected failure using non-secure client with secure service");
102 		} catch (SoapFault sf){
103 			sf.printStackTrace();
104 			assertTrue("Non-secure client did not get expected exception.",
105 					sf.getMessage().startsWith("An error was discovered processing the <wsse:Security> header"));
106 		}
107 		
108 		//Now try a secure client
109 		echoService = GlobalResourceLoader.getService(new QName("urn:TestCl1", "soap-echoServiceSecure"));
110 		String result = echoService.echo("I can echo");
111 		assertTrue("I can echo".equals(result));		
112 	}
113 
114     /**
115      * Tests WSDL generation from a URL.
116      *
117      * This is similar to another KEW test but it is good to have it as part of the KSB tests.  Note that the
118      * {@link Client} modifies the current thread's class loader.
119      *
120      * @throws Exception for any errors connecting to the client
121      */
122 	@Test
123 	public void testWsdlGeneration() throws Exception {
124 		ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
125 
126         try {
127             JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
128             Client client = dcf.createClient(new URI(getWsdlUrl()).toString());
129             client.getInInterceptors().add(new LoggingInInterceptor());
130             client.getOutInterceptors().add(new LoggingOutInterceptor());
131             Object[] results = client.invoke("echo", "testing");
132             assertNotNull(results);
133             assertEquals(1, results.length);
134             assertEquals("testing", results[0]);
135         } finally {
136             Thread.currentThread().setContextClassLoader(originalClassLoader);
137         }
138 	}
139 
140 }