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.spi.PaginationOption
27 import org.onap.cps.utils.PrefixResolver
29 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
30 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
33 import com.fasterxml.jackson.databind.ObjectMapper
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.onap.cps.api.CpsQueryService
36 import org.onap.cps.spi.model.DataNodeBuilder
37 import org.spockframework.spring.SpringBean
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.beans.factory.annotation.Value
40 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
41 import org.springframework.http.HttpStatus
42 import org.springframework.test.web.servlet.MockMvc
43 import spock.lang.Specification
45 @WebMvcTest(QueryRestController)
46 class QueryRestControllerSpec extends Specification {
49 CpsQueryService mockCpsQueryService = Mock()
52 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
55 PrefixResolver prefixResolver = Mock()
60 @Value('${rest.api.cps-base-path}')
63 def dataspaceName = 'my_dataspace'
64 def anchorName = 'my_anchor'
65 def cpsPath = 'some cps-path'
66 def dataNodeEndpointV2
69 dataNodeEndpointV2 = "$basePath/v2/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
72 def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
73 given: 'service method returns a list containing a data node'
74 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
75 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
76 mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode1]
77 and: 'the query endpoint'
78 def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
79 when: 'query data nodes API is invoked'
83 .param('cps-path', cpsPath)
84 .param('include-descendants', includeDescendantsOption))
86 then: 'the response contains the the datanode in json format'
87 response.status == HttpStatus.OK.value()
88 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
89 where: 'the following options for include descendants are provided in the request'
90 scenario | includeDescendantsOption || expectedCpsDataServiceOption
91 'no descendants by default' | '' || OMIT_DESCENDANTS
92 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS
93 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS
96 def 'Query data node v2 api by cps path for the given dataspace and anchor with #scenario.'() {
97 given: 'service method returns a list containing a data node'
98 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
99 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
100 mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, { descendantsOption -> {
101 assert descendantsOption.depth == 2}}) >> [dataNode1, dataNode1]
102 when: 'query data nodes API is invoked'
105 get(dataNodeEndpointV2)
106 .param('cps-path', cpsPath)
107 .param('descendants', '2'))
108 .andReturn().response
109 then: 'the response contains the the datanode in json format'
110 assert response.status == HttpStatus.OK.value()
111 assert response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
114 def 'Query data node by cps path for the given dataspace across all anchors with #scenario.'() {
115 given: 'service method returns a list containing a data node from different anchors'
116 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
117 .withAnchor('my_anchor')
118 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
119 def dataNode2 = new DataNodeBuilder().withXpath('/xpath')
120 .withAnchor('my_anchor_2')
121 .withLeaves([leaf: 'value', leafList: ['leaveListElement3', 'leaveListElement4']]).build()
122 and: 'second data node for the same anchor'
123 def dataNode3 = new DataNodeBuilder().withXpath('/xpath')
124 .withAnchor('my_anchor_2')
125 .withLeaves([leaf: 'value', leafList: ['leaveListElement5', 'leaveListElement6']]).build()
126 and: 'the query endpoint'
127 def dataspaceName = 'my_dataspace'
128 def cpsPath = 'some/cps/path'
129 def dataNodeEndpoint = "$basePath/v2/dataspaces/$dataspaceName/nodes/query"
130 mockCpsQueryService.queryDataNodesAcrossAnchors(dataspaceName, cpsPath,
131 expectedCpsDataServiceOption, PaginationOption.NO_PAGINATION) >> [dataNode1, dataNode2, dataNode3]
132 mockCpsQueryService.countAnchorsForDataspaceAndCpsPath(dataspaceName, cpsPath) >> 2
133 when: 'query data nodes API is invoked'
136 get(dataNodeEndpoint)
137 .param('cps-path', cpsPath)
138 .param('descendants', includeDescendantsOptionString))
139 .andReturn().response
140 then: 'the response contains the the datanode in json format'
141 response.status == HttpStatus.OK.value()
142 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
143 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement3","leaveListElement4"]}}')
144 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement5","leaveListElement6"]}}')
145 where: 'the following options for include descendants are provided in the request'
146 scenario | includeDescendantsOptionString || expectedCpsDataServiceOption
147 'no descendants by default' | '' || OMIT_DESCENDANTS
148 'no descendant explicitly' | 'none' || OMIT_DESCENDANTS
149 'descendants' | 'all' || INCLUDE_ALL_DESCENDANTS
152 def 'Query data node by cps path for the given dataspace across all anchors with pagination #scenario.'() {
153 given: 'service method returns a list containing a data node from different anchors'
154 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
155 .withAnchor('my_anchor')
156 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
157 def dataNode2 = new DataNodeBuilder().withXpath('/xpath')
158 .withAnchor('my_anchor_2')
159 .withLeaves([leaf: 'value', leafList: ['leaveListElement3', 'leaveListElement4']]).build()
160 and: 'the query endpoint'
161 def dataspaceName = 'my_dataspace'
162 def cpsPath = 'some/cps/path'
163 def dataNodeEndpoint = "$basePath/v2/dataspaces/$dataspaceName/nodes/query"
164 mockCpsQueryService.queryDataNodesAcrossAnchors(dataspaceName, cpsPath,
165 INCLUDE_ALL_DESCENDANTS, new PaginationOption(pageIndex,pageSize)) >> [dataNode1, dataNode2]
166 mockCpsQueryService.countAnchorsForDataspaceAndCpsPath(dataspaceName, cpsPath) >> totalAnchors
167 when: 'query data nodes API is invoked'
170 get(dataNodeEndpoint)
171 .param('cps-path', cpsPath)
172 .param('descendants', "all")
173 .param('pageIndex', String.valueOf(pageIndex))
174 .param('pageSize', String.valueOf(pageSize)))
175 .andReturn().response
176 then: 'the response contains the the datanode in json format'
177 assert response.status == HttpStatus.OK.value()
178 assert Integer.valueOf(response.getHeaderValue("total-pages")) == expectedPageSize
179 assert response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
180 assert response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement3","leaveListElement4"]}}')
181 where: 'the following options for include descendants are provided in the request'
182 scenario | pageIndex | pageSize | totalAnchors || expectedPageSize
183 '1st page with all anchors' | 1 | 3 | 3 || 1
184 '1st page with less anchors' | 1 | 2 | 3 || 2