From 9541d415d03f9b3a994cbad27098a04c4a37fadc Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Tue, 16 Jul 2024 16:27:57 -0400 Subject: [PATCH] set datasetType using Semantic API #10517 --- .../iq/dataverse/util/json/JSONLDUtil.java | 12 +++++++ .../iq/dataverse/api/DatasetTypesIT.java | 34 +++++++++++++++---- .../edu/harvard/iq/dataverse/api/UtilIT.java | 9 +++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/util/json/JSONLDUtil.java b/src/main/java/edu/harvard/iq/dataverse/util/json/JSONLDUtil.java index 52491a5a7e1..512576adff7 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/json/JSONLDUtil.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/json/JSONLDUtil.java @@ -49,6 +49,7 @@ import com.apicatalog.jsonld.document.JsonDocument; import edu.harvard.iq.dataverse.DatasetVersion.VersionState; +import edu.harvard.iq.dataverse.dataset.DatasetType; import edu.harvard.iq.dataverse.license.License; import edu.harvard.iq.dataverse.license.LicenseServiceBean; import edu.harvard.iq.dataverse.pidproviders.PidProvider; @@ -96,6 +97,17 @@ public static Dataset updateDatasetMDFromJsonLD(Dataset ds, String jsonLDBody, //Store the metadatalanguage if sent - the caller needs to check whether it is allowed (as with any GlobalID) ds.setMetadataLanguage(jsonld.getString(JsonLDTerm.schemaOrg("inLanguage").getUrl(),null)); + try (StringReader rdr = new StringReader(jsonLDBody)) { + try (JsonReader jsonReader = Json.createReader(rdr)) { + JsonObject jsonObject = jsonReader.readObject(); + String datasetType = jsonObject.getString("datasetType", null); + logger.info("datasetType: " + datasetType); + if (datasetType != null) { + ds.setDatasetType(new DatasetType(DatasetType.Type.fromString(datasetType))); + } + } + } + dsv = updateDatasetVersionMDFromJsonLD(dsv, jsonld, metadataBlockSvc, datasetFieldSvc, append, migrating, licenseSvc); dsv.setDataset(ds); 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 bb3ec1e94ba..6c67e3d32f2 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java @@ -5,10 +5,8 @@ import io.restassured.response.Response; import static jakarta.ws.rs.core.Response.Status.CREATED; import static jakarta.ws.rs.core.Response.Status.OK; -import org.junit.jupiter.api.Assertions; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; public class DatasetTypesIT { @@ -46,14 +44,38 @@ public void testCreateSoftwareDatasetNative() { getDatasetJson.then().assertThat().statusCode(OK.getStatusCode()); String dataseType = JsonPath.from(getDatasetJson.getBody().asString()).getString("data.datasetType"); System.out.println("datasetType: " + dataseType); - assertEquals("software", dataseType); + assertEquals("SOFTWARE", dataseType); } - @Disabled @Test public void testCreateSoftwareDatasetSemantic() { - String jsonIn = "doc/sphinx-guides/source/_static/api/dataset-create-software.jsonld"; - System.out.println("semantic: " + jsonIn); + Response createUser = UtilIT.createRandomUser(); + createUser.then().assertThat().statusCode(OK.getStatusCode()); + String username = UtilIT.getUsernameFromResponse(createUser); + String apiToken = UtilIT.getApiTokenFromResponse(createUser); + + Response createDataverse = UtilIT.createRandomDataverse(apiToken); + createDataverse.then().assertThat().statusCode(CREATED.getStatusCode()); + String dataverseAlias = UtilIT.getAliasFromResponse(createDataverse); + Integer dataverseId = UtilIT.getDataverseIdFromResponse(createDataverse); + + String jsonIn = UtilIT.getDatasetJson("doc/sphinx-guides/source/_static/api/dataset-create-software.jsonld"); + + Response createSoftware = UtilIT.createDatasetSemantic(dataverseAlias, jsonIn, apiToken); + createSoftware.prettyPrint(); + createSoftware.then().assertThat() + .statusCode(CREATED.getStatusCode()); + + //TODO: try sending "junk" instead of "software". + Integer datasetId = UtilIT.getDatasetIdFromResponse(createSoftware); + String datasetPid = JsonPath.from(createSoftware.getBody().asString()).getString("data.persistentId"); + + Response getDatasetJson = UtilIT.nativeGet(datasetId, apiToken); + getDatasetJson.prettyPrint(); + getDatasetJson.then().assertThat().statusCode(OK.getStatusCode()); + String dataseType = JsonPath.from(getDatasetJson.getBody().asString()).getString("data.datasetType"); + System.out.println("datasetType: " + dataseType); + assertEquals("SOFTWARE", dataseType); } } 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 0216859b869..585b60834d4 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -497,6 +497,15 @@ static Response createDataset(String dataverseAlias, String datasetJson, String return createDatasetResponse; } + static Response createDatasetSemantic(String dataverseAlias, String datasetJson, String apiToken) { + Response response = given() + .header(API_TOKEN_HTTP_HEADER, apiToken) + .body(datasetJson) + .contentType("application/ld+json") + .post("/api/dataverses/" + dataverseAlias + "/datasets"); + return response; + } + static String getDatasetJson(String pathToJsonFile) { File datasetVersionJson = new File(pathToJsonFile); try {