Skip to content

Commit

Permalink
assert that importing software via JSON works #10517
Browse files Browse the repository at this point in the history
Also fix case of returned dataset type.
  • Loading branch information
pdurbin committed Jul 17, 2024
1 parent 9541d41 commit 4f055b6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

}
}
25 changes: 25 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit 4f055b6

Please sign in to comment.