1
|
package eu.dnetlib.enabling.resultset; // NOPMD
|
2
|
|
3
|
import static org.junit.Assert.assertFalse;
|
4
|
import static org.junit.Assert.assertNotNull;
|
5
|
import static org.junit.Assert.assertTrue;
|
6
|
import static org.mockito.ArgumentMatchers.any;
|
7
|
import static org.mockito.Matchers.anyInt;
|
8
|
import static org.mockito.Matchers.anyObject;
|
9
|
import static org.mockito.Matchers.anyString;
|
10
|
import static org.mockito.Matchers.eq;
|
11
|
import static org.mockito.Mockito.when;
|
12
|
|
13
|
import java.util.List;
|
14
|
|
15
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
16
|
|
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.invocation.InvocationOnMock;
|
23
|
import org.mockito.junit.MockitoJUnitRunner;
|
24
|
import org.mockito.stubbing.Answer;
|
25
|
|
26
|
import com.google.common.collect.Lists;
|
27
|
|
28
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
|
29
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
|
30
|
import eu.dnetlib.enabling.tools.ServiceResolver;
|
31
|
import eu.dnetlib.miscutils.factory.Factory;
|
32
|
import eu.dnetlib.miscutils.functional.ThreadSafeUnaryFunction;
|
33
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
34
|
|
35
|
/**
|
36
|
* test xslt resultset.
|
37
|
*
|
38
|
* @author marko
|
39
|
*
|
40
|
*/
|
41
|
@RunWith(MockitoJUnitRunner.class)
|
42
|
public class MappedResultSetTest {
|
43
|
|
44
|
/**
|
45
|
* instance under test.
|
46
|
*/
|
47
|
private transient MappedResultSet mappedResultSet;
|
48
|
|
49
|
/**
|
50
|
* service resolver.
|
51
|
*/
|
52
|
@Mock
|
53
|
private transient ServiceResolver serviceResolver;
|
54
|
|
55
|
/**
|
56
|
* resultset service mock.
|
57
|
*/
|
58
|
@Mock
|
59
|
private transient ResultSetService resultSetService;
|
60
|
|
61
|
/**
|
62
|
* resultset mock.
|
63
|
*/
|
64
|
@Mock
|
65
|
private transient ResultSet resultSet;
|
66
|
|
67
|
private Answer<Object> resultSetAnswer = new Answer<Object>() {
|
68
|
|
69
|
private List<String> list = Lists.newArrayList("1","2","3","4","5","6","7","8","9","10");
|
70
|
|
71
|
@Override
|
72
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
73
|
String method = invocation.getMethod().getName();
|
74
|
if (method.equals("getResult") || method.equals("getResults")) {
|
75
|
int from = (Integer) invocation.getArguments()[1];
|
76
|
int to = (Integer) invocation.getArguments()[2];
|
77
|
|
78
|
return list.subList(from-1, to);
|
79
|
}
|
80
|
if (method.equals("getNumberOfElements") || method.equals("getNumberOfResults")) {
|
81
|
return list.size();
|
82
|
}
|
83
|
if (method.equals("getRSStatus")) {
|
84
|
return "closed";
|
85
|
}
|
86
|
if (method.equals("isOpen")) {
|
87
|
return false;
|
88
|
}
|
89
|
System.out.println(invocation.toString());
|
90
|
return null;
|
91
|
}
|
92
|
};
|
93
|
|
94
|
/**
|
95
|
* Common setup.
|
96
|
* @throws ResultSetException
|
97
|
*
|
98
|
*/
|
99
|
@Before
|
100
|
public void setUp() throws ResultSetException {
|
101
|
|
102
|
when(serviceResolver.getService(eq(ResultSetService.class), any(W3CEndpointReference.class))).thenReturn(resultSetService);
|
103
|
when(serviceResolver.getResourceIdentifier(any(W3CEndpointReference.class))).thenReturn("123");
|
104
|
|
105
|
when(resultSetService.getNumberOfElements(anyString())).thenAnswer(resultSetAnswer);
|
106
|
when(resultSetService.getResult(anyString(), anyInt(), anyInt(), anyString())).thenAnswer(resultSetAnswer );
|
107
|
when(resultSetService.getRSStatus(anyString())).thenAnswer(resultSetAnswer);
|
108
|
|
109
|
when(resultSet.getNumberOfResults()).thenAnswer(resultSetAnswer);
|
110
|
when(resultSet.getResults(anyInt(), anyInt())).thenAnswer(resultSetAnswer );
|
111
|
when(resultSet.isOpen()).thenAnswer(resultSetAnswer);
|
112
|
}
|
113
|
|
114
|
@Test
|
115
|
@Ignore
|
116
|
public void testThreadSafeMappedResultSet_1() {
|
117
|
|
118
|
final Factory<UnaryFunction<String, String>> functionFactory = new Factory<UnaryFunction<String,String>>() {
|
119
|
@Override
|
120
|
public UnaryFunction<String, String> newInstance() {
|
121
|
return new UnaryFunction<String, String>() {
|
122
|
@Override
|
123
|
public String evaluate(String arg) {
|
124
|
return "mapped-" + arg;
|
125
|
}
|
126
|
};
|
127
|
}
|
128
|
};
|
129
|
mappedResultSet = new MappedResultSet(null, new ThreadSafeUnaryFunction<String, String>(functionFactory), serviceResolver);
|
130
|
mappedResultSet.setResultSet(resultSet);
|
131
|
|
132
|
for (String s : mappedResultSet.getResult(1, 10)) {
|
133
|
assertNotNull("null result", s);
|
134
|
assertFalse("empty result", s.isEmpty());
|
135
|
assertNotNull("null element", s);
|
136
|
assertTrue("transformed correctly", s.startsWith("mapped-"));
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
|