3cd6b9a9f01172857f6cdb037ac0038180fadff8
[cps.git] / cps-nf-proxy-rest / src / test / groovy / org / onap / cps / nfproxy / rest / controller / NfProxyControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  ================================================================================
5  *  Modification Copyright (C) 2021 highstreet technologies GmbH
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.nfproxy.rest.controller
23
24 import org.onap.cps.nfproxy.api.NfProxyDataService
25 import org.onap.cps.spi.model.DataNodeBuilder
26 import org.spockframework.spring.SpringBean
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.beans.factory.annotation.Value
29 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
30 import org.springframework.http.HttpStatus
31 import org.springframework.test.web.servlet.MockMvc
32 import spock.lang.Specification
33 import spock.lang.Unroll
34
35 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
36 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
37
38 @WebMvcTest
39 class NfProxyControllerSpec extends Specification {
40
41     @Autowired
42     MockMvc mvc
43
44     @SpringBean
45     NfProxyDataService mockNfProxyDataService = Mock()
46
47     @Value('${rest.api.xnf-base-path}')
48     def basePath
49
50     def dataNodeBaseEndpoint
51
52     def setup() {
53         dataNodeBaseEndpoint = "$basePath/v1"
54     }
55
56     @Unroll
57     def 'Get data node.'() {
58         given: 'the service returns a data node'
59             def xpath = 'some xpath'
60             def cmHandle = 'some handle'
61             def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
62             def endpoint = "$dataNodeBaseEndpoint/cm-handles/$cmHandle/node"
63             mockNfProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
64         when: 'get request is performed through REST API'
65             def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
66         then: 'a success response is returned'
67             response.status == HttpStatus.OK.value()
68         and: 'response contains expected leaf and value'
69             response.contentAsString.contains('"leaf":"value"')
70     }
71 }