dhp-graph-dump/dump/src/test/java/eu/dnetlib/dhp/oa/graph/dump/eosc/SelectEoscResultTest.java

176 lines
8.4 KiB
Java

package eu.dnetlib.dhp.oa.graph.dump.eosc;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.eosc.model.EoscResult;
import eu.dnetlib.dhp.eosc.model.Organization;
import eu.dnetlib.dhp.oa.graph.dump.complete.SelectRelationTest;
import eu.dnetlib.dhp.oa.graph.dump.complete.SparkSelectValidRelationsJob;
import eu.dnetlib.dhp.schema.oaf.Relation;
import org.apache.commons.io.FileUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @author miriam.baglioni
* @Date 21/09/22
*/
public class SelectEoscResultTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static SparkSession spark;
private static Path workingDir;
private static final Logger log = LoggerFactory
.getLogger(SelectEoscResultTest.class);
private static HashMap<String, String> map = new HashMap<>();
@BeforeAll
public static void beforeAll() throws IOException {
workingDir = Files
.createTempDirectory(SelectEoscResultTest.class.getSimpleName());
log.info("using work dir {}", workingDir);
SparkConf conf = new SparkConf();
conf.setAppName(SelectRelationTest.class.getSimpleName());
conf.setMaster("local[*]");
conf.set("spark.driver.host", "localhost");
conf.set("hive.metastore.local", "true");
conf.set("spark.ui.enabled", "false");
conf.set("spark.sql.warehouse.dir", workingDir.toString());
conf.set("hive.metastore.warehouse.dir", workingDir.resolve("warehouse").toString());
spark = SparkSession
.builder()
.appName(SelectRelationTest.class.getSimpleName())
.config(conf)
.getOrCreate();
}
@AfterAll
public static void afterAll() throws IOException {
FileUtils.deleteDirectory(workingDir.toFile());
spark.stop();
}
@Test
public void selectEoscResults() throws Exception {
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/eosc/input/publication")
.getPath();
final String cmp = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/communityMapPath/communitymap.json")
.getPath();
SelectEoscResultsJobStep1.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/publication",
"-sourcePath", sourcePath,
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-communityMapPath", cmp
});
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
JavaRDD<EoscResult> tmp = sc
.textFile(workingDir.toString() + "/publication")
.map(item -> OBJECT_MAPPER.readValue(item, EoscResult.class));
Assertions.assertEquals(3, tmp.count());
Assertions.assertEquals(0, tmp.filter(r -> Optional.ofNullable(r.getAffiliation()).isPresent() && r.getAffiliation().size() > 0).count());
}
// "source":"20|13811704aa70::51a6ade52065e3b371d1ae822e07f1ff","subRelType":"affiliation","target":"50|06cdd3ff4700::93859bd27121c3ee7c6ee4bfb1790cba"
// legalname = Doris Engineering (France)
// pid = [(pidtype:"GRID","value":"grid.432986.2"),("classid":"ROR","value":"https://ror.org/03nd0ms94"),("classid":"GRID","value":"grid.432986.2"),("classid":"ROR","value":"https://ror.org/03nd0ms94")]
// sono replicate e ci dovrebbero essere uniche nel dump
// "source":"20|MetisRadboud::b58bdbe8ae5acead04fc76777d2f8017","subRelType":"affiliation","target":"50|06cdd3ff4700::93859bd27121c3ee7c6ee4bfb1790cba"
// legalname = RENNES METROPOLE
//pid = [(PIC, 892062829)]
// "source":"20|____________::d1b0ee22411434cf905692d0fac25749","subRelType":"affiliation","target":"50|06cdd3ff4700::cd7711c65d518859f1d87056e2c45d98"
//legalname = MIKARE RESEARCH
//pid = []
//for 50|06cdd3ff4700::ff21e3c55d527fa7db171137c5fd1f1f no affiliation relation is provided
@Test
public void ExtendEoscResultWithOrganizationTest() throws Exception {
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/eosc/input")
.getPath();
final String cmp = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/communityMapPath/communitymap.json")
.getPath();
String resultPath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/eosc/working/publication")
.getPath();
ExtendEoscResultWithOrganizationStep2.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/publication",
"-sourcePath", sourcePath,
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-resultPath", resultPath
});
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
JavaRDD<EoscResult> tmp = sc
.textFile(workingDir.toString() + "/publication")
.map(item -> OBJECT_MAPPER.readValue(item, EoscResult.class));
Assertions.assertEquals(3, tmp.count());
Assertions.assertEquals(2, tmp.filter(r -> Optional.ofNullable(r.getAffiliation()).isPresent() && r.getAffiliation().size() > 0).count());
Assertions.assertEquals(2, tmp.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::93859bd27121c3ee7c6ee4bfb1790cba")).first().getAffiliation().size());
List<Organization> affiliations = tmp.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::93859bd27121c3ee7c6ee4bfb1790cba")).first().getAffiliation();
Assertions.assertTrue(affiliations.stream().anyMatch(a -> a.getName().equalsIgnoreCase("Doris Engineering (France)")));
Assertions.assertTrue(affiliations.stream().anyMatch(a -> a.getName().equalsIgnoreCase("RENNES METROPOLE")));
Organization organization = affiliations.stream().filter(a -> a.getId().equalsIgnoreCase("20|13811704aa70::51a6ade52065e3b371d1ae822e07f1ff")).findFirst().get();
Assertions.assertEquals("Doris Engineering (France)", organization.getName());
Assertions.assertTrue(organization.getPid().stream().anyMatch(p -> p.getValue().equalsIgnoreCase("grid.432986.2") && p.getType().equalsIgnoreCase("grid")));
Assertions.assertTrue(organization.getPid().stream().anyMatch(p -> p.getValue().equalsIgnoreCase("https://ror.org/03nd0ms94") && p.getType().equalsIgnoreCase("ror")));
Assertions.assertEquals(2, organization.getPid().size());
organization = affiliations.stream().filter(a -> a.getId().equalsIgnoreCase("20|MetisRadboud::b58bdbe8ae5acead04fc76777d2f8017")).findFirst().get();
Assertions.assertEquals("RENNES METROPOLE", organization.getName());
Assertions.assertEquals(1, organization.getPid().size());
Assertions.assertTrue(organization.getPid().get(0).getValue().equalsIgnoreCase("892062829") && organization.getPid().get(0).getType().equalsIgnoreCase("pic"));
Assertions.assertEquals(1, tmp.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::cd7711c65d518859f1d87056e2c45d98")).first().getAffiliation().size());
Assertions.assertEquals("MIKARE RESEARCH", tmp.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::cd7711c65d518859f1d87056e2c45d98")).first().getAffiliation().get(0).getName());
Assertions.assertEquals(0, tmp.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::cd7711c65d518859f1d87056e2c45d98")).first().getAffiliation().get(0).getPid().size() );
Assertions.assertFalse( Optional.ofNullable(tmp.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::ff21e3c55d527fa7db171137c5fd1f1f")).first().getAffiliation()).isPresent());
}
}