Skip to content

Commit

Permalink
Add a getStatus method on the client and service classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
taojing2002 committed Feb 11, 2022
1 parent 0dee14b commit 11aca03
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
25 changes: 14 additions & 11 deletions src/main/java/edu/ucsb/nceas/osti_elink/OSTIElinkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,22 @@ public String mintIdentifier(String siteCode) throws OSTIElinkException {
* @throws OSTIElinkException
*/
public String getMetadata(String identifier) throws OSTIElinkException {
String metadata = null;
try {
metadata = service.getMetadata(identifier);
} catch (OSTIElinkException e) {
if (errorAgent != null) {
errorAgent.notify(e);
}
throw e;
}
return metadata;
return service.getMetadata(identifier);
}


/**
* Get the status for the given identifier. If there are multiple records
* associate with the identifier, the status of the first one will be returned.
* The thread blocks until the status is returned
* @param identifier id to identify whose status should be returned
* @return the metadata associated with the identifier
* @throws OSTIElinkException
*/
public String getStatus(String identifier) throws OSTIElinkException {
return service.getStatus(identifier);
}


private void startExecutorLoop() {
// Query the runtime to see how many CPUs are available, and configure that many threads
Runtime runtime = Runtime.getRuntime();
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/edu/ucsb/nceas/osti_elink/OSTIElinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public String mintIdentifier(String siteCode) throws OSTIElinkException {

/**
* Get the metadata associated with the given identifier, which should be a doi. An OSTIElinkNotFoundException
* will be thrown if the identifier can't be found.
* will be thrown if the identifier can't be found. It may contains multiple records.
* @param doi the identifier for which the metadata should be returned
* @return the metadata in the xml format
* @throws OSTIElinkException
Expand Down Expand Up @@ -214,6 +214,25 @@ public void setMetadata(String doi, String doiPrefix, String metadataXML) throws
}
}

/**
* Get the status of a DOI. If there are multiple records for a DOI, the status of
* the first one will be returned
* @param doi the doi to identify the record
* @return the status of the doi
* @throws OSTIElinkException
*/
public String getStatus(String doi) throws OSTIElinkException {
String status = null;
String metadata = getMetadata(doi);
if (metadata == null) {
throw new OSTIElinkException("OSTIElinkService.getStatus - the metadata of the DOI " + doi + " can't be found.");
}
Document doc = generateDOM(metadata.getBytes());
status = getAttributeValue(doc, "record", "status");//get the attribute value of the first element
log.debug("OSTIElinkService.getStatus - the status of " + doi + " is " + status);
return status;
}

/**
* Add the osti id element to the metadata as the first child if the metadata doesn't have one;otherwise, it will
* replace with the new value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public void testSetAndGetMetadata() throws Exception {
String metadata = client.getMetadata(identifier);
assertTrue(metadata.contains(identifier));
assertTrue(metadata.contains("<title>unknown</title>"));
String status = client.getStatus(identifier);
assertTrue(status.equals("Saved"));

try (InputStream is = getClass().getClassLoader().getResourceAsStream("test-files/input-two-osti-id.xml")) {
String newMetadata = OSTIElinkServiceTest.toString(is);
Expand All @@ -100,6 +102,8 @@ public void testSetAndGetMetadata() throws Exception {
metadata = client.getMetadata(identifier);
assertTrue(metadata.contains(identifier));
assertTrue(metadata.contains("<title>unknown</title>"));
status = client.getStatus(identifier);
assertTrue(status.equals("Saved"));
}

try (InputStream is = getClass().getClassLoader().getResourceAsStream("test-files/input-no-osti-id.xml")) {
Expand All @@ -109,6 +113,8 @@ public void testSetAndGetMetadata() throws Exception {
metadata = client.getMetadata(identifier);
assertTrue(metadata.contains(identifier));
assertTrue(metadata.contains("<title>0 - Data from Raczka et al., Interactions between"));
status = client.getStatus(identifier);
assertTrue(status.equals("Pending"));
}

try (InputStream is = getClass().getClassLoader().getResourceAsStream("test-files/input-one-osti-id.xml")) {
Expand All @@ -118,6 +124,8 @@ public void testSetAndGetMetadata() throws Exception {
metadata = client.getMetadata(identifier);
assertTrue(metadata.contains(identifier));
assertTrue(metadata.contains("<title>1 - Data from Raczka et al., Interactions between"));
status = client.getStatus(identifier);
assertTrue(status.equals("Pending"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,11 @@ public void testNotify() throws Exception {
} catch (Exception e) {
assertTrue(e instanceof OSTIElinkException);
}
String uuid2 = UUID.randomUUID().toString();
String doi = "doi:" + uuid2;
try {
String metadata2 = client.getMetadata(doi);
fail("Test can't reach here");
} catch (Exception e) {
assertTrue(e instanceof OSTIElinkException);
}
client.shutdown();
//System.out.println("the error message from agent is " + agent.getError());
assertTrue(agent.getError().contains("the metadata shouldn't have more than one osti id"));
assertTrue(agent.getError().contains(uuid));
assertTrue(agent.getError().contains(KNB));
assertTrue(agent.getError().contains(uuid2));

}

}
14 changes: 14 additions & 0 deletions src/test/java/edu/ucsb/nceas/osti_elink/OSTIElinkServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,20 @@ public void testSetMetadata() throws Exception {

}

/**
* Test the getStatus method
* @throws Exception
*/
@Test
public void testGetStatus() throws Exception {
String doi = "10.15485/1523924";
String status = ostiService.getStatus(doi);
assertTrue(status.equals("Pending"));
String withDOI = "doi:" + doi;
status = ostiService.getStatus(withDOI);
assertTrue(status.equals("Pending"));
}

/**
* Read a input stream object to a string
* @param inputStream the source of input
Expand Down

0 comments on commit 11aca03

Please sign in to comment.