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