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.net.URL;
|
7
|
import java.util.Iterator;
|
8
|
|
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 = 1772;
|
27
|
private Re3DataRepositoriesIterator re3dataIterator;
|
28
|
private String baseURL = "https://www.re3data.org";
|
29
|
private String repoListURL = baseURL + "/api/v1/repositories";
|
30
|
|
31
|
@Before
|
32
|
public void setUp() throws Exception {
|
33
|
String input = IOUtils.toString(new URL(repoListURL));
|
34
|
re3dataIterator = new Re3DataRepositoriesIterator(IOUtils.toInputStream(input, "UTF-8"), baseURL);
|
35
|
|
36
|
File tmpDir = new File(TMP_DIR);
|
37
|
if (tmpDir.exists()) {
|
38
|
log.info("deleting directory: " + tmpDir.getAbsolutePath());
|
39
|
FileUtils.forceDelete(tmpDir);
|
40
|
}
|
41
|
log.info("creating directory: " + tmpDir.getAbsolutePath());
|
42
|
FileUtils.forceMkdir(tmpDir);
|
43
|
}
|
44
|
|
45
|
@Test
|
46
|
public void testHasNext() {
|
47
|
assertTrue(re3dataIterator.hasNext());
|
48
|
}
|
49
|
|
50
|
@Test
|
51
|
public void testNext() {
|
52
|
String repo = null;
|
53
|
if (re3dataIterator.hasNext()) {
|
54
|
repo = re3dataIterator.next();
|
55
|
}
|
56
|
assertNotNull(repo);
|
57
|
}
|
58
|
|
59
|
@Test(expected = UnsupportedOperationException.class)
|
60
|
public void testRemove() {
|
61
|
re3dataIterator.remove();
|
62
|
}
|
63
|
|
64
|
@Test
|
65
|
public void testIterator() throws IOException {
|
66
|
|
67
|
for (String repo : re3dataIterator) {
|
68
|
countRepos++;
|
69
|
assertNotNull(repo);
|
70
|
FileOutputStream st = new FileOutputStream(TMP_DIR + countRepos + ".xml");
|
71
|
IOUtils.write(repo, st);
|
72
|
IOUtils.closeQuietly(st);
|
73
|
}
|
74
|
assertEquals(expectedRepos, countRepos);
|
75
|
}
|
76
|
|
77
|
@Test
|
78
|
public void testBadIterator() throws IOException {
|
79
|
|
80
|
final Iterator<String> iter = re3dataIterator.iterator();
|
81
|
|
82
|
while (iter.hasNext()) {
|
83
|
|
84
|
iter.hasNext();
|
85
|
iter.next();
|
86
|
|
87
|
countRepos++;
|
88
|
}
|
89
|
assertEquals(expectedRepos, countRepos);
|
90
|
}
|
91
|
|
92
|
}
|