Skip to content

Commit

Permalink
increase code coverage of openaire exporter IQSS#4257 IQSS#3697
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed May 31, 2018
1 parent 272290c commit 5336e67
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 130 deletions.
146 changes: 16 additions & 130 deletions src/test/java/edu/harvard/iq/dataverse/export/OpenAireExporterTest.java
Original file line number Diff line number Diff line change
@@ -1,127 +1,27 @@
package edu.harvard.iq.dataverse.export;

import edu.harvard.iq.dataverse.ControlledVocabularyValue;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.DatasetFieldType;
import com.jayway.restassured.path.xml.XmlPath;
import edu.harvard.iq.dataverse.DatasetVersion;
import edu.harvard.iq.dataverse.Dataverse;
import static edu.harvard.iq.dataverse.util.SystemConfig.SITE_URL;
import edu.harvard.iq.dataverse.util.json.JsonParser;
import edu.harvard.iq.dataverse.util.xml.XmlPrinter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import static org.junit.Assert.*;

public class OpenAireExporterTest {

private final OpenAireExporter openAireExporter;
DDIExporterTest.MockDatasetFieldSvc datasetFieldTypeSvc = null;

public OpenAireExporterTest() {
openAireExporter = new OpenAireExporter();
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {

datasetFieldTypeSvc = new DDIExporterTest.MockDatasetFieldSvc();

DatasetFieldType titleType = datasetFieldTypeSvc.add(new DatasetFieldType("title", DatasetFieldType.FieldType.TEXTBOX, false));
DatasetFieldType authorType = datasetFieldTypeSvc.add(new DatasetFieldType("author", DatasetFieldType.FieldType.TEXT, true));
Set<DatasetFieldType> authorChildTypes = new HashSet<>();
authorChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("authorName", DatasetFieldType.FieldType.TEXT, false)));
authorChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("authorAffiliation", DatasetFieldType.FieldType.TEXT, false)));
authorChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("authorIdentifier", DatasetFieldType.FieldType.TEXT, false)));
DatasetFieldType authorIdentifierSchemeType = datasetFieldTypeSvc.add(new DatasetFieldType("authorIdentifierScheme", DatasetFieldType.FieldType.TEXT, false));
authorIdentifierSchemeType.setAllowControlledVocabulary(true);
authorIdentifierSchemeType.setControlledVocabularyValues(Arrays.asList(
// Why aren't these enforced? Should be ORCID, etc.
new ControlledVocabularyValue(1l, "ark", authorIdentifierSchemeType),
new ControlledVocabularyValue(2l, "doi", authorIdentifierSchemeType),
new ControlledVocabularyValue(3l, "url", authorIdentifierSchemeType)
));
authorChildTypes.add(datasetFieldTypeSvc.add(authorIdentifierSchemeType));
for (DatasetFieldType t : authorChildTypes) {
t.setParentDatasetFieldType(authorType);
}
authorType.setChildDatasetFieldTypes(authorChildTypes);

DatasetFieldType datasetContactType = datasetFieldTypeSvc.add(new DatasetFieldType("datasetContact", DatasetFieldType.FieldType.TEXT, true));
Set<DatasetFieldType> datasetContactTypes = new HashSet<>();
datasetContactTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("datasetContactEmail", DatasetFieldType.FieldType.TEXT, false)));
datasetContactTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("datasetContactName", DatasetFieldType.FieldType.TEXT, false)));
datasetContactTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("datasetContactAffiliation", DatasetFieldType.FieldType.TEXT, false)));
for (DatasetFieldType t : datasetContactTypes) {
t.setParentDatasetFieldType(datasetContactType);
}
datasetContactType.setChildDatasetFieldTypes(datasetContactTypes);

DatasetFieldType dsDescriptionType = datasetFieldTypeSvc.add(new DatasetFieldType("dsDescription", DatasetFieldType.FieldType.TEXT, true));
Set<DatasetFieldType> dsDescriptionTypes = new HashSet<>();
dsDescriptionTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("dsDescriptionValue", DatasetFieldType.FieldType.TEXT, false)));
for (DatasetFieldType t : dsDescriptionTypes) {
t.setParentDatasetFieldType(dsDescriptionType);
}
dsDescriptionType.setChildDatasetFieldTypes(dsDescriptionTypes);

DatasetFieldType keywordType = datasetFieldTypeSvc.add(new DatasetFieldType("keyword", DatasetFieldType.FieldType.TEXT, true));
DatasetFieldType descriptionType = datasetFieldTypeSvc.add(new DatasetFieldType("description", DatasetFieldType.FieldType.TEXTBOX, false));

