From 32446dce35b5bf9d2c84751718cb4ece7f96fa72 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Mon, 1 Mar 2021 13:25:13 +0000 Subject: CPS-265 - updating cps path to support include-descendants option. Issue-ID: CPS-265 Signed-off-by: niamhcore Change-Id: I9e9b84760dbc8b5eb4b31ab972fdb2d186c6bb48 --- .../cps/rest/controller/QueryRestController.java | 7 ++++-- .../rest/controller/QueryRestControllerSpec.groovy | 27 ++++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'cps-rest/src') diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java index a8816f02b4..c6b5284fbd 100644 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/QueryRestController.java @@ -24,6 +24,7 @@ import java.util.Collection; import javax.validation.Valid; import org.onap.cps.api.CpsQueryService; import org.onap.cps.rest.api.CpsQueryApi; +import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -40,9 +41,11 @@ public class QueryRestController implements CpsQueryApi { @Override public ResponseEntity getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName, - final String anchorName, @Valid final String cpsPath) { + final String anchorName, @Valid final String cpsPath, @Valid final Boolean includeDescendants) { + final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants) + ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS; final Collection dataNodes = - cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath); + cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption); return new ResponseEntity<>(new Gson().toJson(dataNodes), HttpStatus.OK); } } diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy index 4d9a558ef0..0927c9d1e8 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/QueryRestControllerSpec.groovy @@ -19,7 +19,7 @@ package org.onap.cps.rest.controller -import com.google.common.collect.ImmutableMap + import com.google.gson.Gson import org.modelmapper.ModelMapper import org.onap.cps.api.CpsAdminService @@ -36,7 +36,10 @@ import org.springframework.http.HttpStatus import org.springframework.test.web.servlet.MockMvc import spock.lang.Shared import spock.lang.Specification +import spock.lang.Unroll +import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS +import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get @WebMvcTest @@ -63,22 +66,28 @@ class QueryRestControllerSpec extends Specification { @Value('${rest.api.cps-base-path}') def basePath - def 'Query data node by cps path for the given dataspace and anchor.'() { + @Unroll + def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() { given: 'service method returns a list containing a data node' + def dataNode = new DataNodeBuilder().withXpath('/xpath').build() def dataspaceName = 'my_dataspace' def anchorName = 'my_anchor' - def cpsPath = '/xpath/leaves[@leaf=\'value\']' - def dataNode = new DataNodeBuilder().withXpath("/xpath") - .withLeaves(ImmutableMap.of("leaf", "value")).build() - ArrayList dataNodeList = new ArrayList(); - dataNodeList.add(dataNode) - mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath) >> dataNodeList + def cpsPath = 'some cps-path' + mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode] and: 'the query endpoint' def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query" when: 'query data nodes API is invoked' - def response = mvc.perform(get(dataNodeEndpoint).param('cps-path', cpsPath)).andReturn().response + def response = mvc.perform(get(dataNodeEndpoint) + .param('cps-path', cpsPath) + .param('include-descendants', includeDescendantsOption)) + .andReturn().response then: 'the response contains the the datanode in json format' response.status == HttpStatus.OK.value() response.getContentAsString().contains(new Gson().toJson(dataNode)) + where: 'the following options for include descendants are provided in the request' + scenario | includeDescendantsOption || expectedCpsDataServiceOption + 'no descendants by default' | '' || OMIT_DESCENDANTS + 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS + 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS } } \ No newline at end of file -- cgit 1.2.3-korg