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 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 	@Test public void testServiceFailOver() throws Exception {
127 		QName serviceName = new QName("KEW", "testServiceFailover");
128 		List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
129 		int i = 0;
130 		while (i < SERVICE_CALLS) {
131 			i++;
132 			services.add((TestServiceInterface)GlobalResourceLoader.getService(serviceName));
133 		}
134 		
135 		String server1Name = "TestClient1";
136 		String server2Name = "TestClient2";
137 		boolean server1Called = false;
138 		boolean server2Called = false;
139 		//stop the server 1 and verify all calls going to server 2
140 		try {
141 			getTestClient1().stop();	
142 		} catch (Throwable t) {
143 			// this is okay
144 		}
145 		
146 		
147 		for (TestServiceInterface service : services) {
148 			service.invoke();
149 		}
150 		
151 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
152 		for (BAMTargetEntry bam : bams) {
153 			if (bam.getServiceURL().contains(server1Name) && bam.getServerInvocation()) {
154 				server1Called = true;
155 			} else if (bam.getServiceURL().contains(server2Name)) {
156 				server2Called = true;
157 			}
158 		}
159 		
160 		assertFalse("server1 should not have been called", server1Called);
161 		assertTrue("server2 should have been called", server2Called);
162 	}
163 	
164 	@Test public void testSOAPFailOver() throws Exception {
165 		QName serviceName = new QName("testNameSpace", "soap-cluster-test");
166 		int i = 0;
167 		List<SOAPService> services = new ArrayList<SOAPService>();
168 		while (i < SERVICE_CALLS) {
169 			i++;
170 			services.add((SOAPService)GlobalResourceLoader.getService(serviceName));
171 		}
172 		
173 //		stop the server 1 and verify all calls going to server 2
174 		try {
175 			getTestClient1().stop();	
176 		} catch (Throwable t) {
177 		    // this is okay
178 		}
179 		
180 		for (SOAPService service : services) {
181 			service.doTheThing("testing one two three");
182 		}
183 		
184 		String server1Name = "TestClient1";
185 		String server2Name = "TestClient2";
186 		boolean server1Called = false;
187 		boolean server2Called = false;
188 		//verify clustering is happening through bam
189 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
190 		for (BAMTargetEntry bam : bams) {
191 			if (bam.getServerInvocation() && bam.getServiceURL().contains(server1Name)) {
192 				server1Called = true;
193 			} else if (bam.getServerInvocation() && bam.getServiceURL().contains(server2Name)) {
194 				server2Called = true;
195 			}
196 		}
197 		
198 		assertFalse(server1Called);
199 		assertTrue(server2Called);
200 	}
201 	
202 	
203 	@Test public void testDefaultToLocalService() throws Exception {
204 		QName serviceName = new QName("KEW", "testLocalServiceFavoriteCall");
205 		List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
206 		int i = 0;
207 		while (i < SERVICE_CALLS) {
208 			i++;
209 			services.add((TestServiceInterface)GlobalResourceLoader.getService(serviceName));
210 		}
211 		
212 		for (TestServiceInterface service : services) {
213 			service.invoke();
214 		}
215 		
216 		String testHarness = "en-test";
217 		String server1Name = "TestClient1";
218 		boolean localCalled = false;
219 		boolean server1Called = false;
220 		//verify clustering is happening
221 		List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService().getCallsForService(serviceName);
222 		for (BAMTargetEntry bam : bams) {
223 			if (bam.getServiceURL().contains(server1Name)) {
224 				server1Called = true;
225 			} else if (bam.getServiceURL().contains(testHarness)) {
226 				localCalled = true;
227 			}
228 		}
229 		
230 		assertFalse("BAM should not have recorded locally called services", localCalled);
231 		assertFalse("Remotely deployed service should have never been called in favor of remote service", server1Called);
232 		
233 		assertTrue("Service should have been called locally", GenericTestService.NUM_CALLS > 0);
234 	}
235 	
236 }