Skip to content

Commit

Permalink
Review made by pdurbin
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromeroucou committed Nov 14, 2024
1 parent 61b8046 commit 646ebd5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
4 changes: 2 additions & 2 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6079,7 +6079,7 @@ Saved Search
~~~~~~~~~~~~
The Saved Search, Linked Dataverses, and Linked Datasets features are only accessible to superusers except for linking a dataset. The following API endpoints were added to help people with access to the "admin" API make use of these features in their current form. Keep in mind that they are partially experimental.
The update of all saved search is run by a timer once a week (See :ref:`saved-search-timer`) so if you just created a saved search, you can run manually ``makelinks`` endpoint that will find new dataverses and datasets that match the saved search and then link the search results to the dataverse in which the saved search is defined.
The update of all saved search is run by a timer once a week (See :ref:`saved-search-timer`) so if you just created a saved search, you can run manually the ``makelinks`` endpoint that will find new dataverses and datasets that match the saved search and then link the search results to the dataverse in which the saved search is defined.
List all saved searches. ::
Expand All @@ -6091,7 +6091,7 @@ List a saved search by database id. ::
Delete a saved search by database id.
The ``unlink=true`` query parameter unlinks all links (linked dataset or Dataverse collection) associated with the deleted saved search. Use of this parameter should be well considered as you cannot know if the links were created manually or by the saved search. After deleting a saved search with ``unlink=true``, we recommend running ``/makelinks/all`` just in case there was a dataset that was linked by another saved search. (Saved searches can link the same dataset.) Reindexing might be necessary as well.::
The ``unlink=true`` query parameter unlinks all links (linked dataset or Dataverse collection) associated with the deleted saved search. Use of this parameter should be well considered as you cannot know if the links were created manually or by the saved search. After deleting a saved search with ``unlink=true``, we recommend running ``/makelinks/all`` just in case there was a dataset that was linked by another saved search. (Saved searches can link the same dataset.) Reindexing might be necessary as well. ::
DELETE http://$SERVER/api/admin/savedsearches/$id?unlink=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Response delete(@PathParam("id") long doomedId, @QueryParam("unlink") boo
try {
wasDeleted = savedSearchSvc.delete(doomedId, unlink);
} catch (Exception e) {
return error(INTERNAL_SERVER_ERROR, "Problem while trying to unlink links of saved search id " + doomedId);
return error(INTERNAL_SERVER_ERROR, "Problem while trying to unlink links of saved search id " + doomedId + ". Exception: " + e.getLocalizedMessage());
}

