1
|
package eu.dnetlib.data.collector.plugins.datasources;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileOutputStream;
|
5
|
import java.io.IOException;
|
6
|
import java.util.Iterator;
|
7
|
|
8
|
import eu.dnetlib.data.collector.plugins.HttpConnector;
|
9
|
import org.apache.commons.io.FileUtils;
|
10
|
import org.apache.commons.io.IOUtils;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.junit.Before;
|
14
|
import org.junit.Ignore;
|
15
|
import org.junit.Test;
|
16
|
|
17
|
import static org.junit.Assert.*;
|
18
|
|
19
|
@Ignore
|
20
|
public class Re3DataRepositoriesIteratorTest {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(Re3DataRepositoriesIteratorTest.class);
|
23
|
|
24
|
private static final String TMP_DIR = "/tmp/re3data/";
|
25
|
int countRepos = 0;
|
26
|
int expectedRepos = 2189;
|
27
|
private Re3DataRepositoriesIterator re3dataIterator;
|
28
|
private String baseURL = "https://www.re3data.org";
|
29
|
private String repoListURL = baseURL + "/api/v1/repositories";
|
30
|
|
31
|
private HttpConnector httpConnector;
|
32
|
|
33
|
@Before
|
34
|
public void setUp() throws Exception {
|
35
|
httpConnector = new HttpConnector();
|
36
|
String input = httpConnector.getInputSource(repoListURL);
|
37
|
re3dataIterator = new Re3DataRepositoriesIterator(IOUtils.toInputStream(input, "UTF-8"), baseURL, httpConnector);
|
38
|
|
39
|
File tmpDir = new File(TMP_DIR);
|
40
|
if (tmpDir.exists()) {
|
41
|
log.info("deleting directory: " + tmpDir.getAbsolutePath());
|
42
|
FileUtils.forceDelete(tmpDir);
|
43
|
}
|
44
|
log.info("creating directory: " + tmpDir.getAbsolutePath());
|
45
|
FileUtils.forceMkdir(tmpDir);
|
46
|
}
|
47
|
|
48
|
@Test
|
49
|
public void testHasNext() {
|
50
|
assertTrue(re3dataIterator.hasNext());
|
51
|
}
|
52
|
|
53
|
@Test
|
54
|
public void testNext() {
|
55
|
String repo = null;
|
56
|
if (re3dataIterator.hasNext()) {
|
57
|
repo = re3dataIterator.next();
|
58
|
}
|
59
|
assertNotNull(repo);
|
60
|
}
|
61
|
|
62
|
@Test(expected = UnsupportedOperationException.class)
|
63
|
public void testRemove() {
|
64
|
re3dataIterator.remove();
|
65
|
}
|
66
|
|
67
|
@Test
|
68
|
public void testIterator() throws IOException {
|
69
|
|
70
|
for (String repo : re3dataIterator) {
|
71
|
|
72
|
countRepos++;
|
73
|
assertNotNull(repo);
|
74
|
FileOutputStream st = new FileOutputStream(TMP_DIR + countRepos + ".xml");
|
75
|
IOUtils.write(repo, st);
|
76
|
IOUtils.closeQuietly(st);
|
77
|
|
78
|
|
79
|
}
|
80
|
assertEquals(expectedRepos, countRepos);
|
81
|
}
|
82
|
|
83
|
@Test
|
84
|
public void testBadIterator() throws IOException {
|
85
|
|
86
|
final Iterator<String> iter = re3dataIterator.iterator();
|
87
|
|
88
|
while (iter.hasNext()) {
|
89
|
|
90
|
iter.hasNext();
|
91
|
iter.next();
|
92
|
|
93
|
countRepos++;
|
94
|
}
|
95
|
assertEquals(expectedRepos, countRepos);
|
96
|
}
|
97
|
|
98
|
|
99
|
}
|