[DUMP CSV] test and resources for the result dumps

This commit is contained in:
Miriam Baglioni 2023-05-17 16:57:25 +02:00
parent 66873c1744
commit 21599598ae
1 changed files with 260 additions and 0 deletions

View File

@ -1,9 +1,269 @@
package eu.dnetlib.dhp.oa.graph.dump.csv;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.schema.common.ModelConstants;
import eu.dnetlib.dhp.utils.DHPUtils;
import org.apache.commons.io.FileUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
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.Optional;
import static org.apache.commons.lang3.StringUtils.split;
/**
* @author miriam.baglioni
* @Date 11/05/23
*/
public class DumpResultTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static SparkSession spark;
private static Path workingDir;
private static final Logger log = LoggerFactory
.getLogger(DumpResultTest.class);
private static HashMap<String, String> map = new HashMap<>();
@BeforeAll
public static void beforeAll() throws IOException {
workingDir = Files
.createTempDirectory(DumpResultTest.class.getSimpleName());
log.info("using work dir {}", workingDir);
SparkConf conf = new SparkConf();
conf.setAppName(DumpResultTest.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(DumpResultTest.class.getSimpleName())
.config(conf)
.getOrCreate();
}
@AfterAll
public static void afterAll() throws IOException {
FileUtils.deleteDirectory(workingDir.toFile());
spark.stop();
}
@Test
public void testDumpResult() throws Exception {
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/input/")
.getPath();
spark.read().text(getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/working/resultIds")
.getPath())
.write()
.text(workingDir.toString() + "/working/resultIds/");
SparkDumpResults.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/output",
"-workingPath", workingDir.toString() + "/working",
"-resultType", "publication",
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-sourcePath", sourcePath
});
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
Dataset<Row> tmp = spark.read().option("header", "true").option("delimiter", Constants.SEP)
.csv(workingDir.toString() + "/working/publication/result");
Assertions.assertEquals(3, tmp.count());
Row row = tmp
.where("id = '50|DansKnawCris::0224aae28af558f21768dbc6439c7a95'")
.first();
Assertions.assertEquals(ModelConstants.OPEN_ACCESS_RIGHT().getClassid(),row.getAs("accessright"));
Assertions.assertEquals("FI" ,row.getAs("country"));
Assertions.assertEquals("Lit.opg., bijl." ,row.getAs("description"));
Assertions.assertEquals(3 ,split(row.getAs("keywords"), ", ").length);
Assertions.assertTrue(row.getAs("keywords").toString().contains("archeologie"));
Assertions.assertTrue(row.getAs("keywords").toString().contains("prospectie"));
Assertions.assertTrue(row.getAs("keywords").toString().contains("archaeology"));
Assertions.assertEquals("nl", row.getAs("language"));
Assertions.assertEquals("2007-01-01", row.getAs("publication_date"));
Assertions.assertEquals("FakePublisher1", row.getAs("publisher"));
Assertions.assertEquals("Inventariserend veldonderzoek d.m.v. boringen (karterende fase) : Raadhuisstraat te Dirkshorn, gemeente Harenkarspel", row.getAs("title"));
Assertions.assertEquals("publication", row.getAs("type"));
row = tmp
.where("id = '50|DansKnawCris::20c414a3b1c742d5dd3851f1b67df2d9'")
.first();
Assertions.assertEquals(ModelConstants.OPEN_ACCESS_RIGHT().getClassid(),row.getAs("accessright"));
Assertions.assertEquals(2 ,split(row.getAs("country"), ", ").length);
Assertions.assertNull(row.getAs("description"));
Assertions.assertEquals(2 ,split(row.getAs("keywords"), ", ").length);
Assertions.assertTrue(row.getAs("keywords").toString().contains("archeologie"));
Assertions.assertTrue(row.getAs("keywords").toString().contains("archaeology"));
Assertions.assertEquals("UNKNOWN", row.getAs("language"));
Assertions.assertNull( row.getAs("publication_date"));
Assertions.assertNull( row.getAs("publisher"));
Assertions.assertEquals("None", row.getAs("title"));
Assertions.assertEquals("publication", row.getAs("type"));
row = tmp
.where("id = '50|DansKnawCris::26780065282e607306372abd0d808245'")
.first();
Assertions.assertEquals(ModelConstants.OPEN_ACCESS_RIGHT().getClassid(),row.getAs("accessright"));
Assertions.assertNull(row.getAs("country"));
Assertions.assertNull(row.getAs("description"));
Assertions.assertEquals(2 ,split(row.getAs("keywords"), ", ").length);
Assertions.assertTrue(row.getAs("keywords").toString().contains("archeologie"));
Assertions.assertTrue(row.getAs("keywords").toString().contains("archaeology"));
Assertions.assertEquals("UNKNOWN", row.getAs("language"));
Assertions.assertNull( row.getAs("publication_date"));
Assertions.assertNull( row.getAs("publisher"));
Assertions.assertEquals("None", row.getAs("title"));
Assertions.assertEquals("publication", row.getAs("type"));
}
@Test
public void testDumpAuthor() throws Exception {
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/input/")
.getPath();
spark.read().text(getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/working/resultIds")
.getPath())
.write()
.text(workingDir.toString() + "/working/resultIds/");
SparkDumpResults.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/output",
"-workingPath", workingDir.toString() + "/working",
"-resultType", "publication",
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-sourcePath", sourcePath
});
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
Dataset<Row> tmp = spark.read().option("header", "true").option("delimiter", Constants.SEP)
.csv(workingDir.toString() + "/working/publication/author");
Assertions.assertEquals(5, tmp.count());
Assertions.assertEquals(1,tmp.where("firstName == 'Maryam'").count());
Assertions.assertEquals(DHPUtils.md5("50|DansKnawCris::0224aae28af558f21768dbc6439c7a951"),tmp.where("firstName == 'Maryam'").first().getAs("id"));
Assertions.assertEquals(DHPUtils.md5("0000-0003-2914-2734"),tmp.where("firstName == 'Michael'").first().getAs("id"));
Assertions.assertEquals(DHPUtils.md5("50|DansKnawCris::20c414a3b1c742d5dd3851f1b67df2d92"),tmp.where("firstName == 'Mikhail'").first().getAs("id"));
}
@Test
public void testDumpResultAuthorRelations() throws Exception {
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/input/")
.getPath();
spark.read().text(getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/working/resultIds")
.getPath())
.write()
.text(workingDir.toString() + "/working/resultIds/");
SparkDumpResults.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/output",
"-workingPath", workingDir.toString() + "/working",
"-resultType", "publication",
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-sourcePath", sourcePath
});
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
Dataset<Row> tmp = spark.read().option("header", "true").option("delimiter", Constants.SEP)
.csv(workingDir.toString() + "/working/publication/result_author");
Assertions.assertEquals(6, tmp.count());
Assertions.assertEquals(2, tmp.where("author_id == '" + DHPUtils.md5("0000-0003-2914-2734") + "'").count());
Assertions.assertEquals(1, tmp.where("author_id == '" + DHPUtils.md5("0000-0003-2914-2734") + "'")
.where("result_id == '50|DansKnawCris::0224aae28af558f21768dbc6439c7a95'").count());
Assertions.assertEquals(1, tmp.where("author_id == '" + DHPUtils.md5("0000-0003-2914-2734") + "'")
.where("result_id == '50|DansKnawCris::20c414a3b1c742d5dd3851f1b67df2d9'").count());
}
@Test
public void testDumpResultPid() throws Exception {
final String sourcePath = getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/input/")
.getPath();
spark.read().text(getClass()
.getResource("/eu/dnetlib/dhp/oa/graph/dump/csv/working/resultIds")
.getPath())
.write()
.text(workingDir.toString() + "/working/resultIds/");
SparkDumpResults.main(new String[] {
"-isSparkSessionManaged", Boolean.FALSE.toString(),
"-outputPath", workingDir.toString() + "/output",
"-workingPath", workingDir.toString() + "/working",
"-resultType", "publication",
"-resultTableName", "eu.dnetlib.dhp.schema.oaf.Publication",
"-sourcePath", sourcePath
});
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
Dataset<Row> tmp = spark.read().option("header", "true").option("delimiter", Constants.SEP)
.csv(workingDir.toString() + "/working/publication/result_pid");
tmp.show(false);
Assertions.assertEquals(4, tmp.count());
Assertions.assertEquals(2, tmp.where("result_id == '50|DansKnawCris::0224aae28af558f21768dbc6439c7a95'").count());
Assertions.assertEquals("10.1023/fakedoi", tmp.where("result_id == '50|DansKnawCris::0224aae28af558f21768dbc6439c7a95' and type == 'doi'").first().getAs("pid"));
}
}