1
|
package eu.dnetlib.enabling.locators;
|
2
|
|
3
|
import java.util.Map;
|
4
|
import java.util.Set;
|
5
|
|
6
|
import com.google.common.collect.Lists;
|
7
|
import com.google.common.collect.Maps;
|
8
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
9
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
10
|
import eu.dnetlib.enabling.locators.comparators.PreferLocalRunningInstanceComparator;
|
11
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
|
12
|
import eu.dnetlib.enabling.tools.OpaqueResource;
|
13
|
import eu.dnetlib.enabling.tools.registration.ServiceNameResolver;
|
14
|
import eu.dnetlib.enabling.tools.registration.ServiceRegistrationManager;
|
15
|
import org.antlr.stringtemplate.StringTemplate;
|
16
|
import org.apache.commons.io.IOUtils;
|
17
|
import org.junit.Before;
|
18
|
import org.junit.Ignore;
|
19
|
import org.junit.Test;
|
20
|
import org.junit.runner.RunWith;
|
21
|
import org.mockito.Mock;
|
22
|
import org.mockito.junit.MockitoJUnitRunner;
|
23
|
import org.springframework.context.ApplicationContext;
|
24
|
|
25
|
import static org.junit.Assert.assertEquals;
|
26
|
import static org.junit.Assert.assertTrue;
|
27
|
import static org.mockito.Matchers.anyString;
|
28
|
import static org.mockito.Mockito.*;
|
29
|
|
30
|
@RunWith(MockitoJUnitRunner.class)
|
31
|
public class DefaultUniqueServiceLocatorTest {
|
32
|
|
33
|
/**
|
34
|
* Class under test.
|
35
|
*/
|
36
|
private DefaultUniqueServiceLocator locator;
|
37
|
|
38
|
@Mock
|
39
|
private ISLookUpService lookupService;
|
40
|
@Mock
|
41
|
private ResultSetService resultsetService;
|
42
|
@Mock
|
43
|
private ApplicationContext appContext;
|
44
|
@Mock
|
45
|
private ServiceNameResolver serviceNameResolver;
|
46
|
@Mock
|
47
|
private ServiceRegistrationManager resultSetRegistrator;
|
48
|
@Mock
|
49
|
private OpaqueResource opaqueResource;
|
50
|
|
51
|
private static final String SERVICE_NAME = "ResultsetService";
|
52
|
|
53
|
private static final String XQUERY = "for $x in collection('/db/DRIVER/ServiceResources/" + SERVICE_NAME + "ResourceType') return $x";
|
54
|
|
55
|
@Before
|
56
|
public void setUp() throws Exception {
|
57
|
locator = new DefaultUniqueServiceLocator();
|
58
|
locator.setApplicationContext(appContext);
|
59
|
locator.setDefaultComparator(new PreferLocalRunningInstanceComparator());
|
60
|
locator.setServiceNameResolver(serviceNameResolver);
|
61
|
locator.setIsLookupService(lookupService);
|
62
|
|
63
|
final Map<String, ServiceRegistrationManager> registratorBeans = Maps.newHashMap();
|
64
|
registratorBeans.put("resultSetRegistrator", resultSetRegistrator);
|
65
|
final Map<String, ResultSetService> serviceBeans = Maps.newHashMap();
|
66
|
serviceBeans.put("resultSetService", resultsetService);
|
67
|
|
68
|
final StringTemplate st1 = new StringTemplate(IOUtils.toString(getClass().getResourceAsStream("serviceProfile.xml.st")));
|
69
|
st1.setAttribute("name", SERVICE_NAME);
|
70
|
st1.setAttribute("id", "1111");
|
71
|
final StringTemplate st2 = new StringTemplate(IOUtils.toString(getClass().getResourceAsStream("serviceProfile.xml.st")));
|
72
|
st2.setAttribute("name", SERVICE_NAME);
|
73
|
st2.setAttribute("id", "2222");
|
74
|
final StringTemplate st3 = new StringTemplate(IOUtils.toString(getClass().getResourceAsStream("serviceProfile.xml.st")));
|
75
|
st3.setAttribute("name", SERVICE_NAME);
|
76
|
st3.setAttribute("id", "3333");
|
77
|
|
78
|
when(serviceNameResolver.getName(ResultSetService.class)).thenReturn(SERVICE_NAME);
|
79
|
when(lookupService.quickSearchProfile(XQUERY)).thenReturn(Lists.newArrayList(st1.toString(), st2.toString(), st3.toString()));
|
80
|
when(lookupService.getResourceProfileByQuery(anyString())).thenReturn(st2.toString());
|
81
|
//when(appContext.getBeansOfType(ServiceRegistrationManager.class)).thenReturn(registratorBeans);
|
82
|
//when(appContext.getBeansOfType(ResultSetService.class)).thenReturn(serviceBeans);
|
83
|
|
84
|
//when(resultSetRegistrator.getServiceProfile()).thenReturn(opaqueResource);
|
85
|
//when(resultSetRegistrator.getService()).thenReturn(resultsetService);
|
86
|
//when(opaqueResource.getResourceId()).thenReturn("2222");
|
87
|
|
88
|
}
|
89
|
|
90
|
@Test
|
91
|
public void testGetServiceClassOfT_1() {
|
92
|
assertEquals(lookupService, locator.getService(ISLookUpService.class));
|
93
|
}
|
94
|
|
95
|
@Test
|
96
|
@Ignore
|
97
|
public void testGetServiceClassOfT_2() throws ISLookUpException {
|
98
|
assertEquals(resultsetService, locator.getService(ResultSetService.class, false));
|
99
|
verify(lookupService, times(1)).quickSearchProfile(XQUERY);
|
100
|
}
|
101
|
|
102
|
@Test
|
103
|
@Ignore
|
104
|
public void testGetServiceClassOfTComparatorOfServiceRunningInstance() throws ISLookUpException {
|
105
|
assertEquals(resultsetService, locator.getService(ResultSetService.class, new PreferLocalRunningInstanceComparator()));
|
106
|
verify(lookupService, times(1)).quickSearchProfile(XQUERY);
|
107
|
}
|
108
|
|
109
|
@Test
|
110
|
@Ignore
|
111
|
public void testGetServiceClassOfTString() {
|
112
|
assertEquals(resultsetService, locator.getService(ResultSetService.class, "rs-1234"));
|
113
|
}
|
114
|
|
115
|
@Test
|
116
|
@Ignore
|
117
|
public void testGetServiceClassOfTBoolean() throws ISLookUpException {
|
118
|
assertEquals(resultsetService, locator.getService(ResultSetService.class, true));
|
119
|
verify(lookupService, never()).quickSearchProfile(XQUERY);
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testGetServiceIdClassOfT() {
|
124
|
assertEquals("1111", locator.getServiceId(ResultSetService.class));
|
125
|
}
|
126
|
|
127
|
@Test
|
128
|
public void testGetServiceIdClassOfTComparatorOfServiceRunningInstance() {
|
129
|
assertEquals("1111", locator.getServiceId(ResultSetService.class, new PreferLocalRunningInstanceComparator()));
|
130
|
}
|
131
|
|
132
|
@Test
|
133
|
public void testGetServiceIdClassOfTString() {
|
134
|
assertEquals("2222", locator.getServiceId(ResultSetService.class, "rs-1234"));
|
135
|
}
|
136
|
|
137
|
@Test
|
138
|
@Ignore
|
139
|
public void testGetAllServices() {
|
140
|
final Set<ResultSetService> list = locator.getAllServices(ResultSetService.class);
|
141
|
assertEquals(3, list.size());
|
142
|
assertTrue(list.contains(resultsetService));
|
143
|
}
|
144
|
|
145
|
@Test
|
146
|
public void testGetAllServiceIds() {
|
147
|
final Set<String> list = locator.getAllServiceIds(ResultSetService.class);
|
148
|
assertEquals(3, list.size());
|
149
|
assertTrue(list.contains("1111"));
|
150
|
assertTrue(list.contains("2222"));
|
151
|
assertTrue(list.contains("3333"));
|
152
|
}
|
153
|
|
154
|
}
|