Skip to content

Commit

Permalink
Merge pull request #10274 from IQSS/9748-fix-ExternalToolsIT
Browse files Browse the repository at this point in the history
#9748 delete tools only added by tests
  • Loading branch information
pdurbin authored Feb 5, 2024
2 parents 989d7b0 + bdc2c8e commit 98231c5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 69 deletions.
26 changes: 26 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/TestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,31 @@ public Response getExternalToolsForFile(@PathParam("id") String idSupplied, @Que
return wr.getResponse();
}
}

@Path("files/{id}/externalTool/{toolId}")
@GET
public Response getExternalToolForFileById(@PathParam("id") String idSupplied, @QueryParam("type") String typeSupplied, @PathParam("toolId") String toolId) {
ExternalTool.Type type;
try {
type = ExternalTool.Type.fromString(typeSupplied);
} catch (IllegalArgumentException ex) {
return error(BAD_REQUEST, ex.getLocalizedMessage());
}
try {
DataFile dataFile = findDataFileOrDie(idSupplied);
List<ExternalTool> datasetTools = externalToolService.findFileToolsByTypeAndContentType(type, dataFile.getContentType());
for (ExternalTool tool : datasetTools) {
ApiToken apiToken = externalToolService.getApiToken(getRequestApiKey());
ExternalToolHandler externalToolHandler = new ExternalToolHandler(tool, dataFile, apiToken, dataFile.getFileMetadata(), null);
JsonObjectBuilder toolToJson = externalToolService.getToolAsJsonWithQueryParameters(externalToolHandler);
if (externalToolService.meetsRequirements(tool, dataFile) && tool.getId().toString().equals(toolId)) {
return ok(toolToJson);
}
}
return error(BAD_REQUEST, "Could not find external tool with id of " + toolId);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}

}
121 changes: 52 additions & 69 deletions src/test/java/edu/harvard/iq/dataverse/api/ExternalToolsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,6 @@ public void testGetExternalTools() {
@Test
public void testFileLevelTool1() {

// Delete all external tools before testing.
Response getTools = UtilIT.getExternalTools();
getTools.prettyPrint();
getTools.then().assertThat()
.statusCode(OK.getStatusCode());
String body = getTools.getBody().asString();
JsonReader bodyObject = Json.createReader(new StringReader(body));
JsonArray tools = bodyObject.readObject().getJsonArray("data");
for (int i = 0; i < tools.size(); i++) {
JsonObject tool = tools.getJsonObject(i);
int id = tool.getInt("id");
Response deleteExternalTool = UtilIT.deleteExternalTool(id);
deleteExternalTool.prettyPrint();
}

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
createUser.then().assertThat()
Expand Down Expand Up @@ -116,7 +101,7 @@ public void testFileLevelTool1() {
.statusCode(OK.getStatusCode())
.body("data.displayName", CoreMatchers.equalTo("AwesomeTool"));

long toolId = JsonPath.from(addExternalTool.getBody().asString()).getLong("data.id");
Long toolId = JsonPath.from(addExternalTool.getBody().asString()).getLong("data.id");

Response getTool = UtilIT.getExternalTool(toolId);
getTool.prettyPrint();
Expand All @@ -130,41 +115,34 @@ public void testFileLevelTool1() {
.statusCode(BAD_REQUEST.getStatusCode())
.body("message", CoreMatchers.equalTo("Type must be one of these values: [explore, configure, preview, query]."));

Response getExternalToolsForTabularFiles = UtilIT.getExternalToolsForFile(tabularFileId.toString(), "explore", apiToken);
// Getting tool by tool Id to avoid issue where there are existing tools
String toolIdString = toolId.toString();
Response getExternalToolsForTabularFiles = UtilIT.getExternalToolForFileById(tabularFileId.toString(), "explore", apiToken, toolIdString);
getExternalToolsForTabularFiles.prettyPrint();

getExternalToolsForTabularFiles.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].displayName", CoreMatchers.equalTo("AwesomeTool"))
.body("data[0].scope", CoreMatchers.equalTo("file"))
.body("data[0].contentType", CoreMatchers.equalTo("text/tab-separated-values"))
.body("data[0].toolUrlWithQueryParams", CoreMatchers.equalTo("http://awesometool.com?fileid=" + tabularFileId + "&key=" + apiToken));
.body("data.displayName", CoreMatchers.equalTo("AwesomeTool"))
.body("data.scope", CoreMatchers.equalTo("file"))
.body("data.contentType", CoreMatchers.equalTo("text/tab-separated-values"))
.body("data.toolUrlWithQueryParams", CoreMatchers.equalTo("http://awesometool.com?fileid=" + tabularFileId + "&key=" + apiToken));

Response getExternalToolsForJuptyerNotebooks = UtilIT.getExternalToolsForFile(jupyterNotebookFileId.toString(), "explore", apiToken);
getExternalToolsForJuptyerNotebooks.prettyPrint();
getExternalToolsForJuptyerNotebooks.then().assertThat()
.statusCode(OK.getStatusCode())
// No tools for this file type.
.body("data", Matchers.hasSize(0));

//Delete the tool added by this test...
Response deleteExternalTool = UtilIT.deleteExternalTool(toolId);
deleteExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());
}

@Test
public void testDatasetLevelTool1() {

// Delete all external tools before testing.
Response getTools = UtilIT.getExternalTools();
getTools.prettyPrint();
getTools.then().assertThat()
.statusCode(OK.getStatusCode());
String body = getTools.getBody().asString();
JsonReader bodyObject = Json.createReader(new StringReader(body));
JsonArray tools = bodyObject.readObject().getJsonArray("data");
for (int i = 0; i < tools.size(); i++) {
JsonObject tool = tools.getJsonObject(i);
int id = tool.getInt("id");
Response deleteExternalTool = UtilIT.deleteExternalTool(id);
deleteExternalTool.prettyPrint();
}

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
createUser.then().assertThat()
Expand All @@ -184,7 +162,6 @@ public void testDatasetLevelTool1() {
createDataset.then().assertThat()
.statusCode(CREATED.getStatusCode());

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

Expand Down Expand Up @@ -219,6 +196,8 @@ public void testDatasetLevelTool1() {
addExternalTool.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.displayName", CoreMatchers.equalTo("DatasetTool1"));

long toolId = JsonPath.from(addExternalTool.getBody().asString()).getLong("data.id");

Response getExternalToolsByDatasetIdInvalidType = UtilIT.getExternalToolsForDataset(datasetId.toString(), "invalidType", apiToken);
getExternalToolsByDatasetIdInvalidType.prettyPrint();
Expand All @@ -233,27 +212,16 @@ public void testDatasetLevelTool1() {
.body("data[0].scope", CoreMatchers.equalTo("dataset"))
.body("data[0].toolUrlWithQueryParams", CoreMatchers.equalTo("http://datasettool1.com?datasetPid=" + datasetPid + "&key=" + apiToken))
.statusCode(OK.getStatusCode());


//Delete the tool added by this test...
Response deleteExternalTool = UtilIT.deleteExternalTool(toolId);
deleteExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());
}

@Test
public void testDatasetLevelToolConfigure() {

// Delete all external tools before testing.
Response getTools = UtilIT.getExternalTools();
getTools.prettyPrint();
getTools.then().assertThat()
.statusCode(OK.getStatusCode());
String body = getTools.getBody().asString();
JsonReader bodyObject = Json.createReader(new StringReader(body));
JsonArray tools = bodyObject.readObject().getJsonArray("data");
for (int i = 0; i < tools.size(); i++) {
JsonObject tool = tools.getJsonObject(i);
int id = tool.getInt("id");
Response deleteExternalTool = UtilIT.deleteExternalTool(id);
deleteExternalTool.prettyPrint();
}

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
createUser.then().assertThat()
Expand Down Expand Up @@ -302,6 +270,8 @@ public void testDatasetLevelToolConfigure() {
addExternalTool.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.displayName", CoreMatchers.equalTo("Dataset Configurator"));

long toolId = JsonPath.from(addExternalTool.getBody().asString()).getLong("data.id");

Response getExternalToolsByDatasetId = UtilIT.getExternalToolsForDataset(datasetId.toString(), "configure", apiToken);
getExternalToolsByDatasetId.prettyPrint();
Expand All @@ -311,6 +281,11 @@ public void testDatasetLevelToolConfigure() {
.body("data[0].types[0]", CoreMatchers.equalTo("configure"))
.body("data[0].toolUrlWithQueryParams", CoreMatchers.equalTo("https://datasetconfigurator.com?datasetPid=" + datasetPid))
.statusCode(OK.getStatusCode());

//Delete the tool added by this test...
Response deleteExternalTool = UtilIT.deleteExternalTool(toolId);
deleteExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());

}

Expand Down Expand Up @@ -400,12 +375,13 @@ public void deleteTools() {
String body = getTools.getBody().asString();
JsonReader bodyObject = Json.createReader(new StringReader(body));
JsonArray tools = bodyObject.readObject().getJsonArray("data");
/*
for (int i = 0; i < tools.size(); i++) {
JsonObject tool = tools.getJsonObject(i);
int id = tool.getInt("id");
Response deleteExternalTool = UtilIT.deleteExternalTool(id);
deleteExternalTool.prettyPrint();
}
}*/
}

// preview only
Expand Down Expand Up @@ -446,12 +422,20 @@ public void createToolShellScript() {
addExternalTool.prettyPrint();
addExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());

long toolId = JsonPath.from(addExternalTool.getBody().asString()).getLong("data.id");

//Delete the tool added by this test...
Response deleteExternalTool = UtilIT.deleteExternalTool(toolId);
deleteExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());
}

// explore only
@Disabled
@Test
public void createToolDataExplorer() {
/*
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("displayName", "Data Explorer");
job.add("description", "");
Expand Down Expand Up @@ -479,6 +463,14 @@ public void createToolDataExplorer() {
addExternalTool.prettyPrint();
addExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());
long toolId = JsonPath.from(addExternalTool.getBody().asString()).getLong("data.id");
//Delete the tool added by this test...
Response deleteExternalTool = UtilIT.deleteExternalTool(toolId);
deleteExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());
*/
}

// both preview and explore
Expand Down Expand Up @@ -527,21 +519,6 @@ public void createToolSpreadsheetViewer() {
@Test
public void testFileLevelToolWithAuxFileReq() throws IOException {

// Delete all external tools before testing.
Response getTools = UtilIT.getExternalTools();
getTools.prettyPrint();
getTools.then().assertThat()
.statusCode(OK.getStatusCode());
String body = getTools.getBody().asString();
JsonReader bodyObject = Json.createReader(new StringReader(body));
JsonArray tools = bodyObject.readObject().getJsonArray("data");
for (int i = 0; i < tools.size(); i++) {
JsonObject tool = tools.getJsonObject(i);
int id = tool.getInt("id");
Response deleteExternalTool = UtilIT.deleteExternalTool(id);
deleteExternalTool.prettyPrint();
}

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
createUser.then().assertThat()
Expand Down Expand Up @@ -640,6 +617,12 @@ public void testFileLevelToolWithAuxFileReq() throws IOException {
.body("data[0].displayName", CoreMatchers.equalTo("HDF5 Tool"))
.body("data[0].scope", CoreMatchers.equalTo("file"))
.body("data[0].contentType", CoreMatchers.equalTo("application/x-hdf5"));

//Delete the tool added by this test...
Response deleteExternalTool = UtilIT.deleteExternalTool(toolId);
deleteExternalTool.then().assertThat()
.statusCode(OK.getStatusCode());

}

}
15 changes: 15 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 @@ -2354,6 +2354,21 @@ static Response getExternalToolsForFile(String idOrPersistentIdOfFile, String ty
}
return requestSpecification.get("/api/admin/test/files/" + idInPath + "/externalTools?type=" + type + optionalQueryParam);
}

static Response getExternalToolForFileById(String idOrPersistentIdOfFile, String type, String apiToken, String toolId) {
String idInPath = idOrPersistentIdOfFile; // Assume it's a number.
String optionalQueryParam = ""; // If idOrPersistentId is a number we'll just put it in the path.
if (!NumberUtils.isCreatable(idOrPersistentIdOfFile)) {
idInPath = ":persistentId";
optionalQueryParam = "&persistentId=" + idOrPersistentIdOfFile;
}
RequestSpecification requestSpecification = given();
if (apiToken != null) {
requestSpecification = given()
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
}
return requestSpecification.get("/api/admin/test/files/" + idInPath + "/externalTool/" + toolId + "?type=" + type + optionalQueryParam);
}

static Response submitFeedback(JsonObjectBuilder job) {
return given()
Expand Down

0 comments on commit 98231c5

Please sign in to comment.