diff options
author | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2024-07-11 21:56:24 +0530 |
---|---|---|
committer | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2024-07-16 11:00:11 +0000 |
commit | 760dd950e6f6fcb6f49c6f9d92a33f538adffd24 (patch) | |
tree | cccab533a2837a60e527ff1e75d6dbcd01197d6a /cps-rest/src | |
parent | ac58e919008c4449b389d3681a6f8aa216cb5f8f (diff) |
XML content support on replace a node with descendants
Issue-ID: CPS-2282
Change-Id: Ibb7ffb65ccbb03703266712c6d5c2eade0e7ab4b
Signed-off-by: Rudrangi Anupriya <ra00745022@techmahindra.com>
Diffstat (limited to 'cps-rest/src')
-rwxr-xr-x | cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java | 28 | ||||
-rwxr-xr-x | cps-rest/src/test/groovy/org/onap/cps/rest/controller/DataRestControllerSpec.groovy | 19 |
2 files changed, 26 insertions, 21 deletions
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 f579c82d25..6100b7edd9 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 @@ -49,7 +49,6 @@ import org.onap.cps.utils.PrefixResolver; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -70,11 +69,10 @@ public class DataRestController implements CpsDataApi { @Override public ResponseEntity<String> createNode(final String apiVersion, final String dataspaceName, final String anchorName, - @RequestHeader(value = "Content-Type") final String contentTypeHeader, + final String contentTypeInHeader, final String nodeData, final String parentNodeXpath, final String observedTimestamp) { - final ContentType contentType = contentTypeHeader.contains(MediaType.APPLICATION_XML_VALUE) ? ContentType.XML - : ContentType.JSON; + final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); if (isRootXpath(parentNodeXpath)) { cpsDataService.saveData(dataspaceName, anchorName, nodeData, toOffsetDateTime(observedTimestamp), contentType); @@ -137,23 +135,23 @@ public class DataRestController implements CpsDataApi { @Override public ResponseEntity<Object> updateNodeLeaves(final String apiVersion, final String dataspaceName, - final String anchorName, final String contentTypeHeader, + final String anchorName, final String contentTypeInHeader, final String nodeData, final String parentNodeXpath, final String observedTimestamp) { - final ContentType contentType = contentTypeHeader.contains(MediaType.APPLICATION_XML_VALUE) ? ContentType.XML - : ContentType.JSON; + final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, nodeData, toOffsetDateTime(observedTimestamp), contentType); return new ResponseEntity<>(HttpStatus.OK); } @Override - public ResponseEntity<Object> replaceNode(final String apiVersion, - final String dataspaceName, final String anchorName, - final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { - cpsDataService - .updateDataNodeAndDescendants(dataspaceName, anchorName, parentNodeXpath, - jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp)); + public ResponseEntity<Object> replaceNode(final String apiVersion, final String dataspaceName, + final String anchorName, final String contentTypeInHeader, + final String nodeData, final String parentNodeXpath, + final String observedTimestamp) { + final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); + cpsDataService.updateDataNodeAndDescendants(dataspaceName, anchorName, parentNodeXpath, + nodeData, toOffsetDateTime(observedTimestamp), contentType); return new ResponseEntity<>(HttpStatus.OK); } @@ -213,6 +211,10 @@ public class DataRestController implements CpsDataApi { return new ResponseEntity<>(jsonObjectMapper.asJsonString(deltaBetweenAnchors), HttpStatus.OK); } + private static ContentType getContentTypeFromHeader(final String contentTypeInHeader) { + return contentTypeInHeader.contains(MediaType.APPLICATION_XML_VALUE) ? ContentType.XML : ContentType.JSON; + } + private static boolean isRootXpath(final String xpath) { return ROOT_XPATH.equals(xpath); } 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 317b9c5b7c..205d85dc26 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 @@ -456,19 +456,22 @@ class DataRestControllerSpec extends Specification { def response = mvc.perform( put(endpoint) - .contentType(MediaType.APPLICATION_JSON) - .content(requestBodyJson) + .contentType(contentType) + .content(requestBody) .param('xpath', inputXpath)) .andReturn().response then: 'the service method is invoked with expected parameters' - 1 * mockCpsDataService.updateDataNodeAndDescendants(dataspaceName, anchorName, xpathServiceParameter, expectedJsonData, noTimestamp) + 1 * mockCpsDataService.updateDataNodeAndDescendants(dataspaceName, anchorName, xpathServiceParameter, expectedData, noTimestamp, expectedContentType) and: 'response status indicates success' response.status == HttpStatus.OK.value() where: - scenario | inputXpath || xpathServiceParameter - 'root node by default' | '' || '/' - 'root node by choice' | '/' || '/' - 'some xpath by parent' | '/some/xpath' || '/some/xpath' + scenario | inputXpath | contentType || xpathServiceParameter | requestBody | expectedData | expectedContentType + 'JSON content: root node by default' | '' | MediaType.APPLICATION_JSON || '/' | requestBodyJson | expectedJsonData | ContentType.JSON + 'JSON content: root node by choice' | '/' | MediaType.APPLICATION_JSON || '/' | requestBodyJson | expectedJsonData | ContentType.JSON + 'JSON content: some xpath by parent' | '/some/xpath' | MediaType.APPLICATION_JSON || '/some/xpath' | requestBodyJson | expectedJsonData | ContentType.JSON + 'XML content: root node by default' | '' | MediaType.APPLICATION_XML || '/' | requestBodyXml | expectedXmlData | ContentType.XML + 'XML content: root node by choice' | '/' | MediaType.APPLICATION_XML || '/' | requestBodyXml | expectedXmlData | ContentType.XML + 'XML content: some xpath by parent' | '/some/xpath' | MediaType.APPLICATION_XML || '/some/xpath' | requestBodyXml | expectedXmlData | ContentType.XML } def 'Update data node and descendants with observedTimestamp.'() { @@ -485,7 +488,7 @@ class DataRestControllerSpec extends Specification { .andReturn().response then: 'the service method is invoked with expected parameters' expectedApiCount * mockCpsDataService.updateDataNodeAndDescendants(dataspaceName, anchorName, '/', expectedJsonData, - { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) }) + { it == DateTimeUtility.toOffsetDateTime(observedTimestamp) }, ContentType.JSON) and: 'response status indicates success' response.status == expectedHttpStatus.value() where: |