1
|
package eu.dnetlib.msro.openaireplus.api.objects;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
5
|
import eu.dnetlib.openaire.directindex.api.DirecIndexApiException;
|
6
|
import eu.dnetlib.openaire.directindex.objects.PidEntry;
|
7
|
import eu.dnetlib.openaire.directindex.objects.ResultEntry;
|
8
|
import org.apache.commons.lang3.StringEscapeUtils;
|
9
|
import org.apache.velocity.app.VelocityEngine;
|
10
|
import org.dom4j.Document;
|
11
|
import org.dom4j.io.OutputFormat;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
import org.dom4j.io.XMLWriter;
|
14
|
import org.junit.Before;
|
15
|
import org.junit.Test;
|
16
|
import org.junit.runner.RunWith;
|
17
|
import org.mockito.Mock;
|
18
|
import org.mockito.runners.MockitoJUnitRunner;
|
19
|
|
20
|
import java.io.InputStreamReader;
|
21
|
import java.io.StringReader;
|
22
|
import java.util.ArrayList;
|
23
|
import java.util.Arrays;
|
24
|
import java.util.Properties;
|
25
|
|
26
|
import static org.mockito.Matchers.anyString;
|
27
|
import static org.mockito.Mockito.when;
|
28
|
|
29
|
/**
|
30
|
* Created by michele on 14/12/15.
|
31
|
*/
|
32
|
@RunWith(MockitoJUnitRunner.class)
|
33
|
public class ResultEntryTest {
|
34
|
|
35
|
private VelocityEngine ve;
|
36
|
|
37
|
@Mock
|
38
|
private ISLookUpService lookUpService;
|
39
|
|
40
|
@Before
|
41
|
public void setUp() throws Exception {
|
42
|
|
43
|
final Properties props = new Properties();
|
44
|
props.setProperty("resource.loader", "class");
|
45
|
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
46
|
props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
|
47
|
ve = new VelocityEngine();
|
48
|
ve.init(props);
|
49
|
|
50
|
when(lookUpService.quickSearchProfile(anyString())).thenAnswer(invocation -> {
|
51
|
final String query = invocation.getArguments()[0].toString();
|
52
|
if (query.contains("dnet:result_typologies")) {
|
53
|
return Arrays.asList("publication @@@ publication", "dataset @@@ dataset", "software @@@ software", "other @@@ other");
|
54
|
} else if (query.contains("dnet:access_modes")) {
|
55
|
return Arrays.asList("OPEN @@@ Open Access");
|
56
|
} else if (query.contains("dnet:publication_resource")) {
|
57
|
return Arrays.asList("0001 @@@ Journal Article");
|
58
|
} else if (query.contains("dnet:pid_types")) {
|
59
|
return Arrays.asList("oai @@@ Open Archive Initiative", "doi @@@ Digital object identifier");
|
60
|
} else if (query.contains("dnet:languages")) {
|
61
|
return Arrays.asList("ita @@@ Italian");
|
62
|
} else if (query.contains("ContextDSResourceType")) {
|
63
|
return Arrays.asList(
|
64
|
"egi::classification::natsc::math::stats @@@ Statistics and Probability",
|
65
|
"egi::classification::natsc::math::pure @@@ Pure Mathematics",
|
66
|
"egi::classification::natsc::math @@@ Mathematics",
|
67
|
"egi::classification::natsc @@@ Natural Sciences",
|
68
|
"egi::classification @@@ EGI classification scheme",
|
69
|
"egi @@@ EGI");
|
70
|
} else {
|
71
|
return new ArrayList<String>();
|
72
|
}
|
73
|
});
|
74
|
|
75
|
when(lookUpService.getResourceProfileByQuery(anyString())).thenAnswer(invocation -> "REPO NAME @@@ repo________");
|
76
|
}
|
77
|
|
78
|
@Test
|
79
|
public void testAsIndexRecord() throws Exception {
|
80
|
final ResultEntry pub = new ResultEntry();
|
81
|
pub.setOriginalId("ORIG_ID_1234");
|
82
|
pub.setTitle("TEST TITLE <test>");
|
83
|
pub.getAuthors().add("Michele Artini");
|
84
|
pub.getAuthors().add("Claudio Atzori");
|
85
|
pub.getAuthors().add("Alessia Bardi");
|
86
|
pub.setPublisher("Test publisher");
|
87
|
pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
|
88
|
pub.setLanguage("ita");
|
89
|
pub.setLicenseCode("OPEN");
|
90
|
pub.setResourceType("0001");
|
91
|
pub.setUrl("http://test.it/a=1&b=2");
|
92
|
pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
|
93
|
pub.getPids().add(new PidEntry("oai", "oai:1234"));
|
94
|
pub.setCollectedFromId("test________::zenodo");
|
95
|
pub.setHostedById("test________::UNKNOWN");
|
96
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
|
97
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
|
98
|
pub.getContexts().add("egi::classification::natsc::math::pure");
|
99
|
pub.getContexts().add("egi::classification::natsc::math::stats");
|
100
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
101
|
|
102
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
103
|
|
104
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
105
|
|
106
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
107
|
|
108
|
writer.write(doc);
|
109
|
|
110
|
/* writer.close(); */
|
111
|
|
112
|
}
|
113
|
|
114
|
@Test
|
115
|
public void testAsIndexRecordAccessRight() throws Exception {
|
116
|
final ResultEntry pub = new ResultEntry();
|
117
|
pub.setOriginalId("ORIG_ID_1234");
|
118
|
pub.setTitle("TEST TITLE <test>");
|
119
|
pub.getAuthors().add("Michele Artini");
|
120
|
pub.getAuthors().add("Claudio Atzori");
|
121
|
pub.getAuthors().add("Alessia Bardi");
|
122
|
pub.setPublisher("Test publisher");
|
123
|
pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
|
124
|
pub.setLanguage("ita");
|
125
|
pub.setLicenseCode("CLOSED");
|
126
|
pub.setAccessRightCode("OPEN");
|
127
|
pub.setResourceType("0001");
|
128
|
pub.setUrl("http://test.it/a=1&b=2");
|
129
|
pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
|
130
|
pub.getPids().add(new PidEntry("oai", "oai:1234"));
|
131
|
pub.setCollectedFromId("test________::zenodo");
|
132
|
pub.setHostedById("test________::UNKNOWN");
|
133
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
|
134
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
|
135
|
pub.getContexts().add("egi::classification::natsc::math::pure");
|
136
|
pub.getContexts().add("egi::classification::natsc::math::stats");
|
137
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
138
|
|
139
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
140
|
|
141
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
142
|
|
143
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
144
|
|
145
|
writer.write(doc);
|
146
|
|
147
|
/* writer.close(); */
|
148
|
|
149
|
}
|
150
|
|
151
|
@Test
|
152
|
public void testAsORP() throws Exception {
|
153
|
final ResultEntry pub = new ResultEntry();
|
154
|
pub.setOriginalId("ORIG_ID_1234");
|
155
|
pub.setTitle("TEST TITLE <test>");
|
156
|
pub.getAuthors().add("Michele Artini");
|
157
|
pub.getAuthors().add("Claudio Atzori");
|
158
|
pub.getAuthors().add("Alessia Bardi");
|
159
|
pub.setPublisher("Test publisher");
|
160
|
pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
|
161
|
pub.setLanguage("ita");
|
162
|
pub.setLicenseCode("CLOSED");
|
163
|
pub.setAccessRightCode("OPEN");
|
164
|
pub.setResourceType("0020");
|
165
|
pub.setType("other");
|
166
|
pub.setUrl("http://test.it/a=1&b=2");
|
167
|
pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
|
168
|
pub.getPids().add(new PidEntry("oai", "oai:1234"));
|
169
|
pub.setCollectedFromId("test________::zenodo");
|
170
|
pub.setHostedById("test________::UNKNOWN");
|
171
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
|
172
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
|
173
|
pub.getContexts().add("egi::classification::natsc::math::pure");
|
174
|
pub.getContexts().add("egi::classification::natsc::math::stats");
|
175
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
176
|
|
177
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
178
|
|
179
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
180
|
|
181
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
182
|
|
183
|
writer.write(doc);
|
184
|
|
185
|
/* writer.close(); */
|
186
|
|
187
|
}
|
188
|
|
189
|
@Test
|
190
|
public void testAsIndexRecord_json() throws Exception {
|
191
|
testAsIndexRecord_json("test_record.json");
|
192
|
|
193
|
}
|
194
|
|
195
|
@Test
|
196
|
public void testAsIndexRecord_json_with_greek_chars() throws Exception {
|
197
|
|
198
|
testAsIndexRecord_json("test_record_with_greek_chars.json");
|
199
|
|
200
|
}
|
201
|
|
202
|
@Test
|
203
|
public void testAsIndexRecord_openaireId() throws Exception {
|
204
|
|
205
|
testAsIndexRecord_json("test_record_openaireId.json");
|
206
|
|
207
|
}
|
208
|
|
209
|
@Test(expected = DirecIndexApiException.class)
|
210
|
public void testAsIndexRecord_wrongOpenaireId() throws Exception {
|
211
|
|
212
|
testAsIndexRecord_json("test_record_wrong_openaireId.json");
|
213
|
|
214
|
}
|
215
|
|
216
|
@Test
|
217
|
public void testAsIndexRecord_json_zenodo() throws Exception {
|
218
|
|
219
|
testAsIndexRecord_json("test_zenodo.json");
|
220
|
|
221
|
}
|
222
|
|
223
|
@Test
|
224
|
public void testAsIndexRecord_json_zenodoWithAmpersand() throws Exception {
|
225
|
|
226
|
testAsIndexRecord_json("test_zenodoAmpersandEverywhere.json");
|
227
|
|
228
|
}
|
229
|
|
230
|
@Test
|
231
|
public void testAsIndexRecord_json_software() throws Exception {
|
232
|
|
233
|
testAsIndexRecord_json("test_record_software.json");
|
234
|
|
235
|
}
|
236
|
|
237
|
@Test
|
238
|
public void testAsIndexRecord_json_orp() throws Exception {
|
239
|
|
240
|
testAsIndexRecord_json("test_record_orp.json");
|
241
|
|
242
|
}
|
243
|
|
244
|
private void testAsIndexRecord_json(final String filename) throws Exception {
|
245
|
final ResultEntry pub =
|
246
|
new Gson().fromJson(new InputStreamReader(getClass().getResourceAsStream(filename)), ResultEntry.class);
|
247
|
|
248
|
|
249
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
250
|
//System.out.println(xml);
|
251
|
|
252
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
253
|
|
254
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
255
|
|
256
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
257
|
|
258
|
writer.write(doc);
|
259
|
|
260
|
/* writer.close(); */
|
261
|
}
|
262
|
|
263
|
@Test
|
264
|
public void testAsIndexRecord_json_zenodocommunities()throws Exception{
|
265
|
testAsIndexRecord_json("test_zenodo_community.json");
|
266
|
}
|
267
|
|
268
|
@Test
|
269
|
public void testEscapeUnicode() {
|
270
|
final String unicodeTxt =
|
271
|
"i.e. closed curves of the form $t\ud835\udfc4 [0,2\u03c0] \u21a6 (\\cos t)u + (\\sin t)v$ for suitable orthogonal vectors $u$";
|
272
|
System.out.println(StringEscapeUtils.escapeXml11(unicodeTxt));
|
273
|
}
|
274
|
|
275
|
}
|