From 2848bf6208322b537a8b066d35c8733c3c49f157 Mon Sep 17 00:00:00 2001 From: MatthieuBarbet Date: Thu, 26 Sep 2024 17:08:49 +0200 Subject: [PATCH] Feat: add patch for fields display names --- .../core/model/CollectionReference.java | 1 + .../services/CollectionReferenceService.java | 27 ++- .../rest/collections/CollectionService.java | 105 +++++++- .../rest/collections/CollectionServiceIT.java | 229 +++++++++++++++++- 4 files changed, 348 insertions(+), 14 deletions(-) diff --git a/arlas-core/src/main/java/io/arlas/server/core/model/CollectionReference.java b/arlas-core/src/main/java/io/arlas/server/core/model/CollectionReference.java index 8bec88055..d10858685 100644 --- a/arlas-core/src/main/java/io/arlas/server/core/model/CollectionReference.java +++ b/arlas-core/src/main/java/io/arlas/server/core/model/CollectionReference.java @@ -42,6 +42,7 @@ public class CollectionReference implements Serializable { public static final String DISPLAY_NAMES = "display_names"; public static final String COLLECTION_DISPLAY_NAME = "collection"; public static final String FIELD_DISPLAY_NAME = "fields"; + public static final String SHAPE_COLUMN_DISPLAY_NAME = "shape_columns"; public static final String ORGANISATIONS = "organisations"; public static final String ORGANISATIONS_OWNER = "owner"; public static final String ORGANISATIONS_SHARED = "shared"; diff --git a/arlas-core/src/main/java/io/arlas/server/core/services/CollectionReferenceService.java b/arlas-core/src/main/java/io/arlas/server/core/services/CollectionReferenceService.java index 380291709..a810db93c 100644 --- a/arlas-core/src/main/java/io/arlas/server/core/services/CollectionReferenceService.java +++ b/arlas-core/src/main/java/io/arlas/server/core/services/CollectionReferenceService.java @@ -109,9 +109,10 @@ public CollectionReference putCollectionReference(CollectionReference collection return collectionReference; } - public CollectionReference updateCollectionDisplayNameCollectionReference(String collection, - String organisations, - String columnFilter, String displayName) + public CollectionReference updateDisplayNamesCollectionReference(String collection, String organisations,String columnFilter, + String collectionDisplayName, + Map fieldsDisplayNames, + Map shapeColumnsDisplayNames) throws ArlasException { CollectionReference collectionReference = getCollectionReference(collection, Optional.ofNullable(organisations)); ColumnFilterUtil.assertCollectionsAllowed(Optional.ofNullable(columnFilter), List.of(collectionReference)); @@ -119,11 +120,16 @@ public CollectionReference updateCollectionDisplayNameCollectionReference(String if (collectionReference.params.collectionDisplayNames == null) { collectionReference.params.collectionDisplayNames = new CollectionDisplayNames(); } - collectionReference.params.collectionDisplayNames.collection = displayName; - putCollectionReferenceWithDao(collectionReference); - cacheManager.removeCollectionReference(collectionReference.collectionName); - cacheManager.removeMapping(collectionReference.params.indexName); - return collectionReference; + if(fieldsDisplayNames != null){ + collectionReference.params.collectionDisplayNames.fields = fieldsDisplayNames; + } + if(shapeColumnsDisplayNames != null){ + collectionReference.params.collectionDisplayNames.shapeColumns = shapeColumnsDisplayNames; + } + if(collectionDisplayName != null){ + collectionReference.params.collectionDisplayNames.collection = collectionDisplayName; + } + return putCollectionReference(collectionReference, true); } public CollectionReference updateOrganisationsParamsCollectionReference(String collection, @@ -137,10 +143,7 @@ public CollectionReference updateOrganisationsParamsCollectionReference(String c checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true); collectionReference.params.collectionOrganisations.isPublic = isPublic; collectionReference.params.collectionOrganisations.sharedWith = sharedWith; - putCollectionReferenceWithDao(collectionReference); - cacheManager.removeCollectionReference(collectionReference.collectionName); - cacheManager.removeMapping(collectionReference.params.indexName); - return collectionReference; + return putCollectionReference(collectionReference, true); } public List describeAllCollections(List collectionReferenceList, diff --git a/arlas-rest/src/main/java/io/arlas/server/rest/collections/CollectionService.java b/arlas-rest/src/main/java/io/arlas/server/rest/collections/CollectionService.java index f1e7c0a92..54718fb56 100644 --- a/arlas-rest/src/main/java/io/arlas/server/rest/collections/CollectionService.java +++ b/arlas-rest/src/main/java/io/arlas/server/rest/collections/CollectionService.java @@ -389,7 +389,110 @@ public Response patchCollectionDisplayName( if (collection != null && collection.equals(META_COLLECTION_NAME)) { throw new NotAllowedException("'" + META_COLLECTION_NAME + "' cannot be updated"); } - return ResponseFormatter.getResultResponse(collectionReferenceService.updateCollectionDisplayNameCollectionReference(collection, organisations, columnFilter,collectionDisplayName )); + return ResponseFormatter.getResultResponse(collectionReferenceService.updateDisplayNamesCollectionReference(collection, organisations, columnFilter,collectionDisplayName, null, null )); + } + + + @Timed + @Path("{collection}/display_names/fields") + @PATCH + @Produces(UTF8JSON) + @Consumes(UTF8JSON) + @Operation( + summary = "Update a collection reference's display fields name attribute.", + description = "Update a collection reference's display fields name attribute." + ) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful operation", + content = @Content(schema = @Schema(implementation = CollectionReference.class))), + @ApiResponse(responseCode = "400", description = "JSON parameter malformed.", + content = @Content(schema = @Schema(implementation = Error.class))), + @ApiResponse(responseCode = "404", description = "Collection not found.", + content = @Content(schema = @Schema(implementation = Error.class))), + @ApiResponse(responseCode = "500", description = "Arlas Server Error.", + content = @Content(schema = @Schema(implementation = Error.class))) + }) + public Response patchFieldsDisplayNames( + @Context HttpHeaders headers, + @Parameter(name = "collection", + description = "collection", + required = true) + @PathParam(value = "collection") String collection, + + @Parameter(name = "fieldsDisplayNames", + description = "fieldsDisplayNames", + required = true) + @NotNull Map fieldsDisplayNames, + + @Parameter(hidden = true) + @HeaderParam(value = COLUMN_FILTER) String columnFilter, + + @Parameter(hidden = true) + @HeaderParam(value = ARLAS_ORGANISATION) String organisations, + // -------------------------------------------------------- + // ----------------------- FORM ----------------------- + // -------------------------------------------------------- + @Parameter(name = "pretty", + description = Documentation.FORM_PRETTY, + schema = @Schema(defaultValue = "false")) + @QueryParam(value = "pretty") Boolean pretty + + ) throws ArlasException { + if (collection != null && collection.equals(META_COLLECTION_NAME)) { + throw new NotAllowedException("'" + META_COLLECTION_NAME + "' cannot be updated"); + } + return ResponseFormatter.getResultResponse(collectionReferenceService.updateDisplayNamesCollectionReference(collection, organisations, columnFilter, null, fieldsDisplayNames, null )); + } + + @Timed + @Path("{collection}/display_names/shape_columns") + @PATCH + @Produces(UTF8JSON) + @Consumes(UTF8JSON) + @Operation( + summary = "Update a collection reference's display shape columns name attribute.", + description = "Update a collection reference's display shape columns name attribute." + ) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful operation", + content = @Content(schema = @Schema(implementation = CollectionReference.class))), + @ApiResponse(responseCode = "400", description = "JSON parameter malformed.", + content = @Content(schema = @Schema(implementation = Error.class))), + @ApiResponse(responseCode = "404", description = "Collection not found.", + content = @Content(schema = @Schema(implementation = Error.class))), + @ApiResponse(responseCode = "500", description = "Arlas Server Error.", + content = @Content(schema = @Schema(implementation = Error.class))) + }) + public Response patchShapeColumnsDisplayNames( + @Context HttpHeaders headers, + @Parameter(name = "collection", + description = "collection", + required = true) + @PathParam(value = "collection") String collection, + + @Parameter(name = "shapeColumnsDisplayNames", + description = "shapeColumnsDisplayNames", + required = true) + @NotNull Map shapeColumnsDisplayNames, + + @Parameter(hidden = true) + @HeaderParam(value = COLUMN_FILTER) String columnFilter, + + @Parameter(hidden = true) + @HeaderParam(value = ARLAS_ORGANISATION) String organisations, + // -------------------------------------------------------- + // ----------------------- FORM ----------------------- + // -------------------------------------------------------- + @Parameter(name = "pretty", + description = Documentation.FORM_PRETTY, + schema = @Schema(defaultValue = "false")) + @QueryParam(value = "pretty") Boolean pretty + + ) throws ArlasException { + if (collection != null && collection.equals(META_COLLECTION_NAME)) { + throw new NotAllowedException("'" + META_COLLECTION_NAME + "' cannot be updated"); + } + return ResponseFormatter.getResultResponse(collectionReferenceService.updateDisplayNamesCollectionReference(collection, organisations, columnFilter,null,null,shapeColumnsDisplayNames )); } public CollectionReference save(String collection, CollectionReferenceParameters collectionReferenceParameters, diff --git a/arlas-tests/src/test/java/io/arlas/server/tests/rest/collections/CollectionServiceIT.java b/arlas-tests/src/test/java/io/arlas/server/tests/rest/collections/CollectionServiceIT.java index 663c339f9..f665bebd3 100644 --- a/arlas-tests/src/test/java/io/arlas/server/tests/rest/collections/CollectionServiceIT.java +++ b/arlas-tests/src/test/java/io/arlas/server/tests/rest/collections/CollectionServiceIT.java @@ -412,7 +412,7 @@ public void test09Organisations() throws Exception { @Test - public void test10DisplayName() throws Exception { + public void test10CollectionDisplayName() throws Exception { Map jsonAsMap = getJsonAsMap("bar.com", null, false); jsonAsMap.put(CollectionReference.INSPIRE_PATH, getInspireJsonAsMap()); jsonAsMap.put(CollectionReference.DUBLIN_CORE_PATH, getDublinJsonAsMap()); @@ -462,6 +462,231 @@ public void test10DisplayName() throws Exception { } + @Test + public void test11FieldsDisplayName() throws Exception { + Map jsonAsMap = getJsonAsMap("bar.com", null, false); + jsonAsMap.put(CollectionReference.INSPIRE_PATH, getInspireJsonAsMap()); + jsonAsMap.put(CollectionReference.DUBLIN_CORE_PATH, getDublinJsonAsMap()); + jsonAsMap.put(CollectionReference.DISPLAY_NAMES, getCollectionDescriptionJsonAsMap()); + // PUT new collection + given().contentType("application/json") + .body(jsonAsMap) + .when().put(arlasPath + "collections/bar") + .then().statusCode(200); + + // GET collection + when().get(arlasPath + "collections/bar") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)) + .body("params.display_names.collection", equalTo(DataSetTool.DATASET_COLLECTION_DISPLAY_NAME)) + .body("params.display_names.fields.id", equalTo(DataSetTool.DATASET_ID_DESC)) + .body("params.display_names.shape_columns.id", equalTo(DataSetTool.DATASET_ID_DESC)); + Map newFieldsNames = new HashMap<>(); + newFieldsNames.put(DataSetTool.DATASET_ID_PATH,"new_id"); + + // PATCH collection display name + given().contentType("application/json") + .body("newFieldsNames") + .when().patch(arlasPath + "collections/bar/display_names/fields") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.display_names.collection", equalTo("My New Awesome collection")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)) + .body("params.display_names.collection", equalTo(DataSetTool.DATASET_COLLECTION_DISPLAY_NAME)) + .body("params.display_names.fields.id", equalTo("new_id")) + .body("params.display_names.shape_columns.id", equalTo(DataSetTool.DATASET_ID_DESC)); + + // DELETE collection + when().delete(arlasPath + "collections/bar") + .then().statusCode(200); + + } + + + @Test + public void test12FieldsDisplayName() throws Exception { + Map jsonAsMap = getJsonAsMap("bar.com", null, false); + jsonAsMap.put(CollectionReference.INSPIRE_PATH, getInspireJsonAsMap()); + jsonAsMap.put(CollectionReference.DUBLIN_CORE_PATH, getDublinJsonAsMap()); + // PUT new collection + given().contentType("application/json") + .body(jsonAsMap) + .when().put(arlasPath + "collections/bar") + .then().statusCode(200); + + // GET collection + when().get(arlasPath + "collections/bar") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)); + Map newFieldsNames = new HashMap<>(); + newFieldsNames.put(DataSetTool.DATASET_ID_PATH,"new_id"); + + // PATCH collection display name + given().contentType("application/json") + .body("newFieldsNames") + .when().patch(arlasPath + "collections/bar/display_names/fields") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.display_names.collection", equalTo("My New Awesome collection")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)) + .body("params.display_names.fields.id", equalTo("new_id")); + + // DELETE collection + when().delete(arlasPath + "collections/bar") + .then().statusCode(200); + + } + + @Test + public void test13ShapeColumnsDisplayName() throws Exception { + Map jsonAsMap = getJsonAsMap("bar.com", null, false); + jsonAsMap.put(CollectionReference.INSPIRE_PATH, getInspireJsonAsMap()); + jsonAsMap.put(CollectionReference.DUBLIN_CORE_PATH, getDublinJsonAsMap()); + jsonAsMap.put(CollectionReference.DISPLAY_NAMES, getCollectionDescriptionJsonAsMap()); + // PUT new collection + given().contentType("application/json") + .body(jsonAsMap) + .when().put(arlasPath + "collections/bar") + .then().statusCode(200); + + // GET collection + when().get(arlasPath + "collections/bar") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)) + .body("params.display_names.collection", equalTo(DataSetTool.DATASET_COLLECTION_DISPLAY_NAME)) + .body("params.display_names.fields.id", equalTo(DataSetTool.DATASET_ID_DESC)) + .body("params.display_names.shape_columns.id", equalTo(DataSetTool.DATASET_ID_DESC)); + Map newShapesColumnsNames = new HashMap<>(); + newShapesColumnsNames.put(DataSetTool.DATASET_ID_PATH,"new_id"); + + // PATCH collection display name + given().contentType("application/json") + .body("newFieldsNames") + .when().patch(arlasPath + "collections/bar/display_names/fields") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.display_names.collection", equalTo("My New Awesome collection")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)) + .body("params.display_names.collection", equalTo(DataSetTool.DATASET_COLLECTION_DISPLAY_NAME)) + .body("params.display_names.shape_columns.id", equalTo("new_id")) + .body("params.display_names.fields.id", equalTo(DataSetTool.DATASET_ID_DESC)); + + // DELETE collection + when().delete(arlasPath + "collections/bar") + .then().statusCode(200); + + } + + @Test + public void test14ShapeColumnsDisplayName() throws Exception { + Map jsonAsMap = getJsonAsMap("bar.com", null, false); + jsonAsMap.put(CollectionReference.INSPIRE_PATH, getInspireJsonAsMap()); + jsonAsMap.put(CollectionReference.DUBLIN_CORE_PATH, getDublinJsonAsMap()); + // PUT new collection + given().contentType("application/json") + .body(jsonAsMap) + .when().put(arlasPath + "collections/bar") + .then().statusCode(200); + + // GET collection + when().get(arlasPath + "collections/bar") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)); + Map newShapesColumnsNames = new HashMap<>(); + newShapesColumnsNames.put(DataSetTool.DATASET_ID_PATH,"new_id"); + + // PATCH collection display name + given().contentType("application/json") + .body("newFieldsNames") + .when().patch(arlasPath + "collections/bar/display_names/fields") + .then().statusCode(200) + .body("collection_name", equalTo("bar")) + .body("params.display_names.collection", equalTo("My New Awesome collection")) + .body("params.index_name", equalTo(DataSetTool.DATASET_INDEX_NAME)) + .body("params.id_path", equalTo(DataSetTool.DATASET_ID_PATH)) + .body("params.geometry_path", equalTo(DataSetTool.DATASET_GEOMETRY_PATH)) + .body("params.centroid_path", equalTo(DataSetTool.DATASET_CENTROID_PATH)) + .body("params.timestamp_path", equalTo(DataSetTool.DATASET_TIMESTAMP_PATH)) + .body("params.exclude_fields", equalTo(DataSetTool.DATASET_EXCLUDE_FIELDS)) + .body("params.exclude_wfs_fields", equalTo(DataSetTool.DATASET_EXCLUDE_WFS_FIELDS)) + .body("params.organisations.owner", equalTo("bar.com")) + .body("params.organisations.shared", hasSize(0)) + .body("params.organisations.public", equalTo(Boolean.FALSE)) + .body("params.display_names.shape_columns.id", equalTo("new_id")); + + // DELETE collection + when().delete(arlasPath + "collections/bar") + .then().statusCode(200); + + } + private void handleInvalidCollectionParameters(ValidatableResponse then) throws Exception { then.statusCode(400); } @@ -513,6 +738,8 @@ private Object getCollectionDescriptionJsonAsMap() { Map jsonAsMap = new HashMap<>(); jsonAsMap.put(CollectionReference.COLLECTION_DISPLAY_NAME, DataSetTool.DATASET_COLLECTION_DISPLAY_NAME); jsonAsMap.put(CollectionReference.FIELD_DISPLAY_NAME, getFieldDescriptionsJsonAsMap()); + jsonAsMap.put(CollectionReference.SHAPE_COLUMN_DISPLAY_NAME, getFieldDescriptionsJsonAsMap()); + return jsonAsMap; }