From ca62d991b9bd30044e3a0004e3516a59f6df77cd Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Fri, 25 Oct 2024 17:07:21 -0400 Subject: [PATCH] more cleanup --- linktest.sh | 2 +- .../harvard/iq/dataverse/MetadataBlock.java | 24 +- .../dataverse/MetadataBlockServiceBean.java | 10 - .../harvard/iq/dataverse/api/Datasets.java | 19 +- .../iq/dataverse/api/MetadataBlocks.java | 69 ------ .../iq/dataverse/dataset/DatasetType.java | 8 +- ...ateDatasetTypeLinksWithMetadataBlocks.java | 1 + ...eMetadataBlockDatasetTypeAssociations.java | 114 --------- .../iq/dataverse/util/json/JsonPrinter.java | 6 - .../iq/dataverse/api/DatasetTypesIT.java | 227 ++---------------- .../edu/harvard/iq/dataverse/api/UtilIT.java | 37 +-- 11 files changed, 41 insertions(+), 476 deletions(-) delete mode 100644 src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateMetadataBlockDatasetTypeAssociations.java diff --git a/linktest.sh b/linktest.sh index 0e87055b49b..bc6734226aa 100755 --- a/linktest.sh +++ b/linktest.sh @@ -1,4 +1,4 @@ #!/bin/bash -x curl -s http://localhost:8080/api/datasets/datasetTypes/dataset -X PUT -d '["geospatial","astrophysics"]' | jq . -curl -s http://localhost:8080/api/datasets/datasetTypes | jq . +curl -s http://localhost:8080/api/datasets/datasetTypes/dataset | jq . curl -s "http://localhost:8080/api/dataverses/root/metadatablocks?onlyDisplayedOnCreate=true&datasetType=dataset" | jq . diff --git a/src/main/java/edu/harvard/iq/dataverse/MetadataBlock.java b/src/main/java/edu/harvard/iq/dataverse/MetadataBlock.java index 2953708a29c..0fd7c2efbc7 100644 --- a/src/main/java/edu/harvard/iq/dataverse/MetadataBlock.java +++ b/src/main/java/edu/harvard/iq/dataverse/MetadataBlock.java @@ -1,6 +1,5 @@ package edu.harvard.iq.dataverse; -import edu.harvard.iq.dataverse.dataset.DatasetType; import edu.harvard.iq.dataverse.util.BundleUtil; import edu.harvard.iq.dataverse.util.SystemConfig; import edu.harvard.iq.dataverse.util.json.JsonLDNamespace; @@ -17,7 +16,6 @@ import jakarta.persistence.Id; import jakarta.persistence.Index; import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToMany; import jakarta.persistence.NamedQueries; import jakarta.persistence.NamedQuery; import jakarta.persistence.OneToMany; @@ -25,7 +23,6 @@ import jakarta.persistence.OrderBy; import jakarta.persistence.Table; import jakarta.persistence.Transient; -import java.util.ArrayList; /** * @@ -35,8 +32,7 @@ , @Index(columnList="owner_id")}) @NamedQueries({ @NamedQuery( name="MetadataBlock.listAll", query = "SELECT mdb FROM MetadataBlock mdb"), - @NamedQuery( name="MetadataBlock.findByName", query = "SELECT mdb FROM MetadataBlock mdb WHERE mdb.name=:name"), - @NamedQuery( name="MetadataBlock.findByDatasetType", query = "SELECT mdb FROM MetadataBlock mdb WHERE mdb.name=:name") + @NamedQuery( name="MetadataBlock.findByName", query = "SELECT mdb FROM MetadataBlock mdb WHERE mdb.name=:name") }) @Entity public class MetadataBlock implements Serializable, Comparable { @@ -54,14 +50,7 @@ public class MetadataBlock implements Serializable, Comparable { @Column( name = "namespaceuri", columnDefinition = "TEXT") private String namespaceUri; - - /** - * The dataset types this metadata block is associated with. - */ -// @ManyToMany(mappedBy = "metadataBlocks", cascade = {CascadeType.MERGE}) -// @ManyToMany(cascade = {CascadeType.MERGE}) -// private List datasetTypes = new ArrayList<>(); - + public Long getId() { return id; } @@ -76,15 +65,6 @@ public void setName(String name) { this.name = name; } -// public List getDatasetTypes() { -// return datasetTypes; -// } -// -// public void setDatasetTypes(List datasetTypes) { -// // should this be ArrayList? this.metadataBlocks = new ArrayList<>(metadataBlocks); -// this.datasetTypes = datasetTypes; -// } - public String getNamespaceUri() { return namespaceUri; } diff --git a/src/main/java/edu/harvard/iq/dataverse/MetadataBlockServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/MetadataBlockServiceBean.java index 4810dfa89f1..1e2a34f5472 100644 --- a/src/main/java/edu/harvard/iq/dataverse/MetadataBlockServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/MetadataBlockServiceBean.java @@ -1,6 +1,5 @@ package edu.harvard.iq.dataverse; -import edu.harvard.iq.dataverse.dataset.DatasetType; import jakarta.ejb.Stateless; import jakarta.inject.Named; import jakarta.persistence.EntityManager; @@ -36,14 +35,6 @@ public List listMetadataBlocks(boolean onlyDisplayedOnCreate) { return em.createNamedQuery("MetadataBlock.listAll", MetadataBlock.class).getResultList(); } - public List listMetadataBlocksAssociatedWithDatasetTypes() { - return em.createNamedQuery("MetadataBlock.listAll", MetadataBlock.class).getResultList(); - } - - public List listMetadataBlocksAssociatedWithDatasetType(DatasetType datasetType) { - return em.createNamedQuery("MetadataBlock.listAll", MetadataBlock.class).getResultList(); - } - public MetadataBlock findById(Long id) { return em.find(MetadataBlock.class, id); } @@ -59,7 +50,6 @@ public MetadataBlock findByName(String name) { } public List listMetadataBlocksDisplayedOnCreate(Dataverse ownerDataverse) { - // TODO return based on dataset type CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(MetadataBlock.class); Root metadataBlockRoot = criteriaQuery.from(MetadataBlock.class); diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java index 86d6c755c4d..e46d44a26aa 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java @@ -5108,21 +5108,11 @@ public Response resetPidGenerator(@Context ContainerRequestContext crc, @PathPar @GET @Path("datasetTypes") public Response getDatasetTypes() { - System.out.println("got here"); JsonArrayBuilder jab = Json.createArrayBuilder(); - List datasetTypes = datasetTypeSvc.listAll(); - for (DatasetType datasetType : datasetTypes) { - JsonObjectBuilder job = Json.createObjectBuilder(); - job.add("id", datasetType.getId()); - job.add("name", datasetType.getName()); - JsonArrayBuilder linkedMetadataBlocks = Json.createArrayBuilder(); - for (MetadataBlock metadataBlock : datasetType.getMetadataBlocks()) { - linkedMetadataBlocks.add(metadataBlock.getName()); - } - job.add("linkedMetadataBlocks", linkedMetadataBlocks); - jab.add(job); - } - return ok(jab.build()); + for (DatasetType datasetType : datasetTypeSvc.listAll()) { + jab.add(datasetType.toJson()); + } + return ok(jab); } @GET @@ -5237,6 +5227,7 @@ public Response deleteDatasetType(@Context ContainerRequestContext crc, @PathPar } } + // TODO: PUT is better than POST, right? @AuthRequired @PUT @Path("datasetTypes/{idOrName}") diff --git a/src/main/java/edu/harvard/iq/dataverse/api/MetadataBlocks.java b/src/main/java/edu/harvard/iq/dataverse/api/MetadataBlocks.java index ecc861fdb7a..8861abd4803 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/MetadataBlocks.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/MetadataBlocks.java @@ -1,35 +1,12 @@ package edu.harvard.iq.dataverse.api; -import edu.harvard.iq.dataverse.DatasetFieldType; import edu.harvard.iq.dataverse.MetadataBlock; -import static edu.harvard.iq.dataverse.api.AbstractApiBean.error; -import edu.harvard.iq.dataverse.api.auth.AuthRequired; -import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; -import edu.harvard.iq.dataverse.dataset.DatasetType; -import edu.harvard.iq.dataverse.engine.command.DataverseRequest; -import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseMetadataBlocksCommand; -import edu.harvard.iq.dataverse.engine.command.impl.UpdateMetadataBlockDatasetTypeAssociations; import jakarta.ws.rs.*; import jakarta.ws.rs.core.Response; import java.util.List; import static edu.harvard.iq.dataverse.util.json.JsonPrinter.json; -import edu.harvard.iq.dataverse.util.json.JsonUtil; -import jakarta.json.Json; -import jakarta.json.JsonArray; -import jakarta.json.JsonArrayBuilder; -import jakarta.json.JsonNumber; -import jakarta.json.JsonObject; -import jakarta.json.JsonString; -import jakarta.json.JsonValue; -import jakarta.ws.rs.container.ContainerRequestContext; -import jakarta.ws.rs.core.Context; -import jakarta.ws.rs.core.MediaType; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Api bean for managing metadata blocks. @@ -53,50 +30,4 @@ public Response getMetadataBlock(@PathParam("identifier") String idtf) { MetadataBlock b = findMetadataBlock(idtf); return (b != null) ? ok(json(b)) : notFound("Can't find metadata block '" + idtf + "'"); } - - @AuthRequired - @PUT - @Path("{identifier}/datasetTypes") - public Response updateAssociationsWithDatasetTypes(@Context ContainerRequestContext crc, @PathParam("identifier") String idtf, String jsonBody) { - System.out.println("jsonbody: " + jsonBody); - // TODO move this to command? - MetadataBlock metadataBlock = findMetadataBlock(idtf); - if (metadataBlock == null) { - return notFound("Can't find metadata block '" + idtf + "'"); - } -// List datasetTypesExisting = metadataBlock.getDatasetTypes(); - JsonArrayBuilder datasetTypesBefore = Json.createArrayBuilder(); -// for (DatasetType datasetType : datasetTypesExisting) { -// datasetTypesBefore.add(datasetType.getName()); -// } - List datasetTypesToSave = new ArrayList<>(); - if (jsonBody != null && !jsonBody.isEmpty()) { - JsonArray json = JsonUtil.getJsonArray(jsonBody); - for (JsonString jsonValue : json.getValuesAs(JsonString.class)) { - String typeName = jsonValue.getString(); - System.out.println("typename: " + typeName); - DatasetType datasetType = datasetTypeSvc.getByName(typeName); - datasetTypesToSave.add(datasetType); - } - } - try { - MetadataBlock saved = execCommand(new UpdateMetadataBlockDatasetTypeAssociations(createDataverseRequest(getRequestUser(crc)), metadataBlock, datasetTypesToSave)); - // Move this to command -// List savedMdb = saved.getDatasetTypes(); - JsonArrayBuilder datasetTypesAfter = Json.createArrayBuilder(); -// for (DatasetType savedDatasetType : savedMdb) { -// System.out.println("found one: " + savedDatasetType.getName()); -// datasetTypesAfter.add(savedDatasetType.getName()); -// } - return ok(Json.createObjectBuilder() - .add("associatedDatasetTypes", Json.createObjectBuilder() - .add("before", datasetTypesBefore) - .add("after", datasetTypesAfter)) - ); - - } catch (WrappedResponse ex) { - return ex.getResponse(); - } - } - } diff --git a/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetType.java b/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetType.java index 7a40ea09d1c..ab23b39a367 100644 --- a/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetType.java +++ b/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetType.java @@ -2,6 +2,7 @@ import edu.harvard.iq.dataverse.MetadataBlock; import jakarta.json.Json; +import jakarta.json.JsonArrayBuilder; import jakarta.json.JsonObjectBuilder; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -81,9 +82,14 @@ public void setMetadataBlocks(List metadataBlocks) { } public JsonObjectBuilder toJson() { + JsonArrayBuilder linkedMetadataBlocks = Json.createArrayBuilder(); + for (MetadataBlock metadataBlock : this.getMetadataBlocks()) { + linkedMetadataBlocks.add(metadataBlock.getName()); + } return Json.createObjectBuilder() .add("id", getId()) - .add("name", getName()); + .add("name", getName()) + .add("linkedMetadataBlocks", linkedMetadataBlocks); } } 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 e06b91f3e3a..11a79dd8cab 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 @@ -13,6 +13,7 @@ // inspired by UpdateDataverseMetadataBlocksCommand @RequiredPermissions({}) public class UpdateDatasetTypeLinksWithMetadataBlocks extends AbstractVoidCommand { + // rename to "to"? UpdateDatasetTypeLinksToMetadataBlocks final DatasetType datasetType; List metadataBlocks; diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateMetadataBlockDatasetTypeAssociations.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateMetadataBlockDatasetTypeAssociations.java deleted file mode 100644 index 39c5dc75758..00000000000 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateMetadataBlockDatasetTypeAssociations.java +++ /dev/null @@ -1,114 +0,0 @@ -package edu.harvard.iq.dataverse.engine.command.impl; - -import edu.harvard.iq.dataverse.DvObject; -import edu.harvard.iq.dataverse.MetadataBlock; -import edu.harvard.iq.dataverse.dataset.DatasetType; -import edu.harvard.iq.dataverse.engine.command.AbstractCommand; -import edu.harvard.iq.dataverse.engine.command.CommandContext; -import edu.harvard.iq.dataverse.engine.command.DataverseRequest; -import edu.harvard.iq.dataverse.engine.command.RequiredPermissions; -import edu.harvard.iq.dataverse.engine.command.exception.CommandException; -import edu.harvard.iq.dataverse.engine.command.exception.PermissionException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -// inspired by UpdateDataverseMetadataBlocksCommand and DeactivateUserCommand -@RequiredPermissions({}) -public class UpdateMetadataBlockDatasetTypeAssociations extends AbstractCommand { - - private static final Logger logger = Logger.getLogger(UpdateMetadataBlockDatasetTypeAssociations.class.getCanonicalName()); - - private DataverseRequest dataverseRequest; - private MetadataBlock metadataBlock; - private List datasetTypes; - - public UpdateMetadataBlockDatasetTypeAssociations(DataverseRequest dataverseRequest, MetadataBlock metadataBlock, List datasetTypes) { - super(dataverseRequest, (DvObject) null); - this.dataverseRequest = dataverseRequest; - this.metadataBlock = metadataBlock; - this.datasetTypes = datasetTypes; - } - - @Override - public MetadataBlock execute(CommandContext ctxt) throws CommandException { - if (true) { - logger.info("exiting early"); -// metadataBlock.setDatasetTypes(datasetTypes); - MetadataBlock savedMetadataBlock = ctxt.em().merge(metadataBlock); - return savedMetadataBlock; - } - if (!getUser().isSuperuser()) { - throw new PermissionException("Command can only be called by superusers.", this, null, null); - } -// logger.info("before changing anything, block " + metadataBlock.getName() + " has these associations: " -// + metadataBlock.getDatasetTypes().stream() -// .map(DatasetType::getName) -// .collect(Collectors.joining(", "))); -// logger.info("about to set these types: " + datasetTypes.stream() -// .map(DatasetType::getName) -// .collect(Collectors.joining(", "))); -// metadataBlock.setDatasetTypes(datasetTypes); -// MetadataBlock savedMetadataBlock = ctxt.em().merge(metadataBlock); - if (datasetTypes.isEmpty()) { - // clear out all dataset types from metadata block - logger.info("dataset types is empty! clearing out"); -// for (DatasetType datasetType : datasetTypes) { -// List existing = datasetType.getMetadataBlocks(); -// List minusOne = existing; -// for (MetadataBlock mdb : existing) { -// if (mdb.equals(metadataBlock)) { -// minusOne.remove(mdb); -// datasetType.setMetadataBlocks(minusOne); -// if (true) { -// // just a test -// logger.info("just a test... sets to empty list"); -// datasetType.setMetadataBlocks(new ArrayList<>()); -// ctxt.em().merge(datasetType); -// } -// } -// } -// } -// MetadataBlock savedMetadataBlock2 = ctxt.em().merge(savedMetadataBlock); - logger.info("returning from is empty"); - return null; -// return savedMetadataBlock2; - } else { - // set incoming dataset types for this metadatablock - logger.info("datasetTypes was not empty"); - return null; - -// MetadataBlock savedMetadataBlock = ctxt.em().merge(metadataBlock); -// ctxt.em().flush(); -// for (DatasetType datasetType : savedMetadataBlock.getDatasetTypes()) { -// System.out.println("type: " + datasetType.getName()); -// List blocks = datasetType.getMetadataBlocks(); -// blocks.add(metadataBlock); -// // We filter the list through a set, so that all blocks are distinct. -// datasetType.setMetadataBlocks(new LinkedList<>(new HashSet<>(blocks))); -// ctxt.em().merge(datasetType); -// } -// // TODO save the block one more time? -//// return savedMetadataBlock; -// MetadataBlock savedMetadataBlock2 = ctxt.em().merge(savedMetadataBlock); -// logger.info("returning from not empty"); -// return savedMetadataBlock2; - } -// MetadataBlock savedMetadataBlock = ctxt.em().merge(metadataBlock); -//// ctxt.em().flush(); -// for (DatasetType datasetType : savedMetadataBlock.getDatasetTypes()) { -// System.out.println("type: " + datasetType.getName()); -// List blocks = datasetType.getMetadataBlocks(); -// blocks.add(metadataBlock); -// // We filter the list through a set, so that all blocks are distinct. -// datasetType.setMetadataBlocks(new LinkedList<>(new HashSet<>(blocks))); -// ctxt.em().merge(datasetType); -// } -// // TODO save the block one more time? -// return savedMetadataBlock; - } - -} diff --git a/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java b/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java index 25e333f730a..1bdee48b14d 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java @@ -657,12 +657,6 @@ public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printO } jsonObjectBuilder.add("fields", fieldsBuilder); - JsonArrayBuilder jab = Json.createArrayBuilder(); -// for (DatasetType datasetType : metadataBlock.getDatasetTypes()) { -// System.out.println("in jsonPrinter, for block " + metadataBlock.getName() + " adding " + datasetType.getName()); -// jab.add(datasetType.getName()); -// } - jsonObjectBuilder.add("associatedDatasetTypes", jab); return jsonObjectBuilder; } 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 839758ae58f..79db5129c52 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java @@ -274,7 +274,7 @@ public void testAddAndDeleteDatasetType() { } @Test - public void testUpdateMetadataBlockDatasetTypeAssociations() { + public void testUpdateDatasetTypeLinksWithMetadataBlocks() { Response createUser = UtilIT.createRandomUser(); createUser.then().assertThat().statusCode(OK.getStatusCode()); String username = UtilIT.getUsernameFromResponse(createUser); @@ -307,23 +307,22 @@ public void testUpdateMetadataBlockDatasetTypeAssociations() { .add(randomName) .build().toString(); - String metadataBlockToAssociateDatasetTypeWith = "geospatial"; + String metadataBlockToAssociateDatasetTypeWith = """ + ["geospatial"] +"""; - Response associateGeospatialWithDatasetType1 = UtilIT.updateMetadataBlockDatasetTypeAssociations(metadataBlockToAssociateDatasetTypeWith, updateToTheseTypes, apiToken); + Response associateGeospatialWithDatasetType1 = UtilIT.updateDatasetTypeLinksWithMetadataBlocks(randomName, metadataBlockToAssociateDatasetTypeWith, apiToken); associateGeospatialWithDatasetType1.prettyPrint(); associateGeospatialWithDatasetType1.then().assertThat(). statusCode(OK.getStatusCode()) - .body("data.associatedDatasetTypes.after[0]", CoreMatchers.is(randomName)); + .body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("geospatial")); - Response getGeospatialBlock = UtilIT.getMetadataBlock(metadataBlockToAssociateDatasetTypeWith); - getGeospatialBlock.prettyPrint(); - getGeospatialBlock.then().assertThat() + getTypeById = UtilIT.getDatasetType(typeId.toString()); + getTypeById.prettyPrint(); + getTypeById.then().assertThat() .statusCode(OK.getStatusCode()); - //TODO renable tests below // .statusCode(OK.getStatusCode()) -// .body("data.associatedDatasetTypes.before[0]", CoreMatchers.is(randomName)) -// .body("data.associatedDatasetTypes", CoreMatchers.containsString(randomName)); -// .body("data.associatedDatasetTypes[0]", CoreMatchers.is(randomName)); //this one +// .body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("geospatial")); Response createDataverse = UtilIT.createRandomDataverse(apiToken); createDataverse.then().assertThat().statusCode(CREATED.getStatusCode()); @@ -335,207 +334,21 @@ public void testUpdateMetadataBlockDatasetTypeAssociations() { listBlocks.prettyPrint(); listBlocks.then().assertThat() .statusCode(OK.getStatusCode()) - .body("data[0].name", is("citation")) - .body("data[1].name", is(metadataBlockToAssociateDatasetTypeWith)); - - Response removeDatasetTypeAssociationsFromGeospatial = UtilIT.updateMetadataBlockDatasetTypeAssociations(metadataBlockToAssociateDatasetTypeWith, null, apiToken); + .body("data[0].name", is("geospatial")); + + // We send an empty array to mean "delete or clear all" + String emptyJsonArray = "[]"; + Response removeDatasetTypeAssociationsFromGeospatial = UtilIT.updateDatasetTypeLinksWithMetadataBlocks(randomName, emptyJsonArray, apiToken); removeDatasetTypeAssociationsFromGeospatial.prettyPrint(); + removeDatasetTypeAssociationsFromGeospatial.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data.linkedMetadataBlocks.after[0]", CoreMatchers.nullValue()); - System.out.println("has geospatial been removed?"); listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken); listBlocks.prettyPrint(); - } - - @Test - public void testSetMetadataBlocks() { - Response createUserResponse = UtilIT.createRandomUser(); - String apiToken = UtilIT.getApiTokenFromResponse(createUserResponse); - - Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken); - createDataverseResponse.then().assertThat().statusCode(CREATED.getStatusCode()); - String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); - - Response setMetadataBlocksResponse = UtilIT.setMetadataBlocks(dataverseAlias, Json.createArrayBuilder().add("citation").add("astrophysics"), apiToken); - setMetadataBlocksResponse.prettyPrint(); - setMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - - Response listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, false, false, apiToken); - listBlocks.prettyPrint(); - listBlocks.then().assertThat().statusCode(OK.getStatusCode()); - -// setMetadataBlocksResponse = UtilIT.setMetadataBlocks(dataverseAlias, Json.createArrayBuilder().add("citation"), apiToken); - setMetadataBlocksResponse = UtilIT.setMetadataBlocks(dataverseAlias, Json.createArrayBuilder(), apiToken); - setMetadataBlocksResponse.prettyPrint(); - setMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - - listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, false, false, apiToken); - listBlocks.prettyPrint(); - listBlocks.then().assertThat().statusCode(OK.getStatusCode()); - - if (true) { - return; - } - - String[] testInputLevelNames = {"geographicCoverage", "country", "city", "notesText"}; - boolean[] testRequiredInputLevels = {false, true, false, false}; - boolean[] testIncludedInputLevels = {false, true, true, false}; - Response updateDataverseInputLevelsResponse = UtilIT.updateDataverseInputLevels(dataverseAlias, testInputLevelNames, testRequiredInputLevels, testIncludedInputLevels, apiToken); - updateDataverseInputLevelsResponse.prettyPrint(); - updateDataverseInputLevelsResponse.then().assertThat().statusCode(OK.getStatusCode()); - - // Dataverse not found - Response listMetadataBlocksResponse = null; -// listMetadataBlocksResponse = UtilIT.listMetadataBlocks("-1", false, false, apiToken); -// listMetadataBlocksResponse.prettyPrint(); -// listMetadataBlocksResponse.then().assertThat().statusCode(NOT_FOUND.getStatusCode()); -// if (true) return; - - // Existent dataverse and no optional params - String[] expectedAllMetadataBlockDisplayNames = {"Astronomy and Astrophysics Metadata", "Citation Metadata", "Geospatial Metadata"}; - - listMetadataBlocksResponse = UtilIT.listMetadataBlocks(dataverseAlias, false, false, apiToken); - listMetadataBlocksResponse.prettyPrint(); - listMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - listMetadataBlocksResponse.then().assertThat() - .statusCode(OK.getStatusCode()) - .body("data[0].fields", equalTo(null)) - .body("data[1].fields", equalTo(null)) - .body("data[2].fields", equalTo(null)) - .body("data.size()", equalTo(3)); - - String actualMetadataBlockDisplayName1 = listMetadataBlocksResponse.then().extract().path("data[0].displayName"); - String actualMetadataBlockDisplayName2 = listMetadataBlocksResponse.then().extract().path("data[1].displayName"); - String actualMetadataBlockDisplayName3 = listMetadataBlocksResponse.then().extract().path("data[2].displayName"); - assertNotEquals(actualMetadataBlockDisplayName1, actualMetadataBlockDisplayName2); - assertNotEquals(actualMetadataBlockDisplayName1, actualMetadataBlockDisplayName3); - assertNotEquals(actualMetadataBlockDisplayName2, actualMetadataBlockDisplayName3); - assertThat(expectedAllMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName1)); - assertThat(expectedAllMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName2)); - assertThat(expectedAllMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName3)); - - if (true) { - return; - } - - // Existent dataverse and onlyDisplayedOnCreate=true - String[] expectedOnlyDisplayedOnCreateMetadataBlockDisplayNames = {"Citation Metadata", "Geospatial Metadata"}; - - listMetadataBlocksResponse = UtilIT.listMetadataBlocks(dataverseAlias, true, false, apiToken); - listMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - listMetadataBlocksResponse.then().assertThat() - .statusCode(OK.getStatusCode()) - .body("data[0].fields", equalTo(null)) - .body("data[1].fields", equalTo(null)) - .body("data.size()", equalTo(2)); - - actualMetadataBlockDisplayName1 = listMetadataBlocksResponse.then().extract().path("data[0].displayName"); - actualMetadataBlockDisplayName2 = listMetadataBlocksResponse.then().extract().path("data[1].displayName"); - assertNotEquals(actualMetadataBlockDisplayName1, actualMetadataBlockDisplayName2); - assertThat(expectedOnlyDisplayedOnCreateMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName1)); - assertThat(expectedOnlyDisplayedOnCreateMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName2)); - - // Existent dataverse and returnDatasetFieldTypes=true - listMetadataBlocksResponse = UtilIT.listMetadataBlocks(dataverseAlias, false, true, apiToken); - listMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - listMetadataBlocksResponse.then().assertThat() - .statusCode(OK.getStatusCode()) - .body("data[0].fields", not(equalTo(null))) - .body("data[1].fields", not(equalTo(null))) - .body("data[2].fields", not(equalTo(null))) - .body("data.size()", equalTo(3)); - - actualMetadataBlockDisplayName1 = listMetadataBlocksResponse.then().extract().path("data[0].displayName"); - actualMetadataBlockDisplayName2 = listMetadataBlocksResponse.then().extract().path("data[1].displayName"); - actualMetadataBlockDisplayName3 = listMetadataBlocksResponse.then().extract().path("data[2].displayName"); - assertNotEquals(actualMetadataBlockDisplayName1, actualMetadataBlockDisplayName2); - assertNotEquals(actualMetadataBlockDisplayName1, actualMetadataBlockDisplayName3); - assertNotEquals(actualMetadataBlockDisplayName2, actualMetadataBlockDisplayName3); - assertThat(expectedAllMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName1)); - assertThat(expectedAllMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName2)); - assertThat(expectedAllMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName3)); - - // Check dataset fields for the updated input levels are retrieved - int geospatialMetadataBlockIndex = actualMetadataBlockDisplayName1.equals("Geospatial Metadata") ? 0 : actualMetadataBlockDisplayName2.equals("Geospatial Metadata") ? 1 : 2; - - // Since the included property of notesText is set to false, we should retrieve the total number of fields minus one - int citationMetadataBlockIndex = geospatialMetadataBlockIndex == 0 ? 1 : 0; - listMetadataBlocksResponse.then().assertThat() - .body(String.format("data[%d].fields.size()", citationMetadataBlockIndex), equalTo(79)); - - // Since the included property of geographicCoverage is set to false, we should retrieve the total number of fields minus one - listMetadataBlocksResponse.then().assertThat() - .body(String.format("data[%d].fields.size()", geospatialMetadataBlockIndex), equalTo(10)); - - String actualGeospatialMetadataField1 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.geographicCoverage.name", geospatialMetadataBlockIndex)); - String actualGeospatialMetadataField2 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.country.name", geospatialMetadataBlockIndex)); - String actualGeospatialMetadataField3 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.city.name", geospatialMetadataBlockIndex)); - - assertNull(actualGeospatialMetadataField1); - assertNotNull(actualGeospatialMetadataField2); - assertNotNull(actualGeospatialMetadataField3); - - // Existent dataverse and onlyDisplayedOnCreate=true and returnDatasetFieldTypes=true - listMetadataBlocksResponse = UtilIT.listMetadataBlocks(dataverseAlias, true, true, apiToken); - listMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - listMetadataBlocksResponse.then().assertThat() - .statusCode(OK.getStatusCode()) - .body("data[0].fields", not(equalTo(null))) - .body("data[1].fields", not(equalTo(null))) - .body("data.size()", equalTo(2)); - - actualMetadataBlockDisplayName1 = listMetadataBlocksResponse.then().extract().path("data[0].displayName"); - actualMetadataBlockDisplayName2 = listMetadataBlocksResponse.then().extract().path("data[1].displayName"); - assertNotEquals(actualMetadataBlockDisplayName1, actualMetadataBlockDisplayName2); - assertThat(expectedOnlyDisplayedOnCreateMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName1)); - assertThat(expectedOnlyDisplayedOnCreateMetadataBlockDisplayNames, hasItemInArray(actualMetadataBlockDisplayName2)); - - // Check dataset fields for the updated input levels are retrieved - geospatialMetadataBlockIndex = actualMetadataBlockDisplayName2.equals("Geospatial Metadata") ? 1 : 0; - - listMetadataBlocksResponse.then().assertThat() - .body(String.format("data[%d].fields.size()", geospatialMetadataBlockIndex), equalTo(1)); - - actualGeospatialMetadataField1 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.geographicCoverage.name", geospatialMetadataBlockIndex)); - actualGeospatialMetadataField2 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.country.name", geospatialMetadataBlockIndex)); - actualGeospatialMetadataField3 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.city.name", geospatialMetadataBlockIndex)); - - assertNull(actualGeospatialMetadataField1); - assertNotNull(actualGeospatialMetadataField2); - assertNull(actualGeospatialMetadataField3); - - citationMetadataBlockIndex = geospatialMetadataBlockIndex == 0 ? 1 : 0; - - // notesText has displayOnCreate=true but has include=false, so should not be retrieved - String notesTextCitationMetadataField = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.notesText.name", citationMetadataBlockIndex)); - assertNull(notesTextCitationMetadataField); - - // producerName is a conditionally required field, so should not be retrieved - String producerNameCitationMetadataField = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.producerName.name", citationMetadataBlockIndex)); - assertNull(producerNameCitationMetadataField); - - // author is a required field, so should be retrieved - String authorCitationMetadataField = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.author.name", citationMetadataBlockIndex)); - assertNotNull(authorCitationMetadataField); - - // User has no permissions on the requested dataverse - Response createSecondUserResponse = UtilIT.createRandomUser(); - String secondApiToken = UtilIT.getApiTokenFromResponse(createSecondUserResponse); - - createDataverseResponse = UtilIT.createRandomDataverse(secondApiToken); - createDataverseResponse.then().assertThat().statusCode(CREATED.getStatusCode()); - String secondDataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); - - listMetadataBlocksResponse = UtilIT.listMetadataBlocks(secondDataverseAlias, true, true, apiToken); - listMetadataBlocksResponse.then().assertThat().statusCode(UNAUTHORIZED.getStatusCode()); - - // List metadata blocks from Root - listMetadataBlocksResponse = UtilIT.listMetadataBlocks("root", true, true, apiToken); - listMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode()); - listMetadataBlocksResponse.then().assertThat() + listBlocks.then().assertThat() .statusCode(OK.getStatusCode()) - .body("data[0].displayName", equalTo("Citation Metadata")) - .body("data[0].fields", not(equalTo(null))) - .body("data.size()", equalTo(1)); + .body("data[0].name", CoreMatchers.nullValue()); } } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java index 690b6fc37da..bc7735143de 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -741,20 +741,6 @@ static Response getMetadataBlock(String block) { .get("/api/metadatablocks/" + block); } - static Response getMetadataBlockDatasetTypeAssociations(String block) { - return given() - .get("/api/metadatablocks/" + block + "/datasetTypes"); - } - - static Response updateMetadataBlockDatasetTypeAssociations(String block, String json, String apiToken) { - RequestSpecification requestSpecification = given() - .header(API_TOKEN_HTTP_HEADER, apiToken); - if (json != null) { - requestSpecification.body(json.getBytes(StandardCharsets.UTF_8)); - } - return requestSpecification.put("/api/metadatablocks/" + block + "/datasetTypes"); - } - static private String getDatasetXml(String title, String author, String description) { String nullLicense = null; String nullRights = null; @@ -4224,26 +4210,13 @@ static Response deleteDatasetTypes(long doomed, String apiToken) { .delete("/api/datasets/datasetTypes/" + doomed); } - static Response listDatasetTypeMetadataBlockAssociations(String apiToken) { + static Response updateDatasetTypeLinksWithMetadataBlocks(String idOrName, String jsonArrayOfMetadataBlocks, String apiToken) { return given() .header(API_TOKEN_HTTP_HEADER, apiToken) - .post("/api/datasets/datasetTypes/mdb"); + .body(jsonArrayOfMetadataBlocks) + // DO we need to send content type = json? + .contentType(ContentType.JSON) + .put("/api/datasets/datasetTypes/" + idOrName); } -// -// static Response addDatasetTypeMetadataBlockAssociation(String jsonIn, String apiToken) { -// return given() -// .header(API_TOKEN_HTTP_HEADER, apiToken) -// .body(jsonIn) -// .contentType(ContentType.JSON) -// .post("/api/datasets/datasetTypes/mdb"); -// } -// -// static Response removeDatasetTypeMetadataBlockAssociation(String jsonIn, String apiToken) { -// return given() -// .header(API_TOKEN_HTTP_HEADER, apiToken) -// .body(jsonIn) -// .contentType(ContentType.JSON) -// .delete("/api/datasets/datasetTypes/mdb"); -// } }