1
|
package eu.dnetlib.wds.parser;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import com.google.gson.GsonBuilder;
|
5
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
6
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
7
|
import eu.dnetlib.springutils.stringtemplate.StringTemplateFactory;
|
8
|
import eu.dnetlib.wds.resolver.OpenAireParser;
|
9
|
import eu.dnetlib.wds.resolver.WDSResolvedObject;
|
10
|
import eu.dnetlib.wds.resolver.serializers.WDSResolverSerializer;
|
11
|
import eu.dnetlib.wds.utils.WDSUtils;
|
12
|
import org.antlr.stringtemplate.StringTemplate;
|
13
|
import org.apache.commons.io.IOUtils;
|
14
|
|
15
|
import static org.junit.Assert.*;
|
16
|
import static org.mockito.Mockito.mock;
|
17
|
import static org.mockito.Mockito.when;
|
18
|
|
19
|
import org.junit.Assert;
|
20
|
import org.junit.Before;
|
21
|
import org.junit.Test;
|
22
|
import org.junit.runner.RunWith;
|
23
|
import org.mockito.Mock;
|
24
|
import org.mockito.runners.MockitoJUnitRunner;
|
25
|
|
26
|
import java.io.IOException;
|
27
|
import java.io.InputStream;
|
28
|
import java.util.Arrays;
|
29
|
import java.util.List;
|
30
|
import java.util.regex.Matcher;
|
31
|
import java.util.regex.Pattern;
|
32
|
|
33
|
|
34
|
@RunWith(MockitoJUnitRunner.class)
|
35
|
public class ParserTest {
|
36
|
|
37
|
|
38
|
@Mock
|
39
|
UniqueServiceLocator serviceLocator;
|
40
|
|
41
|
|
42
|
@Mock
|
43
|
ISLookUpService isLookUpService;
|
44
|
|
45
|
|
46
|
@Mock
|
47
|
WDSUtils wdsUtils;
|
48
|
|
49
|
|
50
|
@Before
|
51
|
public void setup() throws Exception {
|
52
|
|
53
|
serviceLocator = mock(UniqueServiceLocator.class);
|
54
|
isLookUpService = mock(ISLookUpService.class);
|
55
|
wdsUtils = mock(WDSUtils.class);
|
56
|
when(serviceLocator.getService(ISLookUpService.class)).thenReturn(isLookUpService);
|
57
|
|
58
|
|
59
|
WDSUtils.setInstance(wdsUtils);
|
60
|
|
61
|
when(wdsUtils.getServiceLocator()).thenReturn(serviceLocator);
|
62
|
|
63
|
|
64
|
when(isLookUpService.quickSearchProfile(WDSUtils.COLLECTEDFROM_DATASOURCE_MAP_QUERY)).thenReturn(
|
65
|
Arrays.asList(
|
66
|
"wds_________::r3d100010134<-->r3d100010134<-->Pangaea",
|
67
|
"wds_________::r3d100010255<-->r3d100010255<-->Inter-university Consortium for Political and Social Research",
|
68
|
"wds_________::r3d100010214<-->r3d100010214<-->World Data Center for Climate",
|
69
|
"wds_________::earthdata<-->cmr_________<-->EarthData",
|
70
|
"wds_________::r3d100000037<-->r3d100000037<-->Oak Ridge National Laboratory Distributed Active Archive Center for Biogeochemical Dynamics",
|
71
|
"wds_________::r3d100010299<-->r3d100010299<-->World Data Center for Climate",
|
72
|
"openaire____::bootstrap<-->openaire____<-->OpenAIRE Bootstrap"));
|
73
|
}
|
74
|
|
75
|
|
76
|
@Test
|
77
|
public void testRegEx() {
|
78
|
|
79
|
final Pattern pattern = Pattern.compile("10\\.\\d{4,9}/[-._;()/:A-Z0-9]+$", Pattern.CASE_INSENSITIVE);
|
80
|
final List<String> input = Arrays.asList("https://doi.org/10.1594/PANGAEA.821878", "http://hs.pangaea.de/Images/Airphoto/PS", "https://doi.org/10.1051/epjcONnf/201009006");
|
81
|
|
82
|
assertEquals(
|
83
|
input.stream()
|
84
|
.filter(it ->
|
85
|
{
|
86
|
Matcher matcher = pattern.matcher(it);
|
87
|
return matcher.find();
|
88
|
}).count(),
|
89
|
2);
|
90
|
|
91
|
|
92
|
}
|
93
|
|
94
|
|
95
|
@Test
|
96
|
public void DMFParserTest() throws Exception {
|
97
|
final InputStream inputStream = this.getClass().getResourceAsStream("/eu/dnetlib/wds/parser/DMFinput.xml");
|
98
|
final String dmf = IOUtils.toString(inputStream);
|
99
|
|
100
|
|
101
|
DMFResolverParser parser = new DMFResolverParser();
|
102
|
|
103
|
WDSResolvedObject resolvedObject = (WDSResolvedObject) parser.parseObject(dmf);
|
104
|
|
105
|
Assert.assertNotNull(resolvedObject.getType());
|
106
|
|
107
|
Gson g = new GsonBuilder().setPrettyPrinting().create();
|
108
|
|
109
|
assertNotNull(resolvedObject);
|
110
|
|
111
|
System.out.println("resolvedObject = " + g.toJson(resolvedObject));
|
112
|
|
113
|
WDSResolverSerializer serializer = new WDSResolverSerializer();
|
114
|
|
115
|
final InputStream aStream = this.getClass().getResourceAsStream("/eu/dnetlib/wds/templates/DMFXML.st");
|
116
|
|
117
|
|
118
|
serializer.setDmfTemplate(new StringTemplate(IOUtils.toString(aStream)));
|
119
|
|
120
|
|
121
|
String xml = serializer.serializeToXML(resolvedObject);
|
122
|
|
123
|
|
124
|
System.out.println("xml = " + xml);
|
125
|
|
126
|
|
127
|
}
|
128
|
|
129
|
@Test
|
130
|
public void testOpenAireParser() throws IOException {
|
131
|
OpenAireParser parser = new OpenAireParser();
|
132
|
final InputStream inputStream = this.getClass().getResourceAsStream("/eu/dnetlib/wds/parser/openaire_input.xml");
|
133
|
System.out.println(parser.parseObject(IOUtils.toString(inputStream)));
|
134
|
}
|
135
|
|
136
|
}
|