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.DIRECT_CHILD_ONLY
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 == expectedDepth}}) >> [dataNode1, dataNode1]
102 when: 'query data nodes API is invoked'
105 get(dataNodeEndpointV2)
106 .param('cps-path', cpsPath)
107 .param('descendants', includeDescendantsOptionString))
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"]}}')
112 where: 'the following options for include descendants are provided in the request'
113 scenario | includeDescendantsOptionString || expectedDepth
114 'direct children' | 'direct' || 1
115 'descendants' | '2' || 2
118 def 'Query data node by cps path for the given dataspace across all anchors with #scenario.'() {
119 given: 'service method returns a list containing a data node'
120 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
121 .withAnchor('my_anchor')
122 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
123 def dataNode2 = new DataNodeBuilder().withXpath('/xpath')
124 .withAnchor('my_anchor_2')
125 .withLeaves([leaf: 'value', leafList: ['leaveListElement3', 'leaveListElement4']]).build()
126 def dataspaceName = 'my_dataspace'
127 def cpsPath = 'some/cps/path'
128 mockCpsQueryService.queryDataNodesAcrossAnchors(dataspaceName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode2]
129 and: 'the query endpoint'
130 def dataNodeEndpoint = "$basePath/v2/dataspaces/$dataspaceName/nodes/query"
131 when: 'query data nodes API is invoked'
134 get(dataNodeEndpoint)
135 .param('cps-path', cpsPath)
136 .param('descendants', includeDescendantsOptionString))
137 .andReturn().response
138 then: 'the response contains the the datanode in json format'
139 response.status == HttpStatus.OK.value()
140 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
141 response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement3","leaveListElement4"]}}')
142 where: 'the following options for include descendants are provided in the request'
143 scenario | includeDescendantsOptionString || expectedCpsDataServiceOption
144 'no descendants by default' | '' || OMIT_DESCENDANTS
145 'no descendant explicitly' | 'none' || OMIT_DESCENDANTS
146 'descendants' | 'all' || INCLUDE_ALL_DESCENDANTS
147 'direct children' | 'direct' || DIRECT_CHILD_ONLY