Skip to content

Commit

Permalink
test: add tests for DDI exports for datasets with PermaLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
vera committed Aug 27, 2024
1 parent f6e1b06 commit 46de8fb
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package edu.harvard.iq.dataverse.export.ddi;

import edu.harvard.iq.dataverse.pidproviders.PidProviderFactory;
import edu.harvard.iq.dataverse.pidproviders.PidUtil;
import edu.harvard.iq.dataverse.pidproviders.doi.datacite.DataCiteDOIProvider;
import edu.harvard.iq.dataverse.pidproviders.doi.datacite.DataCiteProviderFactory;
import edu.harvard.iq.dataverse.pidproviders.perma.PermaLinkPidProvider;
import edu.harvard.iq.dataverse.pidproviders.perma.PermaLinkProviderFactory;
import edu.harvard.iq.dataverse.settings.JvmSettings;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.testing.JvmSetting;
import edu.harvard.iq.dataverse.util.testing.LocalJvmSettings;
import edu.harvard.iq.dataverse.util.xml.XmlPrinter;

import java.io.ByteArrayOutputStream;
Expand All @@ -11,12 +20,17 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import edu.harvard.iq.dataverse.util.xml.html.HtmlPrinter;

import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -33,6 +47,30 @@
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@LocalJvmSettings
//Perma 1
@JvmSetting(key = JvmSettings.PID_PROVIDER_LABEL, value = "perma 1", varArgs = "perma1")
@JvmSetting(key = JvmSettings.PID_PROVIDER_TYPE, value = PermaLinkPidProvider.TYPE, varArgs = "perma1")
@JvmSetting(key = JvmSettings.PID_PROVIDER_AUTHORITY, value = "PERM", varArgs = "perma1")
@JvmSetting(key = JvmSettings.PERMALINK_BASE_URL, value = "https://example.org", varArgs = "perma1")
//Perma 2
@JvmSetting(key = JvmSettings.PID_PROVIDER_LABEL, value = "perma 2", varArgs = "perma2")
@JvmSetting(key = JvmSettings.PID_PROVIDER_TYPE, value = PermaLinkPidProvider.TYPE, varArgs = "perma2")
@JvmSetting(key = JvmSettings.PID_PROVIDER_AUTHORITY, value = "PERM2", varArgs = "perma2")
@JvmSetting(key = JvmSettings.PERMALINK_SEPARATOR, value = "-", varArgs = "perma2")
@JvmSetting(key = JvmSettings.PERMALINK_BASE_URL, value = "https://example.org", varArgs = "perma2")
// Datacite 1
@JvmSetting(key = JvmSettings.PID_PROVIDER_LABEL, value = "dataCite 1", varArgs = "dc1")
@JvmSetting(key = JvmSettings.PID_PROVIDER_TYPE, value = DataCiteDOIProvider.TYPE, varArgs = "dc1")
@JvmSetting(key = JvmSettings.PID_PROVIDER_AUTHORITY, value = "10.5072", varArgs = "dc1")
@JvmSetting(key = JvmSettings.PID_PROVIDER_SHOULDER, value = "FK2", varArgs = "dc1")
@JvmSetting(key = JvmSettings.DATACITE_MDS_API_URL, value = "https://mds.test.datacite.org/", varArgs = "dc1")
@JvmSetting(key = JvmSettings.DATACITE_REST_API_URL, value = "https://api.test.datacite.org", varArgs ="dc1")
@JvmSetting(key = JvmSettings.DATACITE_USERNAME, value = "test", varArgs ="dc1")
@JvmSetting(key = JvmSettings.DATACITE_PASSWORD, value = "changeme", varArgs ="dc1")

