Rest & Java API layer - Query Datanodes using cpsPath that contains contains a leaf...
[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 import com.google.common.collect.ImmutableMap
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
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
41
42 @WebMvcTest
43 class QueryRestControllerSpec extends Specification {
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     def 'Query data node by cps path for the given dataspace and anchor.'() {
67         given: 'service method returns a list containing a data node'
68             def dataspaceName = 'my_dataspace'
69             def anchorName = 'my_anchor'
70             def cpsPath = '/xpath/leaves[@leaf=\'value\']'
71             def dataNode = new DataNodeBuilder().withXpath("/xpath")
72                     .withLeaves(ImmutableMap.of("leaf", "value")).build()
73             ArrayList<DataNode> dataNodeList = new ArrayList();
74             dataNodeList.add(dataNode)
75             mockCpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath) >> dataNodeList
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 = mvc.perform(get(dataNodeEndpoint).param('cps-path', cpsPath)).andReturn().response
80         then: 'the response contains the the datanode in json format'
81             response.status == HttpStatus.OK.value()
82             response.getContentAsString().contains(new Gson().toJson(dataNode))
83     }
84 }