From 6e8f573f4f88b700becf491ec244db08d601541d Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Mon, 28 Oct 2024 15:49:24 -0400 Subject: [PATCH] handle recursion in link command --- .../impl/ListMetadataBlocksCommand.java | 38 +++++------ ...ateDatasetTypeLinksWithMetadataBlocks.java | 2 +- .../iq/dataverse/api/DatasetTypesIT.java | 63 +++++++++++++++---- 3 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/ListMetadataBlocksCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/ListMetadataBlocksCommand.java index 327cda35f39..c0818b01fe0 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/ListMetadataBlocksCommand.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/ListMetadataBlocksCommand.java @@ -8,11 +8,13 @@ import edu.harvard.iq.dataverse.engine.command.CommandContext; import edu.harvard.iq.dataverse.engine.command.DataverseRequest; import edu.harvard.iq.dataverse.engine.command.exception.CommandException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.logging.Logger; import java.util.stream.Stream; /** @@ -23,6 +25,8 @@ // no annotations here, since permissions are dynamically decided public class ListMetadataBlocksCommand extends AbstractCommand> { + private static final Logger logger = Logger.getLogger(ListMetadataBlocksCommand.class.getCanonicalName()); + private final Dataverse dataverse; private final boolean onlyDisplayedOnCreate; private final DatasetType datasetType; @@ -36,35 +40,27 @@ public ListMetadataBlocksCommand(DataverseRequest request, Dataverse dataverse, @Override public List execute(CommandContext ctxt) throws CommandException { - System.out.println("got to execute..."); if (onlyDisplayedOnCreate) { return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse); } - return dataverse.getMetadataBlocks(); + List orig = dataverse.getMetadataBlocks(); + List extraFromDatasetTypes = new ArrayList<>(); + if (datasetType != null) { + extraFromDatasetTypes = datasetType.getMetadataBlocks(); + } + return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList(); } private List listMetadataBlocksDisplayedOnCreate(CommandContext ctxt, Dataverse dataverse) { - System.out.println("got to listMetadataBlocksDisplayedOnCreate..."); - // TODO keep all this isMetadataBlockRoot and is root collection business? -// if (dataverse.isMetadataBlockRoot() || dataverse.getOwner() == null) { -// return ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse); -// } -// return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse.getOwner()); - List metadataBlocks = ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse); - if (datasetType == null) { - System.out.println("no dataset type, returning normal list"); - return metadataBlocks; - } else { - // Add the metadata blocks based on the dataset type - System.out.println("yes dataset type, returning extra"); - List extra = datasetType.getMetadataBlocks(); - System.out.println("size of extra: " + extra.size()); - for (MetadataBlock metadataBlock : extra) { - System.out.println("name: " + metadataBlock.getDisplayName()); + if (dataverse.isMetadataBlockRoot() || dataverse.getOwner() == null) { + List orig = ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse); + List extraFromDatasetTypes = new ArrayList<>(); + if (datasetType != null) { + extraFromDatasetTypes = datasetType.getMetadataBlocks(); } - return Stream.concat(metadataBlocks.stream(), extra.stream()).toList(); + return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList(); } - + return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse.getOwner()); } @Override diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetTypeLinksWithMetadataBlocks.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetTypeLinksWithMetadataBlocks.java index 11a79dd8cab..d04ff18ced6 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetTypeLinksWithMetadataBlocks.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetTypeLinksWithMetadataBlocks.java @@ -11,7 +11,7 @@ import java.util.List; // inspired by UpdateDataverseMetadataBlocksCommand -@RequiredPermissions({}) +@RequiredPermissions({}) //TODO add auth! public class UpdateDatasetTypeLinksWithMetadataBlocks extends AbstractVoidCommand { // rename to "to"? UpdateDatasetTypeLinksToMetadataBlocks diff --git a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java index 79db5129c52..923d8d004ba 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java @@ -16,6 +16,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItemInArray; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -281,11 +282,21 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() { String apiToken = UtilIT.getApiTokenFromResponse(createUser); UtilIT.setSuperuserStatus(username, true).then().assertThat().statusCode(OK.getStatusCode()); - Response getCitationBlock = UtilIT.getMetadataBlock("citation"); - getCitationBlock.prettyPrint(); - getCitationBlock.then().assertThat() + System.out.println("listing root collection blocks with display on create: only citation"); + Response listBlocks = UtilIT.listMetadataBlocks(":root", true, false, apiToken); + listBlocks.prettyPrint(); + listBlocks.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data[0].name", is("citation")) + .body("data[1].name", nullValue()); + + System.out.println("listing root collection blocks without display on create: only citation"); + listBlocks = UtilIT.listMetadataBlocks(":root", false, false, apiToken); + listBlocks.prettyPrint(); + listBlocks.then().assertThat() .statusCode(OK.getStatusCode()) - .body("data.associatedDatasetTypes[0]", CoreMatchers.nullValue()); + .body("data[0].name", is("citation")) + .body("data[1].name", nullValue()); //Avoid all-numeric names (which are not allowed) String randomName = "zzz" + UUID.randomUUID().toString().substring(0, 8); @@ -320,9 +331,26 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() { getTypeById = UtilIT.getDatasetType(typeId.toString()); getTypeById.prettyPrint(); getTypeById.then().assertThat() - .statusCode(OK.getStatusCode()); -// .statusCode(OK.getStatusCode()) -// .body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("geospatial")); + .statusCode(OK.getStatusCode()) + .body("data.linkedMetadataBlocks[0]", CoreMatchers.is("geospatial")); + + System.out.println("listing root collection blocks with display on create"); + listBlocks = UtilIT.listMetadataBlocks(":root", true, false, randomName, apiToken); + listBlocks.prettyPrint(); + listBlocks.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data[0].name", is("citation")) + .body("data[1].name", is("geospatial")) + .body("data[2].name", nullValue()); + + System.out.println("listing root collection blocks without display on create"); + listBlocks = UtilIT.listMetadataBlocks(":root", false, false, randomName, apiToken); + listBlocks.prettyPrint(); + listBlocks.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data[0].name", is("citation")) + .body("data[1].name", is("geospatial")) + .body("data[2].name", nullValue()); Response createDataverse = UtilIT.createRandomDataverse(apiToken); createDataverse.then().assertThat().statusCode(CREATED.getStatusCode()); @@ -330,11 +358,25 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() { String dataverseAlias = UtilIT.getAliasFromResponse(createDataverse); Integer dataverseId = UtilIT.getDataverseIdFromResponse(createDataverse); - Response listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken); + UtilIT.publishDataverseViaNativeApi(dataverseAlias, apiToken).then().assertThat().statusCode(OK.getStatusCode()); + + System.out.println("listing " + dataverseAlias + "collection blocks with display on create using dataset type " + randomName); + listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken); + listBlocks.prettyPrint(); + listBlocks.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data[0].name", is("citation")) + .body("data[1].name", is("geospatial")) + .body("data[2].name", nullValue()); + + System.out.println("listing " + dataverseAlias + "collection blocks without display on create using dataset type " + randomName); + listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, false, false, randomName, apiToken); listBlocks.prettyPrint(); listBlocks.then().assertThat() .statusCode(OK.getStatusCode()) - .body("data[0].name", is("geospatial")); + .body("data[0].name", is("citation")) + .body("data[1].name", is("geospatial")) + .body("data[2].name", nullValue()); // We send an empty array to mean "delete or clear all" String emptyJsonArray = "[]"; @@ -347,8 +389,7 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() { listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken); listBlocks.prettyPrint(); listBlocks.then().assertThat() - .statusCode(OK.getStatusCode()) - .body("data[0].name", CoreMatchers.nullValue()); + .body("data[0].name", is("citation")); } }