c4bb23ce4ee29fd6bb38f535fd00c7a899526f6d
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / QueryRestControllerSpec.groovy
1 /*
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.rest.controller
25
26 import org.onap.cps.utils.PrefixResolver
27
28 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
29 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
31
32 import com.fasterxml.jackson.databind.ObjectMapper
33 import org.onap.cps.utils.JsonObjectMapper
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(QueryRestController)
45 class QueryRestControllerSpec extends Specification {
46
47     @SpringBean
48     CpsQueryService mockCpsQueryService = Mock()
49
50     @SpringBean
51     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
52
53     @SpringBean
54     PrefixResolver prefixResolver = Mock()
55
56     @Autowired
57     MockMvc mvc
58
59     @Value('${rest.api.cps-base-path}')
60     def basePath
61
62     def dataspaceName = 'my_dataspace'
63     def anchorName = 'my_anchor'
64     def cpsPath = 'some cps-path'
65     def dataNodeEndpointV2
66
67     def setup() {
68          dataNodeEndpointV2 = "$basePath/v2/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
69     }
70
71     def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
72         given: 'service method returns a list containing a data node'
73              def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
74                     .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
75             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode1]
76         and: 'the query endpoint'
77             def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
78         when: 'query data nodes API is invoked'
79             def response =
80                     mvc.perform(
81                             get(dataNodeEndpoint)
82                                     .param('cps-path', cpsPath)
83                                     .param('include-descendants', includeDescendantsOption))
84                             .andReturn().response
85         then: 'the response contains the the datanode in json format'
86             response.status == HttpStatus.OK.value()
87             response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
88         where: 'the following options for include descendants are provided in the request'
89             scenario                    | includeDescendantsOption || expectedCpsDataServiceOption
90             'no descendants by default' | ''                       || OMIT_DESCENDANTS
91             'no descendant explicitly'  | 'false'                  || OMIT_DESCENDANTS
92             'descendants'               | 'true'                   || INCLUDE_ALL_DESCENDANTS
93     }
94
95    def 'Query data node v2 api by cps path for the given dataspace and anchor with #scenario.'() {
96         given: 'service method returns a list containing a data node'
97             def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
98                 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
99             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, { descendantsOption -> {
100                     assert descendantsOption.depth == 2}}) >> [dataNode1, dataNode1]
101         when: 'query data nodes API is invoked'
102             def response =
103                 mvc.perform(
104                         get(dataNodeEndpointV2)
105                                 .param('cps-path', cpsPath)
106                                 .param('descendants', '2'))
107                         .andReturn().response
108         then: 'the response contains the the datanode in json format'
109             assert response.status == HttpStatus.OK.value()
110             assert response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
111     }
112
113     def 'Query data node by cps path for the given dataspace across all anchors with #scenario.'() {
114         given: 'service method returns a list containing a data node'
115             def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
116                 .withAnchor('my_anchor')
117                 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
118             def dataNode2 = new DataNodeBuilder().withXpath('/xpath')
119                 .withAnchor('my_anchor_2')
120                 .withLeaves([leaf: 'value', leafList: ['leaveListElement3', 'leaveListElement4']]).build()
121             def dataspaceName = 'my_dataspace'
122             def cpsPath = 'some/cps/path'
123             mockCpsQueryService.queryDataNodesAcrossAnchors(dataspaceName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode2]
124         and: 'the query endpoint'
125             def dataNodeEndpoint = "$basePath/v2/dataspaces/$dataspaceName/nodes/query"
126         when: 'query data nodes API is invoked'
127             def response =
128                 mvc.perform(
129                         get(dataNodeEndpoint)
130                                 .param('cps-path', cpsPath)
131                                 .param('descendants', includeDescendantsOptionString))
132                         .andReturn().response
133         then: 'the response contains the the datanode in json format'
134             response.status == HttpStatus.OK.value()
135             response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
136             response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement3","leaveListElement4"]}}')
137         where: 'the following options for include descendants are provided in the request'
138             scenario                    | includeDescendantsOptionString || expectedCpsDataServiceOption
139             'no descendants by default' | ''                             || OMIT_DESCENDANTS
140             'no descendant explicitly'  | 'none'                         || OMIT_DESCENDANTS
141             'descendants'               | 'all'                          || INCLUDE_ALL_DESCENDANTS
142     }
143 }