001/**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging;
017
018import static org.junit.Assert.assertNotNull;
019import static org.junit.Assert.assertTrue;
020import static org.junit.Assert.fail;
021
022import javax.xml.namespace.QName;
023
024import org.apache.commons.httpclient.URI;
025import org.apache.cxf.aegis.databinding.AegisDatabinding;
026import org.apache.cxf.binding.soap.SoapFault;
027import org.apache.cxf.endpoint.Client;
028import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
029import org.apache.cxf.frontend.ClientProxyFactoryBean;
030import org.apache.cxf.interceptor.LoggingInInterceptor;
031import org.apache.cxf.interceptor.LoggingOutInterceptor;
032import org.junit.Test;
033import org.kuali.rice.core.api.config.property.ConfigContext;
034import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
035import org.kuali.rice.ksb.messaging.remotedservices.EchoService;
036import org.kuali.rice.ksb.messaging.remotedservices.JaxWsEchoService;
037import org.kuali.rice.ksb.messaging.remotedservices.SOAPService;
038import org.kuali.rice.ksb.service.KSBServiceLocator;
039import org.kuali.rice.ksb.test.KSBTestCase;
040
041
042/**
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046public class SOAPServiceTest extends KSBTestCase {
047        
048        public boolean startClient1() {
049                return true;
050        }
051        
052        private String getEndpointUrl() {
053                return "http://localhost:" + 
054                        getClient1Port() +
055                        "/TestClient1/remoting/secure/soap-echoServiceSecure";
056        }
057        
058        private String getWsdlUrl() {
059                return "http://localhost:" +
060                        getClient1Port() +
061                        "/TestClient1/remoting/soap-echoService?wsdl";
062        }
063        
064        private String getClient1Port() {
065                return ConfigContext.getCurrentContextConfig().getProperty("ksb.client1.port");
066        }
067
068        
069        @Test public void testSimpleSOAPService() throws Exception{
070 
071                
072                EchoService echoService = (EchoService)GlobalResourceLoader.getService(new QName("TestCl1", "soap-echoService"));
073                String result = echoService.trueEcho("Yo yo yo");
074                assertNotNull(result);
075                
076                QName serviceName = new QName("testNameSpace", "soap-repeatTopic");
077                SOAPService soapService = (SOAPService) GlobalResourceLoader.getService(serviceName);
078                soapService.doTheThing("hello");
079        }
080        
081        @Test
082        public void testJaxWsSOAPService(){     
083                
084                JaxWsEchoService jaxwsEchoService = (JaxWsEchoService) GlobalResourceLoader.getService(new QName("TestCl1", "jaxwsEchoService"));
085                String result = jaxwsEchoService.doEcho("Fi Fi Fo Fum");
086                assertTrue(("Fi Fi Fo Fum").equals(result));
087        }
088        
089        @Test 
090        public void testBusSecureSoapService() throws Exception{
091                //Create non-secure client to access secure service
092                ClientProxyFactoryBean clientFactory;           
093                clientFactory = new ClientProxyFactoryBean();
094
095                clientFactory.setBus(KSBServiceLocator.getCXFBus());
096                clientFactory.getServiceFactory().setDataBinding(new AegisDatabinding());       
097                clientFactory.setServiceClass(EchoService.class);
098                clientFactory.setServiceName(new QName("urn:TestCl1", "soap-echoServiceSecure"));
099                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}