Skip to content

Commit 9b2fb8d

Browse files
committed
stop loading Codemeta in Docker, add setDisplayOnCreate API #10519
In Docker, we were testing with the Codemeta block. This made for a nice real-world story of creating a "software" dataset type and using it with the Codemeta block. The Codemeta block also has the advantage of having some fields that are set to displayOnCreate, which helped us make assertions that the code is working properly. It's the only non-citation block with fields set to displayOnCreate. However, Jenkins doesn't have the Codemeta block, meaning that tests are failing. Also, we aren't ready to promote the Codemeta block to be shipped with Dataverse because a new version is out: #10859 So, we are switching from Codemeta to the Astrophysics block. We create an "instrument" dataset type. Like all non-citation blocks (except for Codemeta), there are no fields that are set to displayOnCreate=true. Therefore, we added an API for this.
1 parent 57e0c71 commit 9b2fb8d

7 files changed

Lines changed: 86 additions & 44 deletions

File tree

doc/release-notes/10519-dataset-types.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ This will have the following effects for the APIs used by the new Dataverse UI (
77
- The list of fields shown when creating a dataset will include fields marked as "displayoncreate" (in the tsv/database) for metadata blocks (e.g. "CodeMeta") that are linked to the dataset type (e.g. "software") that is passed to the API.
88
- The metadata blocks shown when editing a dataset will include metadata blocks (e.g. "CodeMeta") that are linked to the dataset type (e.g. "software") that is passed to the API.
99

10-
The CodeMeta metadata block is now available in the Dockerized development environment.
10+
Mostly in order to write automated tests for the above, a [displayOnCreate](https://dataverse-guide--11001.org.readthedocs.build/en/11001/api/native-api.html#set-displayoncreate-for-a-dataset-field) API endpoint has been added.
1111

12-
For more information, see the guides and #10519.
12+
For more information, see the guides ([overview](https://dataverse-guide--11001.org.readthedocs.build/en/11001/user/dataset-management.html#dataset-types), [new APIs](https://dataverse-guide--11001.org.readthedocs.build/en/11001/api/native-api.html#link-dataset-type-with-metadata-blocks)), #10519 and #11001.

doc/sphinx-guides/source/api/native-api.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,6 +5090,27 @@ The fully expanded example above (without environment variables) looks like this
50905090
50915091
curl "https://demo.dataverse.org/api/datasetfields/facetables"
50925092
5093+
.. _setDisplayOnCreate:
5094+
5095+
Set displayOnCreate for a Dataset Field
5096+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5097+
5098+
Set displayOnCreate for a dataset field. See also :doc:`/admin/metadatacustomization` in the Admin Guide.
5099+
5100+
.. code-block:: bash
5101+
5102+
export SERVER_URL=http://localhost:8080
5103+
export FIELD=subtitle
5104+
export BOOLEAN=true
5105+
5106+
curl -X POST "$SERVER_URL/api/admin/datasetfield/setDisplayOnCreate?datasetFieldType=$FIELD&setDisplayOnCreate=$BOOLEAN"
5107+
5108+
The fully expanded example above (without environment variables) looks like this:
5109+
5110+
.. code-block:: bash
5111+
5112+
curl -X POST "http://localhost:8080/api/admin/datasetfield/setDisplayOnCreate?datasetFieldType=studyAssayCellType&setDisplayOnCreate=true"
5113+
50935114
.. _Notifications:
50945115
50955116
Notifications

modules/container-configbaker/scripts/bootstrap/dev/init.sh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ export API_TOKEN
1717
# ${ENV_OUT} comes from bootstrap.sh and will expose the saved information back to the host if enabled.
1818
echo "API_TOKEN=${API_TOKEN}" >> "${ENV_OUT}"
1919

20-
echo "Loading CodeMeta metadata block (needed for API tests)..."
21-
curl "${DATAVERSE_URL}/api/admin/datasetfield/load" -X POST --data-binary @/scripts/bootstrap/base/data/metadatablocks/codemeta.tsv -H "Content-type: text/tab-separated-values"
22-
23-
echo "Fetching Solr schema from Dataverse and running update-fields.sh..."
24-
curl "${DATAVERSE_URL}/api/admin/index/solr/schema" | /scripts/update-fields.sh /var/solr/data/collection1/conf/schema.xml
25-
26-
echo "Reloading Solr..."
27-
curl "http://solr:8983/solr/admin/cores?action=RELOAD&core=collection1"
28-
2920
echo "Publishing root dataverse..."
3021
curl -H "X-Dataverse-key:$API_TOKEN" -X POST "${DATAVERSE_URL}/api/dataverses/:root/actions/:publish"
3122

src/main/java/edu/harvard/iq/dataverse/api/DatasetFieldServiceApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.logging.Logger;
4343
import jakarta.persistence.NoResultException;
4444
import jakarta.persistence.TypedQuery;
45+
import jakarta.ws.rs.QueryParam;
4546
import jakarta.ws.rs.core.Response.Status;
4647

4748
import java.io.BufferedInputStream;
@@ -545,4 +546,19 @@ public static String getDataverseLangDirectory() {
545546
return dataverseLangDirectory;
546547
}
547548

549+
/**
550+
* Set setDisplayOnCreate for a DatasetFieldType.
551+
*/
552+
@POST
553+
@Path("/setDisplayOnCreate")
554+
public Response setDisplayOnCreate(@QueryParam("datasetFieldType") String datasetFieldTypeIn, @QueryParam("setDisplayOnCreate") boolean setDisplayOnCreateIn) {
555+
DatasetFieldType dft = datasetFieldService.findByName(datasetFieldTypeIn);
556+
if (dft == null) {
557+
return error(Status.NOT_FOUND, "Cound not find a DatasetFieldType by looking up " + datasetFieldTypeIn);
558+
}
559+
dft.setDisplayOnCreate(setDisplayOnCreateIn);
560+
DatasetFieldType saved = datasetFieldService.save(dft);
561+
return ok("DisplayOnCreate for DatasetFieldType " + saved.getName() + " is now " + saved.isDisplayOnCreate());
562+
}
563+
548564
}

src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,32 @@
2020

2121
public class DatasetTypesIT {
2222

23+
final static String INSTRUMENT = "instrument";
24+
2325
@BeforeAll
2426
public static void setUpClass() {
2527
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();
2628

27-
Response getSoftwareType = UtilIT.getDatasetType(DatasetType.DATASET_TYPE_SOFTWARE);
28-
getSoftwareType.prettyPrint();
29-
30-
String typeFound = JsonPath.from(getSoftwareType.getBody().asString()).getString("data.name");
31-
System.out.println("type found: " + typeFound);
32-
if (DatasetType.DATASET_TYPE_SOFTWARE.equals(typeFound)) {
33-
return;
34-
}
35-
36-
System.out.println("The \"software\" type wasn't found. Create it.");
3729
Response createUser = UtilIT.createRandomUser();
3830
createUser.then().assertThat().statusCode(OK.getStatusCode());
3931
String username = UtilIT.getUsernameFromResponse(createUser);
4032
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
4133
UtilIT.setSuperuserStatus(username, true).then().assertThat().statusCode(OK.getStatusCode());
4234

43-
String jsonIn = Json.createObjectBuilder().add("name", DatasetType.DATASET_TYPE_SOFTWARE).build().toString();
35+
ensureDatasetTypeIsPresent(DatasetType.DATASET_TYPE_SOFTWARE, apiToken);
36+
ensureDatasetTypeIsPresent(INSTRUMENT, apiToken);
37+
}
4438

39+
private static void ensureDatasetTypeIsPresent(String datasetType, String apiToken) {
40+
Response getDatasetType = UtilIT.getDatasetType(datasetType);
41+
getDatasetType.prettyPrint();
42+
String typeFound = JsonPath.from(getDatasetType.getBody().asString()).getString("data.name");
43+
System.out.println("type found: " + typeFound);
44+
if (datasetType.equals(typeFound)) {
45+
return;
46+
}
47+
System.out.println("The " + datasetType + "type wasn't found. Create it.");
48+
String jsonIn = Json.createObjectBuilder().add("name", datasetType).build().toString();
4549
Response typeAdded = UtilIT.addDatasetType(jsonIn, apiToken);
4650
typeAdded.prettyPrint();
4751
typeAdded.then().assertThat().statusCode(OK.getStatusCode());
@@ -402,23 +406,23 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() {
402406
}
403407

404408
@Test
405-
public void testLinkSoftwareToCodemeta() {
409+
public void testLinkInstrumentToAstro() {
406410
Response createUser = UtilIT.createRandomUser();
407411
createUser.then().assertThat().statusCode(OK.getStatusCode());
408412
String username = UtilIT.getUsernameFromResponse(createUser);
409413
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
410414
UtilIT.setSuperuserStatus(username, true).then().assertThat().statusCode(OK.getStatusCode());
411415

412416
String metadataBlockLink = """
413-
["codeMeta20"]
417+
["astrophysics"]
414418
//""";
415419

416-
String datasetType = "software";
417-
Response linkSoftwareToCodemeta = UtilIT.updateDatasetTypeLinksWithMetadataBlocks(datasetType, metadataBlockLink, apiToken);
418-
linkSoftwareToCodemeta.prettyPrint();
419-
linkSoftwareToCodemeta.then().assertThat().
420+
String datasetType = "instrument";
421+
Response linkInstrumentToAstro = UtilIT.updateDatasetTypeLinksWithMetadataBlocks(datasetType, metadataBlockLink, apiToken);
422+
linkInstrumentToAstro.prettyPrint();
423+
linkInstrumentToAstro.then().assertThat().
420424
statusCode(OK.getStatusCode())
421-
.body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("codeMeta20"));
425+
.body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("astrophysics"));
422426

423427
Response createDataverse = UtilIT.createRandomDataverse(apiToken);
424428
createDataverse.then().assertThat().statusCode(CREATED.getStatusCode());
@@ -428,58 +432,62 @@ public void testLinkSoftwareToCodemeta() {
428432

429433
UtilIT.publishDataverseViaNativeApi(dataverseAlias, apiToken).then().assertThat().statusCode(OK.getStatusCode());
430434

435+
// displayOnCreate will only be true for fields that are set this way in the database.
436+
// We set it here so we can make assertions below.
437+
UtilIT.setDisplayOnCreate("astroInstrument", true);
438+
431439
Response listBlocks = null;
432440
System.out.println("listing root collection blocks with display on create using dataset type " + datasetType);
433441
listBlocks = UtilIT.listMetadataBlocks(":root", true, true, datasetType, apiToken);
434442
listBlocks.prettyPrint();
435443
listBlocks.then().assertThat()
436444
.statusCode(OK.getStatusCode())
437445
.body("data[0].name", is("citation"))
438-
.body("data[1].name", is("codeMeta20"))
446+
.body("data[1].name", is("astrophysics"))
439447
.body("data[2].name", nullValue())
440448
.body("data[0].fields.title.displayOnCreate", equalTo(true))
441-
.body("data[1].fields.codeVersion.displayOnCreate", equalTo(true));
449+
.body("data[1].fields.astroInstrument.displayOnCreate", equalTo(true));
442450

443451
System.out.println("listing root collection blocks with all fields (not display on create) using dataset type " + datasetType);
444452
listBlocks = UtilIT.listMetadataBlocks(":root", false, true, datasetType, apiToken);
445453
listBlocks.prettyPrint();
446454
listBlocks.then().assertThat()
447455
.statusCode(OK.getStatusCode())
448456
.body("data[0].name", is("citation"))
449-
.body("data[1].name", is("codeMeta20"))
457+
.body("data[1].name", is("astrophysics"))
450458
.body("data[2].name", nullValue())
451459
.body("data[0].fields.title.displayOnCreate", equalTo(true))
452460
.body("data[0].fields.subtitle.displayOnCreate", equalTo(false))
453-
.body("data[1].fields.codeVersion.displayOnCreate", equalTo(true))
454-
.body("data[1].fields.issueTracker.displayOnCreate", equalTo(false));
461+
.body("data[1].fields.astroInstrument.displayOnCreate", equalTo(true))
462+
.body("data[1].fields.astroObject.displayOnCreate", equalTo(false));
455463

456464
System.out.println("listing " + dataverseAlias + " collection blocks with display on create using dataset type " + datasetType);
457465
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, true, datasetType, apiToken);
458466
listBlocks.prettyPrint();
459467
listBlocks.then().assertThat()
460468
.statusCode(OK.getStatusCode())
461469
.body("data[0].name", is("citation"))
462-
.body("data[1].name", is("codeMeta20"))
470+
.body("data[1].name", is("astrophysics"))
463471
.body("data[2].name", nullValue())
464472
.body("data[0].fields.title.displayOnCreate", equalTo(true))
465473
// subtitle is hidden because it is not "display on create"
466474
.body("data[0].fields.subtitle", nullValue())
467-
.body("data[1].fields.codeVersion.displayOnCreate", equalTo(true))
468-
// issueTracker is hidden because it is not "display on create"
469-
.body("data[1].fields.issueTracker", nullValue());
475+
.body("data[1].fields.astroInstrument.displayOnCreate", equalTo(true))
476+
// astroObject is hidden because it is not "display on create"
477+
.body("data[1].fields.astroObject", nullValue());
470478

471479
System.out.println("listing " + dataverseAlias + " collection blocks with all fields (not display on create) using dataset type " + datasetType);
472480
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, false, true, datasetType, apiToken);
473481
listBlocks.prettyPrint();
474482
listBlocks.then().assertThat()
475483
.statusCode(OK.getStatusCode())
476484
.body("data[0].name", is("citation"))
477-
.body("data[1].name", is("codeMeta20"))
485+
.body("data[1].name", is("astrophysics"))
478486
.body("data[2].name", nullValue())
479487
.body("data[0].fields.title.displayOnCreate", equalTo(true))
480488
.body("data[0].fields.subtitle.displayOnCreate", equalTo(false))
481-
.body("data[1].fields.codeVersion.displayOnCreate", equalTo(true))
482-
.body("data[1].fields.issueTracker.displayOnCreate", equalTo(false));
489+
.body("data[1].fields.astroInstrument.displayOnCreate", equalTo(true))
490+
.body("data[1].fields.astroObject.displayOnCreate", equalTo(false));
483491

484492
}
485493

src/test/java/edu/harvard/iq/dataverse/api/MetadataBlocksIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@ public static void setUpClass() {
2727
void testListMetadataBlocks() {
2828
// No optional params enabled
2929
Response listMetadataBlocksResponse = UtilIT.listMetadataBlocks(false, false);
30-
int expectedDefaultNumberOfMetadataBlocks = 7;
30+
int expectedDefaultNumberOfMetadataBlocks = 6;
3131
listMetadataBlocksResponse.then().assertThat()
3232
.statusCode(OK.getStatusCode())
3333
.body("data[0].fields", equalTo(null))
3434
.body("data.size()", equalTo(expectedDefaultNumberOfMetadataBlocks));
3535

3636
// onlyDisplayedOnCreate=true
3737
listMetadataBlocksResponse = UtilIT.listMetadataBlocks(true, false);
38-
int expectedOnlyDisplayedOnCreateNumberOfMetadataBlocks = 2;
38+
int expectedOnlyDisplayedOnCreateNumberOfMetadataBlocks = 1;
3939
listMetadataBlocksResponse.then().assertThat()
4040
.statusCode(OK.getStatusCode())
4141
.body("data[0].fields", equalTo(null))
4242
.body("data[0].displayName", equalTo("Citation Metadata"))
43-
.body("data[1].displayName", equalTo("Software Metadata (CodeMeta v2.0)"))
4443
.body("data.size()", equalTo(expectedOnlyDisplayedOnCreateNumberOfMetadataBlocks));
4544

4645
// returnDatasetFieldTypes=true

src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,13 @@ static Response getMetadataBlock(String block) {
828828
.get("/api/metadatablocks/" + block);
829829
}
830830

831+
static Response setDisplayOnCreate(String datasetFieldType, boolean setDisplayOnCreate) {
832+
return given()
833+
.queryParam("datasetFieldType", datasetFieldType)
834+
.queryParam("setDisplayOnCreate", setDisplayOnCreate)
835+
.post("/api/admin/datasetfield/setDisplayOnCreate");
836+
}
837+
831838
static private String getDatasetXml(String title, String author, String description) {
832839
String nullLicense = null;
833840
String nullRights = null;

0 commit comments

Comments
 (0)