Merge "Implement DMI Registration (NCMP-Side)"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / 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  *
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 package org.onap.cps.ncmp.api.impl
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.api.CpsQueryService
26 import org.onap.cps.ncmp.api.models.CmHandle
27 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
28 import org.onap.cps.spi.FetchDescendantsOption
29 import spock.lang.Specification
30
31 class NetworkCmProxyDataServiceImplSpec extends Specification {
32
33     def mockCpsDataService = Mock(CpsDataService)
34     def mockCpsQueryService = Mock(CpsQueryService)
35     def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, mockCpsQueryService, new ObjectMapper())
36
37     def cmHandle = 'some handle'
38     def expectedDataspaceName = 'NFP-Operational'
39     def 'Query data nodes by cps path with #fetchDescendantsOption.'() {
40         given: 'a cm Handle and a cps path'
41             def cpsPath = '/cps-path'
42         when: 'queryDataNodes is invoked'
43             objectUnderTest.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption)
44         then: 'the persistence service is called once with the correct parameters'
45             1 * mockCpsQueryService.queryDataNodes(expectedDataspaceName, cmHandle, cpsPath, fetchDescendantsOption)
46         where: 'all fetch descendants options are supported'
47             fetchDescendantsOption << FetchDescendantsOption.values()
48     }
49     def 'Create full data node: #scenario.'() {
50         given: 'a cm handle and root xpath'
51             def jsonData = 'some json'
52         when: 'createDataNode is invoked'
53             objectUnderTest.createDataNode(cmHandle, xpath, jsonData)
54         then: 'the CPS service method is invoked once with the expected parameters'
55             1 * mockCpsDataService.saveData(expectedDataspaceName, cmHandle, jsonData)
56         where: 'following parameters were used'
57             scenario           | xpath
58             'no xpath'         | ''
59             'root level xpath' | '/'
60     }
61     def 'Create child data node.'() {
62         given: 'a cm handle and parent node xpath'
63             def jsonData = 'some json'
64             def xpath = '/test-node'
65         when: 'createDataNode is invoked'
66             objectUnderTest.createDataNode(cmHandle, xpath, jsonData)
67         then: 'the CPS service method is invoked once with the expected parameters'
68             1 * mockCpsDataService.saveData(expectedDataspaceName, cmHandle, xpath, jsonData)
69     }
70     def 'Add list-node elements.'() {
71         given: 'a cm handle and parent node xpath'
72             def jsonData = 'some json'
73             def xpath = '/test-node'
74         when: 'addListNodeElements is invoked'
75             objectUnderTest.addListNodeElements(cmHandle, xpath, jsonData)
76         then: 'the CPS service method is invoked once with the expected parameters'
77             1 * mockCpsDataService.saveListNodeData(expectedDataspaceName, cmHandle, xpath, jsonData)
78     }
79     def 'Update data node leaves.'() {
80         given: 'a cm Handle and a cps path'
81             def xpath = '/xpath'
82             def jsonData = 'some json'
83         when: 'updateNodeLeaves is invoked'
84             objectUnderTest.updateNodeLeaves(cmHandle, xpath, jsonData)
85         then: 'the persistence service is called once with the correct parameters'
86             1 * mockCpsDataService.updateNodeLeaves(expectedDataspaceName, cmHandle, xpath, jsonData)
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     def 'Register CM Handle Event.'() {
98         given: 'a registration '
99             def dmiPluginRegistration = new DmiPluginRegistration()
100             dmiPluginRegistration.dmiPlugin = 'my-server'
101             def cmHandle = new CmHandle()
102             cmHandle.cmHandle = '123'
103             cmHandle.cmHandleProperties = [ name1: 'value1', name2: 'value2']
104             dmiPluginRegistration.createdCmHandles = [ cmHandle ]
105             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","additional-properties":[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]}]}'
106         when: 'registration is updated'
107             objectUnderTest.updateDmiPluginRegistration(dmiPluginRegistration)
108         then: 'the CPS service method is invoked once with the expected parameters'
109             1 * mockCpsDataService.saveListNodeData('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData)
110     }
111 }