View Javadoc

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