170 lines
4.5 KiB
Java
170 lines
4.5 KiB
Java
|
|
package eu.dnetlib.dhp.oa.graph.dump.eosc;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import eu.dnetlib.dhp.eosc.model.*;
|
|
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.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.Optional;
|
|
|
|
/**
|
|
* @author miriam.baglioni
|
|
* @Date 25/10/23
|
|
*/
|
|
public class ExtendProjectTest {
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
private static SparkSession spark;
|
|
|
|
private static Path workingDir;
|
|
|
|
private static final Logger log = LoggerFactory
|
|
.getLogger(ExtendProjectTest.class);
|
|
|
|
private static HashMap<String, String> map = new HashMap<>();
|
|
|
|
@BeforeAll
|
|
public static void beforeAll() throws IOException {
|
|
workingDir = Files
|
|
.createTempDirectory(ExtendProjectTest.class.getSimpleName());
|
|
log.info("using work dir {}", workingDir);
|
|
|
|
SparkConf conf = new SparkConf();
|
|
conf.setAppName(ExtendProjectTest.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(ExtendProjectTest.class.getSimpleName())
|
|
.config(conf)
|
|
.getOrCreate();
|
|
}
|
|
|
|
@AfterAll
|
|
public static void afterAll() throws IOException {
|
|
FileUtils.deleteDirectory(workingDir.toFile());
|
|
spark.stop();
|
|
}
|
|
|
|
@Test
|
|
public void ExtendEoscResultWithProjectTest() throws Exception {
|
|
final String sourcePath = getClass()
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/eosc/input")
|
|
.getPath();
|
|
|
|
final String workingPath = getClass()
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/eosc/working/")
|
|
.getPath();
|
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
|
|
|
spark
|
|
.read()
|
|
.textFile(workingPath + "publication")
|
|
.write()
|
|
.text(workingDir.toString() + "/working/publicationextendedaffiliation");
|
|
|
|
|
|
|
|
SparkUpdateProjectInfo.main(new String[] {
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
"-outputPath", workingDir.toString() + "/",
|
|
"-sourcePath", sourcePath,
|
|
"-resultType", "publication",
|
|
"-workingPath", workingDir.toString() + "/working/",
|
|
"-preparedInfoPath", workingPath + "preparedInfo"
|
|
});
|
|
|
|
JavaRDD<Result> tmp = sc
|
|
.textFile(workingDir.toString() + "/working/publicationextendedproject")
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Result.class));
|
|
|
|
Assertions.assertEquals(3, tmp.count());
|
|
|
|
Assertions
|
|
.assertEquals(
|
|
2,
|
|
tmp
|
|
.filter(r -> Optional.ofNullable(r.getProjects()).isPresent() && r.getProjects().size() > 0)
|
|
.count());
|
|
|
|
Assertions
|
|
.assertEquals(
|
|
2,
|
|
tmp
|
|
.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::93859bd27121c3ee7c6ee4bfb1790cba"))
|
|
.first()
|
|
.getProjects()
|
|
.size());
|
|
|
|
Assertions
|
|
.assertEquals(
|
|
3,
|
|
tmp
|
|
.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::cd7711c65d518859f1d87056e2c45d98"))
|
|
.first()
|
|
.getProjects()
|
|
.size());
|
|
|
|
List<ProjectSummary> projectSummaries = tmp
|
|
.filter(r -> r.getId().equalsIgnoreCase("50|06cdd3ff4700::93859bd27121c3ee7c6ee4bfb1790cba"))
|
|
.first()
|
|
.getProjects();
|
|
|
|
Assertions
|
|
.assertTrue(
|
|
projectSummaries.stream().anyMatch(p -> p.getFunder().getShortName().equals("NSF")));
|
|
|
|
Assertions
|
|
.assertTrue(
|
|
projectSummaries.stream().anyMatch(p -> p.getFunder().getShortName().equals("UKRI")));
|
|
|
|
|
|
|
|
JavaRDD<Project> projects = sc
|
|
.textFile(workingDir.toString() + "/project")
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Project.class));
|
|
|
|
JavaRDD<Relation> rels = sc
|
|
.textFile(workingDir.toString() + "/resultProject")
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class));
|
|
|
|
System.out.println(projects.count());
|
|
|
|
Assertions.assertEquals(5, projects.count());
|
|
|
|
Assertions.assertEquals(5, rels.count());
|
|
|
|
rels.foreach(r -> Assertions.assertTrue(r.getSource().startsWith("50|")));
|
|
rels.foreach(r -> Assertions.assertTrue(r.getTarget().startsWith("40|")));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|