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 */
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 @Test
048 public void testAsyncJavaCall() throws Exception {
049 KSBTestUtils.setMessagingToAsync();
050
051 QName serviceName = new QName("TestCl1", "testJavaAsyncService");
052 SimpleCallback callback = new SimpleCallback();
053 KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper()
054 .getServiceAsynchronously(serviceName, callback);
055 synchronized (callback) {
056 testJavaAsyncService.invoke(new MessagingTestObject("message content"));
057 callback.waitForAsyncCall();
058 }
059 verifyServiceCalls(serviceName);
060 }
061
062 @Test
063 public void testAsyncXmlCall() throws Exception {
064 KSBTestUtils.setMessagingToAsync();
065
066 QName serviceName = new QName("TestCl1", "testXmlAsyncService");
067 SimpleCallback callback = new SimpleCallback();
068 KSBXMLService testXmlAsyncService = (KSBXMLService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
069 serviceName, callback);
070 synchronized (callback) {
071 testXmlAsyncService.invoke("message content");
072 callback.waitForAsyncCall();
073 }
074 verifyServiceCalls(serviceName);
075 }
076
077 private void verifyServiceCalls(QName serviceName) throws Exception {
078 BAMService bamService = KSBServiceLocator.getBAMService();
079 List<BAMTargetEntry> bamCalls = bamService.getCallsForService(serviceName);
080 assertTrue("No service call recorded", bamCalls.size() > 0);
081 boolean foundClientCall = false;
082 boolean foundServiceCall = false;
083 for (BAMTargetEntry bamEntry : bamCalls) {
084 if (bamEntry.getServerInvocation()) {
085 foundServiceCall = true;
086 } else {
087 foundClientCall = true;
088 }
089 }
090 assertTrue("No client call recorded", foundClientCall);
091 assertTrue("No service call recorded", foundServiceCall);
092 }
093
094 }