Skip to content

Commit

Permalink
add API test to download a file with and w/o token #2746
Browse files Browse the repository at this point in the history
Also added todo to work on zip files not being unpacked.
  • Loading branch information
pdurbin committed Dec 8, 2015
1 parent 8016a2a commit 42d1a67
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/SwordIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import java.util.logging.Logger;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.OK;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -23,7 +25,7 @@ public static void setUpClass() {
}

@Test
public void testCreateDatasetUploadFile() {
public void testCreateDatasetUploadFileDownloadFile() {

Response createUser1 = UtilIT.createRandomUser();
// createUser1.prettyPrint();
Expand All @@ -37,7 +39,7 @@ public void testCreateDatasetUploadFile() {
Response createDataset1Response = UtilIT.createRandomDatasetViaSwordApi(dataverseAlias1, apiToken1);
createDataset1Response.prettyPrint();
datasetPersistentId1 = UtilIT.getDatasetPersistentIdFromResponse(createDataset1Response);
logger.info("peristent id: " + datasetPersistentId1);
logger.info("persistent id: " + datasetPersistentId1);

Response uploadFile1 = UtilIT.uploadRandomFile(datasetPersistentId1, apiToken1);
uploadFile1.prettyPrint();
Expand All @@ -49,6 +51,31 @@ public void testCreateDatasetUploadFile() {
assertEquals(Integer.class, fileId.getClass());

logger.info("Id of uploaded file: " + fileId);
String filename = UtilIT.getFilenameFromSwordStatementResponse(swordStatement);
assertNotNull(filename);
assertEquals(String.class, filename.getClass());
/**
* @todo Above we are using UtilIT.uploadRandomFile to upload a zip file
* via SWORD but the zip file (trees.zip) is supposed to be unpacked and
* the file within (trees.png) is supposed to be added to the dataset.
*
* What the line below indicates is that "trees.zip" is being uploaded
* as-is and not unpacked:
*
* "Filename of uploaded file: trees.zip"
*/
logger.info("Filename of uploaded file: " + filename);
boolean uploadBugFixed = false;
if (uploadBugFixed) {
assertEquals("trees.png", filename);
}

Response attemptToDownloadUnpublishedFileWithoutApiToken = UtilIT.downloadFile(fileId);
assertEquals(FORBIDDEN.getStatusCode(), attemptToDownloadUnpublishedFileWithoutApiToken.getStatusCode());

Response downloadUnpublishedFileWithValidApiToken = UtilIT.downloadFile(fileId, apiToken1);
assertEquals(OK.getStatusCode(), downloadUnpublishedFileWithValidApiToken.getStatusCode());
logger.info("downloaded " + downloadUnpublishedFileWithValidApiToken.getContentType() + " (" + downloadUnpublishedFileWithValidApiToken.asByteArray().length + " bytes)");
}

@AfterClass
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ public static Response uploadRandomFile(String persistentId, String apiToken) {

}

static Response downloadFile(Integer fileId) {
return given()
// .header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/access/datafile/" + fileId);
}

static Response downloadFile(Integer fileId, String apiToken) {
return given()
/**
* Data Access API does not support X-Dataverse-key header -
* https://github.com/IQSS/dataverse/issues/2662
*/
//.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/access/datafile/" + fileId + "?key=" + apiToken);
}

static Response getSwordStatement(String persistentId, String apiToken) {
Response swordStatementResponse = given()
.auth().basic(apiToken, EMPTY_STRING)
Expand All @@ -224,6 +240,21 @@ private static Integer getFileIdFromSwordStatementBody(String swordStatement) {
}
}

static String getFilenameFromSwordStatementResponse(Response swordStatement) {
String filename = getFilenameFromSwordStatementResponse(swordStatement.body().asString());
return filename;
}

private static String getFilenameFromSwordStatementResponse(String swordStatement) {
XmlPath xmlPath = new XmlPath(swordStatement);
try {
String filename = xmlPath.get("feed.entry[0].id").toString().split("/")[11];
return filename;
} catch (IndexOutOfBoundsException ex) {
return null;
}
}

public static Response deleteUser(String username) {
Response deleteUserResponse = given()
.delete("/api/admin/authenticatedUsers/" + username + "/");
Expand Down

0 comments on commit 42d1a67

Please sign in to comment.