View Javadoc

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