diff options
Diffstat (limited to 'cps-rest')
4 files changed, 42 insertions, 7 deletions
diff --git a/cps-rest/docs/openapi/components.yml b/cps-rest/docs/openapi/components.yml index 25ef6a452a..40f0e170ff 100644 --- a/cps-rest/docs/openapi/components.yml +++ b/cps-rest/docs/openapi/components.yml @@ -320,6 +320,15 @@ components: schema: type: integer example: 10 + dryRunInQuery: + name: dry-run + in: query + description: Boolean flag to validate data, without persisting it. Default value is set to false. + required: false + schema: + type: boolean + default: false + example: false responses: NotFound: diff --git a/cps-rest/docs/openapi/cpsData.yml b/cps-rest/docs/openapi/cpsData.yml index 4418a3b9b7..daf59bbfbf 100644 --- a/cps-rest/docs/openapi/cpsData.yml +++ b/cps-rest/docs/openapi/cpsData.yml @@ -102,6 +102,7 @@ nodesByDataspaceAndAnchor: - $ref: 'components.yml#/components/parameters/dataspaceNameInPath' - $ref: 'components.yml#/components/parameters/anchorNameInPath' - $ref: 'components.yml#/components/parameters/xpathInQuery' + - $ref: 'components.yml#/components/parameters/dryRunInQuery' - $ref: 'components.yml#/components/parameters/observedTimestampInQuery' - $ref: 'components.yml#/components/parameters/contentTypeInHeader' requestBody: diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java index f86073fb06..7390afcf98 100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java @@ -74,16 +74,21 @@ public class DataRestController implements CpsDataApi { final String dataspaceName, final String anchorName, final String contentTypeInHeader, final String nodeData, final String parentNodeXpath, - final String observedTimestamp) { + final Boolean dryRunEnabled, final String observedTimestamp) { final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); - if (isRootXpath(parentNodeXpath)) { - cpsDataService.saveData(dataspaceName, anchorName, nodeData, - toOffsetDateTime(observedTimestamp), contentType); + if (Boolean.TRUE.equals(dryRunEnabled)) { + cpsDataService.validateData(dataspaceName, anchorName, parentNodeXpath, nodeData, contentType); + return ResponseEntity.ok().build(); } else { - cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, - nodeData, toOffsetDateTime(observedTimestamp), contentType); + if (isRootXpath(parentNodeXpath)) { + cpsDataService.saveData(dataspaceName, anchorName, nodeData, + toOffsetDateTime(observedTimestamp), contentType); + } else { + cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, + nodeData, toOffsetDateTime(observedTimestamp), contentType); + } + return ResponseEntity.status(HttpStatus.CREATED).build(); } - return new ResponseEntity<>(HttpStatus.CREATED); } @Override diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy index e101ea6c41..705c2fee91 100755 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy @@ -164,6 +164,26 @@ class DataRestControllerSpec extends Specification { 'with invalid observed-timestamp' | 'invalid' | MediaType.APPLICATION_JSON | requestBodyJson || 0 | HttpStatus.BAD_REQUEST | expectedJsonData | ContentType.JSON } + def 'Validate data using create a node API'() { + given: 'an endpoint to create a node' + def endpoint = "$dataNodeBaseEndpointV1/anchors/$anchorName/nodes" + def parentNodeXpath = '/' + def dryRunEnabled = 'true' + when: 'post is invoked with json data and dry-run flag enabled' + def response = + mvc.perform( + post(endpoint) + .contentType(MediaType.APPLICATION_JSON) + .param('xpath', parentNodeXpath) + .param('dry-run', dryRunEnabled) + .content(requestBodyJson) + ).andReturn().response + then: 'a 200 OK response is returned' + response.status == HttpStatus.OK.value() + then: 'the service was called with correct parameters' + 1 * mockCpsDataService.validateData(dataspaceName, anchorName, parentNodeXpath, requestBodyJson, ContentType.JSON) + } + def 'Create a child node #scenario'() { given: 'endpoint to create a node' def endpoint = "$dataNodeBaseEndpointV1/anchors/$anchorName/nodes" |