Skip to content

Commit

Permalink
move methods to UtilIT #6783
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Dec 5, 2023
1 parent 6a7d8d1 commit b9f4891
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 63 deletions.
75 changes: 12 additions & 63 deletions src/test/java/edu/harvard/iq/dataverse/api/S3AccessIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testNonDirectUpload() {
String superuserApiToken = UtilIT.getApiTokenFromResponse(createSuperuser);
String superusername = UtilIT.getUsernameFromResponse(createSuperuser);
UtilIT.makeSuperUser(superusername).then().assertThat().statusCode(200);
Response storageDrivers = listStorageDrivers(superuserApiToken);
Response storageDrivers = UtilIT.listStorageDrivers(superuserApiToken);
storageDrivers.prettyPrint();
// TODO where is "Local/local" coming from?
String drivers = """
Expand All @@ -127,18 +127,18 @@ public void testNonDirectUpload() {
createDataverseResponse.prettyPrint();
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

Response originalStorageDriver = getStorageDriver(dataverseAlias, superuserApiToken);
Response originalStorageDriver = UtilIT.getStorageDriver(dataverseAlias, superuserApiToken);
originalStorageDriver.prettyPrint();
originalStorageDriver.then().assertThat()
.body("data.message", equalTo("undefined"))
.statusCode(200);

Response setStorageDriverToS3 = setStorageDriver(dataverseAlias, driverLabel, superuserApiToken);
Response setStorageDriverToS3 = UtilIT.setStorageDriver(dataverseAlias, driverLabel, superuserApiToken);
setStorageDriverToS3.prettyPrint();
setStorageDriverToS3.then().assertThat()
.statusCode(200);

Response updatedStorageDriver = getStorageDriver(dataverseAlias, superuserApiToken);
Response updatedStorageDriver = UtilIT.getStorageDriver(dataverseAlias, superuserApiToken);
updatedStorageDriver.prettyPrint();
updatedStorageDriver.then().assertThat()
.statusCode(200);
Expand Down Expand Up @@ -214,7 +214,7 @@ public void testDirectUpload() {
String superuserApiToken = UtilIT.getApiTokenFromResponse(createSuperuser);
String superusername = UtilIT.getUsernameFromResponse(createSuperuser);
UtilIT.makeSuperUser(superusername).then().assertThat().statusCode(200);
Response storageDrivers = listStorageDrivers(superuserApiToken);
Response storageDrivers = UtilIT.listStorageDrivers(superuserApiToken);
storageDrivers.prettyPrint();
// TODO where is "Local/local" coming from?
String drivers = """
Expand All @@ -238,18 +238,18 @@ public void testDirectUpload() {
createDataverseResponse.prettyPrint();
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

Response originalStorageDriver = getStorageDriver(dataverseAlias, superuserApiToken);
Response originalStorageDriver = UtilIT.getStorageDriver(dataverseAlias, superuserApiToken);
originalStorageDriver.prettyPrint();
originalStorageDriver.then().assertThat()
.body("data.message", equalTo("undefined"))
.statusCode(200);

Response setStorageDriverToS3 = setStorageDriver(dataverseAlias, driverLabel, superuserApiToken);
Response setStorageDriverToS3 = UtilIT.setStorageDriver(dataverseAlias, driverLabel, superuserApiToken);
setStorageDriverToS3.prettyPrint();
setStorageDriverToS3.then().assertThat()
.statusCode(200);

Response updatedStorageDriver = getStorageDriver(dataverseAlias, superuserApiToken);
Response updatedStorageDriver = UtilIT.getStorageDriver(dataverseAlias, superuserApiToken);
updatedStorageDriver.prettyPrint();
updatedStorageDriver.then().assertThat()
.statusCode(200);
Expand All @@ -275,7 +275,7 @@ public void testDirectUpload() {
//
// String fileId = JsonPath.from(addFileResponse.body().asString()).getString("data.files[0].dataFile.id");
long size = 1000000000l;
Response getUploadUrls = getUploadUrls(datasetPid, size, apiToken);
Response getUploadUrls = UtilIT.getUploadUrls(datasetPid, size, apiToken);
getUploadUrls.prettyPrint();
getUploadUrls.then().assertThat().statusCode(200);

Expand All @@ -298,7 +298,7 @@ public void testDirectUpload() {
String contentsOfFile = "foobar";

InputStream inputStream = new ByteArrayInputStream(contentsOfFile.getBytes(StandardCharsets.UTF_8));
Response uploadFileDirect = uploadFileDirect(localhostUrl, inputStream);
Response uploadFileDirect = UtilIT.uploadFileDirect(localhostUrl, inputStream);
uploadFileDirect.prettyPrint();
/*
Direct upload to MinIO is failing with errors like this:
Expand Down Expand Up @@ -357,7 +357,7 @@ public void testDirectUpload() {
assertEquals(contentsOfFile, s3Object);

System.out.println("direct download...");
Response getHeaders = downloadFileNoRedirect(Integer.valueOf(fileId), apiToken);
Response getHeaders = UtilIT.downloadFileNoRedirect(Integer.valueOf(fileId), apiToken);
for (Header header : getHeaders.getHeaders()) {
System.out.println("direct download header: " + header);
}
Expand All @@ -371,7 +371,7 @@ public void testDirectUpload() {
} catch (UnsupportedEncodingException ex) {
}

Response downloadFile = downloadFromUrl(decodedDownloadUrl);
Response downloadFile = UtilIT.downloadFromUrl(decodedDownloadUrl);
downloadFile.prettyPrint();
downloadFile.then().assertThat().statusCode(200);

Expand All @@ -394,55 +394,4 @@ public void testDirectUpload() {

}

//TODO: move these into UtilIT. They are here for now to avoid merge conflicts
static Response listStorageDrivers(String apiToken) {
return given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/admin/dataverse/storageDrivers");
}

static Response getStorageDriver(String dvAlias, String apiToken) {
return given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/admin/dataverse/" + dvAlias + "/storageDriver");
}

static Response setStorageDriver(String dvAlias, String label, String apiToken) {
return given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken)
.body(label)
.put("/api/admin/dataverse/" + dvAlias + "/storageDriver");
}

static Response getUploadUrls(String idOrPersistentIdOfDataset, long sizeInBytes, String apiToken) {
String idInPath = idOrPersistentIdOfDataset; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isCreatable(idOrPersistentIdOfDataset)) {
idInPath = ":persistentId";
optionalQueryParam = "&persistentId=" + idOrPersistentIdOfDataset;
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification.get("/api/datasets/" + idInPath + "/uploadurls?size=" + sizeInBytes + optionalQueryParam);
}

static Response uploadFileDirect(String url, InputStream inputStream) {
return given()
.header("x-amz-tagging", "dv-state=temp")
.body(inputStream)
.put(url);
}

static Response downloadFileNoRedirect(Integer fileId, String apiToken) {
return given().when().redirects().follow(false)
.get("/api/access/datafile/" + fileId + "?key=" + apiToken);
}

static Response downloadFromUrl(String url) {
return given().get(url);
}

}
50 changes: 50 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 @@ -2361,6 +2361,56 @@ static Response deleteStorageSite(long storageSiteId) {
.delete("/api/admin/storageSites/" + storageSiteId);
}

static Response listStorageDrivers(String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/admin/dataverse/storageDrivers");
}

static Response getStorageDriver(String dvAlias, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/admin/dataverse/" + dvAlias + "/storageDriver");
}

static Response setStorageDriver(String dvAlias, String label, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.body(label)
.put("/api/admin/dataverse/" + dvAlias + "/storageDriver");
}

static Response getUploadUrls(String idOrPersistentIdOfDataset, long sizeInBytes, String apiToken) {
String idInPath = idOrPersistentIdOfDataset; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isCreatable(idOrPersistentIdOfDataset)) {
idInPath = ":persistentId";
optionalQueryParam = "&persistentId=" + idOrPersistentIdOfDataset;
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification.get("/api/datasets/" + idInPath + "/uploadurls?size=" + sizeInBytes + optionalQueryParam);
}

static Response uploadFileDirect(String url, InputStream inputStream) {
return given()
.header("x-amz-tagging", "dv-state=temp")
.body(inputStream)
.put(url);
}

static Response downloadFileNoRedirect(Integer fileId, String apiToken) {
return given().when().redirects().follow(false)
.get("/api/access/datafile/" + fileId + "?key=" + apiToken);
}

static Response downloadFromUrl(String url) {
return given().get(url);
}

static Response metricsDataversesToMonth(String yyyymm, String queryParams) {
String optionalYyyyMm = "";
if (yyyymm != null) {
Expand Down

0 comments on commit b9f4891

Please sign in to comment.