dnet-hadoop/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/dump/funderresult/ResultLinkedToProjectTest.java

138 lines
4.3 KiB
Java
Raw Normal View History

2020-11-25 18:23:28 +01:00
package eu.dnetlib.dhp.oa.graph.dump.funderresult;
2020-11-25 18:23:28 +01:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
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.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;
2020-11-25 18:23:28 +01:00
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.oa.graph.dump.funderresults.SparkResultLinkedToProject;
import eu.dnetlib.dhp.schema.oaf.Publication;
import eu.dnetlib.dhp.schema.oaf.Result;
public class ResultLinkedToProjectTest {
2020-11-25 18:23:28 +01:00
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
2020-11-25 18:23:28 +01:00
private static SparkSession spark;
2020-11-25 18:23:28 +01:00
private static Path workingDir;
2020-11-25 18:23:28 +01:00
private static final Logger log = LoggerFactory
.getLogger(eu.dnetlib.dhp.oa.graph.dump.funderresult.ResultLinkedToProjectTest.class);
2021-05-14 10:58:12 +02:00
private static final HashMap<String, String> map = new HashMap<>();
2020-11-25 18:23:28 +01:00
@BeforeAll
public static void beforeAll() throws IOException {
workingDir = Files
.createTempDirectory(
eu.dnetlib.dhp.oa.graph.dump.funderresult.ResultLinkedToProjectTest.class.getSimpleName());
log.info("using work dir {}", workingDir);
2020-11-25 18:23:28 +01:00
SparkConf conf = new SparkConf();
conf.setAppName(eu.dnetlib.dhp.oa.graph.dump.funderresult.ResultLinkedToProjectTest.class.getSimpleName());
2020-11-25 18:23:28 +01:00
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());
2020-11-25 18:23:28 +01:00
spark = SparkSession
.builder()
.appName(eu.dnetlib.dhp.oa.graph.dump.funderresult.ResultLinkedToProjectTest.class.getSimpleName())
.config(conf)
.getOrCreate();
}
2020-11-25 18:23:28 +01:00
@AfterAll
public static void afterAll() throws IOException {
FileUtils.deleteDirectory(workingDir.toFile());
spark.stop();
}
2020-11-25 18:23:28 +01:00
@Test
public void testNoMatch() throws Exception {
2020-11-25 18:23:28 +01:00
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/funderresource/nomatch/papers.json")
.getPath();
final String graphPath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/funderresource/nomatch")
2020-11-25 18:23:28 +01:00
.getPath();
2020-11-25 18:23:28 +01:00
SparkResultLinkedToProject.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/preparedInfo",
"-sourcePath", sourcePath,
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-graphPath", graphPath
2020-11-25 18:23:28 +01:00
});
2020-11-25 18:23:28 +01:00
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
2020-11-25 18:23:28 +01:00
JavaRDD<Result> tmp = sc
.textFile(workingDir.toString() + "/preparedInfo")
.map(item -> OBJECT_MAPPER.readValue(item, Result.class));
2020-11-25 18:23:28 +01:00
org.apache.spark.sql.Dataset<Result> verificationDataset = spark
.createDataset(tmp.rdd(), Encoders.bean(Result.class));
2020-11-25 18:23:28 +01:00
Assertions.assertEquals(0, verificationDataset.count());
2020-11-25 18:23:28 +01:00
}
2020-11-25 18:23:28 +01:00
@Test
public void testMatchOne() throws Exception {
2020-11-25 18:23:28 +01:00
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/funderresource/match/papers.json")
.getPath();
2020-11-25 18:23:28 +01:00
final String relationPath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/funderresource/match")
2020-11-25 18:23:28 +01:00
.getPath();
2020-11-25 18:23:28 +01:00
SparkResultLinkedToProject.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/preparedInfo",
"-sourcePath", sourcePath,
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-graphPath", relationPath
2020-11-25 18:23:28 +01:00
});
2020-11-25 18:23:28 +01:00
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
2020-11-25 18:23:28 +01:00
JavaRDD<Publication> tmp = sc
.textFile(workingDir.toString() + "/preparedInfo")
.map(item -> OBJECT_MAPPER.readValue(item, Publication.class));
2020-11-25 18:23:28 +01:00
org.apache.spark.sql.Dataset<Publication> verificationDataset = spark
.createDataset(tmp.rdd(), Encoders.bean(Publication.class));
2020-11-25 18:23:28 +01:00
Assertions.assertEquals(1, verificationDataset.count());
2020-11-25 18:23:28 +01:00
}
}