Revision 29073
Added by Michele Artini over 10 years ago
modules/dnet-index-client/trunk/src/test/java/eu/dnetlib/enabling/resultset/ResultSetPageProviderTest.java | ||
---|---|---|
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.runners.MockitoJUnit44Runner; |
|
18 |
import org.mockito.stubbing.Answer; |
|
19 |
|
|
20 |
import com.google.common.collect.Lists; |
|
21 |
|
|
22 |
import eu.dnetlib.enabling.resultset.client.ResultSetPageProvider; |
|
23 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetService; |
|
24 |
|
|
25 |
@RunWith(MockitoJUnit44Runner.class) |
|
26 |
public class ResultSetPageProviderTest { |
|
27 |
|
|
28 |
private static final String RSID = "RS_123"; |
|
29 |
private static final int PAGE_SIZE = 10; |
|
30 |
private static final String RSSTATUS = "closed"; |
|
31 |
|
|
32 |
// Class Under test |
|
33 |
private ResultSetPageProvider pageProvider; |
|
34 |
|
|
35 |
@Mock |
|
36 |
private ResultSetService mockResultSet; |
|
37 |
|
|
38 |
private class ResultAnswer implements Answer<List<String>> { |
|
39 |
private int size; |
|
40 |
|
|
41 |
public ResultAnswer(int size) { |
|
42 |
this.size = size; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public List<String> answer(InvocationOnMock invocation) throws Throwable { |
|
47 |
Object[] args = invocation.getArguments(); |
|
48 |
int from = Integer.parseInt(args[1].toString()); |
|
49 |
int to = Integer.parseInt(args[2].toString()); |
|
50 |
|
|
51 |
|
|
52 |
if (to > size) to = size; |
|
53 |
if (from > to) throw new Exception("FROM IS GREATER THAN TO"); |
|
54 |
|
|
55 |
List<String> list = Lists.newArrayList(); |
|
56 |
for (int i = from; i<=to; i++) { |
|
57 |
list.add("RECORD " + i); |
|
58 |
} |
|
59 |
return list; |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
@Before |
|
64 |
public void setUp() throws Exception { |
|
65 |
pageProvider = new ResultSetPageProvider(mockResultSet, RSID); |
|
66 |
pageProvider.setMaxWaitTime(60000); |
|
67 |
pageProvider.setPageSize(PAGE_SIZE); |
|
68 |
} |
|
69 |
|
|
70 |
@Test |
|
71 |
public void testNextPage5() throws Exception { |
|
72 |
performTest(5); |
|
73 |
} |
|
74 |
|
|
75 |
@Test |
|
76 |
public void testNextPage10() throws Exception { |
|
77 |
performTest(10); |
|
78 |
} |
|
79 |
|
|
80 |
@Test |
|
81 |
public void testNextPage15() throws Exception { |
|
82 |
performTest(15); |
|
83 |
} |
|
84 |
|
|
85 |
@Test |
|
86 |
public void testNextPage20() throws Exception { |
|
87 |
performTest(20); |
|
88 |
} |
|
89 |
|
|
90 |
@Test |
|
91 |
public void testNextPage250() throws Exception { |
|
92 |
performTest(250); |
|
93 |
} |
|
94 |
|
|
95 |
@Test |
|
96 |
public void testNextPage254() throws Exception { |
|
97 |
performTest(254); |
|
98 |
} |
|
99 |
|
|
100 |
public void performTest(int size) throws Exception { |
|
101 |
when(mockResultSet.getRSStatus(RSID)).thenReturn(RSSTATUS); |
|
102 |
when(mockResultSet.getNumberOfElements(RSID)).thenReturn(size); |
|
103 |
when(mockResultSet.getResult(anyString(), anyInt(), anyInt(), anyString())).thenAnswer(new ResultAnswer(size)); |
|
104 |
|
|
105 |
List<String> list = Lists.newArrayList(); |
|
106 |
|
|
107 |
while (true) { |
|
108 |
List<String> res = pageProvider.nextPage(); |
|
109 |
if (res == null || res.isEmpty()) break; |
|
110 |
list.addAll(res); |
|
111 |
} |
|
112 |
|
|
113 |
assertEquals(list.size(), size); |
|
114 |
verify(mockResultSet, times(((size - 1) / PAGE_SIZE) + 1)).getResult(anyString(), anyInt(), anyInt(), anyString()); |
|
115 |
} |
|
116 |
|
|
117 |
} |
modules/dnet-index-client/trunk/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/cnr-resultset-client/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "cnr-resultset-client"} |
|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/dnet-index-client/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "dnet-index-client"} |
modules/dnet-index-client/trunk/pom.xml | ||
---|---|---|
3 | 3 |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
4 | 4 |
<parent> |
5 | 5 |
<groupId>eu.dnetlib</groupId> |
6 |
<artifactId>dnet-hadoop-parent</artifactId>
|
|
6 |
<artifactId>dnet-parent</artifactId> |
|
7 | 7 |
<version>1.0.0-SNAPSHOT</version> |
8 |
<relativePath></relativePath> |
|
8 | 9 |
</parent> |
9 | 10 |
<modelVersion>4.0.0</modelVersion> |
10 | 11 |
<groupId>eu.dnetlib</groupId> |
11 |
<artifactId>cnr-resultset-client</artifactId>
|
|
12 |
<artifactId>dnet-index-client</artifactId>
|
|
12 | 13 |
<packaging>jar</packaging> |
13 | 14 |
<version>1.0.0-SNAPSHOT</version> |
14 | 15 |
<dependencies> |
... | ... | |
19 | 20 |
<scope>test</scope> |
20 | 21 |
</dependency> |
21 | 22 |
<dependency> |
22 |
<groupId>eu.dnetlib</groupId> |
|
23 |
<artifactId>cnr-resultset-service</artifactId> |
|
24 |
<version>[1.0.0-SNAPSHOT]</version> |
|
25 |
</dependency> |
|
26 |
<dependency> |
|
27 | 23 |
<groupId>org.mockito</groupId> |
28 | 24 |
<artifactId>mockito-core</artifactId> |
29 | 25 |
<version>1.6</version> |
Also available in: Unified diff
cleaning