Skip to content

Commit

Permalink
Update tests to test sending datacite 4.0 XML.
Browse files Browse the repository at this point in the history
Plus general codebase cleanup and documentation.
  • Loading branch information
mbjones committed Mar 27, 2020
1 parent eaf6a2e commit 9a67b1d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 35 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,42 @@ service and then managing identifiers. Using the service requires an account
with the EZID service. For more information, see:
http://ezid.cdlib.org/doc/apidoc.html

Once you have added the library to your project classpath (see below), you can generate identifiers with EZID with simple Java calls. Here's a simple example to mint a new identifier using a DataCite 4.0 compliant metadata file in the current directory:

```java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import edu.ucsb.nceas.ezid.EZIDService;
import edu.ucsb.nceas.ezid.EZIDException;

public class EZIDExample {
public static void main(String args[]) {
try {
String USERNAME = "apitest";
String PASSWORD = "apitest";
String DOISHOULDER = "doi:10.5072/FK2";

EZIDService ezid = new EZIDService();
ezid.login(USERNAME, PASSWORD);

HashMap<String, String> metadata = new HashMap<String, String>();
String xml = new String(Files.readAllBytes((Paths.get("datacite.xml"))));
metadata.put("datacite", xml);
String testId = ezid.mintIdentifier(DOISHOULDER, metadata);
System.out.println("Generated: " + testId);
} catch (EZIDException e) {
System.err.println("Identifier minting failed: " + e.getMessage());
} catch (IOException e) {
System.err.println("Reading DataCiteXML failed: " + e.getMessage());
}
}
}
```

See the javadoc documentation for an overview of usage, as well as the
EZIDServiceTest.java JUnit class for examples of usage.
EZIDServiceTest.java JUnit class for more examples of usage.

See the License section below and LICENSE.txt for the details of distributing this software.

Expand Down Expand Up @@ -45,7 +79,7 @@ this snippet should work:
<dependency>
<groupId>edu.ucsb.nceas</groupId>
<artifactId>ezid</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<type>jar</type>
</dependency>
</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>edu.ucsb.nceas</groupId>
<artifactId>ezid</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>jar</packaging>

<name>ezid</name>
Expand All @@ -16,7 +16,7 @@
</properties>

