CPS-265 - updating cps path to support include-descendants option.
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / QueryRestControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.rest.controller
21
22
23 import com.google.gson.Gson
24 import org.modelmapper.ModelMapper
25 import org.onap.cps.api.CpsAdminService
26 import org.onap.cps.api.CpsDataService
27 import org.onap.cps.api.CpsModuleService
28 import org.onap.cps.api.CpsQueryService
29 import org.onap.cps.spi.model.DataNode
30 import org.onap.cps.spi.model.DataNodeBuilder
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.beans.factory.annotation.Value
34 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
35 import org.springframework.http.HttpStatus
36 import org.springframework.test.web.servlet.MockMvc
37 import spock.lang.Shared
38 import spock.lang.Specification
39 import spock.lang.Unroll
40
41 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
42 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
43 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
44
45 @WebMvcTest
46 class QueryRestControllerSpec extends Specification {
47
48     @SpringBean
49     CpsDataService mockCpsDataService = Mock()
50
51     @SpringBean
52     CpsModuleService mockCpsModuleService = Mock()
53
54     @SpringBean
55     CpsAdminService mockCpsAdminService = Mock()
56
57     @SpringBean
58     CpsQueryService mockCpsQueryService = Mock()
59
60     @SpringBean
61     ModelMapper modelMapper = Mock()
62
63     @Autowired
64     MockMvc mvc
65
66     @Value('${rest.api.cps-base-path}')
67     def basePath
68
69     @Unroll
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 dataNode = new DataNodeBuilder().withXpath('/xpath').build()
73             def dataspaceName = 'my_dataspace'
74             def anchorName = 'my_anchor'
75             def cpsPath = 'some cps-path'
76             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
77         and: 'the query endpoint'
78             def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
79         when: 'query data nodes API is invoked'
80             def response = mvc.perform(get(dataNodeEndpoint)
81                     .param('cps-path', cpsPath)
82                     .param('include-descendants', includeDescendantsOption))
83                     .andReturn().response
84         then: 'the response contains the the datanode in json format'
85             response.status == HttpStatus.OK.value()
86             response.getContentAsString().contains(new Gson().toJson(dataNode))
87         where: 'the following options for include descendants are provided in the request'
88             scenario                    | includeDescendantsOption || expectedCpsDataServiceOption
89             'no descendants by default' | ''                       || OMIT_DESCENDANTS
90             'no descendant explicitly'  | 'false'                  || OMIT_DESCENDANTS
91             'descendants'               | 'true'                   || INCLUDE_ALL_DESCENDANTS
92     }
93 }