Skip to content

Commit

Permalink
Added: returnDatasetVersion optional parameter to getFileInfo API end…
Browse files Browse the repository at this point in the history
…point
  • Loading branch information
GPortas committed Feb 19, 2024
1 parent a267adc commit ff2e86c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
14 changes: 7 additions & 7 deletions src/main/java/edu/harvard/iq/dataverse/api/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@
import jakarta.ejb.EJB;
import jakarta.ejb.EJBException;
import jakarta.inject.Inject;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.*;
import jakarta.json.stream.JsonParsingException;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.*;
Expand Down Expand Up @@ -489,9 +486,10 @@ public Response updateFileMetadata(@Context ContainerRequestContext crc, @FormDa
public Response getFileData(@Context ContainerRequestContext crc,
@PathParam("id") String fileIdOrPersistentId,
@QueryParam("includeDeaccessioned") boolean includeDeaccessioned,
@QueryParam("returnDatasetVersion") boolean returnDatasetVersion,
@Context UriInfo uriInfo,
@Context HttpHeaders headers) {
return response( req -> getFileDataResponse(req, fileIdOrPersistentId, DS_VERSION_LATEST, includeDeaccessioned, uriInfo, headers), getRequestUser(crc));
return response( req -> getFileDataResponse(req, fileIdOrPersistentId, DS_VERSION_LATEST, includeDeaccessioned, returnDatasetVersion, uriInfo, headers), getRequestUser(crc));
}

@GET
Expand All @@ -501,15 +499,17 @@ public Response getFileData(@Context ContainerRequestContext crc,
@PathParam("id") String fileIdOrPersistentId,
@PathParam("datasetVersionId") String datasetVersionId,
@QueryParam("includeDeaccessioned") boolean includeDeaccessioned,
@QueryParam("returnDatasetVersion") boolean returnDatasetVersion,
@Context UriInfo uriInfo,
@Context HttpHeaders headers) {
return response( req -> getFileDataResponse(req, fileIdOrPersistentId, datasetVersionId, includeDeaccessioned, uriInfo, headers), getRequestUser(crc));
return response( req -> getFileDataResponse(req, fileIdOrPersistentId, datasetVersionId, includeDeaccessioned, returnDatasetVersion, uriInfo, headers), getRequestUser(crc));
}

private Response getFileDataResponse(final DataverseRequest req,
String fileIdOrPersistentId,
String datasetVersionId,
boolean includeDeaccessioned,
boolean returnDatasetVersion,
UriInfo uriInfo,
HttpHeaders headers) throws WrappedResponse {
final DataFile dataFile = execCommand(new GetDataFileCommand(req, findDataFileOrDie(fileIdOrPersistentId)));
Expand Down Expand Up @@ -546,7 +546,7 @@ public Command<FileMetadata> handleLatestPublished() {

return Response.ok(Json.createObjectBuilder()
.add("status", ApiConstants.STATUS_OK)
.add("data", json(fileMetadata)).build())
.add("data", json(fileMetadata, returnDatasetVersion)).build())
.type(MediaType.APPLICATION_JSON)
.build();
}
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,35 +602,46 @@ public static JsonObjectBuilder json(DatasetFieldType fld) {
*/

public static JsonObjectBuilder json(FileMetadata fmd) {
return jsonObjectBuilder()
return json(fmd, false);
}

public static JsonObjectBuilder json(FileMetadata fmd, boolean printDatasetVersion) {
NullSafeJsonBuilder builder = jsonObjectBuilder()
// deprecated: .add("category", fmd.getCategory())
// TODO: uh, figure out what to do here... it's deprecated
// in a sense that there's no longer the category field in the
// fileMetadata object; but there are now multiple, oneToMany file
// TODO: uh, figure out what to do here... it's deprecated
// in a sense that there's no longer the category field in the
// fileMetadata object; but there are now multiple, oneToMany file
// categories - and we probably need to export them too!) -- L.A. 4.5
// DONE: catgegories by name
// DONE: catgegories by name
.add("description", fmd.getDescription())
.add("label", fmd.getLabel()) // "label" is the filename
.add("restricted", fmd.isRestricted())
.add("restricted", fmd.isRestricted())
.add("directoryLabel", fmd.getDirectoryLabel())
.add("version", fmd.getVersion())
.add("datasetVersionId", fmd.getDatasetVersion().getId())
.add("categories", getFileCategories(fmd))
.add("dataFile", JsonPrinter.json(fmd.getDataFile(), fmd, false));

if (printDatasetVersion) {
builder.add("datasetVersion", json(fmd.getDatasetVersion(), false));
}

return builder;
}

public static JsonObjectBuilder json(AuxiliaryFile auxFile) {
public static JsonObjectBuilder json(AuxiliaryFile auxFile) {
return jsonObjectBuilder()
.add("formatTag", auxFile.getFormatTag())
.add("formatVersion", auxFile.getFormatVersion()) // "label" is the filename
.add("origin", auxFile.getOrigin())
.add("origin", auxFile.getOrigin())
.add("isPublic", auxFile.getIsPublic())
.add("type", auxFile.getType())
.add("contentType", auxFile.getContentType())
.add("fileSize", auxFile.getFileSize())
.add("checksum", auxFile.getChecksum())
.add("dataFile", JsonPrinter.json(auxFile.getDataFile()));
}

public static JsonObjectBuilder json(DataFile df) {
return JsonPrinter.json(df, null, false);
}
Expand Down
47 changes: 30 additions & 17 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1530,65 +1530,65 @@ public void testGetFileInfo() {
deaccessionDatasetResponse.then().assertThat().statusCode(OK.getStatusCode());

// Superuser should get to see file data if the latest version is deaccessioned filtering by latest and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameSecondUpdate))
.statusCode(OK.getStatusCode());

// Superuser should get to see file data if the latest version is deaccessioned filtering by latest published and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST_PUBLISHED, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST_PUBLISHED, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameSecondUpdate))
.statusCode(OK.getStatusCode());

// Superuser should get to see version 2.0 file data if the latest version is deaccessioned filtering by latest and includeDeaccessioned is false
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST, false);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST, false, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());

// Superuser should get to see version 2.0 file data if the latest version is deaccessioned filtering by latest published and includeDeaccessioned is false
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST_PUBLISHED, false);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST_PUBLISHED, false, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());

