Merge "Fix security hotspots n Regex"
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / NetworkCmProxyControllerSpec.groovy
1 /*
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
10  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.rest.controller
23
24
25 import com.google.gson.Gson
26 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
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
37
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.*
41
42 @WebMvcTest
43 class NetworkCmProxyControllerSpec extends Specification {
44
45     @Autowired
46     MockMvc mvc
47
48     @SpringBean
49     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
50
51     @Value('${rest.api.ncmp-base-path}')
52     def basePath
53
54     def dataNodeBaseEndpoint
55
56     def setup() {
57         dataNodeBaseEndpoint = "$basePath/v1"
58     }
59
60     def cmHandle = 'some handle'
61     def xpath = 'some xpath'
62
63     @Unroll
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))
75                     .andReturn().response
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
85     }
86
87     def 'Update data node leaves.'() {
88         given: 'json data'
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(
94                     patch(endpoint)
95                             .contentType(MediaType.APPLICATION_JSON)
96                             .content(jsonData)
97                             .param('xpath', xpath)
98             ).andReturn().response
99         then: 'the service method is invoked once with expected parameters'
100             1 * mockNetworkCmProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
101         and: 'response status indicates success'
102             response.status == HttpStatus.OK.value()
103     }
104
105     def 'Replace data node tree.'() {
106         given: 'json data'
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(
112                     put(endpoint)
113                             .contentType(MediaType.APPLICATION_JSON)
114                             .content(jsonData)
115                             .param('xpath', xpath)
116             ).andReturn().response
117         then: 'the service method is invoked once with expected parameters'
118             1 * mockNetworkCmProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
119         and: 'response status indicates success'
120             response.status == HttpStatus.OK.value()
121     }
122
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             mockNetworkCmProxyDataService.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"')
136     }
137 }
138