2021-02-26 10:19:28 +01:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
|
|
|
|
|
|
|
import static java.nio.file.Files.createTempDirectory;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2021-04-07 18:42:16 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2021-02-26 10:19:28 +01:00
|
|
|
import static org.mockito.Mockito.lenient;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.nio.file.Paths;
|
2021-04-07 18:42:16 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2021-02-26 10:19:28 +01:00
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
2021-04-07 18:42:16 +02:00
|
|
|
import org.apache.spark.api.java.JavaPairRDD;
|
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
2021-02-26 10:19:28 +01:00
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
2021-04-07 18:42:16 +02:00
|
|
|
import org.apache.spark.api.java.function.PairFunction;
|
2021-04-06 14:31:00 +02:00
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.Row;
|
2021-02-26 10:19:28 +01:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2021-04-07 14:27:43 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
2021-02-26 10:19:28 +01:00
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
2021-04-07 18:42:16 +02:00
|
|
|
import eu.dnetlib.pace.util.MapDocumentUtil;
|
|
|
|
import scala.Tuple2;
|
2021-02-26 10:19:28 +01:00
|
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
2021-04-07 18:42:16 +02:00
|
|
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
|
|
|
public class SparkOpenorgsProvisionTest implements Serializable {
|
2021-02-26 10:19:28 +01:00
|
|
|
|
|
|
|
@Mock(serializable = true)
|
|
|
|
ISLookUpService isLookUpService;
|
|
|
|
|
|
|
|
private static SparkSession spark;
|
|
|
|
private static JavaSparkContext jsc;
|
|
|
|
|
|
|
|
private static String testGraphBasePath;
|
|
|
|
private static String testOutputBasePath;
|
|
|
|
private static String testDedupGraphBasePath;
|
|
|
|
private static final String testActionSetId = "test-orchestrator";
|
|
|
|
|
|
|
|
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
|
|
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
public static void cleanUp() throws IOException, URISyntaxException {
|
|
|
|
|
|
|
|
testGraphBasePath = Paths
|
2021-04-07 18:42:16 +02:00
|
|
|
.get(SparkOpenorgsProvisionTest.class.getResource("/eu/dnetlib/dhp/dedup/openorgs/provision").toURI())
|
2021-02-26 10:19:28 +01:00
|
|
|
.toFile()
|
|
|
|
.getAbsolutePath();
|
2021-04-07 18:42:16 +02:00
|
|
|
testOutputBasePath = createTempDirectory(SparkOpenorgsProvisionTest.class.getSimpleName() + "-")
|
2021-02-26 10:19:28 +01:00
|
|
|
.toAbsolutePath()
|
|
|
|
.toString();
|
2021-04-07 18:42:16 +02:00
|
|
|
testDedupGraphBasePath = createTempDirectory(SparkOpenorgsProvisionTest.class.getSimpleName() + "-")
|
2021-02-26 10:19:28 +01:00
|
|
|
.toAbsolutePath()
|
|
|
|
.toString();
|
|
|
|
|
2021-03-19 16:57:40 +01:00
|
|
|
FileUtils.deleteDirectory(new File(testOutputBasePath));
|
2021-02-26 10:19:28 +01:00
|
|
|
FileUtils.deleteDirectory(new File(testDedupGraphBasePath));
|
|
|
|
|
|
|
|
final SparkConf conf = new SparkConf();
|
|
|
|
conf.set("spark.sql.shuffle.partitions", "200");
|
|
|
|
spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(SparkDedupTest.class.getSimpleName())
|
|
|
|
.master("local[*]")
|
|
|
|
.config(conf)
|
|
|
|
.getOrCreate();
|
|
|
|
|
|
|
|
jsc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
public void setUp() throws IOException, ISLookUpException {
|
|
|
|
|
|
|
|
lenient()
|
|
|
|
.when(isLookUpService.getResourceProfileByQuery(Mockito.contains(testActionSetId)))
|
|
|
|
.thenReturn(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkDedupTest.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/dedup/profiles/mock_orchestrator_openorgs.xml")));
|
|
|
|
|
|
|
|
lenient()
|
|
|
|
.when(isLookUpService.getResourceProfileByQuery(Mockito.contains("organization")))
|
|
|
|
.thenReturn(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkDedupTest.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/dedup/conf/org.curr.conf.json")));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-04-07 18:42:16 +02:00
|
|
|
@Order(1)
|
|
|
|
public void copyOpenorgsMergeRelTest() throws Exception {
|
|
|
|
|
2021-02-26 10:19:28 +01:00
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkCopyOpenorgsMergeRels.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/dedup/copyOpenorgsMergeRels_parameters.json")));
|
|
|
|
parser
|
|
|
|
.parseArgument(
|
|
|
|
new String[] {
|
2021-04-07 18:42:16 +02:00
|
|
|
"-i",
|
|
|
|
testGraphBasePath,
|
|
|
|
"-asi",
|
|
|
|
testActionSetId,
|
|
|
|
"-la",
|
|
|
|
"lookupurl",
|
|
|
|
"-w",
|
|
|
|
testOutputBasePath
|
2021-02-26 10:19:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
new SparkCopyOpenorgsMergeRels(parser, spark).run(isLookUpService);
|
|
|
|
|
|
|
|
long orgs_mergerel = spark
|
|
|
|
.read()
|
2021-04-06 14:31:00 +02:00
|
|
|
.load(DedupUtility.createMergeRelPath(testOutputBasePath, testActionSetId, "organization"))
|
2021-02-26 10:19:28 +01:00
|
|
|
.count();
|
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
assertEquals(140, orgs_mergerel);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Order(2)
|
|
|
|
public void createOrgsDedupRecordTest() throws Exception {
|
|
|
|
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkCopyOpenorgsMergeRels.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/dedup/copyOpenorgs_parameters.json")));
|
|
|
|
parser
|
|
|
|
.parseArgument(
|
|
|
|
new String[] {
|
|
|
|
"-i",
|
|
|
|
testGraphBasePath,
|
|
|
|
"-asi",
|
|
|
|
testActionSetId,
|
2021-04-13 09:08:05 +02:00
|
|
|
"-la",
|
|
|
|
"lookupurl",
|
2021-04-07 18:42:16 +02:00
|
|
|
"-w",
|
|
|
|
testOutputBasePath
|
|
|
|
});
|
2021-04-06 14:31:00 +02:00
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
new SparkCreateOrgsDedupRecord(parser, spark).run(isLookUpService);
|
2021-04-06 14:31:00 +02:00
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
long orgs_deduprecord = spark
|
|
|
|
.read()
|
|
|
|
.json(DedupUtility.createDedupRecordPath(testOutputBasePath, testActionSetId, "organization"))
|
|
|
|
.count();
|
2021-02-26 10:19:28 +01:00
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
assertEquals(10, orgs_deduprecord);
|
2021-02-26 10:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-04-07 18:42:16 +02:00
|
|
|
@Order(3)
|
|
|
|
public void updateEntityTest() throws Exception {
|
|
|
|
|
2021-02-26 10:19:28 +01:00
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
2021-04-07 18:42:16 +02:00
|
|
|
SparkUpdateEntity.class
|
2021-02-26 10:19:28 +01:00
|
|
|
.getResourceAsStream(
|
2021-04-07 18:42:16 +02:00
|
|
|
"/eu/dnetlib/dhp/oa/dedup/updateEntity_parameters.json")));
|
2021-02-26 10:19:28 +01:00
|
|
|
parser
|
|
|
|
.parseArgument(
|
|
|
|
new String[] {
|
2021-04-07 18:42:16 +02:00
|
|
|
"-i", testGraphBasePath, "-w", testOutputBasePath, "-o", testDedupGraphBasePath
|
2021-02-26 10:19:28 +01:00
|
|
|
});
|
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
new SparkUpdateEntity(parser, spark).run(isLookUpService);
|
|
|
|
|
|
|
|
long organizations = jsc.textFile(testDedupGraphBasePath + "/organization").count();
|
2021-02-26 10:19:28 +01:00
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
long mergedOrgs = spark
|
2021-02-26 10:19:28 +01:00
|
|
|
.read()
|
2021-04-07 18:42:16 +02:00
|
|
|
.load(testOutputBasePath + "/" + testActionSetId + "/organization_mergerel")
|
|
|
|
.as(Encoders.bean(Relation.class))
|
|
|
|
.where("relClass=='merges'")
|
|
|
|
.javaRDD()
|
|
|
|
.map(Relation::getTarget)
|
|
|
|
.distinct()
|
2021-02-26 10:19:28 +01:00
|
|
|
.count();
|
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
assertEquals(80, organizations);
|
|
|
|
|
|
|
|
long deletedOrgs = jsc
|
|
|
|
.textFile(testDedupGraphBasePath + "/organization")
|
|
|
|
.filter(this::isDeletedByInference)
|
|
|
|
.count();
|
|
|
|
|
|
|
|
assertEquals(mergedOrgs, deletedOrgs);
|
2021-02-26 10:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-04-07 18:42:16 +02:00
|
|
|
@Order(4)
|
2021-02-26 10:19:28 +01:00
|
|
|
public void copyRelationsNoOpenorgsTest() throws Exception {
|
2021-04-07 18:42:16 +02:00
|
|
|
|
2021-02-26 10:19:28 +01:00
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkCopyRelationsNoOpenorgs.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/dedup/updateEntity_parameters.json")));
|
|
|
|
parser
|
|
|
|
.parseArgument(
|
|
|
|
new String[] {
|
2021-04-07 18:42:16 +02:00
|
|
|
"-i", testGraphBasePath, "-w", testOutputBasePath, "-o", testDedupGraphBasePath
|
2021-02-26 10:19:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
new SparkCopyRelationsNoOpenorgs(parser, spark).run(isLookUpService);
|
|
|
|
|
|
|
|
long relations = jsc.textFile(testDedupGraphBasePath + "/relation").count();
|
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
assertEquals(2380, relations);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Order(5)
|
|
|
|
public void propagateRelationsTest() throws Exception {
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkPropagateRelation.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/dedup/propagateRelation_parameters.json")));
|
|
|
|
parser
|
|
|
|
.parseArgument(
|
|
|
|
new String[] {
|
|
|
|
"-i", testGraphBasePath, "-w", testOutputBasePath, "-o", testDedupGraphBasePath
|
|
|
|
});
|
|
|
|
|
|
|
|
new SparkPropagateRelation(parser, spark).run(isLookUpService);
|
|
|
|
|
|
|
|
long relations = jsc.textFile(testDedupGraphBasePath + "/relation").count();
|
|
|
|
|
|
|
|
assertEquals(2520, relations);
|
|
|
|
|
|
|
|
// check deletedbyinference
|
|
|
|
final Dataset<Relation> mergeRels = spark
|
|
|
|
.read()
|
|
|
|
.load(DedupUtility.createMergeRelPath(testOutputBasePath, "*", "*"))
|
|
|
|
.as(Encoders.bean(Relation.class));
|
|
|
|
final JavaPairRDD<String, String> mergedIds = mergeRels
|
|
|
|
.where("relClass == 'merges'")
|
|
|
|
.select(mergeRels.col("target"))
|
|
|
|
.distinct()
|
|
|
|
.toJavaRDD()
|
|
|
|
.mapToPair(
|
|
|
|
(PairFunction<Row, String, String>) r -> new Tuple2<String, String>(r.getString(0), "d"));
|
|
|
|
|
|
|
|
JavaRDD<String> toCheck = jsc
|
|
|
|
.textFile(testDedupGraphBasePath + "/relation")
|
|
|
|
.mapToPair(json -> new Tuple2<>(MapDocumentUtil.getJPathString("$.source", json), json))
|
|
|
|
.join(mergedIds)
|
|
|
|
.map(t -> t._2()._1())
|
|
|
|
.mapToPair(json -> new Tuple2<>(MapDocumentUtil.getJPathString("$.target", json), json))
|
|
|
|
.join(mergedIds)
|
|
|
|
.map(t -> t._2()._1());
|
|
|
|
|
|
|
|
long deletedbyinference = toCheck.filter(this::isDeletedByInference).count();
|
|
|
|
long updated = toCheck.count();
|
|
|
|
|
|
|
|
assertEquals(updated, deletedbyinference);
|
2021-02-26 10:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@AfterAll
|
|
|
|
public static void finalCleanUp() throws IOException {
|
|
|
|
FileUtils.deleteDirectory(new File(testOutputBasePath));
|
|
|
|
FileUtils.deleteDirectory(new File(testDedupGraphBasePath));
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:42:16 +02:00
|
|
|
public boolean isDeletedByInference(String s) {
|
|
|
|
return s.contains("\"deletedbyinference\":true");
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:19:28 +01:00
|
|
|
}
|