// Superuser should get to see file data from specific deaccessioned version filtering by tag and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, "3.0", true);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, "3.0", true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameSecondUpdate))
.statusCode(OK.getStatusCode());

// Superuser should not get to see file data from specific deaccessioned version filtering by tag and includeDeaccessioned is false
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, "3.0", false);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, "3.0", false, false);
getFileDataResponse.then().assertThat()
.statusCode(NOT_FOUND.getStatusCode());

// Regular user should get to see version 2.0 file data if the latest version is deaccessioned filtering by latest and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());

// Regular user should get to see version 2.0 file data if the latest version is deaccessioned filtering by latest published and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());

// Regular user should get to see version 2.0 file data if the latest version is deaccessioned filtering by latest published and includeDeaccessioned is false
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, false);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, false, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());

// Regular user should not get to see file data from specific deaccessioned version filtering by tag and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, "3.0", true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, "3.0", true, false);
getFileDataResponse.then().assertThat()
.statusCode(NOT_FOUND.getStatusCode());

// Regular user should not get to see file data from specific deaccessioned version filtering by tag and includeDeaccessioned is false
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, "3.0", false);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, "3.0", false, false);
getFileDataResponse.then().assertThat()
.statusCode(NOT_FOUND.getStatusCode());

Expand All @@ -1600,25 +1600,25 @@ public void testGetFileInfo() {
updateFileMetadataResponse.then().statusCode(OK.getStatusCode());

// Superuser should get to see draft file data if draft exists filtering by latest and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameThirdUpdate))
.statusCode(OK.getStatusCode());

// Superuser should get to see latest published file data if draft exists filtering by latest published and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST_PUBLISHED, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, superUserApiToken, DS_VERSION_LATEST_PUBLISHED, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameSecondUpdate))
.statusCode(OK.getStatusCode());

// Regular user should get to see version 2.0 file data if the latest version is deaccessioned and draft exists filtering by latest and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());

// Regular user should get to see version 2.0 file data if the latest version is deaccessioned and draft exists filtering by latest published and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameFirstUpdate))
.statusCode(OK.getStatusCode());
Expand All @@ -1629,17 +1629,30 @@ public void testGetFileInfo() {
.statusCode(OK.getStatusCode());

// Regular user should get to see file data if the latest version is not deaccessioned filtering by latest and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameThirdUpdate))
.statusCode(OK.getStatusCode());

// Regular user should get to see file data if the latest version is not deaccessioned filtering by latest published and includeDeaccessioned is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, true);
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, DS_VERSION_LATEST_PUBLISHED, true, false);
getFileDataResponse.then().assertThat()
.body("data.label", equalTo(newFileNameThirdUpdate))
.statusCode(OK.getStatusCode());

// The following tests cover cases where the user requests to include the dataset version information in the response
// User should get to see dataset version info in the response if returnDatasetVersion is true
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, "1.0", false, true);
getFileDataResponse.then().assertThat()
.body("data.datasetVersion.versionState", equalTo("RELEASED"))
.statusCode(OK.getStatusCode());

// User should not get to see dataset version info in the response if returnDatasetVersion is false
getFileDataResponse = UtilIT.getFileData(dataFileId, regularApiToken, "1.0", false, false);
getFileDataResponse.then().assertThat()
.body("data.datasetVersion", equalTo(null))
.statusCode(OK.getStatusCode());

// Cleanup
Response destroyDatasetResponse = UtilIT.destroyDataset(datasetId, superUserApiToken);
destroyDatasetResponse.then().assertThat().statusCode(OK.getStatusCode());
Expand Down
5 changes: 3 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 @@ -1092,13 +1092,14 @@ static Response getFileData(String fileId, String apiToken) {
}

static Response getFileData(String fileId, String apiToken, String datasetVersionId) {
return getFileData(fileId, apiToken, datasetVersionId, false);
return getFileData(fileId, apiToken, datasetVersionId, false, false);
}

static Response getFileData(String fileId, String apiToken, String datasetVersionId, boolean includeDeaccessioned) {
static Response getFileData(String fileId, String apiToken, String datasetVersionId, boolean includeDeaccessioned, boolean returnDatasetVersion) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.queryParam("includeDeaccessioned", includeDeaccessioned)
.queryParam("returnDatasetVersion", returnDatasetVersion)
.get("/api/files/" + fileId + "/versions/" + datasetVersionId);
}

Expand Down

0 comments on commit ff2e86c

Please sign in to comment.