Project

General

Profile

1
package eu.dnetlib.openaire.directindex.objects;
2

    
3
import com.google.common.collect.Lists;
4
import com.google.gson.Gson;
5
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
6
import eu.dnetlib.openaire.directindex.api.DirecIndexApiException;
7
import eu.dnetlib.openaire.directindex.api.OpenAIRESubmitterUtils;
8
import eu.dnetlib.openaire.directindex.objects.PidEntry;
9
import eu.dnetlib.openaire.directindex.objects.ResultEntry;
10
import org.apache.commons.lang3.StringEscapeUtils;
11
import org.apache.velocity.app.VelocityEngine;
12
import org.dom4j.Document;
13
import org.dom4j.io.OutputFormat;
14
import org.dom4j.io.SAXReader;
15
import org.dom4j.io.XMLWriter;
16
import org.junit.Before;
17
import org.junit.Test;
18
import org.junit.runner.RunWith;
19
import org.mockito.Mock;
20
import org.mockito.runners.MockitoJUnitRunner;
21

    
22
import java.io.InputStreamReader;
23
import java.io.StringReader;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.Properties;
27

    
28
import static org.mockito.Matchers.anyString;
29
import static org.mockito.Mockito.*;
30
import static org.mockito.Mockito.doReturn;
31

    
32
/**
33
 * Created by michele on 14/12/15.
34
 */
