Project

General

Profile

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
	private final String community_api = "https://dev-openaire.d4science.org/openaire/community/";
38

    
39
	@Mock
40
	private ISLookUpService lookUpService;
41

    
42
	@Before
43
	public void setUp() throws Exception {
44

    
45
		final Properties props = new Properties();
46
		props.setProperty("resource.loader", "class");
47
		props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
48
		props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
49
		ve = new VelocityEngine();
50
		ve.init(props);
51

    
52
		when(lookUpService.quickSearchProfile(anyString())).thenAnswer(invocation -> {
53
			final String query = invocation.getArguments()[0].toString();
54
			if (query.contains("dnet:result_typologies")) {
55
				return Arrays.asList("publication @@@ publication", "dataset @@@ dataset", "software @@@ software", "other @@@ other");
56
			} else if (query.contains("dnet:access_modes")) {
57
				return Arrays.asList("OPEN @@@ Open Access");
58
			} else if (query.contains("dnet:publication_resource")) {
59
				return Arrays.asList("0001 @@@ Journal Article");
60
			} else if (query.contains("dnet:pid_types")) {
61
				return Arrays.asList("oai @@@ Open Archive Initiative", "doi @@@ Digital object identifier");
62
			} else if (query.contains("dnet:languages")) {
63
				return Arrays.asList("ita @@@ Italian");
64
			} else if (query.contains("ContextDSResourceType")) {
65
				return Arrays.asList(
66
						"egi::classification::natsc::math::stats @@@ Statistics and Probability",
67
						"egi::classification::natsc::math::pure @@@ Pure Mathematics",
68
						"egi::classification::natsc::math @@@ Mathematics",
69
						"egi::classification::natsc @@@ Natural Sciences",
70
						"egi::classification @@@ EGI classification scheme",
71
						"egi @@@ EGI");
72
			} else {
73
				return new ArrayList<String>();
74
			}
75
		});
76

    
77
		when(lookUpService.getResourceProfileByQuery(anyString())).thenAnswer(invocation -> "REPO NAME @@@ repo________");
78
	}
79

    
80
	@Test
81
	public void testAsIndexRecord() throws Exception {
82
		final ResultEntry pub = new ResultEntry();
83
		pub.setOriginalId("ORIG_ID_1234");
84
		pub.setTitle("TEST TITLE <test>");
85
		pub.getAuthors().add("Michele Artini");
86
		pub.getAuthors().add("Claudio Atzori");
87
		pub.getAuthors().add("Alessia Bardi");
88
		pub.setPublisher("Test publisher");
89
		pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
90
		pub.setLanguage("ita");
91
		pub.setLicenseCode("OPEN");
92
		pub.setResourceType("0001");
93
		pub.setUrl("http://test.it/a=1&b=2");
94
		pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
95
		pub.getPids().add(new PidEntry("oai", "oai:1234"));
96
		pub.setCollectedFromId("test________::zenodo");
97
		pub.setHostedById("test________::UNKNOWN");
98
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
99
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/RCUK//244%2F909/EU/Making Capabilities Work/WorkAble");
100
		pub.getContexts().add("egi::classification::natsc::math::pure");
101
		pub.getContexts().add("egi::classification::natsc::math::stats");
102
		final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd",community_api);
103

    
104
		final Document doc = new SAXReader().read(new StringReader(xml));
105

    
106
		final OutputFormat format = OutputFormat.createPrettyPrint();
107

    
108
		final XMLWriter writer = new XMLWriter(System.out, format);
109

    
110
		writer.write(doc);
111

    
112
		/* writer.close(); */
113

    
114
	}
115

    
116
	@Test
117
	public void testAsIndexRecordAccessRight() throws Exception {
118
		final ResultEntry pub = new ResultEntry();
119
		pub.setOriginalId("ORIG_ID_1234");
120
		pub.setTitle("TEST TITLE <test>");
121
		pub.getAuthors().add("Michele Artini");
122
		pub.getAuthors().add("Claudio Atzori");
123
		pub.getAuthors().add("Alessia Bardi");
124
		pub.setPublisher("Test publisher");
125
		pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
126
		pub.setLanguage("ita");
127
		pub.setLicenseCode("CLOSED");
128
		pub.setAccessRightCode("OPEN");
129
		pub.setResourceType("0001");
130
		pub.setUrl("http://test.it/a=1&b=2");
131
		pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
132
		pub.getPids().add(new PidEntry("oai", "oai:1234"));
133
		pub.setCollectedFromId("test________::zenodo");
134
		pub.setHostedById("test________::UNKNOWN");
135
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
136
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
137
		pub.getContexts().add("egi::classification::natsc::math::pure");
138
		pub.getContexts().add("egi::classification::natsc::math::stats");
139
		final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd",community_api);
140

    
141
		final Document doc = new SAXReader().read(new StringReader(xml));
142

    
143
		final OutputFormat format = OutputFormat.createPrettyPrint();
144

    
145
		final XMLWriter writer = new XMLWriter(System.out, format);
146

    
147
		writer.write(doc);
148

    
149
		/* writer.close(); */
150

    
151
	}
