Skip to content

Commit

Permalink
Fix DOI/ID message when deaccession a Dataset (#10610)
Browse files Browse the repository at this point in the history
* Fix DOI/ID message when deaccession a Dataset

* Change to pull the DS from the Version

* Test added and changed to use asString over toString

* Change Global to PID

* Refactor deaccessionDataset method to handle both datasetId and persistentId
  • Loading branch information
jp-tosca authored Jun 21, 2024
1 parent b405081 commit 9c098f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -4839,7 +4839,10 @@ public Response deaccessionDataset(@Context ContainerRequestContext crc, @PathPa
}
}
execCommand(new DeaccessionDatasetVersionCommand(req, datasetVersion, false));
return ok("Dataset " + datasetId + " deaccessioned for version " + versionId);

return ok("Dataset " +
(":persistentId".equals(datasetId) ? datasetVersion.getDataset().getGlobalId().asString() : datasetId) +
" deaccessioned for version " + versionId);
} catch (JsonParsingException jpe) {
return error(Response.Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3910,6 +3910,7 @@ public void getDatasetVersionCitation() {
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

Response createDatasetResponse = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken);
createDatasetResponse.prettyPrint();
createDatasetResponse.then().assertThat().statusCode(CREATED.getStatusCode());
int datasetId = JsonPath.from(createDatasetResponse.body().asString()).getInt("data.id");

Expand All @@ -3928,7 +3929,9 @@ public void getDatasetVersionCitation() {
publishDatasetResponse.then().assertThat().statusCode(OK.getStatusCode());

Response deaccessionDatasetResponse = UtilIT.deaccessionDataset(datasetId, DS_VERSION_LATEST_PUBLISHED, "Test deaccession reason.", null, apiToken);
deaccessionDatasetResponse.then().assertThat().statusCode(OK.getStatusCode());
deaccessionDatasetResponse.prettyPrint();
deaccessionDatasetResponse.then().assertThat().statusCode(OK.getStatusCode())
.assertThat().body("data.message", containsString(String.valueOf(datasetId)));

// includeDeaccessioned false
Response getDatasetVersionCitationNotDeaccessioned = UtilIT.getDatasetVersionCitation(datasetId, DS_VERSION_LATEST_PUBLISHED, false, apiToken);
Expand All @@ -3939,8 +3942,20 @@ public void getDatasetVersionCitation() {
getDatasetVersionCitationDeaccessioned.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.message", containsString("DEACCESSIONED VERSION"));

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

String persistentId = JsonPath.from(createDatasetResponse.body().asString()).getString("data.persistentId");

deaccessionDatasetResponse = UtilIT.deaccessionDataset(persistentId, DS_VERSION_LATEST_PUBLISHED, "Test deaccession reason.", null, apiToken);
deaccessionDatasetResponse.prettyPrint();
deaccessionDatasetResponse.then().assertThat().statusCode(OK.getStatusCode())
.assertThat().body("data.message", containsString(String.valueOf(persistentId)));
}


@Test
public void getVersionFiles() throws IOException, InterruptedException {
Response createUser = UtilIT.createRandomUser();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,7 @@ public void testGetFileInfo() {
.body("data.label", equalTo(newFileNameSecondUpdate));

// The following tests cover cases where the dataset version is deaccessioned

Response deaccessionDatasetResponse = UtilIT.deaccessionDataset(datasetId, "3.0", "Test reason", null, superUserApiToken);
deaccessionDatasetResponse.then().assertThat().statusCode(OK.getStatusCode());

Expand Down
29 changes: 25 additions & 4 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3845,17 +3845,38 @@ static Response getHasBeenDeleted(String dataFileId, String apiToken) {
.get("/api/files/" + dataFileId + "/hasBeenDeleted");
}

static Response deaccessionDataset(Integer datasetId, String version, String deaccessionReason, String deaccessionForwardURL, String apiToken) {
static Response deaccessionDataset(int datasetId, String version, String deaccessionReason, String deaccessionForwardURL, String apiToken) {
return deaccessionDataset(String.valueOf(datasetId), version, deaccessionReason, deaccessionForwardURL, apiToken);
}

static Response deaccessionDataset(String datasetIdOrPersistentId, String versionId, String deaccessionReason, String deaccessionForwardURL, String apiToken) {

String idInPath = datasetIdOrPersistentId; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isCreatable(datasetIdOrPersistentId)) {
idInPath = ":persistentId";
optionalQueryParam = "?persistentId=" + datasetIdOrPersistentId;
}

JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
jsonObjectBuilder.add("deaccessionReason", deaccessionReason);
if (deaccessionForwardURL != null) {
jsonObjectBuilder.add("deaccessionForwardURL", deaccessionForwardURL);
}

String jsonString = jsonObjectBuilder.build().toString();
StringBuilder query = new StringBuilder()
.append("/api/datasets/")
.append(idInPath)
.append("/versions/")
.append(versionId)
.append("/deaccession")
.append(optionalQueryParam);

return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.body(jsonString)
.post("/api/datasets/" + datasetId + "/versions/" + version + "/deaccession");
.header(API_TOKEN_HTTP_HEADER, apiToken)
.body(jsonString)
.post(query.toString());
}

static Response getDownloadSize(Integer datasetId,
Expand Down

0 comments on commit 9c098f9

Please sign in to comment.