1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
40
41
42 public class ServiceUpdateAndRemovalTest extends KSBTestCase {
43
44
45
46
47
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
83
84
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
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
100 regularInfo = findLocallyPublishedService(serviceBus.getInstanceId(), serviceName, serviceRegistry);
101
102 assertRegistryRefreshHasExpectedResults(serviceBus, serviceRegistry, regularInfo, serviceName, false);
103
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
112
113
114
115
116
117
118
119
120
121
122
123 private void assertRegistryRefreshHasExpectedResults(ServiceBus serviceBus, ServiceRegistry serviceRegistry, ServiceInfo regularInfo,
124 QName serviceName, boolean serviceDefinitionsShouldDiffer) throws Exception {
125
126 serviceBus.synchronize();
127 ServiceInfo newRegularInfo = findLocallyPublishedService(regularInfo.getInstanceId(), serviceName, serviceRegistry);
128
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
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 }