001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package edu.iu.uis.eden.messaging;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import javax.xml.namespace.QName;
022:
023: import org.junit.Test;
024: import org.kuali.bus.services.KSBServiceLocator;
025: import org.kuali.bus.test.KSBTestCase;
026: import org.kuali.rice.resourceloader.GlobalResourceLoader;
027:
028: import edu.iu.uis.eden.messaging.bam.BAMTargetEntry;
029: import edu.iu.uis.eden.messaging.remotedservices.GenericTestService;
030: import edu.iu.uis.eden.messaging.remotedservices.SOAPService;
031: import edu.iu.uis.eden.messaging.remotedservices.TestServiceInterface;
032: import edu.iu.uis.eden.messaging.resourceloading.KSBResourceLoaderFactory;
033:
034: /**
035: * Verify services in a cluster are being both being called
036: * Verify a locally deployed service is always called instead of a remote service in a cluster
037: * Verify that a service in a cluster fails over when one of the services in the cluster goes down.
038: *
039: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
040: *
041: */
042: public class ServiceCallClusterTest extends KSBTestCase {
043:
044: private final static int SERVICE_CALLS = 15;
045:
046: public boolean startClient1() {
047: return true;
048: }
049:
050: public boolean startClient2() {
051: return true;
052: }
053:
054: @Override
055: public void setUp() throws Exception {
056: super .setUp();
057: ((Runnable) KSBResourceLoaderFactory.getRemoteResourceLocator())
058: .run();
059: }
060:
061: @Test
062: public void testSOAPClustering() throws Exception {
063: QName serviceName = new QName("testNameSpace",
064: "soap-cluster-test");
065: int i = 0;
066: List<SOAPService> services = new ArrayList<SOAPService>();
067: while (i < SERVICE_CALLS) {
068: i++;
069: services.add((SOAPService) GlobalResourceLoader
070: .getService(serviceName));
071: }
072:
073: for (SOAPService service : services) {
074: service.doTheThing("testing one two three");
075: }
076:
077: String server1Name = "TestClient1";
078: String server2Name = "TestClient2";
079: boolean server1Called = false;
080: boolean server2Called = false;
081: //verify clustering is happening through bam
082: List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService()
083: .getCallsForService(serviceName);
084: for (BAMTargetEntry bam : bams) {
085: System.out.println("Found bam service URL of "
086: + bam.getServiceURL());
087: if (bam.getServiceURL().indexOf(server1Name) > -1) {
088: server1Called = true;
089: } else if (bam.getServiceURL().indexOf(server2Name) > -1) {
090: server2Called = true;
091: }
092: }
093:
094: assertTrue(server1Called);
095: assertTrue(server2Called);
096: }
097:
098: @Test
099: public void testClustering() throws Exception {
100: QName serviceName = new QName("KEW", "testServiceFailover");
101: List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
102: int i = 0;
103: while (i < SERVICE_CALLS) {
104: i++;
105: services.add((TestServiceInterface) GlobalResourceLoader
106: .getService(serviceName));
107: }
108:
109: for (TestServiceInterface service : services) {
110: service.invoke();
111: }
112:
113: String server1Name = "TestClient1";
114: String server2Name = "TestClient2";
115: boolean server1Called = false;
116: boolean server2Called = false;
117: //verify clustering is happening
118: List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService()
119: .getCallsForService(serviceName);
120: for (BAMTargetEntry bam : bams) {
121: if (bam.getServiceURL().indexOf(server1Name) > -1) {
122: server1Called = true;
123: } else if (bam.getServiceURL().indexOf(server2Name) > -1) {
124: server2Called = true;
125: }
126: }
127:
128: assertTrue(server1Called);
129: assertTrue(server2Called);
130: }
131:
132: @Test
133: public void testServiceFailOver() throws Exception {
134: QName serviceName = new QName("KEW", "testServiceFailover");
135: List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
136: int i = 0;
137: while (i < SERVICE_CALLS) {
138: i++;
139: services.add((TestServiceInterface) GlobalResourceLoader
140: .getService(serviceName));
141: }
142:
143: String server1Name = "TestClient1";
144: String server2Name = "TestClient2";
145: boolean server1Called = false;
146: boolean server2Called = false;
147: //stop the server 1 and verify all calls going to server 2
148: try {
149: getTestClient1().stop();
150: } catch (Throwable t) {
151: // this is okay
152: }
153:
154: for (TestServiceInterface service : services) {
155: service.invoke();
156: }
157:
158: List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService()
159: .getCallsForService(serviceName);
160: for (BAMTargetEntry bam : bams) {
161: if (bam.getServiceURL().indexOf(server1Name) > -1
162: && bam.getServerInvocation()) {
163: server1Called = true;
164: } else if (bam.getServiceURL().indexOf(server2Name) > -1) {
165: server2Called = true;
166: }
167: }
168:
169: assertFalse("server1 should not have been called",
170: server1Called);
171: assertTrue("server2 should have been called", server2Called);
172: }
173:
174: @Test
175: public void testSOAPFailOver() throws Exception {
176: QName serviceName = new QName("testNameSpace",
177: "soap-cluster-test");
178: int i = 0;
179: List<SOAPService> services = new ArrayList<SOAPService>();
180: while (i < SERVICE_CALLS) {
181: i++;
182: services.add((SOAPService) GlobalResourceLoader
183: .getService(serviceName));
184: }
185:
186: // stop the server 1 and verify all calls going to server 2
187: try {
188: getTestClient1().stop();
189: } catch (Throwable t) {
190: // this is okay
191: }
192:
193: for (SOAPService service : services) {
194: service.doTheThing("testing one two three");
195: }
196:
197: String server1Name = "TestClient1";
198: String server2Name = "TestClient2";
199: boolean server1Called = false;
200: boolean server2Called = false;
201: //verify clustering is happening through bam
202: List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService()
203: .getCallsForService(serviceName);
204: for (BAMTargetEntry bam : bams) {
205: if (bam.getServerInvocation()
206: && bam.getServiceURL().indexOf(server1Name) > -1) {
207: server1Called = true;
208: } else if (bam.getServerInvocation()
209: && bam.getServiceURL().indexOf(server2Name) > -1) {
210: server2Called = true;
211: }
212: }
213:
214: assertFalse(server1Called);
215: assertTrue(server2Called);
216: }
217:
218: @Test
219: public void testDefaultToLocalService() throws Exception {
220: QName serviceName = new QName("KEW",
221: "testLocalServiceFavoriteCall");
222: List<TestServiceInterface> services = new ArrayList<TestServiceInterface>();
223: int i = 0;
224: while (i < SERVICE_CALLS) {
225: i++;
226: services.add((TestServiceInterface) GlobalResourceLoader
227: .getService(serviceName));
228: }
229:
230: for (TestServiceInterface service : services) {
231: service.invoke();
232: }
233:
234: String testHarness = "en-test";
235: String server1Name = "TestClient1";
236: boolean localCalled = false;
237: boolean server1Called = false;
238: //verify clustering is happening
239: List<BAMTargetEntry> bams = KSBServiceLocator.getBAMService()
240: .getCallsForService(serviceName);
241: for (BAMTargetEntry bam : bams) {
242: if (bam.getServiceURL().indexOf(server1Name) > -1) {
243: server1Called = true;
244: } else if (bam.getServiceURL().indexOf(testHarness) > -1) {
245: localCalled = true;
246: }
247: }
248:
249: assertFalse(
250: "BAM should not have recorded locally called services",
251: localCalled);
252: assertFalse(
253: "Remotely deployed service should have never been called in favor of remote service",
254: server1Called);
255:
256: assertTrue("Service should have been called locally",
257: GenericTestService.NUM_CALLS > 0);
258: }
259:
260: }
|