View Javadoc

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