Skip to content

Commit

Permalink
Merge pull request #11019 from Recherche-Data-Gouv/10893-improvements…
Browse files Browse the repository at this point in the history
…-by-pdurbin

Improvements on PR #10198
  • Loading branch information
ofahimIQSS authored Nov 22, 2024
2 parents 975907f + d77ff0d commit ffcc65c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 59 deletions.
57 changes: 45 additions & 12 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6085,29 +6085,62 @@ 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. ::
List All Saved Searches
^^^^^^^^^^^^^^^^^^^^^^^
GET http://$SERVER/api/admin/savedsearches/list
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
curl "$SERVER_URL/api/admin/savedsearches/list"
List a Saved Search by Database ID
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
export ID=1
curl "$SERVER_URL/api/admin/savedsearches/$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.
List a saved search by database id. ::
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
export ID=1
GET http://$SERVER/api/admin/savedsearches/$id
curl -X DELETE "$SERVER_URL/api/admin/savedsearches/$ID?unlink=true"
Delete a saved search by database id.
Execute a Saved Search and Make Links
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.::
Execute a saved search by database id and make links to Dataverse collections and datasets that are found. The JSON response indicates which Dataverse collections and datasets were newly linked versus already linked. The ``debug=true`` query parameter adds to the JSON response extra information about the saved search being executed (which you could also get by listing the saved search).
DELETE http://$SERVER/api/admin/savedsearches/$id?unlink=true
.. code-block:: bash
Execute a saved search by database id and make links to Dataverse collections and datasets that are found. The JSON response indicates which Dataverse collections and datasets were newly linked versus already linked. The ``debug=true`` query parameter adds to the JSON response extra information about the saved search being executed (which you could also get by listing the saved search). ::
export SERVER_URL=https://demo.dataverse.org
export ID=1
PUT http://$SERVER/api/admin/savedsearches/makelinks/$id?debug=true
curl -X PUT "$SERVER_URL/api/admin/savedsearches/makelinks/$ID?debug=true"
Execute all saved searches and make links to Dataverse collections and datasets that are found. ``debug`` works as described above. This happens automatically with a timer. For details, see :ref:`saved-search-timer` in the Admin Guide. ::
Execute All Saved Searches and Make Links
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Execute all saved searches and make links to Dataverse collections and datasets that are found. ``debug`` works as described above. This happens automatically with a timer. For details, see :ref:`saved-search-timer` in the Admin Guide.
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
export ID=1
PUT http://$SERVER/api/admin/savedsearches/makelinks/all?debug=true
curl -X PUT "$SERVER_URL/api/admin/savedsearches/makelinks/all?debug=true"
Dataset Integrity
~~~~~~~~~~~~~~~~~
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 @@ -4134,8 +4134,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 ffcc65c

Please sign in to comment.