View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.opensource.org/licenses/ecl2.php
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
10   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
11   * language governing permissions and limitations under the License.
12   */
13  package org.kuali.rice.ksb.messaging;
14  
15  import org.junit.Test;
16  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
17  import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
18  import org.kuali.rice.ksb.messaging.bam.service.BAMService;
19  import org.kuali.rice.ksb.messaging.callbacks.SimpleCallback;
20  import org.kuali.rice.ksb.messaging.service.KSBJavaService;
21  import org.kuali.rice.ksb.messaging.service.KSBXMLService;
22  import org.kuali.rice.ksb.service.KSBServiceLocator;
23  import org.kuali.rice.ksb.test.KSBTestCase;
24  
25  import javax.xml.namespace.QName;
26  import java.util.List;
27  
28  import static org.junit.Assert.assertTrue;
29  
30  
31  /**
32   * Tests calling services in a very simple scenario. This test could probably go now that more 'feature-full' tests are out
33   * there.
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class SimpleServiceCallTest extends KSBTestCase {
39  
40  	public boolean startClient1() {
41  		return true;
42  	}
43  	
44      @Test
45      public void testAsyncJavaCall() throws Exception {
46  	    KSBTestUtils.setMessagingToAsync();
47  		
48  		QName serviceName = new QName("TestCl1", "testJavaAsyncService");
49  		SimpleCallback callback = new SimpleCallback();
50  	KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper()
51  		.getServiceAsynchronously(serviceName, callback);
52  	    synchronized (callback) {
53  	        testJavaAsyncService.invoke(new MessagingTestObject("message content"));
54  	        callback.waitForAsyncCall();
55  	    }
56  		verifyServiceCalls(serviceName);
57  	}
58  	
59      @Test
60      public void testAsyncXmlCall() throws Exception {
61  	    KSBTestUtils.setMessagingToAsync();
62  		
63  		QName serviceName = new QName("TestCl1", "testXmlAsyncService");
64  		SimpleCallback callback = new SimpleCallback();
65  	KSBXMLService testXmlAsyncService = (KSBXMLService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
66  		serviceName, callback);
67  	    synchronized (callback) {	
68  	        testXmlAsyncService.invoke("message content");
69  	        callback.waitForAsyncCall();
70  	    }
71  		verifyServiceCalls(serviceName);
72  	}
73  	
74  	private void verifyServiceCalls(QName serviceName) throws Exception {
75  		BAMService bamService = KSBServiceLocator.getBAMService();
76  		List<BAMTargetEntry> bamCalls = bamService.getCallsForService(serviceName);
77  		assertTrue("No service call recorded", bamCalls.size() > 0);
78  		boolean foundClientCall = false;
79  		boolean foundServiceCall = false;
80  		for (BAMTargetEntry bamEntry : bamCalls) {
81  			if (bamEntry.getServerInvocation()) {
82  				foundServiceCall = true;
83  			} else {
84  				foundClientCall = true;
85  			}
86  		}
87  		assertTrue("No client call recorded", foundClientCall);
88  		assertTrue("No service call recorded", foundServiceCall);
89  	}
90  	
91  }