1
|
package eu.dnetlib.enabling.resultset;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.mockito.Matchers.anyInt;
|
5
|
import static org.mockito.Matchers.anyString;
|
6
|
import static org.mockito.Mockito.times;
|
7
|
import static org.mockito.Mockito.verify;
|
8
|
import static org.mockito.Mockito.when;
|
9
|
|
10
|
import java.util.List;
|
11
|
|
12
|
import org.junit.Before;
|
13
|
import org.junit.Test;
|
14
|
import org.junit.runner.RunWith;
|
15
|
import org.mockito.Mock;
|
16
|
import org.mockito.invocation.InvocationOnMock;
|
17
|
import org.mockito.junit.MockitoJUnitRunner;
|
18
|
|
19
|
import org.mockito.stubbing.Answer;
|
20
|
|
21
|
import com.google.common.collect.Lists;
|
22
|
|
23
|
import eu.dnetlib.enabling.resultset.client.ResultSetPageProvider;
|
24
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
|
25
|
|
26
|
@RunWith(MockitoJUnitRunner.class)
|
27
|
public class ResultSetPageProviderTest {
|
28
|
|
29
|
private static final String RSID = "RS_123";
|
30
|
private static final int PAGE_SIZE = 10;
|
31
|
private static final String RSSTATUS = "closed";
|
32
|
|
33
|
// Class Under test
|
34
|
private ResultSetPageProvider pageProvider;
|
35
|
|
36
|
@Mock
|
37
|
private ResultSetService mockResultSet;
|
38
|
|
39
|
private class ResultAnswer implements Answer<List<String>> {
|
40
|
private int size;
|
41
|
|
42
|
public ResultAnswer(int size) {
|
43
|
this.size = size;
|
44
|
}
|
45
|
|
46
|
@Override
|
47
|
public List<String> answer(InvocationOnMock invocation) throws Throwable {
|
48
|
Object[] args = invocation.getArguments();
|
49
|
int from = Integer.parseInt(args[1].toString());
|
50
|
int to = Integer.parseInt(args[2].toString());
|
51
|
|
52
|
|
53
|
if (to > size) to = size;
|
54
|
if (from > to) throw new Exception("FROM IS GREATER THAN TO");
|
55
|
|
56
|
List<String> list = Lists.newArrayList();
|
57
|
for (int i = from; i<=to; i++) {
|
58
|
list.add("RECORD " + i);
|
59
|
}
|
60
|
return list;
|
61
|
}
|
62
|
}
|
63
|
|
64
|
@Before
|
65
|
public void setUp() throws Exception {
|
66
|
pageProvider = new ResultSetPageProvider(mockResultSet, RSID);
|
67
|
pageProvider.setMaxWaitTime(60000);
|
68
|
pageProvider.setPageSize(PAGE_SIZE);
|
69
|
}
|
70
|
|
71
|
@Test
|
72
|
public void testNextPage5() throws Exception {
|
73
|
performTest(5);
|
74
|
}
|
75
|
|
76
|
@Test
|
77
|
public void testNextPage10() throws Exception {
|
78
|
performTest(10);
|
79
|
}
|
80
|
|
81
|
@Test
|
82
|
public void testNextPage15() throws Exception {
|
83
|
performTest(15);
|
84
|
}
|
85
|
|
86
|
@Test
|
87
|
public void testNextPage20() throws Exception {
|
88
|
performTest(20);
|
89
|
}
|
90
|
|
91
|
@Test
|
92
|
public void testNextPage250() throws Exception {
|
93
|
performTest(250);
|
94
|
}
|
95
|
|
96
|
@Test
|
97
|
public void testNextPage254() throws Exception {
|
98
|
performTest(254);
|
99
|
}
|
100
|
|
101
|
public void performTest(int size) throws Exception {
|
102
|
when(mockResultSet.getRSStatus(RSID)).thenReturn(RSSTATUS);
|
103
|
when(mockResultSet.getNumberOfElements(RSID)).thenReturn(size);
|
104
|
when(mockResultSet.getResult(anyString(), anyInt(), anyInt(), anyString())).thenAnswer(new ResultAnswer(size));
|
105
|
|
106
|
List<String> list = Lists.newArrayList();
|
107
|
|
108
|
while (true) {
|
109
|
List<String> res = pageProvider.nextPage();
|
110
|
if (res == null || res.isEmpty()) break;
|
111
|
list.addAll(res);
|
112
|
}
|
113
|
|
114
|
assertEquals(list.size(), size);
|
115
|
verify(mockResultSet, times(((size - 1) / PAGE_SIZE) + 1)).getResult(anyString(), anyInt(), anyInt(), anyString());
|
116
|
}
|
117
|
|
118
|
}
|