diff options
author | 2021-02-26 10:13:48 +0000 | |
---|---|---|
committer | 2021-02-26 17:21:02 +0000 | |
commit | b5d573b02c7d1376e92e59887371965a2fb43c25 (patch) | |
tree | 366dd39ab6391c1dc5adbfd684eee943d65b6e6a /cps-nf-proxy-rest/src | |
parent | d3609db46635b9bdb0a49a7d0e18ebe144bb7f29 (diff) |
CPS-240 - Create REST End-point on NF-Proxy for DataNode Update & cpsPath Query
Issue-ID: CPS-240
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I2aed92f8ab34282b12e23ae7807a391446165eb0
Diffstat (limited to 'cps-nf-proxy-rest/src')
2 files changed, 92 insertions, 8 deletions
diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java index 494e7f659..93ed06580 100644 --- a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * ================================================================================ + * Modifications (C) 2021 Nordix Foundation * Modification Copyright (C) 2021 highstreet technologies GmbH * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +21,9 @@ package org.onap.cps.nfproxy.rest.controller; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.util.Collection; import javax.validation.Valid; import org.onap.cps.nfproxy.api.NfProxyDataService; import org.onap.cps.nfproxy.rest.api.NfProxyApi; @@ -38,6 +41,7 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("${rest.api.xnf-base-path}") public class NfProxyController implements NfProxyApi { + private static final Gson GSON = new GsonBuilder().create(); private static final String XPATH_ROOT = "/"; @Autowired @@ -45,7 +49,7 @@ public class NfProxyController implements NfProxyApi { @Override public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath, - @Valid final Boolean includeDescendants) { + @Valid final Boolean includeDescendants) { if (XPATH_ROOT.equals(xpath)) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @@ -54,4 +58,24 @@ public class NfProxyController implements NfProxyApi { final DataNode dataNode = nfProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption); return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK); } + + @Override + public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath) { + final Collection<DataNode> dataNodes = nfProxyDataService.queryDataNodes(cmHandle, cpsPath); + return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK); + } + + @Override + public ResponseEntity<Object> replaceNode(@Valid final String jsonData, final String cmHandle, + @Valid final String parentNodeXpath) { + nfProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData); + return new ResponseEntity<>(HttpStatus.OK); + } + + @Override + public ResponseEntity<Object> updateNodeLeaves(@Valid final String jsonData, final String cmHandle, + @Valid final String parentNodeXpath) { + nfProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy index 3cd6b9a9f..742a643fa 100644 --- a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy +++ b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * ================================================================================ * Modification Copyright (C) 2021 highstreet technologies GmbH + * Modification Copyright (C) 2021 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,19 +21,22 @@ package org.onap.cps.nfproxy.rest.controller +import com.google.common.collect.ImmutableMap +import com.google.gson.Gson import org.onap.cps.nfproxy.api.NfProxyDataService +import org.onap.cps.spi.model.DataNode import org.onap.cps.spi.model.DataNodeBuilder import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc import spock.lang.Specification -import spock.lang.Unroll import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* @WebMvcTest class NfProxyControllerSpec extends Specification { @@ -53,14 +56,70 @@ class NfProxyControllerSpec extends Specification { dataNodeBaseEndpoint = "$basePath/v1" } - @Unroll + def cmHandle = 'some handle' + def xpath = 'some xpath' + + def 'Query data node by cps path for the given cm handle.'() { + given: 'service method returns a list containing a data node' + def cpsPath = '/xpath/leaves[@leaf=\'value\']' + def dataNode = new DataNodeBuilder().withXpath("/xpath") + .withLeaves(ImmutableMap.of("leaf", "value")).build() + ArrayList<DataNode> dataNodeList = new ArrayList(); + dataNodeList.add(dataNode) + mockNfProxyDataService.queryDataNodes(cmHandle, cpsPath) >> dataNodeList + and: 'the query endpoint' + def dataNodeEndpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes/query" + when: 'query data nodes API is invoked' + def response = mvc.perform(get(dataNodeEndpoint).param('cps-path', cpsPath)).andReturn().response + then: 'the response contains the the datanode in json format' + response.status == HttpStatus.OK.value() + def expectedJsonContent = new Gson().toJson(dataNode) + response.getContentAsString().contains(expectedJsonContent) + } + + def 'Update data node leaves.'() { + given: 'json data' + def jsonData = 'json data' + and: 'the query endpoint' + def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes" + when: 'patch request is performed' + def response = mvc.perform( + patch(endpoint) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonData) + .param('xpath', xpath) + ).andReturn().response + then: 'the service method is invoked once with expected parameters' + 1 * mockNfProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData) + and: 'response status indicates success' + response.status == HttpStatus.OK.value() + } + + def 'Replace data node tree.'() { + given: 'json data' + def jsonData = 'json data' + and: 'the query endpoint' + def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes" + when: 'put request is performed' + def response = mvc.perform( + put(endpoint) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonData) + .param('xpath', xpath) + ).andReturn().response + then: 'the service method is invoked once with expected parameters' + 1 * mockNfProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData) + and: 'response status indicates success' + response.status == HttpStatus.OK.value() + } + def 'Get data node.'() { given: 'the service returns a data node' def xpath = 'some xpath' - def cmHandle = 'some handle' def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build() - def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node" mockNfProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode + and: 'the query endpoint' + def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node" when: 'get request is performed through REST API' def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response then: 'a success response is returned' @@ -69,3 +128,4 @@ class NfProxyControllerSpec extends Specification { response.contentAsString.contains('"leaf":"value"') } } + |