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
24 import com.google.common.collect.ImmutableMap
25 import com.google.gson.Gson
26 import org.onap.cps.nfproxy.api.NfProxyDataService
27 import org.onap.cps.spi.model.DataNode
28 import org.onap.cps.spi.model.DataNodeBuilder
29 import org.spockframework.spring.SpringBean
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.beans.factory.annotation.Value
32 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
33 import org.springframework.http.HttpStatus
34 import org.springframework.http.MediaType
35 import org.springframework.test.web.servlet.MockMvc
36 import spock.lang.Specification
38 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
39 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
42 class NfProxyControllerSpec extends Specification {
48 NfProxyDataService mockNfProxyDataService = Mock()
50 @Value('${rest.api.xnf-base-path}')
53 def dataNodeBaseEndpoint
56 dataNodeBaseEndpoint = "$basePath/v1"
59 def cmHandle = 'some handle'
60 def xpath = 'some xpath'
62 def 'Query data node by cps path for the given cm handle.'() {
63 given: 'service method returns a list containing a data node'
64 def cpsPath = '/xpath/leaves[@leaf=\'value\']'
65 def dataNode = new DataNodeBuilder().withXpath("/xpath")
66 .withLeaves(ImmutableMap.of("leaf", "value")).build()
67 ArrayList<DataNode> dataNodeList = new ArrayList();
68 dataNodeList.add(dataNode)
69 mockNfProxyDataService.queryDataNodes(cmHandle, cpsPath) >> dataNodeList
70 and: 'the query endpoint'
71 def dataNodeEndpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes/query"
72 when: 'query data nodes API is invoked'
73 def response = mvc.perform(get(dataNodeEndpoint).param('cps-path', cpsPath)).andReturn().response
74 then: 'the response contains the the datanode in json format'
75 response.status == HttpStatus.OK.value()
76 def expectedJsonContent = new Gson().toJson(dataNode)
77 response.getContentAsString().contains(expectedJsonContent)
80 def 'Update data node leaves.'() {
82 def jsonData = 'json data'
83 and: 'the query endpoint'
84 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes"
85 when: 'patch request is performed'
86 def response = mvc.perform(
88 .contentType(MediaType.APPLICATION_JSON)
90 .param('xpath', xpath)
91 ).andReturn().response
92 then: 'the service method is invoked once with expected parameters'
93 1 * mockNfProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
94 and: 'response status indicates success'
95 response.status == HttpStatus.OK.value()
98 def 'Replace data node tree.'() {
100 def jsonData = 'json data'
101 and: 'the query endpoint'
102 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/nodes"
103 when: 'put request is performed'
104 def response = mvc.perform(
106 .contentType(MediaType.APPLICATION_JSON)
108 .param('xpath', xpath)
109 ).andReturn().response
110 then: 'the service method is invoked once with expected parameters'
111 1 * mockNfProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
112 and: 'response status indicates success'
113 response.status == HttpStatus.OK.value()
116 def 'Get data node.'() {
117 given: 'the service returns a data node'
118 def xpath = 'some xpath'
119 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
120 mockNfProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
121 and: 'the query endpoint'
122 def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node"
123 when: 'get request is performed through REST API'
124 def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
125 then: 'a success response is returned'
126 response.status == HttpStatus.OK.value()
127 and: 'response contains expected leaf and value'
128 response.contentAsString.contains('"leaf":"value"')