Skip to content

Commit

Permalink
set datasetType using Semantic API #10517
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jul 16, 2024
1 parent b3c2dce commit 9541d41
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JSONLDUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
34 changes: 28 additions & 6 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

}
9 changes: 9 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 @@ -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 {
Expand Down

0 comments on commit 9541d41

Please sign in to comment.