133 lines
3.6 KiB
Java
133 lines
3.6 KiB
Java
|
|
package eu.dnetlib.dhp.oa.graph.dump.eosc;
|
|
|
|
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.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 com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.dnetlib.dhp.eosc.model.Relation;
|
|
|
|
/**
|
|
* @author miriam.baglioni
|
|
* @Date 25/10/23
|
|
*/
|
|
public class OrganizationProjectRelationTest {
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
private static SparkSession spark;
|
|
|
|
private static Path workingDir;
|
|
|
|
private static final Logger log = LoggerFactory
|
|
.getLogger(OrganizationProjectRelationTest.class);
|
|
|
|
private static HashMap<String, String> map = new HashMap<>();
|
|
|
|
@BeforeAll
|
|
public static void beforeAll() throws IOException {
|
|
workingDir = Files
|
|
.createTempDirectory(OrganizationProjectRelationTest.class.getSimpleName());
|
|
log.info("using work dir {}", workingDir);
|
|
|
|
SparkConf conf = new SparkConf();
|
|
conf.setAppName(OrganizationProjectRelationTest.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(OrganizationProjectRelationTest.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 + "organization")
|
|
.write()
|
|
.text(workingDir.toString() + "/working/organization");
|
|
|
|
spark
|
|
.read()
|
|
.textFile(workingPath + "project")
|
|
.write()
|
|
.text(workingDir.toString() + "/working/project");
|
|
|
|
SparkDumpOrganizationProject.main(new String[] {
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
"-outputPath", workingDir.toString() + "/working/",
|
|
"-sourcePath", sourcePath
|
|
|
|
});
|
|
|
|
JavaRDD<Relation> tmp = sc
|
|
.textFile(workingDir.toString() + "/working/organizationProject")
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class));
|
|
|
|
Assertions.assertEquals(3, tmp.count());
|
|
|
|
Assertions
|
|
.assertEquals(
|
|
1,
|
|
tmp
|
|
.filter(r -> r.getSource().equalsIgnoreCase("20|chistera____::9146e9ef10640675f361d674e77bd254"))
|
|
.count());
|
|
Assertions
|
|
.assertEquals(
|
|
2,
|
|
tmp
|
|
.filter(r -> r.getSource().equalsIgnoreCase("20|corda__h2020::dfe84ab5cad50d4dcfaf5bd0c86e1b64"))
|
|
.count());
|
|
|
|
Assertions
|
|
.assertEquals(
|
|
1,
|
|
tmp
|
|
.filter(
|
|
r -> r.getSource().equalsIgnoreCase("20|chistera____::9146e9ef10640675f361d674e77bd254") &&
|
|
r.getTarget().equalsIgnoreCase("40|nsf_________::d1c070f4252c32e23ccc3f4211c9c621"))
|
|
.count());
|
|
|
|
}
|
|
|
|
}
|