Skip to content

Commit

Permalink
make sure file is unzipped after SWORD upload #2746
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Dec 8, 2015
1 parent 3d2ac41 commit 596a8c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
23 changes: 7 additions & 16 deletions src/test/java/edu/harvard/iq/dataverse/api/SwordIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import java.util.logging.Logger;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.OK;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;

public class SwordIT {

Expand All @@ -35,14 +36,17 @@ public void testCreateDatasetUploadFileDownloadFile() {
Response createDataverse1Response = UtilIT.createRandomDataverse(apiToken1);
createDataverse1Response.prettyPrint();
dataverseAlias1 = UtilIT.getAliasFromResponse(createDataverse1Response);
assertEquals(CREATED.getStatusCode(), createDataverse1Response.getStatusCode());

Response createDataset1Response = UtilIT.createRandomDatasetViaSwordApi(dataverseAlias1, apiToken1);
createDataset1Response.prettyPrint();
assertEquals(CREATED.getStatusCode(), createDataset1Response.getStatusCode());
datasetPersistentId1 = UtilIT.getDatasetPersistentIdFromResponse(createDataset1Response);
logger.info("persistent id: " + datasetPersistentId1);

Response uploadFile1 = UtilIT.uploadRandomFile(datasetPersistentId1, apiToken1);
uploadFile1.prettyPrint();
assertEquals(CREATED.getStatusCode(), uploadFile1.getStatusCode());

Response swordStatement = UtilIT.getSwordStatement(datasetPersistentId1, apiToken1);
swordStatement.prettyPrint();
Expand All @@ -54,21 +58,8 @@ public void testCreateDatasetUploadFileDownloadFile() {
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);
}
assertEquals("trees.png", filename);

Response attemptToDownloadUnpublishedFileWithoutApiToken = UtilIT.downloadFile(fileId);
assertEquals(FORBIDDEN.getStatusCode(), attemptToDownloadUnpublishedFileWithoutApiToken.getStatusCode());
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,24 @@ static private String getDatasetXml(String title, String author, String descript
public static Response uploadRandomFile(String persistentId, String apiToken) {
String zipfilename = "trees.zip";
String pathToFileName = "scripts/search/data/binary/" + zipfilename;
byte[] bytes = null;
try {
bytes = Files.readAllBytes(Paths.get(pathToFileName));
logger.info("Number of bytes to upload: " + bytes.length);
} catch (IOException ex) {
throw new RuntimeException("Problem getting bytes from " + pathToFileName + ": " + ex);
}
Response swordStatementResponse = given()
.multiPart(new File(pathToFileName))
.body(bytes)
.header("Packaging", "http://purl.org/net/sword/package/SimpleZip")
.header("Content-Disposition", "filename=" + zipfilename)
.auth().basic(apiToken, EMPTY_STRING)
/**
* It's unclear why we need to add "preemptive" to auth but
* without it we can't use send bytes using the body/content
* method. See
* https://github.com/jayway/rest-assured/issues/507#issuecomment-162963787
*/
.auth().preemptive().basic(apiToken, EMPTY_STRING)
.post(swordConfiguration.getBaseUrlPathCurrent() + "/edit-media/study/" + persistentId);
return swordStatementResponse;

Expand Down

0 comments on commit 596a8c4

Please sign in to comment.