2020-04-02 18:37:53 +02:00
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-02 18:37:53 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2020-04-08 18:02:30 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
2020-04-01 18:48:31 +02:00
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
2020-04-02 18:37:53 +02:00
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
2020-04-03 18:32:25 +02:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2020-04-01 18:48:31 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-04-03 18:32:25 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2020-04-08 18:02:30 +02:00
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
2020-04-03 18:32:25 +02:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.junit.jupiter.api.*;
|
2020-04-02 18:37:53 +02:00
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
2020-04-01 18:48:31 +02:00
|
|
|
import org.mockito.Mock;
|
2020-04-02 18:37:53 +02:00
|
|
|
import org.mockito.Mockito;
|
2020-04-01 19:05:48 +02:00
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-03 18:32:25 +02:00
|
|
|
import java.io.File;
|
2020-04-01 18:48:31 +02:00
|
|
|
import java.io.IOException;
|
2020-04-08 18:02:30 +02:00
|
|
|
import java.io.Serializable;
|
2020-04-03 18:32:25 +02:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.nio.file.Paths;
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-16 12:13:26 +02:00
|
|
|
import static java.nio.file.Files.createTempDirectory;
|
2020-04-03 18:32:25 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2020-04-08 18:02:30 +02:00
|
|
|
import static org.mockito.Mockito.lenient;
|
2020-04-01 19:05:48 +02:00
|
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
2020-04-03 18:32:25 +02:00
|
|
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
2020-04-08 18:02:30 +02:00
|
|
|
public class SparkDedupTest implements Serializable {
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
@Mock(serializable = true)
|
2020-04-03 18:32:25 +02:00
|
|
|
ISLookUpService isLookUpService;
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
private static SparkSession spark;
|
|
|
|
private static JavaSparkContext jsc;
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-06 16:30:31 +02:00
|
|
|
private static String testGraphBasePath;
|
2020-04-16 12:15:38 +02:00
|
|
|
private static String testOutputBasePath;
|
|
|
|
private static String testDedupGraphBasePath;
|
2020-04-06 16:30:31 +02:00
|
|
|
private final static String testActionSetId = "test-orchestrator";
|
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
private static void cleanUp() throws IOException, URISyntaxException {
|
|
|
|
|
|
|
|
testGraphBasePath = Paths.get(SparkDedupTest.class.getResource("/eu/dnetlib/dhp/dedup/entities").toURI()).toFile().getAbsolutePath();
|
|
|
|
|
2020-04-16 12:13:26 +02:00
|
|
|
testOutputBasePath = createTempDirectory(SparkDedupTest.class.getSimpleName() + "-").toAbsolutePath().toString();
|
|
|
|
testDedupGraphBasePath = createTempDirectory(SparkDedupTest.class.getSimpleName() + "-").toAbsolutePath().toString();
|
|
|
|
|
2020-04-06 16:30:31 +02:00
|
|
|
FileUtils.deleteDirectory(new File(testOutputBasePath));
|
2020-04-16 12:13:26 +02:00
|
|
|
FileUtils.deleteDirectory(new File(testDedupGraphBasePath));
|
2020-04-03 18:32:25 +02:00
|
|
|
|
|
|
|
spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(SparkCreateSimRels.class.getSimpleName())
|
|
|
|
.master("local[*]")
|
|
|
|
.config(new SparkConf())
|
|
|
|
.getOrCreate();
|
|
|
|
|
2020-04-16 12:13:26 +02:00
|
|
|
jsc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
2020-04-08 18:02:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
private void setUp() throws IOException, ISLookUpException {
|
|
|
|
|
|
|
|
lenient().when(isLookUpService.getResourceProfileByQuery(Mockito.contains(testActionSetId)))
|
2020-04-06 16:30:31 +02:00
|
|
|
.thenReturn(IOUtils.toString(SparkDedupTest.class.getResourceAsStream("/eu/dnetlib/dhp/dedup/profiles/mock_orchestrator.xml")));
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
lenient().when(isLookUpService.getResourceProfileByQuery(Mockito.contains("organization")))
|
2020-04-06 16:30:31 +02:00
|
|
|
.thenReturn(IOUtils.toString(SparkDedupTest.class.getResourceAsStream("/eu/dnetlib/dhp/dedup/conf/org.curr.conf.json")));
|
2020-04-01 18:48:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
lenient().when(isLookUpService.getResourceProfileByQuery(Mockito.contains("publication")))
|
2020-04-06 16:30:31 +02:00
|
|
|
.thenReturn(IOUtils.toString(SparkDedupTest.class.getResourceAsStream("/eu/dnetlib/dhp/dedup/conf/pub.curr.conf.json")));
|
2020-04-01 18:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-04-03 18:32:25 +02:00
|
|
|
@Order(1)
|
2020-04-01 18:48:31 +02:00
|
|
|
public void createSimRelsTest() throws Exception {
|
2020-04-02 18:37:53 +02:00
|
|
|
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils.toString(
|
|
|
|
SparkCreateSimRels.class.getResourceAsStream("/eu/dnetlib/dhp/oa/dedup/createSimRels_parameters.json")));
|
|
|
|
parser.parseArgument(new String[]{
|
2020-04-01 18:48:31 +02:00
|
|
|
"-mt", "local[*]",
|
2020-04-03 18:32:25 +02:00
|
|
|
"-i", testGraphBasePath,
|
|
|
|
"-asi", testActionSetId,
|
2020-04-01 18:48:31 +02:00
|
|
|
"-la", "lookupurl",
|
2020-04-03 18:32:25 +02:00
|
|
|
"-w", testOutputBasePath});
|
|
|
|
|
|
|
|
new SparkCreateSimRels(parser, spark).run(isLookUpService);
|
|
|
|
|
|
|
|
long orgs_simrel = spark.read().load(testOutputBasePath + "/" + testActionSetId + "/organization_simrel").count();
|
|
|
|
long pubs_simrel = spark.read().load(testOutputBasePath + "/" + testActionSetId + "/publication_simrel").count();
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
assertEquals(3288, orgs_simrel);
|
|
|
|
assertEquals(7260, pubs_simrel);
|
2020-04-01 18:48:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:30:31 +02:00
|
|
|
@Test
|
|
|
|
@Order(2)
|
2020-04-16 12:13:26 +02:00
|
|
|
public void createMergeRelsTest() throws Exception {
|
2020-04-06 16:30:31 +02:00
|
|
|
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils.toString(
|
2020-04-16 12:13:26 +02:00
|
|
|
SparkCreateMergeRels.class.getResourceAsStream("/eu/dnetlib/dhp/oa/dedup/createCC_parameters.json")));
|
2020-04-06 16:30:31 +02:00
|
|
|
parser.parseArgument(new String[]{
|
|
|
|
"-mt", "local[*]",
|
|
|
|
"-i", testGraphBasePath,
|
|
|
|
"-asi", testActionSetId,
|
|
|
|
"-la", "lookupurl",
|
|
|
|
"-w", testOutputBasePath});
|
|
|
|
|
2020-04-16 12:13:26 +02:00
|
|
|
new SparkCreateMergeRels(parser, spark).run(isLookUpService);
|
2020-04-06 16:30:31 +02:00
|
|
|
|
|
|
|
long orgs_mergerel = spark.read().load(testOutputBasePath + "/" + testActionSetId + "/organization_mergerel").count();
|
|
|
|
long pubs_mergerel = spark.read().load(testOutputBasePath + "/" + testActionSetId + "/publication_mergerel").count();
|
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
assertEquals(1244, orgs_mergerel);
|
|
|
|
assertEquals(1460, pubs_mergerel);
|
2020-04-06 16:30:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Order(3)
|
2020-04-08 18:02:30 +02:00
|
|
|
public void createDedupRecordTest() throws Exception {
|
2020-04-06 16:30:31 +02:00
|
|
|
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils.toString(
|
|
|
|
SparkCreateDedupRecord.class.getResourceAsStream("/eu/dnetlib/dhp/oa/dedup/createDedupRecord_parameters.json")));
|
|
|
|
parser.parseArgument(new String[]{
|
|
|
|
"-mt", "local[*]",
|
|
|
|
"-i", testGraphBasePath,
|
|
|
|
"-asi", testActionSetId,
|
|
|
|
"-la", "lookupurl",
|
|
|
|
"-w", testOutputBasePath});
|
|
|
|
|
|
|
|
new SparkCreateDedupRecord(parser, spark).run(isLookUpService);
|
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
long orgs_deduprecord = jsc.textFile(testOutputBasePath + "/" + testActionSetId + "/organization_deduprecord").count();
|
|
|
|
long pubs_deduprecord = jsc.textFile(testOutputBasePath + "/" + testActionSetId + "/publication_deduprecord").count();
|
2020-04-06 16:30:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
assertEquals(82, orgs_deduprecord);
|
|
|
|
assertEquals(66, pubs_deduprecord);
|
2020-04-06 16:30:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Order(4)
|
|
|
|
public void updateEntityTest() throws Exception {
|
|
|
|
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils.toString(
|
|
|
|
SparkUpdateEntity.class.getResourceAsStream("/eu/dnetlib/dhp/oa/dedup/updateEntity_parameters.json")));
|
|
|
|
parser.parseArgument(new String[]{
|
|
|
|
"-mt", "local[*]",
|
|
|
|
"-i", testGraphBasePath,
|
|
|
|
"-w", testOutputBasePath,
|
|
|
|
"-o", testDedupGraphBasePath
|
|
|
|
});
|
|
|
|
|
|
|
|
new SparkUpdateEntity(parser, spark).run(isLookUpService);
|
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
long organizations = jsc.textFile(testDedupGraphBasePath + "/organization").count();
|
|
|
|
long publications = jsc.textFile(testDedupGraphBasePath + "/publication").count();
|
|
|
|
|
|
|
|
long mergedOrgs = spark
|
|
|
|
.read().load(testOutputBasePath + "/" + testActionSetId + "/organization_mergerel").as(Encoders.bean(Relation.class))
|
|
|
|
.where("relClass=='merges'")
|
|
|
|
.javaRDD()
|
|
|
|
.map(Relation::getTarget)
|
|
|
|
.distinct().count();
|
|
|
|
|
|
|
|
long mergedPubs = spark
|
|
|
|
.read().load(testOutputBasePath + "/" + testActionSetId + "/publication_mergerel").as(Encoders.bean(Relation.class))
|
|
|
|
.where("relClass=='merges'")
|
|
|
|
.javaRDD()
|
|
|
|
.map(Relation::getTarget)
|
|
|
|
.distinct().count();
|
2020-04-06 16:30:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
assertEquals(897, publications);
|
|
|
|
assertEquals(835, organizations);
|
2020-04-06 16:30:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
long deletedOrgs = jsc.textFile(testDedupGraphBasePath + "/organization")
|
|
|
|
.filter(this::isDeletedByInference).count();
|
|
|
|
long deletedPubs = jsc.textFile(testDedupGraphBasePath + "/publication")
|
|
|
|
.filter(this::isDeletedByInference).count();
|
|
|
|
|
|
|
|
assertEquals(mergedOrgs, deletedOrgs);
|
|
|
|
assertEquals(mergedPubs, deletedPubs);
|
2020-04-06 16:30:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Order(5)
|
|
|
|
public void propagateRelationTest() throws Exception {
|
|
|
|
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils.toString(
|
|
|
|
SparkPropagateRelation.class.getResourceAsStream("/eu/dnetlib/dhp/oa/dedup/propagateRelation_parameters.json")));
|
|
|
|
parser.parseArgument(new String[]{
|
|
|
|
"-mt", "local[*]",
|
|
|
|
"-i", testGraphBasePath,
|
|
|
|
"-w", testOutputBasePath,
|
|
|
|
"-o", testDedupGraphBasePath
|
|
|
|
});
|
|
|
|
|
|
|
|
new SparkPropagateRelation(parser, spark).run(isLookUpService);
|
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
long relations = jsc.textFile(testDedupGraphBasePath + "/relation").count();
|
2020-04-06 16:30:31 +02:00
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
assertEquals(826, relations);
|
2020-04-06 16:30:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:13:26 +02:00
|
|
|
@AfterAll
|
|
|
|
public static void finalCleanUp() throws IOException {
|
|
|
|
FileUtils.deleteDirectory(new File(testOutputBasePath));
|
|
|
|
FileUtils.deleteDirectory(new File(testDedupGraphBasePath));
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:02:30 +02:00
|
|
|
public boolean isDeletedByInference(String s) {
|
|
|
|
return s.contains("\"deletedbyinference\":true");
|
2020-04-02 18:37:53 +02:00
|
|
|
}
|
2020-04-01 18:48:31 +02:00
|
|
|
}
|