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.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotSame;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.junit.Test;
29  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
30  import org.kuali.rice.ksb.api.bus.ServiceBus;
31  import org.kuali.rice.ksb.api.bus.support.JavaServiceDefinition;
32  import org.kuali.rice.ksb.api.registry.ServiceInfo;
33  import org.kuali.rice.ksb.api.registry.ServiceRegistry;
34  import org.kuali.rice.ksb.messaging.remotedservices.TestRepeatMessageQueue;
35  import org.kuali.rice.ksb.test.KSBTestCase;
36  
37  /**
38   * This test ensures that ServiceInfo and ServiceDefinition instances are being modified and removed correctly. 
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class ServiceUpdateAndRemovalTest extends KSBTestCase {
43  
44  	/**
45  	 * Tests the removeLocallyPublishedServices() method of the service registry to ensure that the local services are being deleted properly.
46  	 * 
47  	 * @throws Exception
48  	 */
49  	@Test
50  	public void testRemovalOfAllLocalServices() throws Exception {
51  		ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
52  		ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
53  		List<ServiceInfo> serviceInfos = findLocallyPublishedServices(serviceBus.getInstanceId(), serviceRegistry);
54  		assertFalse("There should be at least one locally published service in the database.", serviceInfos.isEmpty());
55  		serviceRegistry.takeInstanceOffline(serviceBus.getInstanceId());
56  		serviceInfos = findLocallyPublishedServices(serviceBus.getInstanceId(), serviceRegistry);
57  		assertEquals("There should not be any locally published services in the database.", 0, serviceInfos.size());
58  	}
59  	
60  	private List<ServiceInfo> findLocallyPublishedServices(String instanceId, ServiceRegistry serviceRegistry) {
61  		List<ServiceInfo> locallyPublishedServices = new ArrayList<ServiceInfo>();
62  		List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices();
63  		for (ServiceInfo serviceInfo : serviceInfos) {
64  			if (serviceInfo.getInstanceId().equals(instanceId)) {
65  				locallyPublishedServices.add(serviceInfo);
66  			}
67  		}
68  		return locallyPublishedServices;
69  	}
70  	
71  	private ServiceInfo findLocallyPublishedService(String instanceId, QName serviceName, ServiceRegistry serviceRegistry) {
72  		List<ServiceInfo> locallyPublishedServices = findLocallyPublishedServices(instanceId, serviceRegistry);
73  		for (ServiceInfo serviceInfo : locallyPublishedServices) {
74  			if (serviceInfo.getServiceName().equals(serviceName)) {
75  				return serviceInfo;
76  			}
77  		}
78  		return null;
79  	}
80  	
81  	/**
82  	 * Tests the deployment and modification of local services to ensure that they are being updated accordingly.
83  	 * 
84  	 * @throws Exception
85  	 */
86  	@Test
87  	public void testModificationOfLocalServices() throws Exception {
88  		ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
89  		ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
90  		QName serviceName = new QName("KEW", "serviceForTestingModifications");
91  		ServiceInfo regularInfo = null;
92  		// Create and deploy a simple test service.
93  		JavaServiceDefinition serviceDefinition = new JavaServiceDefinition();
94  		serviceDefinition.setServiceName(serviceName);
95  		serviceDefinition.setPriority(4);
96  		serviceDefinition.setService(new TestRepeatMessageQueue());
97  		serviceDefinition.validate();
98  		serviceBus.publishService(serviceDefinition, true);
99  		// Retrieve the ServiceInfo for the original service and the ServiceInfo for the related forward.
100 		regularInfo = findLocallyPublishedService(serviceBus.getInstanceId(), serviceName, serviceRegistry);
101 		// Ensure that refreshing the local registry without modifying the ServiceDefinition yields the expected results.
102 		assertRegistryRefreshHasExpectedResults(serviceBus, serviceRegistry, regularInfo, serviceName, false);
103 		// Ensure that refreshing the local registry after modifying the ServiceDefinition yields the expected results.
104 		regularInfo = findLocallyPublishedService(serviceBus.getInstanceId(), serviceName, serviceRegistry);
105 		serviceDefinition.setPriority(3);
106 		serviceDefinition.validate();
107 		assertRegistryRefreshHasExpectedResults(serviceBus, serviceRegistry, regularInfo, serviceName, true);
108 	}
109 	
110 	/**
111 	 * A convenience method for asserting that expected similarities and differences should have occurred after refreshing the registry. This includes
112 	 * comparing the checksums and ensuring that non-similar checksums are corresponding to modifications to the serialized and non-serialized
113 	 * ServiceDefinition instances.
114 	 * 
115 	 * @param remotedServiceRegistry The service registry to test with.
116 	 * @param regularInfo The ServiceInfo containing the configured service.
117 	 * @param forwardInfo The ServiceInfo containing the ForwardedCallHandler for the configured service.
118 	 * @param serviceName The QName of the configured service.
119 	 * @param forwardServiceName The QName of the ForwardedCallHandler for the configured service.
120 	 * @param serviceDefinitionsShouldDiffer A flag indicating if the service definitions should be tested for similarity or difference after the refresh.
121 	 * @throws Exception
122 	 */
123 	private void assertRegistryRefreshHasExpectedResults(ServiceBus serviceBus, ServiceRegistry serviceRegistry, ServiceInfo regularInfo,
124 			QName serviceName, boolean serviceDefinitionsShouldDiffer) throws Exception {
125 		// Sync the bus
126 		serviceBus.synchronize();
127 		ServiceInfo newRegularInfo = findLocallyPublishedService(regularInfo.getInstanceId(), serviceName, serviceRegistry);
128 		// Perform the assertions that should have the same outcome regardless of whether or not a ServiceDefinition was modified.
129 		assertTrue("The ServiceInfo instances for the service should satisy the non-ServiceDefinition part of an isSame() check",
130 				regularInfo.getStatus().equals(newRegularInfo.getStatus()) && regularInfo.getServiceName().equals(newRegularInfo.getServiceName()) &&
131 						regularInfo.getServerIpAddress().equals(newRegularInfo.getServerIpAddress()) &&
132 								regularInfo.getApplicationId().equals(newRegularInfo.getApplicationId()));
133 		// Perform the appropriate assertions based on whether or not any updates are expected.
134 		if (serviceDefinitionsShouldDiffer) {
135 			assertNotSame("The checksum for the configured service should have been modified after refreshing the registry.",
136 					regularInfo.getChecksum(), newRegularInfo.getChecksum());
137 		} else {
138 			assertEquals("The checksum for the configured service should not have been modified after refreshing the registry.",
139 					regularInfo.getChecksum(), newRegularInfo.getChecksum());
140 		}
141 	}
142 }