2020-05-27 12:38:42 +02:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.provision;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.StringReader;
|
2021-02-10 11:22:09 +01:00
|
|
|
import java.util.List;
|
2020-05-27 12:38:42 +02:00
|
|
|
|
2021-07-20 19:31:43 +02:00
|
|
|
import eu.dnetlib.dhp.oa.provision.utils.ContextDef;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Dataset;
|
2020-05-27 12:38:42 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.dom4j.Document;
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
2020-12-14 11:14:03 +01:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
2020-05-27 12:38:42 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2021-02-10 12:03:54 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2020-05-27 12:38:42 +02:00
|
|
|
|
|
|
|
import eu.dnetlib.dhp.oa.provision.model.JoinedEntity;
|
2021-02-10 11:22:09 +01:00
|
|
|
import eu.dnetlib.dhp.oa.provision.model.RelatedEntity;
|
|
|
|
import eu.dnetlib.dhp.oa.provision.model.RelatedEntityWrapper;
|
2020-05-27 12:38:42 +02:00
|
|
|
import eu.dnetlib.dhp.oa.provision.utils.ContextMapper;
|
|
|
|
import eu.dnetlib.dhp.oa.provision.utils.XmlRecordFactory;
|
2021-02-10 11:22:09 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Project;
|
2020-12-11 12:44:53 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Publication;
|
2021-02-10 11:22:09 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
2020-05-27 12:38:42 +02:00
|
|
|
|
|
|
|
public class XmlRecordFactoryTest {
|
|
|
|
|
2021-05-13 16:11:22 +02:00
|
|
|
public static final String otherDsTypeId = "scholarcomminfra,infospace,pubsrepository::mock,entityregistry,entityregistry::projects,entityregistry::repositories,websource";
|
2020-05-27 12:38:42 +02:00
|
|
|
|
2021-05-13 16:11:22 +02:00
|
|
|
public static ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
2020-12-14 11:14:03 +01:00
|
|
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
2020-05-27 12:38:42 +02:00
|
|
|
@Test
|
|
|
|
public void testXMLRecordFactory() throws IOException, DocumentException {
|
|
|
|
|
|
|
|
ContextMapper contextMapper = new ContextMapper();
|
|
|
|
|
|
|
|
XmlRecordFactory xmlRecordFactory = new XmlRecordFactory(contextMapper, false, XmlConverterJob.schemaLocation,
|
2020-12-02 11:23:49 +01:00
|
|
|
otherDsTypeId);
|
2020-05-27 12:38:42 +02:00
|
|
|
|
2020-12-14 11:14:03 +01:00
|
|
|
Publication p = OBJECT_MAPPER
|
2020-12-11 12:44:53 +01:00
|
|
|
.readValue(IOUtils.toString(getClass().getResourceAsStream("publication.json")), Publication.class);
|
|
|
|
|
|
|
|
String xml = xmlRecordFactory.build(new JoinedEntity<>(p));
|
2020-05-27 12:38:42 +02:00
|
|
|
|
|
|
|
assertNotNull(xml);
|
|
|
|
|
|
|
|
Document doc = new SAXReader().read(new StringReader(xml));
|
|
|
|
|
|
|
|
assertNotNull(doc);
|
|
|
|
|
2020-12-14 11:14:03 +01:00
|
|
|
System.out.println(doc.asXML());
|
2020-12-11 12:44:53 +01:00
|
|
|
|
2020-12-14 11:14:03 +01:00
|
|
|
Assertions.assertEquals("0000-0001-9613-6638", doc.valueOf("//creator[@rank = '1']/@orcid"));
|
2020-12-11 12:44:53 +01:00
|
|
|
Assertions.assertEquals("0000-0001-9613-6639", doc.valueOf("//creator[@rank = '1']/@orcid_pending"));
|
2020-12-14 11:14:03 +01:00
|
|
|
|
|
|
|
Assertions.assertEquals("0000-0001-9613-9956", doc.valueOf("//creator[@rank = '2']/@orcid"));
|
|
|
|
Assertions.assertEquals("", doc.valueOf("//creator[@rank = '2']/@orcid_pending"));
|
|
|
|
|
2021-05-28 16:57:30 +02:00
|
|
|
Assertions.assertEquals("doi", doc.valueOf("//instance/pid/@classid"));
|
|
|
|
Assertions.assertEquals("10.1109/TED.2018.2853550", doc.valueOf("//instance/pid/text()"));
|
|
|
|
|
|
|
|
Assertions.assertEquals("doi", doc.valueOf("//instance/alternateidentifier/@classid"));
|
|
|
|
Assertions.assertEquals("10.5689/LIB.2018.2853550", doc.valueOf("//instance/alternateidentifier/text()"));
|
2020-05-27 19:31:54 +02:00
|
|
|
// TODO add assertions based of values extracted from the XML record
|
2020-05-27 12:38:42 +02:00
|
|
|
}
|
2021-02-10 11:22:09 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testXMLRecordFactoryWithValidatedProject() throws IOException, DocumentException {
|
|
|
|
|
|
|
|
ContextMapper contextMapper = new ContextMapper();
|
|
|
|
|
|
|
|
XmlRecordFactory xmlRecordFactory = new XmlRecordFactory(contextMapper, false, XmlConverterJob.schemaLocation,
|
|
|
|
otherDsTypeId);
|
|
|
|
|
|
|
|
Publication p = OBJECT_MAPPER
|
|
|
|
.readValue(IOUtils.toString(getClass().getResourceAsStream("publication.json")), Publication.class);
|
|
|
|
Project pj = OBJECT_MAPPER
|
|
|
|
.readValue(IOUtils.toString(getClass().getResourceAsStream("project.json")), Project.class);
|
|
|
|
Relation rel = OBJECT_MAPPER
|
|
|
|
.readValue(
|
|
|
|
(IOUtils.toString(getClass().getResourceAsStream("relToValidatedProject.json"))), Relation.class);
|
|
|
|
RelatedEntity relatedProject = CreateRelatedEntitiesJob_phase1.asRelatedEntity(pj, Project.class);
|
|
|
|
List<RelatedEntityWrapper> links = Lists.newArrayList();
|
|
|
|
RelatedEntityWrapper rew = new RelatedEntityWrapper(rel, relatedProject);
|
|
|
|
links.add(rew);
|
|
|
|
JoinedEntity je = new JoinedEntity<>(p);
|
|
|
|
je.setLinks(links);
|
|
|
|
|
|
|
|
String xml = xmlRecordFactory.build(je);
|
|
|
|
|
|
|
|
assertNotNull(xml);
|
|
|
|
|
|
|
|
Document doc = new SAXReader().read(new StringReader(xml));
|
|
|
|
assertNotNull(doc);
|
|
|
|
System.out.println(doc.asXML());
|
|
|
|
Assertions.assertEquals("2021-01-01", doc.valueOf("//validated/@date"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testXMLRecordFactoryWithNonValidatedProject() throws IOException, DocumentException {
|
|
|
|
|
|
|
|
ContextMapper contextMapper = new ContextMapper();
|
|
|
|
|
|
|
|
XmlRecordFactory xmlRecordFactory = new XmlRecordFactory(contextMapper, false, XmlConverterJob.schemaLocation,
|
|
|
|
otherDsTypeId);
|
|
|
|
|
|
|
|
Publication p = OBJECT_MAPPER
|
|
|
|
.readValue(IOUtils.toString(getClass().getResourceAsStream("publication.json")), Publication.class);
|
|
|
|
Project pj = OBJECT_MAPPER
|
|
|
|
.readValue(IOUtils.toString(getClass().getResourceAsStream("project.json")), Project.class);
|
|
|
|
Relation rel = OBJECT_MAPPER
|
|
|
|
.readValue((IOUtils.toString(getClass().getResourceAsStream("relToProject.json"))), Relation.class);
|
|
|
|
RelatedEntity relatedProject = CreateRelatedEntitiesJob_phase1.asRelatedEntity(pj, Project.class);
|
|
|
|
List<RelatedEntityWrapper> links = Lists.newArrayList();
|
|
|
|
RelatedEntityWrapper rew = new RelatedEntityWrapper(rel, relatedProject);
|
|
|
|
links.add(rew);
|
|
|
|
JoinedEntity je = new JoinedEntity<>(p);
|
|
|
|
je.setLinks(links);
|
|
|
|
|
|
|
|
String xml = xmlRecordFactory.build(je);
|
|
|
|
|
|
|
|
assertNotNull(xml);
|
|
|
|
|
|
|
|
Document doc = new SAXReader().read(new StringReader(xml));
|
|
|
|
assertNotNull(doc);
|
|
|
|
System.out.println(doc.asXML());
|
|
|
|
assertEquals("", doc.valueOf("//rel/validated"));
|
|
|
|
}
|
2021-07-20 19:31:43 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEnermapsRecord() throws IOException, DocumentException {
|
|
|
|
|
|
|
|
String contextmap = "<entries><entry id=\"enermaps\" label=\"Energy Research\" name=\"context\" type=\"community\"/>" +
|
|
|
|
"<entry id=\"enermaps::selection\" label=\"Featured dataset\" name=\"category\"/>"+
|
|
|
|
"<entry id=\"enermaps::selection::tgs00004\" label=\"Dataset title\" name=\"concept\"/>"+
|
|
|
|
"</entries>";
|
|
|
|
|
|
|
|
ContextMapper contextMapper = ContextMapper.fromXml(contextmap);
|
|
|
|
XmlRecordFactory xmlRecordFactory = new XmlRecordFactory(contextMapper, false, XmlConverterJob.schemaLocation,
|
|
|
|
otherDsTypeId);
|
|
|
|
|
|
|
|
Dataset d = OBJECT_MAPPER
|
|
|
|
.readValue(IOUtils.toString(getClass().getResourceAsStream("enermaps.json")), Dataset.class);
|
|
|
|
|
|
|
|
JoinedEntity je = new JoinedEntity<>(d);
|
|
|
|
|
|
|
|
String xml = xmlRecordFactory.build(je);
|
|
|
|
|
|
|
|
assertNotNull(xml);
|
|
|
|
|
|
|
|
Document doc = new SAXReader().read(new StringReader(xml));
|
|
|
|
assertNotNull(doc);
|
|
|
|
System.out.println(doc.asXML());
|
|
|
|
assertEquals("enermaps::selection::tgs00004", doc.valueOf("//concept/@id"));
|
|
|
|
}
|
2020-05-27 12:38:42 +02:00
|
|
|
}
|