Skip to content

Commit

Permalink
Added: naming refactor and managing not found files in new files API …
Browse files Browse the repository at this point in the history
…endpoints
  • Loading branch information
GPortas committed Jul 9, 2023
1 parent 6ead834 commit 9f35bf7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,6 @@ public String getDirectStorageLocatrion(String storageLocation) {
}

/**
*
* Checks if a user can download a file based on the file metadata and the permissions of the user
*
* This method is based on {@link edu.harvard.iq.dataverse.FileDownloadHelper#canDownloadFile(FileMetadata),
Expand Down
21 changes: 14 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 @@ -106,7 +106,6 @@ public class Files extends AbstractApiBean {
MakeDataCountLoggingServiceBean mdcLogService;
@Inject
GuestbookResponseServiceBean guestbookResponseService;

@Inject
FileDownloadServiceBean fileDownloadServiceBean;

Expand Down Expand Up @@ -828,18 +827,26 @@ public Response getFixityAlgorithm() {

@GET
@Path("{id}/guestbookResponses/count")
public Response getCountGuestbookResponsesByDataFileId(@PathParam("id") long dataFileId) {
public Response getCountGuestbookResponses(@PathParam("id") String dataFileId) {
DataFile dataFile;
try {
return ok(guestbookResponseService.getCountGuestbookResponsesByDataFileId(dataFileId).toString());
} catch (NumberFormatException nfe) {
return badRequest("File identifier has to be numeric.");
dataFile = findDataFileOrDie(dataFileId);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
return ok(guestbookResponseService.getCountGuestbookResponsesByDataFileId(dataFile.getId()).toString());
}

@GET
@AuthRequired
@Path("{id}/canBeDownloaded")
public Response canDataFileBeDownloaded(@Context ContainerRequestContext crc, @PathParam("id") long dataFileId) {
return ok(fileDownloadServiceBean.canDownloadFile(getRequestUser(crc), fileSvc.find(dataFileId).getFileMetadata()));
public Response canFileBeDownloaded(@Context ContainerRequestContext crc, @PathParam("id") String dataFileId) {
DataFile dataFile;
try {
dataFile = findDataFileOrDie(dataFileId);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
return ok(fileDownloadServiceBean.canDownloadFile(getRequestUser(crc), dataFile.getFileMetadata()));
}
}
20 changes: 10 additions & 10 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@ public void testDeleteFile() {
}

@Test
public void testGetCountGuestbookResponsesByDataFileId() {
public void testGetCountGuestbookResponses() {
Response createUser = UtilIT.createRandomUser();
createUser.then().assertThat().statusCode(OK.getStatusCode());
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
Expand All @@ -2043,20 +2043,20 @@ public void testGetCountGuestbookResponsesByDataFileId() {
UtilIT.publishDatasetViaNativeApi(datasetId, "major", apiToken).then().assertThat().statusCode(OK.getStatusCode());

// Download test file
Integer testFileId = JsonPath.from(uploadResponse.body().asString()).getInt("data.files[0].dataFile.id");
int testFileId = JsonPath.from(uploadResponse.body().asString()).getInt("data.files[0].dataFile.id");

Response downloadResponse = UtilIT.downloadFile(testFileId, apiToken);
downloadResponse.then().assertThat().statusCode(OK.getStatusCode());

// Get count guestbook responses and assert it is 1
Response getGuestbookResponsesByDataFileIdResponse = UtilIT.getCountGuestbookResponsesByDataFileId(testFileId, apiToken);
getGuestbookResponsesByDataFileIdResponse.then().assertThat()
Response getCountGuestbookResponsesResponse = UtilIT.getCountGuestbookResponses(testFileId, apiToken);
getCountGuestbookResponsesResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.message", equalTo("1"));
}

@Test
public void testCanDataFileBeDownloaded() {
public void testCanFileBeDownloaded() {
Response createUser = UtilIT.createRandomUser();
createUser.then().assertThat().statusCode(OK.getStatusCode());
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
Expand All @@ -2079,11 +2079,11 @@ public void testCanDataFileBeDownloaded() {
UtilIT.publishDatasetViaNativeApi(datasetId, "major", apiToken).then().assertThat().statusCode(OK.getStatusCode());

// Assert user can download test file
Integer testFileId = JsonPath.from(uploadResponse.body().asString()).getInt("data.files[0].dataFile.id");
Response canDataFileBeDownloadedResponse = UtilIT.canDataFileBeDownloaded(testFileId, apiToken);
int testFileId = JsonPath.from(uploadResponse.body().asString()).getInt("data.files[0].dataFile.id");
Response canFileBeDownloadedResponse = UtilIT.canFileBeDownloaded(testFileId, apiToken);

canDataFileBeDownloadedResponse.then().assertThat().statusCode(OK.getStatusCode());
boolean canDownloadTestFile = JsonPath.from(canDataFileBeDownloadedResponse.body().asString()).getBoolean("data");
assertTrue(canDownloadTestFile);
canFileBeDownloadedResponse.then().assertThat().statusCode(OK.getStatusCode());
boolean canFileBeDownloaded = JsonPath.from(canFileBeDownloadedResponse.body().asString()).getBoolean("data");
assertTrue(canFileBeDownloaded);
}
}
8 changes: 2 additions & 6 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.harvard.iq.dataverse.api;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
Expand All @@ -12,8 +11,6 @@
import javax.json.JsonObjectBuilder;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -44,7 +41,6 @@
import static com.jayway.restassured.path.xml.XmlPath.from;
import static com.jayway.restassured.RestAssured.given;
import edu.harvard.iq.dataverse.DatasetField;
import edu.harvard.iq.dataverse.DatasetFieldConstant;
import edu.harvard.iq.dataverse.DatasetFieldType;
import edu.harvard.iq.dataverse.DatasetFieldValue;
import edu.harvard.iq.dataverse.util.StringUtil;
Expand Down Expand Up @@ -3270,13 +3266,13 @@ static Response createAndUploadTestFile(String persistentId, String testFileName
return uploadZipFileViaSword(persistentId, pathToTestFile, apiToken);
}

static Response getCountGuestbookResponsesByDataFileId(Integer dataFileId, String apiToken) {
static Response getCountGuestbookResponses(int dataFileId, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/files/" + dataFileId + "/guestbookResponses/count");
}

static Response canDataFileBeDownloaded(Integer dataFileId, String apiToken) {
static Response canFileBeDownloaded(int dataFileId, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/files/" + dataFileId + "/canBeDownloaded");
Expand Down

0 comments on commit 9f35bf7

Please sign in to comment.