Merge "Upgrade version number"
[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  *  Modifications Copyright (C) 2021 Bell Canada.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller
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.DataNodeBuilder
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.beans.factory.annotation.Value
33 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
34 import org.springframework.http.HttpStatus
35 import org.springframework.test.web.servlet.MockMvc
36 import spock.lang.Unroll
37
38 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
39 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
41
42 @WebMvcTest
43 class QueryRestControllerSpec extends RestControllerSpecification {
44
45     @SpringBean
46     CpsDataService mockCpsDataService = Mock()
47
48     @SpringBean
49     CpsModuleService mockCpsModuleService = Mock()
50
51     @SpringBean
52     CpsAdminService mockCpsAdminService = Mock()
53
54     @SpringBean
55     CpsQueryService mockCpsQueryService = Mock()
56
57     @SpringBean
58     ModelMapper modelMapper = Mock()
59
60     @Autowired
61     MockMvc mvc
62
63     @Value('${rest.api.cps-base-path}')
64     def basePath
65
66     @Unroll
67     def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
68         given: 'service method returns a list containing a data node'
69             def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
70             def dataspaceName = 'my_dataspace'
71             def anchorName = 'my_anchor'
72             def cpsPath = 'some cps-path'
73             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
74         and: 'the query endpoint'
75             def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
76         when: 'query data nodes API is invoked'
77             def response =
78                     mvc.perform(
79                             get(dataNodeEndpoint)
80                                     .header("Authorization", getAuthorizationHeader())
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 }