From 4f055b6d89e790ff4df6af490f7152e88e1c431d Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Wed, 17 Jul 2024 17:01:55 -0400 Subject: [PATCH] assert that importing software via JSON works #10517 Also fix case of returned dataset type. --- .../iq/dataverse/api/DatasetTypesIT.java | 37 ++++++++++++++++++- .../edu/harvard/iq/dataverse/api/UtilIT.java | 25 +++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) 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 6c67e3d32f2..e5f3eb20a7f 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java @@ -44,7 +44,7 @@ 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); } @Test @@ -75,7 +75,40 @@ public void testCreateSoftwareDatasetSemantic() { 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); } + @Test + public void testImportJson() { + Response createUser = UtilIT.createRandomUser(); + createUser.then().assertThat().statusCode(OK.getStatusCode()); + String username = UtilIT.getUsernameFromResponse(createUser); + String apiToken = UtilIT.getApiTokenFromResponse(createUser); + + UtilIT.setSuperuserStatus(username, true).then().assertThat().statusCode(OK.getStatusCode()); + + 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.json"); + + String randomString = UtilIT.getRandomString(6); + + Response importJson = UtilIT.importDatasetNativeJson(apiToken, dataverseAlias, jsonIn, "doi:10.5072/FK2/" + randomString, "no"); + importJson.prettyPrint(); + importJson.then().assertThat().statusCode(CREATED.getStatusCode()); + + Integer datasetId = JsonPath.from(importJson.getBody().asString()).getInt("data.id"); + String datasetPid = JsonPath.from(importJson.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 585b60834d4..3d5b97d00e8 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -3551,6 +3551,31 @@ private static DatasetField constructPrimitive(String fieldName, String value) { return field; } + static Response importDatasetNativeJson(String apiToken, String dataverseAlias, String jsonString, String pid, String release) { + + String postString = "/api/dataverses/" + dataverseAlias + "/datasets/:import"; + if (pid != null || release != null) { + //postString = postString + "?"; + if (pid != null) { + postString = postString + "?pid=" + pid; + if (release != null && release.compareTo("yes") == 0) { + postString = postString + "&release=" + release.toString(); + } + } else { + if (release != null && release.compareTo("yes") == 0) { + postString = postString + "?release=" + release.toString(); + } + } + } + + RequestSpecification importJson = given() + .header(API_TOKEN_HTTP_HEADER, apiToken) + .urlEncodingEnabled(false) + .body(jsonString) + .contentType("application/json"); + + return importJson.post(postString); + } static Response importDatasetDDIViaNativeApi(String apiToken, String dataverseAlias, String xml, String pid, String release) {