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.nfproxy.rest.controller
25 import com.google.gson.Gson
26 import org.onap.cps.nfproxy.api.NfProxyDataService
27 import org.onap.cps.spi.model.DataNodeBuilder
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.beans.factory.annotation.Value
31 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
32 import org.springframework.http.HttpStatus
33 import org.springframework.http.MediaType
34 import org.springframework.test.web.servlet.MockMvc
35 import spock.lang.Specification
36 import spock.lang.Unroll
38 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
39 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
43 class NfProxyControllerSpec extends Specification {
49 NfProxyDataService mockNfProxyDataService = Mock()
51 @Value('${rest.api.xnf-base-path}')
54 def dataNodeBaseEndpoint
57 dataNodeBaseEndpoint = "$basePath/v1"
60 def cmHandle = 'some handle'
61 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 mockNfProxyDataService.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 'Update data node leaves.'() {
89 def jsonData = 'json data'
90 and: 'the query endpoint'
91 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes"
92 when: 'patch request is performed'
93 def response = mvc.perform(
95 .contentType(MediaType.APPLICATION_JSON)
97 .param('xpath', xpath)
98 ).andReturn().response
99 then: 'the service method is invoked once with expected parameters'
100 1 * mockNfProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
101 and: 'response status indicates success'
102 response.status == HttpStatus.OK.value()
105 def 'Replace data node tree.'() {
107 def jsonData = 'json data'
108 and: 'the query endpoint'
109 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes"
110 when: 'put request is performed'
111 def response = mvc.perform(
113 .contentType(MediaType.APPLICATION_JSON)
115 .param('xpath', xpath)
116 ).andReturn().response
117 then: 'the service method is invoked once with expected parameters'
118 1 * mockNfProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
119 and: 'response status indicates success'
120 response.status == HttpStatus.OK.value()
123 def 'Get data node.'() {
124 given: 'the service returns a data node'
125 def xpath = 'some xpath'
126 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
127 mockNfProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
128 and: 'the query endpoint'
129 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node"
130 when: 'get request is performed through REST API'
131 def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
132 then: 'a success response is returned'
133 response.status == HttpStatus.OK.value()
134 and: 'response contains expected leaf and value'
135 response.contentAsString.contains('"leaf":"value"')