From 8e4b74968c04f1c7b3ec7ef2323403621d57506a Mon Sep 17 00:00:00 2001 From: bmckinney Date: Mon, 24 Oct 2016 09:48:01 -0400 Subject: [PATCH] adds test to check that directory labels with leading or trailing separators fail --- .../iq/dataverse/api/FileMetadataIT.java | 20 +- ...t-with-files-invalid-directory-labels.json | 210 ++++++++++++++++++ .../json/complete-dataset-with-files.json | 4 +- 3 files changed, 229 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/json/complete-dataset-with-files-invalid-directory-labels.json diff --git a/src/test/java/edu/harvard/iq/dataverse/api/FileMetadataIT.java b/src/test/java/edu/harvard/iq/dataverse/api/FileMetadataIT.java index 1011844e58e..8f243451c68 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/FileMetadataIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/FileMetadataIT.java @@ -21,6 +21,7 @@ import com.jayway.restassured.RestAssured; import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.response.Response; import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.AfterClass; @@ -36,6 +37,8 @@ /** * FileMetadata Integration Tests + * + * @author bmckinney */ public class FileMetadataIT { @@ -133,6 +136,17 @@ public void tearDownDataverse() { @Test public void testJsonParserWithDirectoryLabels() { try { + + // try to create a dataset with directory labels that contain both leading and trailing file separators + Response resp = given() + .header(keyString, token) + .body(IOUtils.toString(classLoader.getResourceAsStream( + "json/complete-dataset-with-files-invalid-directory-labels.json"))) + .contentType("application/json") + .post("/api/dataverses/" + testName + "/datasets"); + // this request should fail since a @Pattern constraint exists in FileMetadata to prevent this + assertEquals(resp.getStatusCode(), 500); + // create dataset and set id dsId = given() .header(keyString, token) @@ -148,14 +162,14 @@ public void testJsonParserWithDirectoryLabels() { .then().assertThat().statusCode(200) .extract().jsonPath().getString("data.latestVersion.files[0].directoryLabel"); System.out.println("directoryLabel 1: " + dirLabel1); - assertEquals("data/subdir1/", dirLabel1); + assertEquals("data/subdir1", dirLabel1); String dirLabel2 = given() .header(keyString, token) .get("/api/datasets/" + dsId) .then().assertThat().statusCode(200) .extract().jsonPath().getString("data.latestVersion.files[1].directoryLabel"); System.out.println("directoryLabel 2: " + dirLabel2); - assertEquals("data/subdir2/", dirLabel2); + assertEquals("data/subdir2", dirLabel2); } catch (Exception e) { System.out.println("Error testJsonParserWithDirectoryLabels: " + e.getMessage()); @@ -163,5 +177,5 @@ public void testJsonParserWithDirectoryLabels() { fail(); } } - + } \ No newline at end of file diff --git a/src/test/resources/json/complete-dataset-with-files-invalid-directory-labels.json b/src/test/resources/json/complete-dataset-with-files-invalid-directory-labels.json new file mode 100644 index 00000000000..29bafe5f234 --- /dev/null +++ b/src/test/resources/json/complete-dataset-with-files-invalid-directory-labels.json @@ -0,0 +1,210 @@ +{ + "datasetVersion": { + "metadataBlocks": { + "citation": { + "displayName": "Citation Metadata", + "fields": [ + { + "typeName": "title", + "multiple": false, + "typeClass": "primitive", + "value": "X-Ray Diffraction data from Lin28/let-7d microRNA complex, source of 3TRZ structure" + }, + { + "typeName": "author", + "multiple": true, + "typeClass": "compound", + "value": [ + { + "authorName": { + "typeName": "authorName", + "multiple": false, + "typeClass": "primitive", + "value": "Sliz, Piotr" + }, + "authorAffiliation": { + "typeName": "authorAffiliation", + "multiple": false, + "typeClass": "primitive", + "value": "Harvard Medical School" + }, + "authorIdentifierScheme": { + "typeName": "authorIdentifierScheme", + "multiple": false, + "typeClass": "controlledVocabulary", + "value": "ORCID" + }, + "authorIdentifier": { + "typeName": "authorIdentifier", + "multiple": false, + "typeClass": "primitive", + "value": "0000-0002-6522-0835" + } + } + ] + }, + { + "typeName": "datasetContact", + "multiple": true, + "typeClass": "compound", + "value": [ + { + "datasetContactName": { + "typeName": "datasetContactName", + "multiple": false, + "typeClass": "primitive", + "value": "Admin, Dataverse" + }, + "datasetContactAffiliation": { + "typeName": "datasetContactAffiliation", + "multiple": false, + "typeClass": "primitive", + "value": "sbgrid.org / SBDG" + }, + "datasetContactEmail": { + "typeName": "datasetContactEmail", + "multiple": false, + "typeClass": "primitive", + "value": "generic@mailinator.com" + } + } + ] + }, + { + "typeName": "dsDescription", + "multiple": true, + "typeClass": "compound", + "value": [ + { + "dsDescriptionValue": { + "typeName": "dsDescriptionValue", + "multiple": false, + "typeClass": "primitive", + "value": "Native dataset" + } + } + ] + }, + { + "typeName": "subject", + "multiple": true, + "typeClass": "controlledVocabulary", + "value": [ + "Medicine, Health and Life Sciences", + "Other" + ] + }, + { + "typeName": "publication", + "multiple": true, + "typeClass": "compound", + "value": [ + { + "publicationIDType": { + "typeName": "publicationIDType", + "multiple": false, + "typeClass": "controlledVocabulary", + "value": "doi" + }, + "publicationIDNumber": { + "typeName": "publicationIDNumber", + "multiple": false, + "typeClass": "primitive", + "value": "10.1016/ j.cell.2011.10.020" + }, + "publicationURL": { + "typeName": "publicationURL", + "multiple": false, + "typeClass": "primitive", + "value": "http://dx.doi.org/10.1016/j.cell.2011.10.020" + } + } + ] + }, + { + "typeName": "contributor", + "multiple": true, + "typeClass": "compound", + "value": [ + { + "contributorType": { + "typeName": "contributorType", + "multiple": false, + "typeClass": "controlledVocabulary", + "value": "Data Collector" + }, + "contributorName": { + "typeName": "contributorName", + "multiple": false, + "typeClass": "primitive", + "value": "Nam, Yunsun" + } + } + ] + }, + { + "typeName": "depositor", + "multiple": false, + "typeClass": "primitive", + "value": "Sliz, Piotr" + }, + { + "typeName": "dateOfDeposit", + "multiple": false, + "typeClass": "primitive", + "value": "2015-04-13T18:14:17Z" + }, + { + "typeName": "relatedDatasets", + "multiple": true, + "typeClass": "primitive", + "value": [ + "doi:10.15785/SBGRID/3" + ] + } + ] + } + }, + + "files": [ + { + "description": "", + "label": "yn152_4_001.img", + "directoryLabel": "data/subdir1/", + "version": 1, + "datasetVersionId": 2, + "dataFile": { + "id": 424, + "filename": "yn152_4_001.img", + "contentType": "application/octet-stream", + "storageIdentifier": "data/subdir/yn152_4_001.img", + "originalFormatLabel": "UNKNOWN", + "checksum": { + "type": "MD5", + "value": "fc4ac06da6fd84785ed1b79cab9b714a73621ac7" + }, + "description": "" + } + }, + { + "description": "", + "label": "yn152_4_002.img", + "directoryLabel": "/data/subdir2", + "version": 1, + "datasetVersionId": 2, + "dataFile": { + "id": 425, + "filename": "yn152_4_002.img", + "contentType": "application/octet-stream", + "storageIdentifier": "data/subdir/yn152_4_002.img", + "originalFormatLabel": "UNKNOWN", + "checksum": { + "type": "MD5", + "value": "756c1963e34eb6dd8bb5b48333c4f20ce64a0307" + }, + "description": "" + } + } + ] + } +} \ No newline at end of file diff --git a/src/test/resources/json/complete-dataset-with-files.json b/src/test/resources/json/complete-dataset-with-files.json index 97fc185d1a8..93c0376ce15 100644 --- a/src/test/resources/json/complete-dataset-with-files.json +++ b/src/test/resources/json/complete-dataset-with-files.json @@ -170,7 +170,7 @@ { "description": "", "label": "yn152_4_001.img", - "directoryLabel": "data/subdir1/", + "directoryLabel": "data/subdir1", "version": 1, "datasetVersionId": 2, "dataFile": { @@ -189,7 +189,7 @@ { "description": "", "label": "yn152_4_002.img", - "directoryLabel": "data/subdir2/", + "directoryLabel": "data/subdir2", "version": 1, "datasetVersionId": 2, "dataFile": {