Revision 52968
Added by Sandro La Bruzzo over 5 years ago
DLIParserTest.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.resolver; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.util.Arrays; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import eu.dnetlib.data.transform.OntologyLoader; |
|
3 |
import com.googlecode.protobuf.format.JsonFormat; |
|
4 |
import eu.dnetlib.data.collector.plugins.datacite.DataciteParser; |
|
5 |
import eu.dnetlib.data.proto.dli.ScholixObjectProtos; |
|
6 |
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions; |
|
9 | 7 |
import eu.dnetlib.dli.DLIUtils; |
10 |
import eu.dnetlib.dli.resolver.NCBINParser; |
|
11 |
import eu.dnetlib.dli.resolver.NCBINResolver; |
|
12 |
import eu.dnetlib.dli.resolver.OpenAireParser; |
|
8 |
import eu.dnetlib.dli.resolver.*; |
|
13 | 9 |
import eu.dnetlib.dli.resolver.model.CompletionStatus; |
14 | 10 |
import eu.dnetlib.dli.resolver.model.DLIObjectProvenance; |
15 | 11 |
import eu.dnetlib.dli.resolver.model.DLIResolvedObject; |
16 | 12 |
import eu.dnetlib.dli.resolver.model.ObjectProvisionMode; |
17 | 13 |
import eu.dnetlib.dli.resolver.model.serializer.DLIResolverSerializer; |
18 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
|
14 |
import eu.dnetlib.pid.resolver.AbstractPIDResolver;
|
|
19 | 15 |
import eu.dnetlib.resolver.parser.DMFResolverParser; |
20 | 16 |
import eu.dnetlib.resolver.parser.PMFResolverParser; |
21 | 17 |
import eu.dnetlib.resolver.parser.ScholixResolverParser; |
22 |
import eu.dnetlib.rmi.enabling.ISLookUpException; |
|
23 |
import eu.dnetlib.rmi.enabling.ISLookUpService; |
|
24 | 18 |
import org.apache.commons.io.IOUtils; |
19 |
import org.apache.commons.lang3.StringUtils; |
|
25 | 20 |
import org.apache.commons.logging.Log; |
26 | 21 |
import org.apache.commons.logging.LogFactory; |
27 | 22 |
import org.junit.Assert; |
28 |
import org.junit.Before; |
|
29 | 23 |
import org.junit.Ignore; |
30 | 24 |
import org.junit.Test; |
31 |
import org.junit.runner.RunWith; |
|
32 |
import org.mockito.Mock; |
|
33 |
import org.mockito.Mockito; |
|
34 | 25 |
import org.springframework.beans.factory.annotation.Autowired; |
35 |
import org.springframework.test.context.ContextConfiguration; |
|
36 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
|
37 | 26 |
|
38 |
import static org.mockito.Mockito.when; |
|
27 |
import java.io.IOException; |
|
28 |
import java.io.InputStream; |
|
29 |
import java.net.URISyntaxException; |
|
30 |
import java.net.URL; |
|
31 |
import java.time.LocalDateTime; |
|
32 |
import java.util.Arrays; |
|
39 | 33 |
|
40 | 34 |
/** |
41 | 35 |
* Created by sandro on 9/12/16. |
... | ... | |
114 | 108 |
} |
115 | 109 |
|
116 | 110 |
@Test |
117 |
public void testScholixParser() throws IOException { |
|
111 |
public void testScholixParser() throws IOException, URISyntaxException {
|
|
118 | 112 |
final InputStream resourceAsStream = getClass().getResourceAsStream("/eu/dnetlib/dli/parser/ieda.xml"); |
119 | 113 |
|
120 | 114 |
String str = IOUtils.toString(resourceAsStream); |
121 | 115 |
ScholixResolverParser parser = new ScholixResolverParser(); |
122 | 116 |
DLIResolvedObject object = parser.parseObject(str); |
123 | 117 |
|
124 |
log.debug("object = " + object);
|
|
118 |
final ScholixObjectProtos.Scholix.Builder scholix = ScholixObjectProtos.Scholix.newBuilder();
|
|
125 | 119 |
|
126 |
Assert.assertNotNull(object); |
|
127 |
log.debug("object.getCompletionStatus() = " + object.getCompletionStatus()); |
|
120 |
scholix.addLinkproviderBuilder() |
|
121 |
.setName(object.getDatasourceProvenance().get(0).getDatasource()) |
|
122 |
.addIdentifiersBuilder() |
|
123 |
.setIdentifier(object.getDatasourceProvenance().get(0).getDatasourceId()) |
|
124 |
.setSchema("dnetIdentifier"); |
|
128 | 125 |
|
129 |
Assert.assertTrue(object.getCompletionStatus().equals(CompletionStatus.incomplete.toString())); |
|
130 | 126 |
|
131 |
Assert.assertNotNull(object.getRelations()); |
|
132 | 127 |
|
133 |
Assert.assertTrue(object.getRelations().size() == 1); |
|
128 |
scholix.setRelationship(ScholixObjectProtos.ScholixRelationship.newBuilder() |
|
129 |
.setName(object.getRelations().get(0).getRelationSemantics()) |
|
130 |
.setInverse(object.getRelations().get(0).getInverseRelation()) |
|
131 |
.setSchema("datacite") |
|
132 |
.build()); |
|
133 |
final CrossRefParserJSON parserJSON = new CrossRefParserJSON(); |
|
134 |
DLIResolvedObject dli_source = parserJSON.parseRecord(IOUtils.toString(new URL("https://api.crossref.org/works/10.1002/2016JG003630").openStream())); |
|
135 |
DLIResolvedObject dli_target = DataciteResolver.parseResponse(AbstractPIDResolver.requestURL("http://search.datacite.org/api?wt=json&q=doi:10.6073/pasta/6038bd9745347610df8a51c92ca0e452")); |
|
136 |
final ScholixObjectProtos.ScholixResource source = generateResource(dli_source); |
|
137 |
final ScholixObjectProtos.ScholixResource target = generateResource(dli_target); |
|
138 |
scholix.setSource(source); |
|
139 |
scholix.setTarget(target); |
|
140 |
scholix.setPublicationDate(LocalDateTime.now().toString()); |
|
141 |
System.out.println(JsonFormat.printToString(scholix.build())); |
|
134 | 142 |
|
135 |
DLIResolvedObject nullObject = parser.parseObject(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dli/parser/InputRecordDMF.xml"))); |
|
136 |
Assert.assertNull(nullObject); |
|
137 |
nullObject = parser.parseObject(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dli/parser/InputPMFRecord.xml"))); |
|
138 |
Assert.assertNull(nullObject); |
|
143 |
System.out.println(generateIdentifier(dli_source, dli_target)); |
|
139 | 144 |
|
145 |
|
|
140 | 146 |
} |
141 | 147 |
|
148 |
private String generateIdentifier(final DLIResolvedObject source, DLIResolvedObject target) { |
|
149 |
|
|
150 |
return AbstractDNetXsltFunctions.md5(String.format("%s::%s", source.getPid().toLowerCase().trim(), target.getPid().toLowerCase().trim())); |
|
151 |
|
|
152 |
} |
|
153 |
|
|
154 |
private ScholixObjectProtos.ScholixResource generateResource(DLIResolvedObject result) { |
|
155 |
final ScholixObjectProtos.ScholixResource.Builder builder = ScholixObjectProtos.ScholixResource.newBuilder(); |
|
156 |
if (result.getDatasourceProvenance() != null) |
|
157 |
result.getDatasourceProvenance().forEach( |
|
158 |
objectProvenance -> { |
|
159 |
builder.addCollectedFrom(ScholixObjectProtos.ScholixCollectedFrom.newBuilder() |
|
160 |
.setProvisionMode(((DLIObjectProvenance) objectProvenance).getProvisionMode()) |
|
161 |
.setCompletionStatus(((DLIObjectProvenance) objectProvenance).getCompletionStatus()) |
|
162 |
.setProvider(ScholixObjectProtos.ScholixEntityId.newBuilder() |
|
163 |
.setName(objectProvenance.getDatasource()) |
|
164 |
.addIdentifiers(ScholixObjectProtos.ScholixIdentifier.newBuilder().setIdentifier(objectProvenance.getDatasourceId()) |
|
165 |
.setSchema("dnetIdentifier").build()) |
|
166 |
.build())); |
|
167 |
if (StringUtils.isNotEmpty(((DLIObjectProvenance) objectProvenance).getPublisher())) { |
|
168 |
builder.addPublisher(ScholixObjectProtos.ScholixEntityId.newBuilder() |
|
169 |
.setName(((DLIObjectProvenance) objectProvenance).getPublisher()) |
|
170 |
.build()); |
|
171 |
} |
|
172 |
|
|
173 |
}); |
|
174 |
builder.addIdentifier(ScholixObjectProtos.ScholixIdentifier.newBuilder(). |
|
175 |
setIdentifier(result.getPid()) |
|
176 |
.setSchema(result.getPidType()) |
|
177 |
.build()); |
|
178 |
builder.setObjectType(result.getType().toString()); |
|
179 |
if (result.getTitles() != null && result.getTitles().size() > 0) |
|
180 |
builder.setTitle(result.getTitles().get(0)); |
|
181 |
if (result.getAuthors() != null) |
|
182 |
result.getAuthors().forEach(author -> builder.addCreator( |
|
183 |
ScholixObjectProtos.ScholixEntityId.newBuilder() |
|
184 |
.setName(author) |
|
185 |
.build())); |
|
186 |
if (StringUtils.isNotBlank(result.getDate())) { |
|
187 |
builder.setPublicationDate(result.getDate()); |
|
188 |
} |
|
189 |
|
|
190 |
String tp = null; |
|
191 |
|
|
192 |
switch (result.getType()) { |
|
193 |
case dataset: |
|
194 |
tp = "60"; |
|
195 |
break; |
|
196 |
case unknown: |
|
197 |
tp = "70"; |
|
198 |
break; |
|
199 |
case publication: |
|
200 |
tp = "50"; |
|
201 |
break; |
|
202 |
} |
|
203 |
builder.setDnetIdentifier(tp + "|dnet________::" + result.getIdentifier()); |
|
204 |
return builder.build(); |
|
205 |
} |
|
206 |
|
|
142 | 207 |
@Test |
143 | 208 |
public void testScholixCompleteParser() throws IOException { |
144 | 209 |
final InputStream resourceAsStream = getClass().getResourceAsStream("/eu/dnetlib/dli/parser/InputRecordScholix.xml"); |
Also available in: Unified diff
changed Datacite Resolver