2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modification Copyright (C) 2021 highstreet technologies GmbH
5 * Modification Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.rest.controller
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
25 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
31 import com.google.gson.Gson
32 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.spockframework.spring.SpringBean
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.beans.factory.annotation.Value
37 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
38 import org.springframework.http.HttpStatus
39 import org.springframework.http.MediaType
40 import org.springframework.test.web.servlet.MockMvc
41 import spock.lang.Specification
44 class NetworkCmProxyControllerSpec extends Specification {
50 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
52 @Value('${rest.api.ncmp-base-path}')
55 def dataNodeBaseEndpoint
58 dataNodeBaseEndpoint = "$basePath/v1"
61 def cmHandle = 'some handle'
62 def xpath = 'some xpath'
64 def 'Query data node by cps path for the given cm handle with #scenario.'() {
65 given: 'service method returns a list containing a data node'
66 def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
67 def cpsPath = 'some cps-path'
68 mockNetworkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
69 and: 'the query endpoint'
70 def dataNodeEndpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes/query"
71 when: 'query data nodes API is invoked'
72 def response = mvc.perform(get(dataNodeEndpoint)
73 .param('cps-path', cpsPath)
74 .param('include-descendants', includeDescendantsOption))
76 then: 'the response contains the the datanode in json format'
77 response.status == HttpStatus.OK.value()
78 def expectedJsonContent = new Gson().toJson(dataNode)
79 response.getContentAsString().contains(expectedJsonContent)
80 where: 'the following options for include descendants are provided in the request'
81 scenario | includeDescendantsOption || expectedCpsDataServiceOption
82 'no descendants by default' | '' || OMIT_DESCENDANTS
83 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS
84 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS
87 def 'Create data node: #scenario.'() {
89 def jsonData = 'json data'
90 when: 'post request is performed'
91 def response = mvc.perform(
92 post("$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes")
93 .contentType(MediaType.APPLICATION_JSON)
95 .param('xpath', reqXpath)
96 ).andReturn().response
97 then: 'the service method is invoked once with expected parameters'
98 1 * mockNetworkCmProxyDataService.createDataNode(cmHandle, usedXpath, jsonData)
99 and: 'response status indicates success'
100 response.status == HttpStatus.CREATED.value()
101 where: 'following parameters were used'
102 scenario | reqXpath || usedXpath
103 'no xpath parameter' | '' || '/'
104 'root xpath' | '/' || '/'
105 'parent node xpath' | '/xpath' || '/xpath'
108 def 'Add list-node elements.'() {
109 given: 'json data and parent node xpath'
110 def jsonData = 'json data'
111 def parentNodeXpath = 'parent node xpath'
112 when: 'post request is performed'
113 def response = mvc.perform(
114 post("$dataNodeBaseEndpoint/cm-handles/$cmHandle/list-node")
115 .contentType(MediaType.APPLICATION_JSON)
117 .param('xpath', parentNodeXpath)
118 ).andReturn().response
119 then: 'the service method is invoked once with expected parameters'
120 1 * mockNetworkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData)
121 and: 'response status indicates success'
122 response.status == HttpStatus.CREATED.value()
125 def 'Update data node leaves.'() {
127 def jsonData = 'json data'
128 and: 'the query endpoint'
129 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes"
130 when: 'patch request is performed'
131 def response = mvc.perform(
133 .contentType(MediaType.APPLICATION_JSON)
135 .param('xpath', xpath)
136 ).andReturn().response
137 then: 'the service method is invoked once with expected parameters'
138 1 * mockNetworkCmProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
139 and: 'response status indicates success'
140 response.status == HttpStatus.OK.value()
143 def 'Replace data node tree.'() {
145 def jsonData = 'json data'
146 and: 'the query endpoint'
147 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes"
148 when: 'put request is performed'
149 def response = mvc.perform(
151 .contentType(MediaType.APPLICATION_JSON)
153 .param('xpath', xpath)
154 ).andReturn().response
155 then: 'the service method is invoked once with expected parameters'
156 1 * mockNetworkCmProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
157 and: 'response status indicates success'
158 response.status == HttpStatus.OK.value()
161 def 'Get data node.'() {
162 given: 'the service returns a data node'
163 def xpath = 'some xpath'
164 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
165 mockNetworkCmProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
166 and: 'the query endpoint'
167 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node"
168 when: 'get request is performed through REST API'
169 def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
170 then: 'a success response is returned'
171 response.status == HttpStatus.OK.value()
172 and: 'response contains expected leaf and value'
173 response.contentAsString.contains('"leaf":"value"')