001/** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */ 016package org.kuali.rice.ksb.messaging; 017 018import static org.junit.Assert.assertEquals; 019import static org.junit.Assert.assertFalse; 020import static org.junit.Assert.assertNotSame; 021import static org.junit.Assert.assertTrue; 022 023import java.util.ArrayList; 024import java.util.List; 025 026import javax.xml.namespace.QName; 027 028import org.junit.Test; 029import org.kuali.rice.ksb.api.KsbApiServiceLocator; 030import org.kuali.rice.ksb.api.bus.ServiceBus; 031import org.kuali.rice.ksb.api.bus.support.JavaServiceDefinition; 032import org.kuali.rice.ksb.api.registry.ServiceInfo; 033import org.kuali.rice.ksb.api.registry.ServiceRegistry; 034import org.kuali.rice.ksb.messaging.remotedservices.TestRepeatMessageQueue; 035import org.kuali.rice.ksb.test.KSBTestCase; 036 037/** 038 * This test ensures that ServiceInfo and ServiceDefinition instances are being modified and removed correctly. 039 * 040 * @author Kuali Rice Team (rice.collab@kuali.org) 041 */ 042public class ServiceUpdateAndRemovalTest extends KSBTestCase { 043 044 /** 045 * Tests the removeLocallyPublishedServices() method of the service registry to ensure that the local services are being deleted properly. 046 * 047 * @throws Exception 048 */ 049 @Test 050 public void testRemovalOfAllLocalServices() throws Exception { 051 ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus(); 052 ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry(); 053 List<ServiceInfo> serviceInfos = findLocallyPublishedServices(serviceBus.getInstanceId(), serviceRegistry); 054 assertFalse("There should be at least one locally published service in the database.", serviceInfos.isEmpty()); 055 serviceRegistry.takeInstanceOffline(serviceBus.getInstanceId()); 056 serviceInfos = findLocallyPublishedServices(serviceBus.getInstanceId(), serviceRegistry); 057 assertEquals("There should not be any locally published services in the database.", 0, serviceInfos.size()); 058 } 059 060 private List<ServiceInfo> findLocallyPublishedServices(String instanceId, ServiceRegistry serviceRegistry) { 061 List<ServiceInfo> locallyPublishedServices = new ArrayList<ServiceInfo>(); 062 List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices(); 063 for (ServiceInfo serviceInfo : serviceInfos) { 064 if (serviceInfo.getInstanceId().equals(instanceId)) { 065 locallyPublishedServices.add(serviceInfo); 066 } 067 } 068 return locallyPublishedServices; 069 } 070 071 private ServiceInfo findLocallyPublishedService(String instanceId, QName serviceName, ServiceRegistry serviceRegistry) { 072 List<ServiceInfo> locallyPublishedServices = findLocallyPublishedServices(instanceId, serviceRegistry); 073 for (ServiceInfo serviceInfo : locallyPublishedServices) { 074 if (serviceInfo.getServiceName().equals(serviceName)) { 075 return serviceInfo; 076 } 077 } 078 return null; 079 } 080 081 /** 082 * Tests the deployment and modification of local services to ensure that they are being updated accordingly. 083 * 084 * @throws Exception 085 */ 086 @Test 087 public void testModificationOfLocalServices() throws Exception { 088 ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus(); 089 ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry(); 090 QName serviceName = new QName("KEW", "serviceForTestingModifications"); 091 ServiceInfo regularInfo = null; 092 // Create and deploy a simple test service. 093 JavaServiceDefinition serviceDefinition = new JavaServiceDefinition(); 094 serviceDefinition.setServiceName(serviceName); 095 serviceDefinition.setPriority(4); 096 serviceDefinition.setService(new TestRepeatMessageQueue()); 097 serviceDefinition.validate(); 098 serviceBus.publishService(serviceDefinition, true); 099 // 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}