152

    
153
	@Test
154
	public void testAsORP() throws Exception {
155
		final ResultEntry pub = new ResultEntry();
156
		pub.setOriginalId("ORIG_ID_1234");
157
		pub.setTitle("TEST TITLE <test>");
158
		pub.getAuthors().add("Michele Artini");
159
		pub.getAuthors().add("Claudio Atzori");
160
		pub.getAuthors().add("Alessia Bardi");
161
		pub.setPublisher("Test publisher");
162
		pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
163
		pub.setLanguage("ita");
164
		pub.setLicenseCode("CLOSED");
165
		pub.setAccessRightCode("OPEN");
166
		pub.setResourceType("0020");
167
		pub.setType("other");
168
		pub.setUrl("http://test.it/a=1&b=2");
169
		pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
170
		pub.getPids().add(new PidEntry("oai", "oai:1234"));
171
		pub.setCollectedFromId("test________::zenodo");
172
		pub.setHostedById("test________::UNKNOWN");
173
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
174
		pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
175
		pub.getContexts().add("egi::classification::natsc::math::pure");
176
		pub.getContexts().add("egi::classification::natsc::math::stats");
177
		final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd",community_api);
178

    
179
		final Document doc = new SAXReader().read(new StringReader(xml));
180

    
181
		final OutputFormat format = OutputFormat.createPrettyPrint();
182

    
183
		final XMLWriter writer = new XMLWriter(System.out, format);
184

    
185
		writer.write(doc);
186

    
187
		/* writer.close(); */
188

    
189
	}
190

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

    
195
	}
196

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

    
200
		testAsIndexRecord_json("test_record_with_greek_chars.json");
201

    
202
	}
203

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

    
207
		testAsIndexRecord_json("test_record_openaireId.json");
208

    
209
	}
210

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

    
214
		testAsIndexRecord_json("test_record_wrong_openaireId.json");
215

    
216
	}
217

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

    
221
		testAsIndexRecord_json("test_zenodo.json");
222

    
223
	}
224

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

    
228
		testAsIndexRecord_json("test_zenodoAmpersandEverywhere.json");
229

    
230
	}
231

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

    
235
		testAsIndexRecord_json("test_record_software.json");
236

    
237
	}
238

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

    
242
		testAsIndexRecord_json("test_record_orp.json");
243

    
244
	}
245

    
246
	private void testAsIndexRecord_json(final String filename) throws Exception {
247
		final ResultEntry pub =
248
				new Gson().fromJson(new InputStreamReader(getClass().getResourceAsStream(filename)), ResultEntry.class);
249

    
250

    
251
		final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd",community_api);
252
		//System.out.println(xml);
253

    
254
		final Document doc = new SAXReader().read(new StringReader(xml));
255

    
256
		final OutputFormat format = OutputFormat.createPrettyPrint();
257

    
258
		final XMLWriter writer = new XMLWriter(System.out, format);
259

    
260
		writer.write(doc);
261

    
262
		/* writer.close(); */
263
	}
264

    
265
	@Test
266
	public void testAsIndexRecord_json_zenodocommunities()throws Exception{
267
		testAsIndexRecord_json("test_zenodo_community.json");
268
	}
269

    
270
	@Test
271
	public void testAsIndexRecordFromSandbox_json_zenodocommunities()throws Exception{
272
		testAsIndexRecord_json("test_zenodo_community2.json");
273
	}
274

    
275
	@Test
276
	public void testEscapeUnicode() {
277
		final String unicodeTxt =
278
				"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$";
279
		System.out.println(StringEscapeUtils.escapeXml11(unicodeTxt));
280
	}
281

    
282
}
    (1-1/1)