Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.api.objects;
2

    
3
import java.io.InputStreamReader;
4
import java.io.StringReader;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.Properties;
8

    
9
import com.google.gson.Gson;
10
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
11
import eu.dnetlib.openaire.directindex.api.DirecIndexApiException;
12
import eu.dnetlib.openaire.directindex.objects.PidEntry;
13
import eu.dnetlib.openaire.directindex.objects.ResultEntry;
14
import org.apache.commons.lang3.StringEscapeUtils;
15
import org.apache.velocity.app.VelocityEngine;
16
import org.dom4j.Document;
17
import org.dom4j.io.OutputFormat;
18
import org.dom4j.io.SAXReader;
19
import org.dom4j.io.XMLWriter;
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 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

    
190
	@Test
191
	public void testAsIndexRecord_json() throws Exception {
192
		testAsIndexRecord_json("test_record.json");
193

    
194
	}
195

    
196
	@Test
197
	public void testAsIndexRecord_json_with_greek_chars() throws Exception {
198

    
199
		testAsIndexRecord_json("test_record_with_greek_chars.json");
200

    
201
	}
202

    
203
	@Test
204
	public void testAsIndexRecord_openaireId() throws Exception {
205

    
206
		testAsIndexRecord_json("test_record_openaireId.json");
207

    
208
	}
209

    
210
	@Test(expected = DirecIndexApiException.class)
211
	public void testAsIndexRecord_wrongOpenaireId() throws Exception {
212

    
213
		testAsIndexRecord_json("test_record_wrong_openaireId.json");
214

    
215
	}
216

    
217
	@Test
218
	public void testAsIndexRecord_json_zenodo() throws Exception {
219

    
220
		testAsIndexRecord_json("test_zenodo.json");
221

    
222
	}
223

    
224
	@Test
225
	public void testAsIndexRecord_json_zenodoWithAmpersand() throws Exception {
226

    
227
		testAsIndexRecord_json("test_zenodoAmpersandEverywhere.json");
228

    
229
	}
230

    
231
	@Test
232
	public void testAsIndexRecord_json_software() throws Exception {
233

    
234
		testAsIndexRecord_json("test_record_software.json");
235

    
236
	}
237

    
238
	@Test
239
	public void testAsIndexRecord_json_orp() throws Exception {
240

    
241
		testAsIndexRecord_json("test_record_orp.json");
242

    
243
	}
244

    
245
	private void testAsIndexRecord_json(final String filename) throws Exception {
246
		final ResultEntry pub =
247
				new Gson().fromJson(new InputStreamReader(getClass().getResourceAsStream(filename)), ResultEntry.class);
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_zenodoProblems()throws Exception{
265
		testAsIndexRecord_json("test_zenodo_problems.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

    
276
	@Test
277
	public void testAsIndexRecord_json_zenodocommunities()throws Exception{
278
		testAsIndexRecord_json("test_zenodo_community.json");
279
	}
280

    
281
}
    (1-1/1)