1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.impl.registry;
17
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.kuali.rice.core.api.criteria.GenericQueryResults;
21 import org.kuali.rice.core.api.criteria.QueryByCriteria;
22 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23 import org.kuali.rice.krad.data.DataObjectService;
24 import org.kuali.rice.ksb.api.registry.ServiceInfo;
25 import org.kuali.rice.ksb.api.registry.ServiceRegistry;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.runners.MockitoJUnitRunner;
29
30 import java.util.List;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Matchers.eq;
35 import static org.mockito.Mockito.when;
36
37
38
39
40
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class ServiceRegistryImplTest {
44
45 @Mock private DataObjectService dataObjectService;
46
47 @InjectMocks private ServiceRegistryImpl serviceRegistryImpl = new ServiceRegistryImpl();
48 private ServiceRegistry serviceRegistry = serviceRegistryImpl;
49
50 public ServiceRegistry getServiceRegistry() {
51 return serviceRegistry;
52 }
53
54 public void setServiceRegistry(ServiceRegistry serviceRegistry) {
55 this.serviceRegistry = serviceRegistry;
56 }
57
58 @Test(expected = RiceIllegalArgumentException.class)
59 public void testGetAllServicesForInstance_Null() {
60 serviceRegistry.getAllServicesForInstance(null);
61 }
62
63 @Test(expected = RiceIllegalArgumentException.class)
64 public void testGetAllServicesForInstance_Empty() {
65 serviceRegistry.getAllServicesForInstance("");
66 }
67
68 @Test(expected = RiceIllegalArgumentException.class)
69 public void testGetAllServicesForInstance_Blank() {
70 serviceRegistry.getAllServicesForInstance(" ");
71 }
72
73 @Test
74 public void testGetAllServicesForInstance() {
75 GenericQueryResults.Builder<ServiceInfoBo> resultBuilder = GenericQueryResults.Builder.<ServiceInfoBo>create();
76 for (int i = 0; i < 5; i++) {
77 resultBuilder.getResults().add(createServiceInfo("service" + i));
78 }
79 when(dataObjectService.findMatching(eq(ServiceInfoBo.class), any(QueryByCriteria.class))).thenReturn(resultBuilder.build());
80
81 List<ServiceInfo> serviceInfos = serviceRegistry.getAllServicesForInstance("MyInstance");
82 assertEquals(5, serviceInfos.size());
83 }
84
85 private static int ID_VALUE = 0;
86
87 private String nextId() {
88 ID_VALUE++;
89 return ID_VALUE + "";
90 }
91
92 private ServiceInfoBo createServiceInfo(String serviceName) {
93 ServiceInfoBo serviceInfo = new ServiceInfoBo();
94 serviceInfo.setServiceId(nextId());
95 serviceInfo.setServiceName(serviceName);
96 serviceInfo.setEndpointUrl("localhost");
97 serviceInfo.setInstanceId("MyInstance");
98 serviceInfo.setApplicationId(nextId());
99 serviceInfo.setServerIpAddress("127.0.0.1");
100 serviceInfo.setType("type");
101 serviceInfo.setServiceVersion("1.0");
102 serviceInfo.setStatusCode("A");
103 serviceInfo.setServiceDescriptorId(nextId());
104 serviceInfo.setChecksum("abcdefg");
105 return serviceInfo;
106 }
107
108
109
110 }