View Javadoc
1   /**
2    * Copyright 2005-2014 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 static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.junit.Test;
27  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
28  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
29  import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
30  import org.kuali.rice.ksb.messaging.remotedservices.GenericTestService;
31  import org.kuali.rice.ksb.messaging.remotedservices.SOAPService;
32  import org.kuali.rice.ksb.messaging.remotedservices.TestServiceInterface;
33  import org.kuali.rice.ksb.service.KSBServiceLocator;
34  import org.kuali.rice.ksb.test.KSBTestCase;
35  
36  
37  /**
38   * Verify services in a cluster are being both being called
39   * Verify a locally deployed service is always called instead of a remote service in a cluster
40   * Verify that a service in a cluster fails over when one of the services in the cluster goes down.
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class ServiceCallClusterTest extends KSBTestCase {
46  
47  	private final static int SERVICE_CALLS = 15;
48  	
49  	public boolean startClient1() {
50  		return true;
51  	}
52  	
53  	public boolean startClient2() {
54  		return true;
55  	}
56  	
57  	@Override
58  	public void setUp() throws Exception {
59  		super.setUp();
60  		KsbApiServiceLocator.getServiceBus().synchronize();
61  	}
62  
63  	@Test public void testSOAPClustering() throws Exception {
64  		QName serviceName = new QName("testNameSpace", "soap-cluster-test");
65  		int i = 0;
66  		List<SOAPService> services = new ArrayList<SOAPService>();
67  		while (i < SERVICE_CALLS) {
68  			i++;
69  			services.add((SOAPService)GlobalResourceLoader.getService(serviceName));
70  		}
71  		
72  		for (SOAPService service : services) {
73  			service.doTheThing("testing one two three");
74  		}
75  		
76  		String server1Name = "TestClient1";
77  		String server2Name = "TestClient2";
78  		boolean server1Called = false;
79  		boolean server2Called = false;
80  		//verify clustering is happening through bam
81  		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
82  		for (BAMTargetEntry bam : bams) {
83  			System.out.println("Found bam service URL of " + bam.getServiceURL());
84  			if (bam.getServiceURL().contains(server1Name)) {
85  				server1Called = true;
86  			} else if (bam.getServiceURL().contains(server2Name)) {
87  				server2Called = true;
88  			}
89  		}
90  		
91  		assertTrue(server1Called);
92  		assertTrue(server2Called);
93  	}
94  	
95  	@Test public void testClustering() throws Exception {
96  		QName serviceName = new QName("KEW", "testServiceFailover");
97  		List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
98  		int i = 0;
99  		while (i < SERVICE_CALLS) {
100 			i++;
101 			services.add((TestServiceInterface) GlobalResourceLoader.getService(serviceName));
102 		}
103 		
104 		for (TestServiceInterface service : services) {
105 			service.invoke();
106 		}
107 		
108 		String server1Name = "TestClient1";
109 		String server2Name = "TestClient2";
110 		boolean server1Called = false;
111 		boolean server2Called = false;
112 		//verify clustering is happening
113 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
114 		for (BAMTargetEntry bam : bams) {
115 			if (bam.getServiceURL().contains(server1Name)) {
116 				server1Called = true;
117 			} else if (bam.getServiceURL().contains(server2Name)) {
118 				server2Called = true;
119 			}
120 		}
121 		
122 		assertTrue(server1Called);
123 		assertTrue(server2Called);
124 	}
125 
126     /**
127      *  The services published to the service def table when ServiceCallClusterTest runs used to have
128      *  have two different application IDs, TestCl1 and TestCl2.  A fix was contributed from IU so that when a
129      *  service fails, it fails over to services only using the same application ID.   This is correct, but it made
130      *  the tests no longer work.  The tests now bring up services with the same application ID, TestCl1.
131      */
132 	@Test public void testServiceFailOver() throws Exception {
133 		QName serviceName = new QName("KEW", "testServiceFailover");
134 		List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
135 		int i = 0;
136 		while (i < SERVICE_CALLS) {
137 			i++;
138 			services.add((TestServiceInterface)GlobalResourceLoader.getService(serviceName));
139 		}
140 		
141 		String server1Name = "TestClient1";
142 		String server2Name = "TestClient2";
143 		boolean server1Called = false;
144 		boolean server2Called = false;
145 		//stop the server 1 and verify all calls going to server 2
146 		try {
147 			getTestClient1().stop();	
148 		} catch (Throwable t) {
149 			// this is okay
150 		}
151 		
152 		
153 		for (TestServiceInterface service : services) {
154 			service.invoke();
155 		}
156 		
157 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
158 		for (BAMTargetEntry bam : bams) {
159 			if (bam.getServiceURL().contains(server1Name) && bam.getServerInvocation()) {
160 				server1Called = true;
161 			} else if (bam.getServiceURL().contains(server2Name)) {
162 				server2Called = true;
163 			}
164 		}
165 		
166 		assertFalse("server1 should not have been called", server1Called);
167 		assertTrue("server2 should have been called", server2Called);
168 	}
169 
170 	
171 	@Test public void testSOAPFailOver() throws Exception {
172 		QName serviceName = new QName("testNameSpace", "soap-cluster-test");
173 		int i = 0;
174 		List<SOAPService> services = new ArrayList<SOAPService>();
175 		while (i < SERVICE_CALLS) {
176 			i++;
177 			services.add((SOAPService)GlobalResourceLoader.getService(serviceName));
178 		}
179 		
180 //		stop the server 1 and verify all calls going to server 2
181 		try {
182 			getTestClient1().stop();	
183 		} catch (Throwable t) {
184 		    // this is okay
185 		}
186 		
187 		for (SOAPService service : services) {
188 			service.doTheThing("testing one two three");
189 		}
190 		
191 		String server1Name = "TestClient1";
192 		String server2Name = "TestClient2";
193 		boolean server1Called = false;
194 		boolean server2Called = false;
195 		//verify clustering is happening through bam
196 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
197 		for (BAMTargetEntry bam : bams) {
198 			if (bam.getServerInvocation() && bam.getServiceURL().contains(server1Name)) {
199 				server1Called = true;
200 			} else if (bam.getServerInvocation() && bam.getServiceURL().contains(server2Name)) {
201 				server2Called = true;
202 			}
203 		}
204 		
205 		assertFalse(server1Called);
206 		assertTrue(server2Called);
207 	}
208 
209 	@Test public void testDefaultToLocalService() throws Exception {
210 		QName serviceName = new QName("KEW", "testLocalServiceFavoriteCall");
211 		List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
212 		int i = 0;
213 		while (i < SERVICE_CALLS) {
214 			i++;
215 			services.add((TestServiceInterface)GlobalResourceLoader.getService(serviceName));
216 		}
217 		
218 		for (TestServiceInterface service : services) {
219 			service.invoke();
220 		}
221 		
222 		String testHarness = "en-test";
223 		String server1Name = "TestClient1";
224 		boolean localCalled = false;
225 		boolean server1Called = false;
226 		//verify clustering is happening
227 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
228 		for (BAMTargetEntry bam : bams) {
229 			if (bam.getServiceURL().contains(server1Name)) {
230 				server1Called = true;
231 			} else if (bam.getServiceURL().contains(testHarness)) {
232 				localCalled = true;
233 			}
234 		}
235 		
236 		assertFalse("BAM should not have recorded locally called services", localCalled);
237 		assertFalse("Remotely deployed service should have never been called in favor of remote service", server1Called);
238 		
239 		assertTrue("Service should have been called locally", GenericTestService.NUM_CALLS > 0);
240 	}
241 	
242 }