Skip to content

Commit

Permalink
adding IT test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenwinship committed Apr 19, 2024
1 parent daab5e2 commit eb82fae
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ public static Dataset persistDatasetLogoToStorageAndCreateThumbnails(Dataset dat
boolean tmpFileWasDeleted = tmpFile.delete();
boolean originalTempFileWasDeleted = tmpFileForResize.delete();
try {
Files.delete(Paths.get(thumbFileLocation));
if (thumbFileLocation != null) {
Files.delete(Paths.get(thumbFileLocation));
}
} catch (IOException ioex) {
logger.fine("Failed to delete temporary thumbnail file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ public DatasetThumbnail execute(CommandContext ctxt) throws CommandException {
Logger.getLogger(UpdateDatasetThumbnailCommand.class.getName()).log(Level.SEVERE, null, ex);
}
Dataset datasetWithNewThumbnail = ctxt.datasets().setNonDatasetFileAsThumbnail(dataset, fileAsStream);
IOUtils.closeQuietly(fileAsStream);
IOUtils.closeQuietly(fileAsStream);
if (datasetWithNewThumbnail != null) {
return datasetWithNewThumbnail.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
} else {
return null;
DatasetThumbnail thumbnail = datasetWithNewThumbnail.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
if (thumbnail != null) {
return thumbnail;
}
}
throw new IllegalCommandException("In setNonDatasetFileAsThumbnail could not generate thumbnail from uploaded file.", this);

case removeThumbnail:
Dataset ds2 = ctxt.datasets().removeDatasetThumbnail(dataset);
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/ThumbnailsIT.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package edu.harvard.iq.dataverse.api;

import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;

import java.util.List;

import static jakarta.ws.rs.core.Response.Status.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ThumbnailsIT {

@Test
Expand Down Expand Up @@ -35,4 +42,65 @@ public void testDatasetThumbnail() {
.statusCode(403);

}

@Test
public void testBadTiffThumbnailFailure() {
String goodTiff = "src/test/resources/images/good.tiff";
String badTiff = "src/test/resources/images/bad.tiff";
// setup dataverse and dataset
Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
String username = UtilIT.getUsernameFromResponse(createUser);
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
UtilIT.makeSuperUser(username);

Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken);
createDataverseResponse.prettyPrint();
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);
String pathToJsonFile = "src/test/resources/json/complete-dataset-with-files.json";
Response createDatasetResponse = UtilIT.createDatasetViaNativeApi(dataverseAlias, pathToJsonFile, apiToken);
createDatasetResponse.prettyPrint();
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDatasetResponse);

Response datasetAsJson = UtilIT.nativeGet(datasetId, apiToken);
datasetAsJson.prettyPrint();
String protocol = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.protocol");
String authority = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.authority");
String identifier = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.identifier");
String datasetPersistentId = protocol + ":" + authority + "/" + identifier;

// check thumbnails are empty

Response thumbnailCandidatesResponse = UtilIT.showDatasetThumbnailCandidates(datasetPersistentId, apiToken);
thumbnailCandidatesResponse.prettyPrint();
thumbnailCandidatesResponse.then().assertThat().statusCode(OK.getStatusCode());
List<Object> images = JsonPath.from(thumbnailCandidatesResponse.getBody().asString()).getList("data");
assertTrue(images.size() == 0);

// upload image files ( 1 good and 1 bad )

Response uploadResponse = UtilIT.uploadFileViaNative(datasetId.toString(), goodTiff, apiToken);
uploadResponse.then().assertThat().statusCode(OK.getStatusCode());
uploadResponse = UtilIT.uploadFileViaNative(datasetId.toString(), badTiff, apiToken);
uploadResponse.then().assertThat().statusCode(OK.getStatusCode());

// check thumbnails only contains 1 good image
thumbnailCandidatesResponse = UtilIT.showDatasetThumbnailCandidates(datasetPersistentId, apiToken);
thumbnailCandidatesResponse.prettyPrint();
thumbnailCandidatesResponse.then().assertThat().statusCode(OK.getStatusCode());
images = JsonPath.from(thumbnailCandidatesResponse.getBody().asString()).getList("data");
assertTrue(images.size() == 1);

// test set logo with badTiff and tiff to large (goodTiff)
Response uploadLogoResponse = UtilIT.uploadDatasetLogo(datasetPersistentId, badTiff, apiToken);
uploadLogoResponse.prettyPrint();
uploadLogoResponse.then().assertThat().statusCode(FORBIDDEN.getStatusCode());
uploadLogoResponse.then().assertThat().body("message", equalTo("In setNonDatasetFileAsThumbnail could not generate thumbnail from uploaded file."));

uploadLogoResponse = UtilIT.uploadDatasetLogo(datasetPersistentId, goodTiff, apiToken);
uploadLogoResponse.prettyPrint();
uploadLogoResponse.then().assertThat().statusCode(FORBIDDEN.getStatusCode());
uploadLogoResponse.then().assertThat().body("message", equalTo("File is larger than maximum size: 500000."));

}
}
Binary file added src/test/resources/images/bad.tiff
Binary file not shown.
Binary file added src/test/resources/images/good.tiff
Binary file not shown.

0 comments on commit eb82fae

Please sign in to comment.