<scm>
<url>https://github.com/mbjones/ezid.git</url>
<url>https://github.com/NCEAS/ezid.git</url>
</scm>
<organization>
<name>National Center for Ecological Analysis and Synthesis, UC Santa Barbara</name>
Expand Down Expand Up @@ -45,8 +45,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand Down
58 changes: 32 additions & 26 deletions src/test/java/edu/ucsb/nceas/ezid/test/EZIDServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public void setAndGetMetadata() {

try {
HashMap<String, String> metadata = ezid.getMetadata(testId);
for (String key : metadata.keySet()) {
log.debug(key + ": " + metadata.get(key));
}
for (String key : metadata.keySet()) {
log.debug(key + ": " + metadata.get(key));
}
String title = metadata.get(TITLEKEY);
log.debug("SetAndGet Title: " + title);
log.debug("SetAndGet Title: " + title);
if (title == null || !title.equals(TITLEVAL)) {
fail("GetMetadata failed: Title does not match.");
}
Expand All @@ -172,26 +172,30 @@ public void setAndGetMetadata() {
public void setAndGetDataCiteXML() {
String testId = null;

try {
HashMap<String, String> metadata = generateDataCiteXML("ToBeMinted");
testId = ezid.mintIdentifier(DOISHOULDER, metadata);
log.debug("SetAndGet: " + testId);
} catch (EZIDException e) {
fail("SetMetadata failed: " + e.getMessage());
}
String[] versions = {"3.1", "4.0"};

try {
HashMap<String, String> metadata = ezid.getMetadata(testId);
for (String key : metadata.keySet()) {
log.debug(key + ": " + metadata.get(key));
}
String xml = metadata.get("datacite");
log.debug("SetAndGet Datacite: " + xml);
if (xml == null || !xml.contains(testId.split("/")[1])) {
fail("GetMetadata failed: metadata does not contain submitted identifier.");
for (String version : versions) {
try {
HashMap<String, String> metadata = generateDataCiteXML("ToBeMinted", version);
testId = ezid.mintIdentifier(DOISHOULDER, metadata);
log.debug("SetAndGet: " + testId);
} catch (EZIDException e) {
fail("SetMetadata failed: " + e.getMessage());
}

try {
HashMap<String, String> metadata = ezid.getMetadata(testId);
for (String key : metadata.keySet()) {
log.debug(key + ": " + metadata.get(key));
}
String xml = metadata.get("datacite");
log.debug("SetAndGet Datacite: " + xml);
if (xml == null || !xml.contains(testId.split("/")[1])) {
fail("GetMetadata failed: metadata does not contain submitted identifier.");
}
} catch (EZIDException e) {
fail("GetMetadata failed: " + e.getMessage());
}
} catch (EZIDException e) {
fail("GetMetadata failed: " + e.getMessage());
}
}

Expand Down Expand Up @@ -231,19 +235,21 @@ public static HashMap<String, String> generateMetadata(String identifier) {
metadata.put("datacite.creator", creator);
String publisher = "EZID Java Library";
metadata.put("datacite.publisher", publisher);
String year = new Integer(Calendar.getInstance().get(Calendar.YEAR)).toString();
String year = Integer.valueOf(Calendar.getInstance().get(Calendar.YEAR)).toString();
metadata.put("datacite.publicationyear", year);
return metadata;
}

/**
* Generate DataCite compliant XML metadata for use in test data insertion.
* Generate DataCite compliant XML metadata for use in test data insertion. This
* test depends on a set of resource files on the classpath that are named according
* to the DataCite version, in the form "/datacite-4.0.xml".
*/
public HashMap<String, String> generateDataCiteXML(String identifier) {
public HashMap<String, String> generateDataCiteXML(String identifier, String version) {
HashMap<String, String> metadata = new HashMap<String, String>();
try {
Path tempFile = null;
URL url = getClass().getResource("/datacite-3.1.xml");
URL url = getClass().getResource("/datacite-" + version + ".xml");
if (url != null) {
tempFile = Paths.get(url.toURI());
} else {
Expand Down
47 changes: 47 additions & 0 deletions src/test/resources/datacite-4.0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<resource xmlns="http://datacite.org/schema/kernel-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datacite.org/schema/kernel-4 https://schema.datacite.org/meta/kernel-4.0/metadata.xsd">
<identifier identifierType="DOI">TBD</identifier>
<creators>
<creator>
<creatorName>United States Census Bureau</creatorName>
</creator>
<creator>
<creatorName>Bachtel, Juliet</creatorName>
<givenName>Juliet</givenName>
<familyName>Bachtel</familyName>
<nameIdentifier schemeURI="http://orcid.org/" nameIdentifierScheme="ORCID">0000-0003-0003-2515</nameIdentifier>
<affiliation>National Center for Ecological Analysis and Synthesis</affiliation>
</creator>
<creator>
<creatorName>Randazzo, John</creatorName>
<givenName>John</givenName>
<familyName>Randazzo</familyName>
<affiliation>National Center for Ecological Analysis and Synthesis</affiliation>
</creator>
<creator>
<creatorName>Gavenus, Erika</creatorName>
<givenName>Erika</givenName>
<familyName>Gavenus</familyName>
</creator>
</creators>
<titles>
<title xml:lang="en">Critical Engineering Literacy Test (CELT)</title>
</titles>
<publisher>Purdue University Research Repository (PURR)</publisher>
<publicationYear>2013</publicationYear>
<subjects>
<subject xml:lang="en">Test</subject>
</subjects>
<language>en</language>
<resourceType resourceTypeGeneral="Dataset">Dataset</resourceType>
<relatedIdentifiers>
<relatedIdentifier relatedIdentifierType="DOI" relationType="IsCitedBy">10.1002/fee.2144</relatedIdentifier>
<relatedIdentifier relatedIdentifierType="URN" relationType="IsDerivedFrom">urn:uuid:6b341709-5acb-45ef-b6ea-e41bbb398b14</relatedIdentifier>
</relatedIdentifiers>
<version>1.1</version>
<descriptions>
<description xml:lang="en" descriptionType="Abstract">
This is a test abstract.
</description>
</descriptions>
</resource>
9 changes: 6 additions & 3 deletions src/test/resources/datacite-4.2.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<resource xmlns="http://datacite.org/schema/kernel-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://datacite.org/schema/kernel-4 https://schema.datacite.org/meta/kernel-4.2/metadata.xsd">
<resource xmlns="http://datacite.org/schema/kernel-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datacite.org/schema/kernel-4 https://schema.datacite.org/meta/kernel-4.2/metadata.xsd">
<identifier identifierType="DOI">TBD</identifier>
<creators>
<creator>
Expand Down Expand Up @@ -35,7 +34,11 @@
</subjects>
<language>en</language>
<resourceType resourceTypeGeneral="Dataset">Dataset</resourceType>
<version>1.0</version>
<relatedIdentifiers>
<relatedIdentifier relatedIdentifierType="DOI" relationType="IsCitedBy">10.1002/fee.2144</relatedIdentifier>
<relatedIdentifier relatedIdentifierType="URN" relationType="IsDerivedFrom">urn:uuid:6b341709-5acb-45ef-b6ea-e41bbb398b14</relatedIdentifier>
</relatedIdentifiers>
<version>1.1</version>
<descriptions>
<description xml:lang="en" descriptionType="Abstract">
This is a test abstract.
Expand Down

0 comments on commit 9a67b1d

Please sign in to comment.