Align JSON DataNode for Get and Post/Put API in CPS
[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  *  ================================================================================
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.fasterxml.jackson.databind.ObjectMapper
30 import org.onap.cps.utils.JsonObjectMapper
31 import org.onap.cps.api.CpsQueryService
32 import org.onap.cps.spi.model.DataNodeBuilder
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.beans.factory.annotation.Value
36 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
37 import org.springframework.http.HttpStatus
38 import org.springframework.test.web.servlet.MockMvc
39 import spock.lang.Specification
40
41 @WebMvcTest(QueryRestController)
42 class QueryRestControllerSpec extends Specification {
43
44     @SpringBean
45     CpsQueryService mockCpsQueryService = Mock()
46
47     @SpringBean
48     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
49
50     @Autowired
51     MockMvc mvc
52
53     @Value('${rest.api.cps-base-path}')
54     def basePath
55
56     def 'Query data node by cps path for the given dataspace and anchor with #scenario.'() {
57         given: 'service method returns a list containing a data node'
58              def dataNode1 = new DataNodeBuilder().withXpath('/xpath')
59                     .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
60             def dataspaceName = 'my_dataspace'
61             def anchorName = 'my_anchor'
62             def cpsPath = 'some cps-path'
63             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, expectedCpsDataServiceOption) >> [dataNode1, dataNode1]
64         and: 'the query endpoint'
65             def dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes/query"
66         when: 'query data nodes API is invoked'
67             def response =
68                     mvc.perform(
69                             get(dataNodeEndpoint)
70                                     .param('cps-path', cpsPath)
71                                     .param('include-descendants', includeDescendantsOption))
72                             .andReturn().response
73         then: 'the response contains the the datanode in json format'
74             response.status == HttpStatus.OK.value()
75             response.getContentAsString().contains('{"xpath":{"leaf":"value","leafList":["leaveListElement1","leaveListElement2"]}}')
76         where: 'the following options for include descendants are provided in the request'
77             scenario                    | includeDescendantsOption || expectedCpsDataServiceOption
78             'no descendants by default' | ''                       || OMIT_DESCENDANTS
79             'no descendant explicitly'  | 'false'                  || OMIT_DESCENDANTS
80             'descendants'               | 'true'                   || INCLUDE_ALL_DESCENDANTS
81     }
82 }