if (wasDeleted) {
Expand Down
58 changes: 14 additions & 44 deletions src/test/java/edu/harvard/iq/dataverse/api/SavedSearchIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SavedSearchIT {

@BeforeAll
public static void setUpClass() {

RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();
}

@AfterAll
Expand Down Expand Up @@ -53,81 +53,55 @@ public void testSavedSearches() {
Integer datasetId2 = UtilIT.getDatasetIdFromResponse(createDatasetResponse2);

// missing body
Response resp = RestAssured.given()
.contentType("application/json")
.post("/api/admin/savedsearches");
Response resp = UtilIT.setSavedSearch();
resp.prettyPrint();
resp.then().assertThat()
.statusCode(INTERNAL_SERVER_ERROR.getStatusCode());

// creatorId null
resp = RestAssured.given()
.body(createSavedSearchJson("*", null, dataverseId, "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", null, dataverseId, "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode());

// creatorId string
resp = RestAssured.given()
.body(createSavedSearchJson("*", "1", dataverseId.toString(), "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", "1", dataverseId.toString(), "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode());

// creatorId not found
resp = RestAssured.given()
.body(createSavedSearchJson("*", 9999, dataverseId, "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", 9999, dataverseId, "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(NOT_FOUND.getStatusCode());

// definitionPointId null
resp = RestAssured.given()
.body(createSavedSearchJson("*", 1, null, "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", 1, null, "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode());

// definitionPointId string
resp = RestAssured.given()
.body(createSavedSearchJson("*", "1", "9999", "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", "1", "9999", "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode());

// definitionPointId not found
resp = RestAssured.given()
.body(createSavedSearchJson("*", 1, 9999, "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", 1, 9999, "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(NOT_FOUND.getStatusCode());

// missing filter
resp = RestAssured.given()
.body(createSavedSearchJson("*", 1, dataverseId))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", 1, dataverseId));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(OK.getStatusCode());

// create a saved search as superuser : OK
resp = RestAssured.given()
.body(createSavedSearchJson("*", 1, dataverseId, "subject_ss:Medicine, Health and Life Sciences"))
.contentType("application/json")
.post("/api/admin/savedsearches");
resp = UtilIT.setSavedSearch(createSavedSearchJson("*", 1, dataverseId, "subject_ss:Medicine, Health and Life Sciences"));
resp.prettyPrint();
resp.then().assertThat()
.statusCode(OK.getStatusCode());
Expand All @@ -136,8 +110,7 @@ public void testSavedSearches() {
Integer createdSavedSearchId = path.getInt("data.id");

// get list as non superuser : OK
Response getListReponse = RestAssured.given()
.get("/api/admin/savedsearches/list");
Response getListReponse = UtilIT.getSavedSearchList();
getListReponse.prettyPrint();
getListReponse.then().assertThat()
.statusCode(OK.getStatusCode());
Expand All @@ -146,22 +119,19 @@ public void testSavedSearches() {
List<Object> listBeforeDelete = path2.getList("data.savedSearches");

// makelinks/all as non superuser : OK
Response makelinksAll = RestAssured.given()
.put("/api/admin/savedsearches/makelinks/all");
Response makelinksAll = UtilIT.setSavedSearchMakelinksAll();
makelinksAll.prettyPrint();
makelinksAll.then().assertThat()
.statusCode(OK.getStatusCode());

//delete a saved search as non superuser : OK
Response deleteReponse = RestAssured.given()
.delete("/api/admin/savedsearches/" + createdSavedSearchId);
Response deleteReponse = UtilIT.deleteSavedSearchById(createdSavedSearchId);
deleteReponse.prettyPrint();
deleteReponse.then().assertThat()
.statusCode(OK.getStatusCode());

// check list count minus 1
getListReponse = RestAssured.given()
.get("/api/admin/savedsearches/list");
getListReponse = UtilIT.getSavedSearchList();
getListReponse.prettyPrint();
JsonPath path3 = JsonPath.from(getListReponse.body().asString());
List<Object> listAfterDelete = path3.getList("data.savedSearches");
Expand Down
33 changes: 31 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 @@ -4131,8 +4131,37 @@ static Response setDatasetStorageDriver(Integer datasetId, String driverLabel, S
.body(driverLabel)
.put("/api/datasets/" + datasetId + "/storageDriver");
}



/** GET on /api/admin/savedsearches/list */
static Response getSavedSearchList() {
return given().get("/api/admin/savedsearches/list");
}

/** POST on /api/admin/savedsearches without body */
static Response setSavedSearch() {
return given()
.contentType("application/json")
.post("/api/admin/savedsearches");
}

/** POST on /api/admin/savedsearches with body */
static Response setSavedSearch(String body) {
return given()
.body(body)
.contentType("application/json")
.post("/api/admin/savedsearches");
}

/** PUT on /api/admin/savedsearches/makelinks/all */
static Response setSavedSearchMakelinksAll() {
return given().put("/api/admin/savedsearches/makelinks/all");
}

/** DELETE on /api/admin/savedsearches/{id} with identifier */
static Response deleteSavedSearchById(Integer id) {
return given().delete("/api/admin/savedsearches/" + id);
}

//Globus Store related - not currently used

static Response getDatasetGlobusUploadParameters(Integer datasetId, String locale, String apiToken) {
Expand Down

0 comments on commit 646ebd5

Please sign in to comment.