001/**
002 * Copyright 2005-2014 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 org.junit.Test;
019import org.kuali.rice.ksb.api.KsbApiServiceLocator;
020import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
021import org.kuali.rice.ksb.messaging.bam.service.BAMService;
022import org.kuali.rice.ksb.messaging.callbacks.SimpleCallback;
023import org.kuali.rice.ksb.messaging.service.KSBJavaService;
024import org.kuali.rice.ksb.messaging.service.KSBXMLService;
025import org.kuali.rice.ksb.service.KSBServiceLocator;
026import org.kuali.rice.ksb.test.KSBTestCase;
027
028import javax.xml.namespace.QName;
029import java.util.List;
030
031import static org.junit.Assert.assertTrue;
032
033
034/**
035 * Tests calling services in a very simple scenario. This test could probably go now that more 'feature-full' tests are out
036 * there.
037 * 
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041public class SimpleServiceCallTest extends KSBTestCase {
042
043        public boolean startClient1() {
044                return true;
045        }
046
047    /**
048     * This test ensures that doing a toString on a service proxy does not cause infinite loop problems along the lines
049     * as reported in https://jira.kuali.org/browse/KULRICE-6708
050     */
051    @Test
052    public void testToStringOnService() {
053        KSBTestUtils.setMessagingToAsync();
054                QName serviceName = new QName("TestCl1", "testJavaAsyncService");
055                SimpleCallback callback = new SimpleCallback();
056            KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName, callback);
057        String toStringValue = testJavaAsyncService.toString();
058        System.out.println("toString value on async service: " + toStringValue);
059        assertTrue("toString should have started with 'Service call proxy' but was instead: " + toStringValue, toStringValue.startsWith("Service call proxy"));
060    }
061        
062    @Test
063    public void testAsyncJavaCall() throws Exception {
064            KSBTestUtils.setMessagingToAsync();
065                
066                QName serviceName = new QName("TestCl1", "testJavaAsyncService");
067                SimpleCallback callback = new SimpleCallback();
068        KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper()
069                .getServiceAsynchronously(serviceName, callback);
070            synchronized (callback) {
071                testJavaAsyncService.invoke(new MessagingTestObject("message content"));
072                callback.waitForAsyncCall();
073            }
074                verifyServiceCalls(serviceName);
075        }
076        
077    @Test
078    public void testAsyncXmlCall() throws Exception {
079            KSBTestUtils.setMessagingToAsync();
080                
081                QName serviceName = new QName("TestCl1", "testXmlAsyncService");
082                SimpleCallback callback = new SimpleCallback();
083        KSBXMLService testXmlAsyncService = (KSBXMLService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
084                serviceName, callback);
085            synchronized (callback) {   
086                testXmlAsyncService.invoke("message content");
087                callback.waitForAsyncCall();
088            }
089                verifyServiceCalls(serviceName);
090        }
091        
092        private void verifyServiceCalls(QName serviceName) throws Exception {
093                BAMService bamService = KSBServiceLocator.getBAMService();
094                List<BAMTargetEntry> bamCalls = bamService.getCallsForService(serviceName);
095                assertTrue("No service call recorded", bamCalls.size() > 0);
096                boolean foundClientCall = false;
097                boolean foundServiceCall = false;
098                for (BAMTargetEntry bamEntry : bamCalls) {
099                        if (bamEntry.getServerInvocation()) {
100                                foundServiceCall = true;
101                        } else {
102                                foundClientCall = true;
103                        }
104                }
105                assertTrue("No client call recorded", foundClientCall);
106                assertTrue("No service call recorded", foundServiceCall);
107        }
108        
109}