2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021-2022 Bell Canada.
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * Modifications Copyright (C) 2022-2023 TechMahindra Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.rest.controller
26 import org.onap.cps.utils.PrefixResolver
28 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
29 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
32 import com.fasterxml.jackson.databind.ObjectMapper
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.onap.cps.api.CpsQueryService
35 import org.onap.cps.spi.model.DataNodeBuilder
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
40 import org.springframework.http.HttpStatus
41 import org.springframework.test.web.servlet.MockMvc
42 import spock.lang.Specification
44 @WebMvcTest(QueryRestController)
45 class QueryRestControllerSpec extends Specification {
48 CpsQueryService mockCpsQueryService = Mock()
51 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
54 PrefixResolver prefixResolver = Mock()
59 @Value('${rest.api.cps-base-path}')
62 def dataspaceName = 'my_dataspace'
63 def anchorName = 'my_anchor'
64 def cpsPath = 'some cps-path'
65 def dataNodeEndpointV2
68 dataNodeEndpointV2 = "$basePath/v2/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
71 def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
72 given: 'service method returns a list containing a data node'
73 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
74 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
75 mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode1]
76 and: 'the query endpoint'
77 def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
78 when: 'query data nodes API is invoked'
82 .param('cps-path', cpsPath)
83 .param('include-descendants', includeDescendantsOption))
85 then: 'the response contains the the datanode in json format'
86 response.status == HttpStatus.OK.value()
87 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
88 where: 'the following options for include descendants are provided in the request'
89 scenario | includeDescendantsOption || expectedCpsDataServiceOption
90 'no descendants by default' | '' || OMIT_DESCENDANTS
91 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS
92 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS
95 def 'Query data node v2 api by cps path for the given dataspace and anchor with #scenario.'() {
96 given: 'service method returns a list containing a data node'
97 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
98 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
99 mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, { descendantsOption -> {
100 assert descendantsOption.depth == 2}}) >> [dataNode1, dataNode1]
101 when: 'query data nodes API is invoked'
104 get(dataNodeEndpointV2)
105 .param('cps-path', cpsPath)
106 .param('descendants', '2'))
107 .andReturn().response
108 then: 'the response contains the the datanode in json format'
109 assert response.status == HttpStatus.OK.value()
110 assert response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
113 def 'Query data node by cps path for the given dataspace across all anchors with #scenario.'() {
114 given: 'service method returns a list containing a data node'
115 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
116 .withAnchor('my_anchor')
117 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
118 def dataNode2 = new DataNodeBuilder().withXpath('/xpath')
119 .withAnchor('my_anchor_2')
120 .withLeaves([leaf: 'value', leafList: ['leaveListElement3', 'leaveListElement4']]).build()
121 def dataspaceName = 'my_dataspace'
122 def cpsPath = 'some/cps/path'
123 mockCpsQueryService.queryDataNodesAcrossAnchors(dataspaceName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode2]
124 and: 'the query endpoint'
125 def dataNodeEndpoint = "$basePath/v2/dataspaces/$dataspaceName/nodes/query"
126 when: 'query data nodes API is invoked'
129 get(dataNodeEndpoint)
130 .param('cps-path', cpsPath)
131 .param('descendants', includeDescendantsOptionString))
132 .andReturn().response
133 then: 'the response contains the the datanode in json format'
134 response.status == HttpStatus.OK.value()
135 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
136 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement3","leaveListElement4"]}}')
137 where: 'the following options for include descendants are provided in the request'
138 scenario | includeDescendantsOptionString || expectedCpsDataServiceOption
139 'no descendants by default' | '' || OMIT_DESCENDANTS
140 'no descendant explicitly' | 'none' || OMIT_DESCENDANTS
141 'descendants' | 'all' || INCLUDE_ALL_DESCENDANTS