35
@RunWith(MockitoJUnitRunner.class)
36
public class ResultEntryToOafTest {
37

    
38
	private VelocityEngine ve;
39

    
40
	private final String community_api = "https://dev-openaire.d4science.org/openaire/community/";
41

    
42
	@Mock
43
	private ISLookUpService lookUpService;
44

    
45
	private ResultEntryToOaf toOaf;
46
	private OpenAIRESubmitterUtils utils;
47

    
48
	@Before
49
	public void setUp() throws Exception {
50

    
51
		utils = mock(OpenAIRESubmitterUtils.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
52
		doReturn(Lists.newArrayList("dh-ch", "dariah", "bar", "x")).when(utils).translateZenodoCommunity("https://zenodo.org/communities/dimpo");
53
		doReturn(Lists.newArrayList()).when(utils).translateZenodoCommunity("https://zenodo.org/communities/dumbo");
54
		doReturn(Lists.newArrayList("noLabel", "enermaps")).when(utils).translateZenodoCommunity("https://sandbox.zenodo.org/communities/oac-ni");
55
		doReturn(Lists.newArrayList("noLabel", "enermaps")).when(utils).translateZenodoCommunity("https://zenodo.org/communities/oac-ni");
56

    
57

    
58

    
59
		toOaf = new ResultEntryToOaf(community_api);
60
		toOaf.setUtils(utils);
61

    
62
		final Properties props = new Properties();
63
		props.setProperty("resource.loader", "class");
64
		props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
65
		props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
66
		ve = new VelocityEngine();
67
		ve.init(props);
68

    
69
		when(lookUpService.quickSearchProfile(anyString())).thenAnswer(invocation -> {
70
			final String query = invocation.getArguments()[0].toString();
71
			if (query.contains("dnet:result_typologies")) {
72
				return Arrays.asList("publication @@@ publication", "dataset @@@ dataset", "software @@@ software", "other @@@ other");
73
			} else if (query.contains("dnet:access_modes")) {
74
				return Arrays.asList("OPEN @@@ Open Access");
75
			} else if (query.contains("dnet:publication_resource")) {
76
				return Arrays.asList("0001 @@@ Journal Article");
77
			} else if (query.contains("dnet:pid_types")) {
78
				return Arrays.asList("oai @@@ Open Archive Initiative", "doi @@@ Digital object identifier");
79
			} else if (query.contains("dnet:languages")) {
80
				return Arrays.asList("ita @@@ Italian");
81
			} else if (query.contains("ContextDSResourceType")) {
82
				return Arrays.asList(
83
						"egi::classification::natsc::math::stats @@@ Statistics and Probability",
84
						"egi::classification::natsc::math::pure @@@ Pure Mathematics",
85
						"egi::classification::natsc::math @@@ Mathematics",
86
						"egi::classification::natsc @@@ Natural Sciences",
87
						"egi::classification @@@ EGI classification scheme",
88
						"egi @@@ EGI",
89
						"enermaps @@@ Energy Research",
90
						"enermaps::selection @@@ Selected by the H2020 EnerMaps project");
91
			} else {
92
				return new ArrayList<String>();
93
			}
94
		});
95

    
96
		when(lookUpService.getResourceProfileByQuery(anyString())).thenAnswer(invocation -> "REPO NAME @@@ repo________");
97

    
98

    
99
	}
100

    
101
	@Test
102
	public void testAsIndexRecord() throws Exception {
103
		final ResultEntry pub = new ResultEntry();
104
		pub.setOriginalId("ORIG_ID_1234");
105
		pub.setTitle("TEST TITLE <test>");
106
		pub.getAuthors().add("Michele Artini");
107
		pub.getAuthors().add("Claudio Atzori");
108
		pub.getAuthors().add("Alessia Bardi");
109
		pub.setPublisher("Test publisher");
110
		pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
111
		pub.setLanguage("ita");
112
		pub.setLicenseCode("OPEN");
113
		pub.setResourceType("0001");
114
		pub.setUrl("http://test.it/a=1&b=2");
115
		pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
116
		pub.getPids().add(new PidEntry("oai", "oai:1234"));
117
		pub.setCollectedFromId("test________::zenodo");
118
		pub.setHostedById("test________::UNKNOWN");
119
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
120
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/RCUK//244%2F909/EU/Making Capabilities Work/WorkAble");
121
		pub.getContexts().add("egi::classification::natsc::math::pure");
122
		pub.getContexts().add("egi::classification::natsc::math::stats");
123
		pub.getContexts().add("enermaps");
124
		pub.getContexts().add("enermaps::selection");
125
		final String xml = toOaf.asOafRecord(pub, ve, lookUpService, "http://oaf/oaf.xsd");
126

    
127
		final Document doc = new SAXReader().read(new StringReader(xml));
128

    
129
		final OutputFormat format = OutputFormat.createPrettyPrint();
130

    
131
		final XMLWriter writer = new XMLWriter(System.out, format);
132

    
133
		writer.write(doc);
134

    
135
		/* writer.close(); */
136

    
137
	}
138

    
139
	@Test
140
	public void testAsIndexRecordAccessRight() throws Exception {
141
		final ResultEntry pub = new ResultEntry();
142
		pub.setOriginalId("ORIG_ID_1234");
143
		pub.setTitle("TEST TITLE <test>");
144
		pub.getAuthors().add("Michele Artini");
145
		pub.getAuthors().add("Claudio Atzori");
146
		pub.getAuthors().add("Alessia Bardi");
147
		pub.setPublisher("Test publisher");
148
		pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
149
		pub.setLanguage("ita");
150
		pub.setLicenseCode("CLOSED");
151
		pub.setAccessRightCode("OPEN");
152
		pub.setResourceType("0001");
153
		pub.setUrl("http://test.it/a=1&b=2");
154
		pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
155
		pub.getPids().add(new PidEntry("oai", "oai:1234"));
156
		pub.setCollectedFromId("test________::zenodo");
157
		pub.setHostedById("test________::UNKNOWN");
158
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
159
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
160
		pub.getContexts().add("egi::classification::natsc::math::pure");
161
		pub.getContexts().add("egi::classification::natsc::math::stats");
162
		final String xml = toOaf.asOafRecord(pub,ve, lookUpService, "http://oaf/oaf.xsd");
163

    
164
		final Document doc = new SAXReader().read(new StringReader(xml));
165

    
166
		final OutputFormat format = OutputFormat.createPrettyPrint();
167

    
168
		final XMLWriter writer = new XMLWriter(System.out, format);
169

    
170
		writer.write(doc);
171

    
172
		/* writer.close(); */
173

    
174
	}
175

    
176
	@Test
177
	public void testAsORP() throws Exception {
178
		final ResultEntry pub = new ResultEntry();
179
		pub.setOriginalId("ORIG_ID_1234");
180
		pub.setTitle("TEST TITLE <test>");
181
		pub.getAuthors().add("Michele Artini");
182
		pub.getAuthors().add("Claudio Atzori");
183
		pub.getAuthors().add("Alessia Bardi");
184
		pub.setPublisher("Test publisher");
185
		pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
186
		pub.setLanguage("ita");
187
		pub.setLicenseCode("CLOSED");
188
		pub.setAccessRightCode("OPEN");
189
		pub.setResourceType("0020");
190
		pub.setType("other");
191
		pub.setUrl("http://test.it/a=1&b=2");
192
		pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
193
		pub.getPids().add(new PidEntry("oai", "oai:1234"));
194
		pub.setCollectedFromId("test________::zenodo");
195
		pub.setHostedById("test________::UNKNOWN");
196
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
197
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
198
		pub.getContexts().add("egi::classification::natsc::math::pure");
199
		pub.getContexts().add("egi::classification::natsc::math::stats");
200
		final String xml = toOaf.asOafRecord(pub,ve, lookUpService, "http://oaf/oaf.xsd");
201

    
202
		final Document doc = new SAXReader().read(new StringReader(xml));
203

    
204
		final OutputFormat format = OutputFormat.createPrettyPrint();
205

    
206
		final XMLWriter writer = new XMLWriter(System.out, format);
207

    
208
		writer.write(doc);
209

    
210
		/* writer.close(); */
211

    
212
	}
213

    
214
	@Test
215
	public void testAsIndexRecord_json() throws Exception {
216
		testAsIndexRecord_json("test_record.json");
217

    
218
	}
219

    
220
	@Test
221
	public void testAsIndexRecord_json_with_greek_chars() throws Exception {
222

    
223
		testAsIndexRecord_json("test_record_with_greek_chars.json");
224

    
225
	}
226

    
227
	@Test
228
	public void testAsIndexRecord_openaireId() throws Exception {
229

    
230
		testAsIndexRecord_json("test_record_openaireId.json");
231

    
232
	}
233

    
234
	@Test(expected = DirecIndexApiException.class)
235
	public void testAsIndexRecord_wrongOpenaireId() throws Exception {
236

    
237
		testAsIndexRecord_json("test_record_wrong_openaireId.json");
238

    
239
	}
240

    
241
	@Test
242
	public void testAsIndexRecord_json_zenodo() throws Exception {
243

    
244
		testAsIndexRecord_json("test_zenodo.json");
245

    
246
	}
247

    
248
	@Test
249
	public void testAsIndexRecord_json_zenodoWithAmpersand() throws Exception {
250

    
251
		testAsIndexRecord_json("test_zenodoAmpersandEverywhere.json");
252

    
253
	}
254

    
255
	@Test
256
	public void testAsIndexRecord_json_software() throws Exception {
257

    
258
		testAsIndexRecord_json("test_record_software.json");
259

    
260
	}
261

    
262
	@Test
263
	public void testAsIndexRecord_json_orp() throws Exception {
264

    
265
		testAsIndexRecord_json("test_record_orp.json");
266

    
267
	}
268

    
269
	private void testAsIndexRecord_json(final String filename) throws Exception {
270
		final ResultEntry pub =
271
				new Gson().fromJson(new InputStreamReader(getClass().getResourceAsStream(filename)), ResultEntry.class);
272

    
273

    
274
		final String xml = toOaf.asOafRecord(pub,ve, lookUpService, "http://oaf/oaf.xsd");
275
		//System.out.println(xml);
276

    
277
		final Document doc = new SAXReader().read(new StringReader(xml));
278

    
279
		final OutputFormat format = OutputFormat.createPrettyPrint();
280

    
281
		final XMLWriter writer = new XMLWriter(System.out, format);
282

    
283
		writer.write(doc);
284

    
285
		/* writer.close(); */
286
	}
287

    
288
	@Test
289
	public void testAsIndexRecord_json_zenodocommunities()throws Exception{
290
		testAsIndexRecord_json("test_zenodo_community.json");
291
	}
292

    
293
	@Test
294
	public void testAsIndexRecordFromSandbox_json_zenodocommunities()throws Exception{
295
		testAsIndexRecord_json("test_zenodo_community2.json");
296
	}
297

    
298
	@Test
299
	public void testEscapeUnicode() {
300
		final String unicodeTxt =
301
				"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$";
302
		System.out.println(StringEscapeUtils.escapeXml11(unicodeTxt));
303
	}
304

    
305
}
    (1-1/1)