DatasetFieldType subjectType = datasetFieldTypeSvc.add(new DatasetFieldType("subject", DatasetFieldType.FieldType.TEXT, true));
subjectType.setAllowControlledVocabulary(true);
subjectType.setControlledVocabularyValues(Arrays.asList(
new ControlledVocabularyValue(1l, "mgmt", subjectType),
new ControlledVocabularyValue(2l, "law", subjectType),
new ControlledVocabularyValue(3l, "cs", subjectType)
));

DatasetFieldType pubIdType = datasetFieldTypeSvc.add(new DatasetFieldType("publicationIdType", DatasetFieldType.FieldType.TEXT, false));
pubIdType.setAllowControlledVocabulary(true);
pubIdType.setControlledVocabularyValues(Arrays.asList(
new ControlledVocabularyValue(1l, "ark", pubIdType),
new ControlledVocabularyValue(2l, "doi", pubIdType),
new ControlledVocabularyValue(3l, "url", pubIdType)
));

DatasetFieldType compoundSingleType = datasetFieldTypeSvc.add(new DatasetFieldType("coordinate", DatasetFieldType.FieldType.TEXT, true));
Set<DatasetFieldType> childTypes = new HashSet<>();
childTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("lat", DatasetFieldType.FieldType.TEXT, false)));
childTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("lon", DatasetFieldType.FieldType.TEXT, false)));

for (DatasetFieldType t : childTypes) {
t.setParentDatasetFieldType(compoundSingleType);
}
compoundSingleType.setChildDatasetFieldTypes(childTypes);

}

@After
public void tearDown() {
}

/**
* Test of getProviderName method, of class OpenAireExporter.
*/
Expand Down Expand Up @@ -152,35 +52,21 @@ public void testGetDisplayName() {
@Test
public void testExportDataset() throws Exception {
System.out.println("exportDataset");

File datasetVersionJson = new File("src/test/resources/json/dataset-finch1.json");
// TODO: add more fields to json to increase code coverage.
File datasetVersionJson = new File("src/test/java/edu/harvard/iq/dataverse/export/dataset-spruce1.json");
String datasetVersionAsJson = new String(Files.readAllBytes(Paths.get(datasetVersionJson.getAbsolutePath())));

JsonReader jsonReader1 = Json.createReader(new StringReader(datasetVersionAsJson));
JsonObject json1 = jsonReader1.readObject();
JsonParser jsonParser = new JsonParser(datasetFieldTypeSvc, null, null);
DatasetVersion version = jsonParser.parseDatasetVersion(json1.getJsonObject("datasetVersion"));
version.setVersionState(DatasetVersion.VersionState.RELEASED);
SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
Date publicationDate = dateFmt.parse("19551105");
version.setReleaseTime(publicationDate);
version.setVersionNumber(1l);
// TODO: It might be nice to test TermsOfUseAndAccess some day
version.setTermsOfUseAndAccess(null);
Dataset dataset = new Dataset();
dataset.setProtocol("doi");
dataset.setAuthority("myAuthority");
dataset.setIdentifier("myIdentifier");
version.setDataset(dataset);
Dataverse dataverse = new Dataverse();
dataverse.setName("LibraScholar");
dataset.setOwner(dataverse);
System.setProperty(SITE_URL, "https://librascholar.org");

JsonReader jsonReader = Json.createReader(new StringReader(datasetVersionAsJson));
JsonObject jsonObject = jsonReader.readObject();
DatasetVersion nullVersion = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// TODO: Get this working.
// openAireExporter.exportDataset(version, json1, byteArrayOutputStream);

openAireExporter.exportDataset(nullVersion, jsonObject, byteArrayOutputStream);
String xmlOnOneLine = new String(byteArrayOutputStream.toByteArray());
String xmlAsString = XmlPrinter.prettyPrintXml(xmlOnOneLine);
System.out.println("XML: " + xmlAsString);
XmlPath xmlpath = XmlPath.from(xmlAsString);
assertEquals("Spruce Goose", xmlpath.getString("resource.titles.title"));
assertEquals("Spruce, Sabrina", xmlpath.getString("resource.creators.creator"));
assertEquals("1.0", xmlpath.getString("resource.version"));
}

/**
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/export/dataset-spruce1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"id": 10,
"identifier": "OZDVMO",
"persistentUrl": "https://doi.org/10.5072/FK2/OZDVMO",
"protocol": "doi",
"authority": "10.5072/FK2",
"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"
]
}
]
}
},
"files": [
],
"citation": "Spruce, Sabrina, 2015, \"Spruce Goose\", https://doi.org/10.5072/FK2/OZDVMO, Root Dataverse, V1"
}
}

0 comments on commit 5336e67

Please sign in to comment.