Editing of Nordix Licenses to ONAP guidelines
[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  *  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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.rest.controller
24
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
26 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
28
29 import com.google.gson.Gson
30 import org.modelmapper.ModelMapper
31 import org.onap.cps.api.CpsAdminService
32 import org.onap.cps.api.CpsDataService
33 import org.onap.cps.api.CpsModuleService
34 import org.onap.cps.api.CpsQueryService
35 import org.onap.cps.spi.model.DataNodeBuilder
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
40 import org.springframework.http.HttpStatus
41 import org.springframework.test.web.servlet.MockMvc
42 import spock.lang.Specification
43
44 @WebMvcTest
45 class QueryRestControllerSpec extends Specification {
46
47     @SpringBean
48     CpsDataService mockCpsDataService = Mock()
49
50     @SpringBean
51     CpsModuleService mockCpsModuleService = Mock()
52
53     @SpringBean
54     CpsAdminService mockCpsAdminService = Mock()
55
56     @SpringBean
57     CpsQueryService mockCpsQueryService = Mock()
58
59     @SpringBean
60     ModelMapper modelMapper = Mock()
61
62     @Autowired
63     MockMvc mvc
64
65     @Value('${rest.api.cps-base-path}')
66     def basePath
67
68     def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
69         given: 'service method returns a list containing a data node'
70             def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
71             def dataspaceName = 'my_dataspace'
72             def anchorName = 'my_anchor'
73             def cpsPath = 'some cps-path'
74             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
75         and: 'the query endpoint'
76             def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
77         when: 'query data nodes API is invoked'
78             def response =
79                     mvc.perform(
80                             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 }