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 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.invocation.InvocationOnMock;
|
21
|
import org.mockito.runners.MockitoJUnitRunner;
|
22
|
import org.mockito.stubbing.Answer;
|
23
|
|
24
|
import static org.mockito.Matchers.anyString;
|
25
|
import static org.mockito.Mockito.when;
|
26
|
|
27
|
/**
|
28
|
* Created by michele on 14/12/15.
|
29
|
*/
|
30
|
@RunWith(MockitoJUnitRunner.class)
|
31
|
public class ResultEntryTest {
|
32
|
|
33
|
private VelocityEngine ve;
|
34
|
|
35
|
@Mock
|
36
|
private ISLookUpService lookUpService;
|
37
|
|
38
|
@Before
|
39
|
public void setUp() throws Exception {
|
40
|
|
41
|
final Properties props = new Properties();
|
42
|
props.setProperty("resource.loader", "class");
|
43
|
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
44
|
props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
|
45
|
ve = new VelocityEngine();
|
46
|
ve.init(props);
|
47
|
|
48
|
when(lookUpService.quickSearchProfile(anyString())).thenAnswer(new Answer<Object>() {
|
49
|
|
50
|
@Override
|
51
|
public Object answer(final InvocationOnMock invocation) throws Throwable {
|
52
|
final String query = invocation.getArguments()[0].toString();
|
53
|
if (query.contains("dnet:result_typologies")) {
|
54
|
return Arrays.asList("publication @@@ publication", "dataset @@@ dataset");
|
55
|
} else if (query.contains("dnet:access_modes")) {
|
56
|
return Arrays.asList("OPEN @@@ Open Access");
|
57
|
} else if (query.contains("dnet:publication_resource")) {
|
58
|
return Arrays.asList("0001 @@@ Journal Article");
|
59
|
} else if (query.contains("dnet:pid_types")) {
|
60
|
return Arrays.asList("oai @@@ Open Archive Initiative", "doi @@@ Digital object identifier");
|
61
|
} else if (query.contains("dnet:languages")) {
|
62
|
return Arrays.asList("ita @@@ Italian");
|
63
|
} else if (query.contains("ContextDSResourceType")) {
|
64
|
return Arrays.asList(
|
65
|
"egi::classification::natsc::math::stats @@@ Statistics and Probability",
|
66
|
"egi::classification::natsc::math::pure @@@ Pure Mathematics",
|
67
|
"egi::classification::natsc::math @@@ Mathematics",
|
68
|
"egi::classification::natsc @@@ Natural Sciences",
|
69
|
"egi::classification @@@ EGI classification scheme",
|
70
|
"egi @@@ EGI");
|
71
|
} else {
|
72
|
return new ArrayList<String>();
|
73
|
}
|
74
|
}
|
75
|
});
|
76
|
|
77
|
when(lookUpService.getResourceProfileByQuery(anyString())).thenAnswer(new Answer<Object>() {
|
78
|
|
79
|
@Override
|
80
|
public Object answer(final InvocationOnMock invocation) throws Throwable {
|
81
|
return "REPO NAME @@@ repo________";
|
82
|
}
|
83
|
});
|
84
|
}
|
85
|
|
86
|
@Test
|
87
|
public void testAsIndexRecord() throws Exception {
|
88
|
final ResultEntry pub = new ResultEntry();
|
89
|
pub.setOriginalId("ORIG_ID_1234");
|
90
|
pub.setTitle("TEST TITLE <test>");
|
91
|
pub.getAuthors().add("Michele Artini");
|
92
|
pub.getAuthors().add("Claudio Atzori");
|
93
|
pub.getAuthors().add("Alessia Bardi");
|
94
|
pub.setPublisher("Test publisher");
|
95
|
pub.setDescription("DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION & DESCRIPTION ");
|
96
|
pub.setLanguage("ita");
|
97
|
pub.setLicenseCode("OPEN");
|
98
|
pub.setResourceType("0001");
|
99
|
pub.setUrl("http://test.it/a=1&b=2");
|
100
|
pub.getPids().add(new PidEntry("doi", "10.000/xyz-123"));
|
101
|
pub.getPids().add(new PidEntry("oai", "oai:1234"));
|
102
|
pub.setCollectedFromId("test________::zenodo");
|
103
|
pub.setHostedById("test________::UNKNOWN");
|
104
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus");
|
105
|
pub.getLinksToProjects().add("info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble");
|
106
|
pub.getContexts().add("egi::classification::natsc::math::pure");
|
107
|
pub.getContexts().add("egi::classification::natsc::math::stats");
|
108
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
109
|
|
110
|
final Document doc = (new SAXReader()).read(new StringReader(xml));
|
111
|
|
112
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
113
|
|
114
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
115
|
|
116
|
writer.write(doc);
|
117
|
|
118
|
/* writer.close(); */
|
119
|
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testAsIndexRecord_json() throws Exception {
|
124
|
final ResultEntry pub = (new Gson()).fromJson(new InputStreamReader(getClass().getResourceAsStream("test_record.json")), ResultEntry.class);
|
125
|
|
126
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
127
|
|
128
|
final Document doc = (new SAXReader()).read(new StringReader(xml));
|
129
|
|
130
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
131
|
|
132
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
133
|
|
134
|
writer.write(doc);
|
135
|
|
136
|
/* writer.close(); */
|
137
|
|
138
|
}
|
139
|
|
140
|
@Test
|
141
|
public void testAsIndexRecord_json_with_greek_chars() throws Exception {
|
142
|
|
143
|
final ResultEntry pub =
|
144
|
(new Gson()).fromJson(new InputStreamReader(getClass().getResourceAsStream("test_record_with_greek_chars.json")), ResultEntry.class);
|
145
|
|
146
|
final String xml = pub.asOafRecord(ve, lookUpService, "http://oaf/oaf.xsd");
|
147
|
|
148
|
final Document doc = (new SAXReader()).read(new StringReader(xml));
|
149
|
|
150
|
final OutputFormat format = OutputFormat.createPrettyPrint();
|
151
|
|
152
|
final XMLWriter writer = new XMLWriter(System.out, format);
|
153
|
|
154
|
writer.write(doc);
|
155
|
|
156
|
/* writer.close(); */
|
157
|
|
158
|
}
|
159
|
|
160
|
}
|