Skip to content

Commit

Permalink
test and document forceReplace #1612 #2290
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jan 26, 2017
1 parent 5fc191b commit 2a56e58
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
4 changes: 2 additions & 2 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ Replace an existing file where ``id`` is the database id of the file to replace.

POST http://$SERVER/api/files/{id}/replace?key=$apiKey

A more detailed "replace" example using curl::
A more detailed "replace" example using curl (note that ``forceReplace`` is for replacing one file type with another)::

curl -H "X-Dataverse-key:$API_TOKEN" -X POST -F '[email protected]' -F 'jsonData={"description":"My description.","categories":["Data"]}' "https://example.dataverse.edu/api/files/$FILE_ID/replace"
curl -H "X-Dataverse-key:$API_TOKEN" -X POST -F '[email protected]' -F 'jsonData={"description":"My description.","categories":["Data"],"forceReplace":false}' "https://example.dataverse.edu/api/files/$FILE_ID/replace"

Example python code to replace a file. This may be run by changing these parameters in the sample code:

Expand Down
71 changes: 68 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,6 @@ public void test_006_ReplaceFileGoodTabular() throws InterruptedException {
msg("Replace file - 1st time");
String pathToFile2 = "scripts/search/data/tabular/120745.dta";
JsonObjectBuilder json = Json.createObjectBuilder()
/**
* @todo forceReplace doesn't seem to work.
*/
.add("forceReplace", true)
.add("description", "tiny Stata file")
.add("categories", Json.createArrayBuilder()
Expand Down Expand Up @@ -522,6 +519,74 @@ public void test_006_ReplaceFileGoodTabular() throws InterruptedException {

}

@Test
public void testForceReplace() {
msgt("testForceReplace");

// Create user
String apiToken = createUserGetToken();

// Create Dataverse
String dataverseAlias = createDataverseGetAlias(apiToken);

// Create Dataset
Integer datasetId = createDatasetGetId(dataverseAlias, apiToken);

// -------------------------
// Add initial file
// -------------------------
msg("Add initial file");
String pathToFile = "src/main/webapp/resources/images/cc0.png";
Response addResponse = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile, apiToken);

String successMsgAdd = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.add");

addResponse.then().assertThat()
.body("data.files[0].dataFile.contentType", equalTo("image/png"))
.body("data.files[0].label", equalTo("cc0.png"))
.statusCode(OK.getStatusCode());

long origFileId = JsonPath.from(addResponse.body().asString()).getLong("data.files[0].dataFile.id");

msg("Orig file id: " + origFileId);
assertNotNull(origFileId); // If checkOut fails, display message

// -------------------------
// Publish dataverse and dataset
// -------------------------
msg("Publish dataverse and dataset");
Response publishDataversetResp = UtilIT.publishDataverseViaSword(dataverseAlias, apiToken);
publishDataversetResp.then().assertThat()
.statusCode(OK.getStatusCode());

Response publishDatasetResp = UtilIT.publishDatasetViaNativeApi(datasetId, "major", apiToken);
publishDatasetResp.prettyPrint();
publishDatasetResp.then().assertThat()
.statusCode(OK.getStatusCode());

// -------------------------
// Replace file
// -------------------------
msg("Replace file - 1st time");
String pathToFile2 = "scripts/search/data/replace_test/growing_file/2016-01/data.tsv";
JsonObjectBuilder json = Json.createObjectBuilder()
.add("forceReplace", true)
.add("description", "not an image")
.add("categories", Json.createArrayBuilder()
.add("Data")
);
Response replaceResp = UtilIT.replaceFile(origFileId, pathToFile2, json.build(), apiToken);

replaceResp.prettyPrint();

replaceResp.then().assertThat()
.body("data.files[0].label", equalTo("data.tsv"))
.body("data.files[0].description", equalTo("not an image"))
.body("data.files[0].categories[0]", equalTo("Data"))
.statusCode(OK.getStatusCode());

}

@Test
public void test_007_ReplaceFileUnpublishedAndBadIds() {
msgt("test_007_ReplaceFileBadIds");
Expand Down

0 comments on commit 2a56e58

Please sign in to comment.