//List to instantiate
@JvmSetting(key = JvmSettings.PID_PROVIDERS, value = "perma1, perma2, dc1")
public class DdiExportUtilTest {

private static final Logger logger = Logger.getLogger(DdiExportUtilTest.class.getCanonicalName());
Expand All @@ -45,6 +83,25 @@ void setup() {
Mockito.lenient().when(settingsSvc.isTrueForKey(SettingsServiceBean.Key.ExportInstallationAsDistributorOnlyWhenNotSet, false)).thenReturn(false);
DdiExportUtil.injectSettingsService(settingsSvc);
}

@BeforeAll
public static void setUpClass() throws Exception {
Map<String, PidProviderFactory> pidProviderFactoryMap = new HashMap<>();
pidProviderFactoryMap.put(PermaLinkPidProvider.TYPE, new PermaLinkProviderFactory());
pidProviderFactoryMap.put(DataCiteDOIProvider.TYPE, new DataCiteProviderFactory());

PidUtil.clearPidProviders();

//Read list of providers to add
List<String> providers = Arrays.asList(JvmSettings.PID_PROVIDERS.lookup().split(",\\s"));
//Iterate through the list of providers and add them using the PidProviderFactory of the appropriate type
for (String providerId : providers) {
System.out.println("Loading provider: " + providerId);
String type = JvmSettings.PID_PROVIDER_TYPE.lookup(providerId);
PidProviderFactory factory = pidProviderFactoryMap.get(type);
PidUtil.addToProviderList(factory.createPidProvider(providerId));
}
}


@Test
Expand All @@ -64,6 +121,42 @@ public void testJson2DdiNoFiles() throws Exception {
XmlAssert.assertThat(result).and(datasetAsDdi).ignoreWhitespace().areSimilar();
}


@Test
public void testJson2DdiPermaLink() throws Exception {
// given
Path datasetVersionJson = Path.of("src/test/java/edu/harvard/iq/dataverse/export/ddi/dataset-perma.json");
String datasetVersionAsJson = Files.readString(datasetVersionJson, StandardCharsets.UTF_8);
Path ddiFile = Path.of("src/test/java/edu/harvard/iq/dataverse/export/ddi/dataset-perma.xml");
String datasetAsDdi = XmlPrinter.prettyPrintXml(Files.readString(ddiFile, StandardCharsets.UTF_8));
logger.fine(datasetAsDdi);

// when
String result = DdiExportUtil.datasetDtoAsJson2ddi(datasetVersionAsJson);
logger.fine(result);

// then
XmlAssert.assertThat(result).and(datasetAsDdi).ignoreWhitespace().areSimilar();
}


@Test
public void testJson2DdiPermaLinkWithSeparator() throws Exception {
// given
Path datasetVersionJson = Path.of("src/test/java/edu/harvard/iq/dataverse/export/ddi/dataset-perma-w-separator.json");
String datasetVersionAsJson = Files.readString(datasetVersionJson, StandardCharsets.UTF_8);
Path ddiFile = Path.of("src/test/java/edu/harvard/iq/dataverse/export/ddi/dataset-perma-w-separator.xml");
String datasetAsDdi = XmlPrinter.prettyPrintXml(Files.readString(ddiFile, StandardCharsets.UTF_8));
logger.fine(datasetAsDdi);

// when
String result = DdiExportUtil.datasetDtoAsJson2ddi(datasetVersionAsJson);
logger.fine(result);

// then
XmlAssert.assertThat(result).and(datasetAsDdi).ignoreWhitespace().areSimilar();
}

@Test
public void testExportDDI() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"id": 10,
"identifier": "123456789",
"persistentUrl": "https://example.org/citation?persistentId=PERM2-123456789",
"protocol": "perma",
"authority": "PERM2",
"separator": "-",
"datasetVersion": {
"id": 1,
"versionNumber": 1,
"versionMinorNumber": 0,
"versionState": "RELEASED",
"productionDate": "Production Date",
"lastUpdateTime": "2015-09-29T17:47:35Z",
"releaseTime": "2015-09-29T17:47:35Z",
"createTime": "2015-09-24T16:47:50Z",
"metadataBlocks": {
"citation": {
"displayName": "Citation Metadata",
"fields": [
{
"typeName": "title",
"multiple": false,
"typeClass": "primitive",
"value": "Spruce Goose"
},
{
"typeName": "author",
"multiple": true,
"typeClass": "compound",
"value": [
{
"authorName": {
"typeName": "authorName",
"multiple": false,
"typeClass": "primitive",
"value": "Spruce, Sabrina"
}
}
]
},
{
"typeName": "datasetContact",
"multiple": true,
"typeClass": "compound",
"value": [
{
"datasetContactEmail": {
"typeName": "datasetContactEmail",
"multiple": false,
"typeClass": "primitive",
"value": "[email protected]"
}
}
]
},
{
"typeName": "dsDescription",
"multiple": true,
"typeClass": "compound",
"value": [
{
"dsDescriptionValue": {
"typeName": "dsDescriptionValue",
"multiple": false,
"typeClass": "primitive",
"value": "What the Spruce Goose was really made of."
}
}
]
},
{
"typeName": "subject",
"multiple": true,
"typeClass": "controlledVocabulary",
"value": [
"Other"
]
},
{
"typeName": "depositor",
"multiple": false,
"typeClass": "primitive",
"value": "Spruce, Sabrina"
}
]
}
},
"files": [],
"citation": "Spruce, Sabrina, 2015, \"Spruce Goose\", https://example.org/citation?persistentId=PERM2-123456789, Root Dataverse, V1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<codeBook xmlns="ddi:codebook:2_5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ddi:codebook:2_5 http://www.ddialliance.org/Specification/DDI-Codebook/2.5/XMLSchema/codebook.xsd" version="2.5">
<docDscr>
<citation>
<titlStmt>
<titl>Spruce Goose</titl>
<IDNo agency="perma">perma:PERM2-123456789</IDNo>
</titlStmt>
<distStmt/>
<verStmt source="archive">
<version date="2015-09-29" type="RELEASED">1</version>
</verStmt>
<biblCit>Spruce, Sabrina, 2015, "Spruce Goose", https://example.org/citation?persistentId=PERM2-123456789, Root Dataverse, V1</biblCit>
</citation>
</docDscr>
<stdyDscr>
<citation>
<titlStmt>
<titl>Spruce Goose</titl>
<IDNo agency="perma">perma:PERM2-123456789</IDNo>
</titlStmt>
<rspStmt>
<AuthEnty>Spruce, Sabrina</AuthEnty>
</rspStmt>
<prodStmt/>
<distStmt>
<depositr>Spruce, Sabrina</depositr>
</distStmt>
<holdings URI="https://example.org/citation?persistentId=perma:PERM2-123456789"/>
</citation>
<stdyInfo>
<subject>
<keyword xml:lang="en">Other</keyword>
</subject>
<abstract>What the Spruce Goose was really made of.</abstract>
<sumDscr/>
</stdyInfo>
<method>
<dataColl>
<sources/>
</dataColl>
<anlyInfo/>
</method>
<dataAccs>
<setAvail/>
<useStmt/>
</dataAccs>
<othrStdyMat/>
</stdyDscr>
</codeBook>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"id": 10,
"identifier": "123456789",
"persistentUrl": "https://example.org/citation?persistentId=PERM123456789",
"protocol": "perma",
"authority": "PERM",
"separator": "",
"datasetVersion": {
"id": 1,
"versionNumber": 1,
"versionMinorNumber": 0,
"versionState": "RELEASED",
"productionDate": "Production Date",
"lastUpdateTime": "2015-09-29T17:47:35Z",
"releaseTime": "2015-09-29T17:47:35Z",
"createTime": "2015-09-24T16:47:50Z",
"metadataBlocks": {
"citation": {
"displayName": "Citation Metadata",
"fields": [
{
"typeName": "title",
"multiple": false,
"typeClass": "primitive",
"value": "Spruce Goose"
},
{
"typeName": "author",
"multiple": true,
"typeClass": "compound",
"value": [
{
"authorName": {
"typeName": "authorName",
"multiple": false,
"typeClass": "primitive",
"value": "Spruce, Sabrina"
}
}
]
},
{
"typeName": "datasetContact",
"multiple": true,
"typeClass": "compound",
"value": [
{
"datasetContactEmail": {
"typeName": "datasetContactEmail",
"multiple": false,
"typeClass": "primitive",
"value": "[email protected]"
}
}
]
},
{
"typeName": "dsDescription",
"multiple": true,
"typeClass": "compound",
"value": [
{
"dsDescriptionValue": {
"typeName": "dsDescriptionValue",
"multiple": false,
"typeClass": "primitive",
"value": "What the Spruce Goose was really made of."
}
}
]
},
{
"typeName": "subject",
"multiple": true,
"typeClass": "controlledVocabulary",
"value": [
"Other"
]
},
{
"typeName": "depositor",
"multiple": false,
"typeClass": "primitive",
"value": "Spruce, Sabrina"
}
]
}
},
"files": [],
"citation": "Spruce, Sabrina, 2015, \"Spruce Goose\", https://example.org/citation?persistentId=PERM123456789, Root Dataverse, V1"
}
}
Loading

0 comments on commit 46de8fb

Please sign in to comment.