Skip to content

Commit

Permalink
adding tests, doc changes, and formatting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenwinship committed Sep 25, 2024
1 parent 2c2cd86 commit 6228d3f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
12 changes: 9 additions & 3 deletions doc/sphinx-guides/source/admin/dataverses-datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,21 @@ Lists the link(s) created between a dataset and a Dataverse collection (see the

curl -H "X-Dataverse-key: $API_TOKEN" http://$SERVER/api/datasets/$linked-dataset-id/links

It returns a list in the following format:
It returns a list in the following format (new format as of v6.4):

.. code-block:: json
{
"status": "OK",
"data": {
"dataverses that link to dataset id 56782": [
"crc990 (id 18802)"
"id": 5,
"identifier": "FK2/OTCWMM",
"linked-dataverses": [
{
"id": 2,
"alias": "dataverse1",
"displayName": "Lab experiments 2023 June"
}
]
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -2076,16 +2076,16 @@ public Response getLinks(@Context ContainerRequestContext crc, @PathParam("id")
List<Dataverse> dvsThatLinkToThisDatasetId = dataverseSvc.findDataversesThatLinkToThisDatasetId(datasetId);
JsonArrayBuilder dataversesThatLinkToThisDatasetIdBuilder = Json.createArrayBuilder();
for (Dataverse dataverse : dvsThatLinkToThisDatasetId) {
JsonObjectBuilder entry = Json.createObjectBuilder();
entry.add("id", dataverse.getId());
entry.add("alias", dataverse.getAlias());
entry.add("displayName", dataverse.getDisplayName());
dataversesThatLinkToThisDatasetIdBuilder.add(entry);
JsonObjectBuilder datasetBuilder = Json.createObjectBuilder();
datasetBuilder.add("id", dataverse.getId());
datasetBuilder.add("alias", dataverse.getAlias());
datasetBuilder.add("displayName", dataverse.getDisplayName());
dataversesThatLinkToThisDatasetIdBuilder.add(datasetBuilder.build());
}
JsonObjectBuilder response = Json.createObjectBuilder();
response.add("id", datasetId);
response.add("identifier", dataset.getIdentifier());
response.add("dataverses that link to dataset id " + datasetId, dataversesThatLinkToThisDatasetIdBuilder);
response.add("linked-dataverses", dataversesThatLinkToThisDatasetIdBuilder);
return ok(response);
} catch (WrappedResponse wr) {
return wr.getResponse();
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2963,6 +2963,34 @@ public void testLinkingDatasets() {
linkDataset.then().assertThat()
.statusCode(OK.getStatusCode());

// Link another to test the list of linked datasets
Response createDataverse3 = UtilIT.createRandomDataverse(apiToken);
createDataverse3.prettyPrint();
createDataverse3.then().assertThat()
.statusCode(CREATED.getStatusCode());
String dataverse3Alias = UtilIT.getAliasFromResponse(createDataverse3);
Integer dataverse3Id = UtilIT.getDatasetIdFromResponse(createDataverse3);
linkDataset = UtilIT.linkDataset(datasetPid, dataverse3Alias, superuserApiToken);
linkDataset.prettyPrint();
linkDataset.then().assertThat()
.statusCode(OK.getStatusCode());
// get the list in Json format
Response linkDatasetsResponse = UtilIT.getDatasetLinks(datasetPid, superuserApiToken);
linkDatasetsResponse.prettyPrint();
linkDatasetsResponse.then().assertThat()
.statusCode(OK.getStatusCode());
JsonObject linkDatasets = Json.createReader(new StringReader(linkDatasetsResponse.asString())).readObject();
JsonArray lst = linkDatasets.getJsonObject("data").getJsonArray("linked-dataverses");
List<Integer> ids = List.of(dataverse2Id, dataverse3Id);
List<Integer> uniqueids = new ArrayList<>();
assertEquals(ids.size(), lst.size());
for (int i = 0; i < lst.size(); i++) {
int id = lst.getJsonObject(i).getInt("id");
assertTrue(ids.contains(id));
assertFalse(uniqueids.contains(id));
uniqueids.add(id);
}

//Experimental code for trying to trick test into thinking the dataset has been harvested
/*
createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverse1Alias, apiToken);
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/edu/harvard/iq/dataverse/api/MoveIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ public void testMoveLinkedDataset() {
.body("message", equalTo("Use the query parameter forceMove=true to complete the move. This dataset is linked to the new host dataverse or one of its parents. This move would remove the link to this dataset. "));

JsonObject linksBeforeData = Json.createReader(new StringReader(getLinksBefore.asString())).readObject();
assertEquals("OK", linksBeforeData.getString("status"));
assertEquals(dataverse2Alias + " (id " + dataverse2Id + ")", linksBeforeData.getJsonObject("data").getJsonArray("dataverses that link to dataset id " + datasetId).getString(0));
assertEquals(datasetId, linksBeforeData.getJsonObject("data").getInt("id"));
assertEquals(dataverse2Id, linksBeforeData.getJsonObject("data").getJsonArray("linked-dataverses").get(0).asJsonObject().getInt("id"));

boolean forceMove = true;
Response forceMoveLinkedDataset = UtilIT.moveDataset(datasetId.toString(), dataverse2Alias, forceMove, superuserApiToken);
Expand Down Expand Up @@ -308,8 +308,7 @@ public void testMoveLinkedDataset() {

JsonObject linksAfterData = Json.createReader(new StringReader(getLinksAfter.asString())).readObject();
assertEquals("OK", linksAfterData.getString("status"));
assertEquals(0, linksAfterData.getJsonObject("data").getJsonArray("dataverses that link to dataset id " + datasetId).size());

assertEquals(0, linksAfterData.getJsonObject("data").getJsonArray("linked-dataverses").size());
}

@Test
Expand Down

0 comments on commit 6228d3f

Please sign in to comment.