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     */
016    package org.kuali.rice.ksb.messaging;
017    
018    import org.junit.Test;
019    import org.kuali.rice.ksb.api.KsbApiServiceLocator;
020    import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
021    import org.kuali.rice.ksb.messaging.bam.service.BAMService;
022    import org.kuali.rice.ksb.messaging.callbacks.SimpleCallback;
023    import org.kuali.rice.ksb.messaging.service.KSBJavaService;
024    import org.kuali.rice.ksb.messaging.service.KSBXMLService;
025    import org.kuali.rice.ksb.service.KSBServiceLocator;
026    import org.kuali.rice.ksb.test.KSBTestCase;
027    
028    import javax.xml.namespace.QName;
029    import java.util.List;
030    
031    import 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     */
041    public 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    }