2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Bell Canada.
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.rest.controller
25 import org.onap.cps.spi.model.DataNode
27 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
28 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
31 import com.google.gson.Gson
32 import org.modelmapper.ModelMapper
33 import org.onap.cps.api.CpsAdminService
34 import org.onap.cps.api.CpsDataService
35 import org.onap.cps.api.CpsModuleService
36 import org.onap.cps.api.CpsQueryService
37 import org.onap.cps.spi.model.DataNodeBuilder
38 import org.spockframework.spring.SpringBean
39 import org.springframework.beans.factory.annotation.Autowired
40 import org.springframework.beans.factory.annotation.Value
41 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
42 import org.springframework.http.HttpStatus
43 import org.springframework.test.web.servlet.MockMvc
44 import spock.lang.Specification
47 class QueryRestControllerSpec extends Specification {
50 CpsDataService mockCpsDataService = Mock()
53 CpsModuleService mockCpsModuleService = Mock()
56 CpsAdminService mockCpsAdminService = Mock()
59 CpsQueryService mockCpsQueryService = Mock()
62 ModelMapper modelMapper = Mock()
67 @Value('${rest.api.cps-base-path}')
70 def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
71 given: 'service method returns a list containing a data node'
72 def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
73 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
74 def dataspaceName = 'my_dataspace'
75 def anchorName = 'my_anchor'
76 def cpsPath = 'some cps-path'
77 mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode1]
78 and: 'the query endpoint'
79 def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
80 when: 'query data nodes API is invoked'
84 .param('cps-path', cpsPath)
85 .param('include-descendants', includeDescendantsOption))
87 then: 'the response contains the the datanode in json format'
88 response.status == HttpStatus.OK.value()
89 response.getContentAsString() == '[{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]},{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}]'
90 where: 'the following options for include descendants are provided in the request'
91 scenario | includeDescendantsOption || expectedCpsDataServiceOption
92 'no descendants by default' | '' || OMIT_DESCENDANTS
93 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS
94 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS