Skip to content

Commit

Permalink
RestAssured tests for the new functionality added to the /versions ap…
Browse files Browse the repository at this point in the history
…i. (#9763)
  • Loading branch information
landreev committed Sep 13, 2023
1 parent 77dc0b5 commit bfe7f9c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
85 changes: 85 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,91 @@ public void testCreatePublishDestroyDataset() {

}

/**
* The apis (/api/datasets/{id}/versions and /api/datasets/{id}/versions/{vid}
* are called from other RestAssured tests, in this class and also FileIT.
* But this test is dedicated to this api specifically, and focuses on the
* functionality added to it in 6.1.
*/
@Test
public void testDatasetVersionsAPI() {
// Create user
String apiToken = UtilIT.createRandomUserGetToken();

// Create user with no permission
String apiTokenNoPerms = UtilIT.createRandomUserGetToken();

// Create Collection
String collectionAlias = UtilIT.createRandomCollectionGetAlias(apiToken);

// Create Dataset
Response createDataset = UtilIT.createRandomDatasetViaNativeApi(collectionAlias, apiToken);
createDataset.then().assertThat()
.statusCode(CREATED.getStatusCode());

Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset);
String datasetPid = JsonPath.from(createDataset.asString()).getString("data.persistentId");

// Upload file
String pathToFile = "src/main/webapp/resources/images/dataverseproject.png";
Response uploadResponse = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile, apiToken);
uploadResponse.then().assertThat().statusCode(OK.getStatusCode());

Integer fileId = JsonPath.from(uploadResponse.body().asString()).getInt("data.files[0].dataFile.id");

// Check that the file we just uploaded is shown by the versions api:
Response unpublishedDraft = UtilIT.getDatasetVersion(datasetPid, ":draft", apiToken);
unpublishedDraft.prettyPrint();
unpublishedDraft.then().assertThat()
.body("data.files.size()", equalTo(1))
.statusCode(OK.getStatusCode());

// Now check that the file is NOT shown, when we ask the versions api to
// skip files:
boolean skipFiles = true;
unpublishedDraft = UtilIT.getDatasetVersion(datasetPid, ":draft", apiToken, skipFiles);
unpublishedDraft.prettyPrint();
unpublishedDraft.then().assertThat()
.body("data.files", equalTo(null))
.statusCode(OK.getStatusCode());

// Publish collection and dataset
UtilIT.publishDataverseViaNativeApi(collectionAlias, apiToken).then().assertThat().statusCode(OK.getStatusCode());
UtilIT.publishDatasetViaNativeApi(datasetId, "major", apiToken).then().assertThat().statusCode(OK.getStatusCode());

// Upload another file:
String pathToFile2 = "src/main/webapp/resources/images/cc0.png";
Response uploadResponse2 = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile2, apiToken);
uploadResponse2.then().assertThat().statusCode(OK.getStatusCode());

// We should now have a published version, and a draft.

// Call /versions api, *with the owner api token*, make sure both
// versions are listed
Response versionsResponse = UtilIT.getDatasetVersions(datasetPid, apiToken);
versionsResponse.prettyPrint();
versionsResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.size()", equalTo(2));

// And now call it with an un-privileged token, to make sure only one
// (the published one) version is shown:

versionsResponse = UtilIT.getDatasetVersions(datasetPid, apiTokenNoPerms);
versionsResponse.prettyPrint();
versionsResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.size()", equalTo(1));

// And now call the "short", no-files version of the same api
versionsResponse = UtilIT.getDatasetVersions(datasetPid, apiTokenNoPerms, skipFiles);
versionsResponse.prettyPrint();
versionsResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].files", equalTo(null));
}


/**
* This test requires the root dataverse to be published to pass.
*/
Expand Down
47 changes: 45 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 @@ -9,6 +9,7 @@
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import static jakarta.ws.rs.core.Response.Status.CREATED;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -51,7 +52,6 @@
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

public class UtilIT {

Expand Down Expand Up @@ -119,6 +119,16 @@ public static Response createRandomUser() {

return createRandomUser("user");
}

/**
* A convenience method for creating a random test user, when all you need
* is the api token.
* @return apiToken
*/
public static String createRandomUserGetToken(){
Response createUser = createRandomUser();
return getApiTokenFromResponse(createUser);
}

public static Response createUser(String username, String email) {
logger.info("Creating user " + username);
Expand Down Expand Up @@ -369,6 +379,20 @@ static Response createRandomDataverse(String apiToken) {
String category = null;
return createDataverse(alias, category, apiToken);
}

/**
* A convenience method for creating a random collection and getting its
* alias in one step.
* @param apiToken
* @return alias
*/
static String createRandomCollectionGetAlias(String apiToken){

Response createCollectionResponse = createRandomDataverse(apiToken);
//createDataverseResponse.prettyPrint();
createCollectionResponse.then().assertThat().statusCode(CREATED.getStatusCode());
return UtilIT.getAliasFromResponse(createCollectionResponse);
}

static Response showDataverseContents(String alias, String apiToken) {
return given()
Expand Down Expand Up @@ -1403,9 +1427,17 @@ static Response nativeGetUsingPersistentId(String persistentId, String apiToken)
}

static Response getDatasetVersion(String persistentId, String versionNumber, String apiToken) {
return getDatasetVersion(persistentId, versionNumber, apiToken, false);
}

static Response getDatasetVersion(String persistentId, String versionNumber, String apiToken, boolean skipFiles) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/datasets/:persistentId/versions/" + versionNumber + "?persistentId=" + persistentId);
.get("/api/datasets/:persistentId/versions/"
+ versionNumber
+ "?persistentId="
+ persistentId
+ (skipFiles ? "&includeFiles=false" : ""));
}

static Response getMetadataBlockFromDatasetVersion(String persistentId, String versionNumber, String metadataBlock, String apiToken) {
Expand Down Expand Up @@ -1767,13 +1799,24 @@ static Response removeDatasetThumbnail(String datasetPersistentId, String apiTok
}

static Response getDatasetVersions(String idOrPersistentId, String apiToken) {
return getDatasetVersions(idOrPersistentId, apiToken, false);
}

static Response getDatasetVersions(String idOrPersistentId, String apiToken, boolean skipFiles) {
logger.info("Getting Dataset Versions");
String idInPath = idOrPersistentId; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isCreatable(idOrPersistentId)) {
idInPath = ":persistentId";
optionalQueryParam = "?persistentId=" + idOrPersistentId;
}
if (skipFiles) {
if ("".equals(optionalQueryParam)) {
optionalQueryParam = "?includeFiles=false";
} else {
optionalQueryParam = optionalQueryParam.concat("&includeFiles=false");
}
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
Expand Down

0 comments on commit bfe7f9c

Please sign in to comment.