Skip to content

Commit

Permalink
stub out passing explanation as JSON #3943
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jun 26, 2017
1 parent 7f4309b commit ce469f0
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,7 @@ public String releaseMajor() {
return releaseDataset(false);
}

// FIXME: No functionality should be GUI-only. Make all functionality reachable via the API. https://github.com/IQSS/dataverse/issues/3440
public String sendBackToContributor() {
Command<Dataset> cmd;
workingVersion = dataset.getEditVersion();
Expand Down Expand Up @@ -1536,6 +1537,7 @@ public String sendBackToContributor() {
return returnToLatestVersion();
}

// FIXME: No functionality should be GUI-only. Make all functionality reachable via the API. https://github.com/IQSS/dataverse/issues/3440
public String submitDataset() {
Command<Dataset> cmd;
workingVersion = dataset.getEditVersion();
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand;
import edu.harvard.iq.dataverse.engine.command.impl.RequestRsyncScriptCommand;
import edu.harvard.iq.dataverse.engine.command.impl.SetDatasetCitationDateCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetTargetURLCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetThumbnailCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand;
Expand Down Expand Up @@ -610,6 +611,53 @@ public Response getRsync(@PathParam("identifier") String id) {
}
}

// FIXME: This is a stub. Some logic has been copied from submitDataset in DatasetPage but not notifications! Make a Command and do lots of cleanup.
@POST
@Path("{id}/submitForReview")
public Response submitForReview(@PathParam("id") String idSupplied) {
try {
Dataset dataset = findDatasetOrDie(idSupplied);
DatasetVersion datasetVersion = dataset.getLatestVersion();
if (!datasetVersion.getVersionState().equals(DatasetVersion.VersionState.DRAFT)) {
return error(Response.Status.BAD_REQUEST, "The dataset must be in draft to submit it for review. The state of the dataset is " + datasetVersion.getVersionState() + ".");
}
datasetVersion.setInReview(true);
Dataset updatedDataset = execCommand(new UpdateDatasetCommand(dataset, createDataverseRequest(findUserOrDie())));
JsonObjectBuilder result = Json.createObjectBuilder();
boolean inReview = updatedDataset.getLatestVersion().isInReview();
result.add("inReview", inReview);
result.add("message", "Dataset id " + updatedDataset.getId() + " has been submitted for review.");
return ok(result);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}

// FIXME: This is a stub. Some logic has been copied from sendBackToContributor in DatasetPage but not notifications! Make a Command and do lots of cleanup.
@POST
@Path("{id}/returnToAuthor")
public Response returnToAuthor(@PathParam("id") String idSupplied, String jsonBody) {
StringReader rdr = new StringReader(jsonBody);
JsonObject json = Json.createReader(rdr).readObject();
try {
Dataset dataset = findDatasetOrDie(idSupplied);
DatasetVersion datasetVersion = dataset.getLatestVersion();
if (!datasetVersion.getVersionState().equals(DatasetVersion.VersionState.DRAFT)) {
return error(Response.Status.BAD_REQUEST, "The dataset must be in draft to return it to the author(s). The state of the dataset is " + datasetVersion.getVersionState() + ".");
}
datasetVersion.setInReview(false);
Dataset updatedDataset = execCommand(new UpdateDatasetCommand(dataset, createDataverseRequest(findUserOrDie())));
boolean inReview = updatedDataset.getLatestVersion().isInReview();
JsonObjectBuilder result = Json.createObjectBuilder();
result.add("inReview", inReview);
result.add("message", "Dataset id " + updatedDataset.getId() + " has been sent back to the author(s).");
result.add("received", json);
return ok(result);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}

/**
* Add a File to an existing Dataset
*
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/RequestAccessIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package edu.harvard.iq.dataverse.api;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import edu.harvard.iq.dataverse.authorization.DataverseRole;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.BeforeClass;
import org.junit.Test;

public class RequestAccessIT {

private static final Logger logger = Logger.getLogger(DatasetsIT.class.getCanonicalName());

@BeforeClass
public static void setUpClass() {
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();

}

@Test
public void testRequestAccess() {
Response createCurator = UtilIT.createRandomUser();
createCurator.prettyPrint();
createCurator.then().assertThat()
.statusCode(OK.getStatusCode());
String curatorUsername = UtilIT.getUsernameFromResponse(createCurator);
String curatorApiToken = UtilIT.getApiTokenFromResponse(createCurator);

Response createDataverseResponse = UtilIT.createRandomDataverse(curatorApiToken);
createDataverseResponse.prettyPrint();
createDataverseResponse.then().assertThat()
.statusCode(CREATED.getStatusCode());

String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

Response createAuthor = UtilIT.createRandomUser();
createAuthor.prettyPrint();
createAuthor.then().assertThat()
.statusCode(OK.getStatusCode());
String authorUsername = UtilIT.getUsernameFromResponse(createAuthor);
String authorApiToken = UtilIT.getApiTokenFromResponse(createAuthor);

// Whoops, the curator forgot to give the author permission to create a dataset.
Response noPermToCreateDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, authorApiToken);
noPermToCreateDataset.prettyPrint();
noPermToCreateDataset.then().assertThat()
.body("message", equalTo("User @" + authorUsername + " is not permitted to perform requested action."))
.statusCode(UNAUTHORIZED.getStatusCode());

Response grantAuthorAddDataset = UtilIT.grantRoleOnDataverse(dataverseAlias, DataverseRole.DS_CONTRIBUTOR.toString(), "@" + authorUsername, curatorApiToken);
grantAuthorAddDataset.prettyPrint();
grantAuthorAddDataset.then().assertThat()
.body("data.assignee", equalTo("@" + authorUsername))
.body("data._roleAlias", equalTo("dsContributor"))
.statusCode(OK.getStatusCode());

Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, authorApiToken);
createDataset.prettyPrint();
createDataset.then().assertThat()
.statusCode(CREATED.getStatusCode());

Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset);

// FIXME: have the initial create return the DOI or Handle to obviate the need for this call.
Response getDatasetJsonBeforePublishing = UtilIT.nativeGet(datasetId, authorApiToken);
getDatasetJsonBeforePublishing.prettyPrint();
String protocol = JsonPath.from(getDatasetJsonBeforePublishing.getBody().asString()).getString("data.protocol");
String authority = JsonPath.from(getDatasetJsonBeforePublishing.getBody().asString()).getString("data.authority");
String identifier = JsonPath.from(getDatasetJsonBeforePublishing.getBody().asString()).getString("data.identifier");

String datasetPersistentId = protocol + ":" + authority + "/" + identifier;
System.out.println("datasetPersistentId: " + datasetPersistentId);

// Whoops, the author tries to publish but isn't allowed. The curator will take a look.
Response noPermToPublish = UtilIT.publishDatasetViaNativeApi(datasetPersistentId, "major", authorApiToken);
noPermToPublish.prettyPrint();
noPermToPublish.then().assertThat()
.body("message", equalTo("User @" + authorUsername + " is not permitted to perform requested action."))
.statusCode(UNAUTHORIZED.getStatusCode());

Response submitForReview = UtilIT.submitDatasetForReview(datasetPersistentId, authorApiToken);
submitForReview.prettyPrint();
submitForReview.then().assertThat()
.body("data.inReview", equalTo(true))
.statusCode(OK.getStatusCode());

String comments = "You forgot to upload your files.";
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
jsonObjectBuilder.add("comments", comments);
Response returnToAuthor = UtilIT.returnDatasetToAuthor(datasetPersistentId, jsonObjectBuilder.build(), curatorApiToken);
returnToAuthor.prettyPrint();
returnToAuthor.then().assertThat()
.body("data.inReview", equalTo(false))
.statusCode(OK.getStatusCode());

}

}
22 changes: 22 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 @@ -573,6 +573,28 @@ static Response publishDataverseViaSword(String alias, String apiToken) {
.post(swordConfiguration.getBaseUrlPathCurrent() + "/edit/dataverse/" + alias);
}

static Response submitDatasetForReview(String datasetPersistentId, String apiToken) {
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification.post("/api/datasets/:persistentId/submitForReview?persistentId=" + datasetPersistentId);
}

static Response returnDatasetToAuthor(String datasetPersistentId, JsonObject jsonObject, String apiToken) {
String jsonIn = jsonObject.toString();
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken)
.body(jsonIn)
.contentType("application/json");
}
return requestSpecification
.post("/api/datasets/:persistentId/returnToAuthor?persistentId=" + datasetPersistentId);
}

static Response nativeGetUsingPersistentId(String persistentId, String apiToken) {
Response response = given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
Expand Down

0 comments on commit ce469f0

Please sign in to comment.