Create child data node (part 2): NCMP service + REST
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / api / impl / NetworkCmProxyDataServiceImplSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.api.impl
22
23 import org.onap.cps.api.CpsDataService
24 import org.onap.cps.api.CpsQueryService
25 import org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServiceImpl
26 import org.onap.cps.spi.FetchDescendantsOption
27 import spock.lang.Specification
28 import spock.lang.Unroll
29
30 class NetworkCmProxyDataServiceImplSpec extends Specification {
31     def objectUnderTest = new NetworkCmProxyDataServiceImpl()
32     def mockcpsDataService = Mock(CpsDataService)
33     def mockcpsQueryService = Mock(CpsQueryService)
34
35     def setup() {
36         objectUnderTest.cpsDataService = mockcpsDataService
37         objectUnderTest.cpsQueryService = mockcpsQueryService
38     }
39
40     def cmHandle = 'some handle'
41     def expectedDataspaceName = 'NFP-Operational'
42
43     def 'Query data nodes by cps path with #fetchDescendantsOption.'() {
44         given: 'a cm Handle and a cps path'
45             def cpsPath = '/cps-path'
46         when: 'queryDataNodes is invoked'
47             objectUnderTest.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption)
48         then: 'the persistence service is called once with the correct parameters'
49             1 * mockcpsQueryService.queryDataNodes(expectedDataspaceName, cmHandle, cpsPath, fetchDescendantsOption)
50         where: 'all fetch descendants options are supported'
51             fetchDescendantsOption << FetchDescendantsOption.values()
52     }
53
54     @Unroll
55     def 'Create full data node: #scenario.'() {
56         given: 'a cm handle and root xpath'
57             def jsonData = 'some json'
58         when: 'createDataNode is invoked'
59             objectUnderTest.createDataNode(cmHandle, xpath, jsonData)
60         then: 'the CPS service method is invoked once with the expected parameters'
61             1 * mockcpsDataService.saveData(expectedDataspaceName, cmHandle, jsonData)
62         where: 'following parameters were used'
63             scenario           | xpath
64             'no xpath'         | ''
65             'root level xpath' | '/'
66     }
67
68     def 'Create child data node.'() {
69         given: 'a cm handle and parent node xpath'
70             def jsonData = 'some json'
71             def xpath = '/test-node'
72         when: 'createDataNode is invoked'
73             objectUnderTest.createDataNode(cmHandle, xpath, jsonData)
74         then: 'the CPS service method is invoked once with the expected parameters'
75             1 * mockcpsDataService.saveData(expectedDataspaceName, cmHandle, xpath, jsonData)
76     }
77
78     def 'Update data node leaves.'() {
79         given: 'a cm Handle and a cps path'
80             def xpath = '/xpath'
81             def jsonData = 'some json'
82         when: 'updateNodeLeaves is invoked'
83             objectUnderTest.updateNodeLeaves(cmHandle, xpath, jsonData)
84         then: 'the persistence service is called once with the correct parameters'
85             1 * mockcpsDataService.updateNodeLeaves(expectedDataspaceName, cmHandle, xpath, jsonData)
86     }
87
88     def 'Replace data node tree.'() {
89         given: 'a cm Handle and a cps path'
90             def xpath = '/xpath'
91             def jsonData = 'some json'
92         when: 'replaceNodeTree is invoked'
93             objectUnderTest.replaceNodeTree(cmHandle, xpath, jsonData)
94         then: 'the persistence service is called once with the correct parameters'
95             1 * mockcpsDataService.replaceNodeTree(expectedDataspaceName, cmHandle, xpath, jsonData)
96     }
97 }