Merge "Update CmHandle in DMI-Registry for a DMI-Plugin Instance in NCMP as part...
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsDataServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021 Bell Canada.
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  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl
24
25 import org.onap.cps.TestUtils
26 import org.onap.cps.api.CpsAdminService
27 import org.onap.cps.api.CpsModuleService
28 import org.onap.cps.notification.NotificationService
29 import org.onap.cps.spi.CpsDataPersistenceService
30 import org.onap.cps.spi.FetchDescendantsOption
31 import org.onap.cps.spi.exceptions.DataValidationException
32 import org.onap.cps.spi.model.Anchor
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.onap.cps.yang.YangTextSchemaSourceSet
35 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
36 import spock.lang.Specification
37
38 class CpsDataServiceImplSpec extends Specification {
39     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
40     def mockCpsAdminService = Mock(CpsAdminService)
41     def mockCpsModuleService = Mock(CpsModuleService)
42     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
43     def mockNotificationService = Mock(NotificationService)
44
45     def objectUnderTest = new CpsDataServiceImpl()
46
47     def setup() {
48         objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
49         objectUnderTest.cpsAdminService = mockCpsAdminService
50         objectUnderTest.cpsModuleService = mockCpsModuleService
51         objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
52         objectUnderTest.notificationService = mockNotificationService
53     }
54
55     def dataspaceName = 'some dataspace'
56     def anchorName = 'some anchor'
57     def schemaSetName = 'some schema set'
58
59     def 'Saving json data.'() {
60         given: 'schema set for given anchor and dataspace references test-tree model'
61             setupSchemaSetMocks('test-tree.yang')
62         when: 'save data method is invoked with test-tree json data'
63             def jsonData = TestUtils.getResourceFileContent('test-tree.json')
64             objectUnderTest.saveData(dataspaceName, anchorName, jsonData)
65         then: 'the persistence service method is invoked with correct parameters'
66             1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
67                     { dataNode -> dataNode.xpath == '/test-tree' })
68         and: 'data updated event is sent to notification service'
69             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
70     }
71
72     def 'Saving child data fragment under existing node.'() {
73         given: 'schema set for given anchor and dataspace references test-tree model'
74             setupSchemaSetMocks('test-tree.yang')
75         when: 'save data method is invoked with test-tree json data'
76             def jsonData = '{"branch": [{"name": "New"}]}'
77             objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData)
78         then: 'the persistence service method is invoked with correct parameters'
79             1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
80                     { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
81         and: 'data updated event is sent to notification service'
82             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
83     }
84
85     def 'Saving list-node data fragment under existing node.'() {
86         given: 'schema set for given anchor and dataspace references test-tree model'
87             setupSchemaSetMocks('test-tree.yang')
88         when: 'save data method is invoked with list-node json data'
89             def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
90             objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
91         then: 'the persistence service method is invoked with correct parameters'
92             1 * mockCpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, '/test-tree',
93                     { dataNodeCollection ->
94                         {
95                             assert dataNodeCollection.size() == 2
96                             assert dataNodeCollection.collect { it.getXpath() }
97                                     .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
98                         }
99                     }
100             )
101         and: 'data updated event is sent to notification service'
102             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
103     }
104
105     def 'Saving empty list-node data fragment.'() {
106         given: 'schema set for given anchor and dataspace references test-tree model'
107             setupSchemaSetMocks('test-tree.yang')
108         when: 'save data method is invoked with empty list-node data fragment'
109             def jsonData = '{"branch": []}'
110             objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
111         then: 'invalid data exception is thrown'
112             thrown(DataValidationException)
113     }
114
115     def 'Get data node with option #fetchDescendantsOption.'() {
116         def xpath = '/xpath'
117         def dataNode = new DataNodeBuilder().withXpath(xpath).build()
118         given: 'persistence service returns data for get data request'
119             mockCpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) >> dataNode
120         expect: 'service returns same data if uses same parameters'
121             objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) == dataNode
122         where: 'all fetch options are supported'
123             fetchDescendantsOption << FetchDescendantsOption.values()
124     }
125
126     def 'Update data node leaves: #scenario.'() {
127         given: 'schema set for given anchor and dataspace references test-tree model'
128             setupSchemaSetMocks('test-tree.yang')
129         when: 'update data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
130             objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData)
131         then: 'the persistence service method is invoked with correct parameters'
132             1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
133         and: 'data updated event is sent to notification service'
134             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
135         where: 'following parameters were used'
136             scenario         | parentNodeXpath | jsonData                        || expectedNodeXpath                   | leaves
137             'top level node' | '/'             | '{"test-tree": {"branch": []}}' || '/test-tree'                        | Collections.emptyMap()
138             'level 2 node'   | '/test-tree'    | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']' | ['name': 'Name']
139     }
140
141     def 'Update list-element data node with : #scenario.'() {
142         given: 'schema set for given anchor and dataspace references bookstore model'
143             setupSchemaSetMocks('bookstore.yang')
144         when: 'update data method is invoked with json data #jsonData and parent node xpath'
145             objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]', jsonData)
146         then: 'the persistence service method is invoked with correct parameters'
147             thrown(DataValidationException)
148         where: 'following parameters were used'
149             scenario          | jsonData
150             'multiple expectedLeaves' | '{"code": "01","name": "some-name"}'
151             'one leaf'        | '{"name": "some-name"}'
152     }
153
154     def 'Update cm-handle properties' () {
155         given: 'a dmi registry model'
156             setupSchemaSetMocks('dmi-registry.yang')
157         and: 'the expected json string'
158             def jsonData = '{"cm-handles":[{"id":"cmHandle001", "additional-properties":[{"name":"P1"}]}]}'
159         when: 'update data method is invoked with json data and parent node xpath'
160             objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName, '/dmi-registry', jsonData)
161         then: 'the persistence service method is invoked with correct parameters'
162             1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, "/dmi-registry/cm-handles[@id='cmHandle001']", ['id': 'cmHandle001'])
163         and: 'the data updated event is sent to the notification service'
164             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
165     }
166
167     def 'Replace data node: #scenario.'() {
168         given: 'schema set for given anchor and dataspace references test-tree model'
169             setupSchemaSetMocks('test-tree.yang')
170         when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
171             objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData)
172         then: 'the persistence service method is invoked with correct parameters'
173             1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
174                     { dataNode -> dataNode.xpath == expectedNodeXpath })
175         and: 'data updated event is sent to notification service'
176             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
177         where: 'following parameters were used'
178             scenario         | parentNodeXpath | jsonData                        || expectedNodeXpath
179             'top level node' | '/'             | '{"test-tree": {"branch": []}}' || '/test-tree'
180             'level 2 node'   | '/test-tree'    | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']'
181     }
182
183     def 'Replace list-node data fragment under existing node.'() {
184         given: 'schema set for given anchor and dataspace references test-tree model'
185             setupSchemaSetMocks('test-tree.yang')
186         when: 'replace list data method is invoked with list-node json data'
187             def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
188             objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
189         then: 'the persistence service method is invoked with correct parameters'
190             1 * mockCpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, '/test-tree',
191                     { dataNodeCollection ->
192                         {
193                             assert dataNodeCollection.size() == 2
194                             assert dataNodeCollection.collect { it.getXpath() }
195                                     .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
196                         }
197                     }
198             )
199         and: 'data updated event is sent to notification service'
200             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
201     }
202
203     def 'Replace with empty list-node data fragment.'() {
204         given: 'schema set for given anchor and dataspace references test-tree model'
205             setupSchemaSetMocks('test-tree.yang')
206         when: 'replace list data method is invoked with empty list-node data fragment'
207             def jsonData = '{"branch": []}'
208             objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
209         then: 'invalid data exception is thrown'
210             thrown(DataValidationException)
211     }
212
213     def 'Delete list-node data fragment under existing node.'() {
214         given: 'schema set for given anchor and dataspace references test-tree model'
215             setupSchemaSetMocks('test-tree.yang')
216         when: 'delete list data method is invoked with list-node json data'
217             objectUnderTest.deleteListNodeData(dataspaceName, anchorName, '/test-tree/branch')
218         then: 'the persistence service method is invoked with correct parameters'
219             1 * mockCpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, '/test-tree/branch')
220         and: 'data updated event is sent to notification service'
221             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
222     }
223
224     def setupSchemaSetMocks(String... yangResources) {
225         def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
226         mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
227         def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
228         mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
229         def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
230         def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
231         mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext
232     }
233 }