Support 'public' Cm Handle Properties
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsDataServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 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.notification.Operation
30 import org.onap.cps.spi.CpsDataPersistenceService
31 import org.onap.cps.spi.FetchDescendantsOption
32 import org.onap.cps.spi.exceptions.DataValidationException
33 import org.onap.cps.spi.model.Anchor
34 import org.onap.cps.spi.model.DataNodeBuilder
35 import org.onap.cps.yang.YangTextSchemaSourceSet
36 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
37 import spock.lang.Specification
38
39 import java.time.OffsetDateTime
40
41 class CpsDataServiceImplSpec extends Specification {
42     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
43     def mockCpsAdminService = Mock(CpsAdminService)
44     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
45     def mockNotificationService = Mock(NotificationService)
46
47     def objectUnderTest = new CpsDataServiceImpl()
48
49     def setup() {
50         objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
51         objectUnderTest.cpsAdminService = mockCpsAdminService
52         objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
53         objectUnderTest.notificationService = mockNotificationService
54     }
55
56     def dataspaceName = 'some dataspace'
57     def anchorName = 'some anchor'
58     def schemaSetName = 'some schema set'
59     def observedTimestamp = OffsetDateTime.now()
60
61     def 'Saving json data.'() {
62         given: 'schema set for given anchor and dataspace references test-tree model'
63             setupSchemaSetMocks('test-tree.yang')
64         when: 'save data method is invoked with test-tree json data'
65             def jsonData = TestUtils.getResourceFileContent('test-tree.json')
66             objectUnderTest.saveData(dataspaceName, anchorName, jsonData, observedTimestamp)
67         then: 'the persistence service method is invoked with correct parameters'
68             1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
69                 { dataNode -> dataNode.xpath == '/test-tree' })
70         and: 'data updated event is sent to notification service'
71             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/', Operation.CREATE)
72     }
73
74     def 'Saving child data fragment under existing node.'() {
75         given: 'schema set for given anchor and dataspace references test-tree model'
76             setupSchemaSetMocks('test-tree.yang')
77         when: 'save data method is invoked with test-tree json data'
78             def jsonData = '{"branch": [{"name": "New"}]}'
79             objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
80         then: 'the persistence service method is invoked with correct parameters'
81             1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
82                 { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
83         and: 'data updated event is sent to notification service'
84             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.CREATE)
85     }
86
87     def 'Saving list element data fragment under existing node.'() {
88         given: 'schema set for given anchor and dataspace references test-tree model'
89             setupSchemaSetMocks('test-tree.yang')
90         when: 'save data method is invoked with list element json data'
91             def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
92             objectUnderTest.saveListElements(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
93         then: 'the persistence service method is invoked with correct parameters'
94             1 * mockCpsDataPersistenceService.addListElements(dataspaceName, anchorName, '/test-tree',
95                 { dataNodeCollection ->
96                     {
97                         assert dataNodeCollection.size() == 2
98                         assert dataNodeCollection.collect { it.getXpath() }
99                             .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
100                     }
101                 }
102             )
103         and: 'data updated event is sent to notification service'
104             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE)
105     }
106
107     def 'Saving empty list element data fragment.'() {
108         given: 'schema set for given anchor and dataspace references test-tree model'
109             setupSchemaSetMocks('test-tree.yang')
110         when: 'save data method is invoked with an empty list'
111             def jsonData = '{"branch": []}'
112             objectUnderTest.saveListElements(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
113         then: 'invalid data exception is thrown'
114             thrown(DataValidationException)
115     }
116
117     def 'Get data node with option #fetchDescendantsOption.'() {
118         def xpath = '/xpath'
119         def dataNode = new DataNodeBuilder().withXpath(xpath).build()
120         given: 'persistence service returns data for get data request'
121             mockCpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) >> dataNode
122         expect: 'service returns same data if uses same parameters'
123             objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) == dataNode
124         where: 'all fetch options are supported'
125             fetchDescendantsOption << FetchDescendantsOption.values()
126     }
127
128     def 'Update data node leaves: #scenario.'() {
129         given: 'schema set for given anchor and dataspace references test-tree model'
130             setupSchemaSetMocks('test-tree.yang')
131         when: 'update data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
132             objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData, observedTimestamp)
133         then: 'the persistence service method is invoked with correct parameters'
134             1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
135         and: 'data updated event is sent to notification service'
136             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE)
137         where: 'following parameters were used'
138             scenario         | parentNodeXpath | jsonData                        || expectedNodeXpath                   | leaves
139             'top level node' | '/'             | '{"test-tree": {"branch": []}}' || '/test-tree'                        | Collections.emptyMap()
140             'level 2 node'   | '/test-tree'    | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']' | ['name': 'Name']
141     }
142
143     def 'Update list-element data node with : #scenario.'() {
144         given: 'schema set for given anchor and dataspace references bookstore model'
145             setupSchemaSetMocks('bookstore.yang')
146         when: 'update data method is invoked with json data #jsonData and parent node xpath'
147             objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]',
148                 jsonData, observedTimestamp)
149         then: 'the persistence service method is invoked with correct parameters'
150             thrown(DataValidationException)
151         where: 'following parameters were used'
152             scenario          | jsonData
153             'multiple expectedLeaves' | '{"code": "01","name": "some-name"}'
154             'one leaf'        | '{"name": "some-name"}'
155     }
156
157     def 'Update Bookstore node leaves' () {
158         given: 'a DMI registry model'
159             setupSchemaSetMocks('bookstore.yang')
160         and: 'the expected json string'
161             def jsonData = '{"categories":[{"code":01,"name":"Romance"}]}'
162         when: 'update data method is invoked with json data and parent node xpath'
163             objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName,
164                 '/bookstore', jsonData, observedTimestamp)
165         then: 'the persistence service method is invoked with correct parameters'
166             1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName,
167                 "/bookstore/categories[@code='01']", ['name':'Romance', 'code': '01'])
168         and: 'the data updated event is sent to the notification service'
169             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/bookstore', Operation.UPDATE)
170     }
171
172     def 'Replace data node: #scenario.'() {
173         given: 'schema set for given anchor and dataspace references test-tree model'
174             setupSchemaSetMocks('test-tree.yang')
175         when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
176             objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, observedTimestamp)
177         then: 'the persistence service method is invoked with correct parameters'
178             1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
179                 { dataNode -> dataNode.xpath == expectedNodeXpath })
180         and: 'data updated event is sent to notification service'
181             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE)
182         where: 'following parameters were used'
183             scenario         | parentNodeXpath | jsonData                        || expectedNodeXpath
184             'top level node' | '/'             | '{"test-tree": {"branch": []}}' || '/test-tree'
185             'level 2 node'   | '/test-tree'    | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']'
186     }
187
188     def 'Replace list content data fragment under parent node.'() {
189         given: 'schema set for given anchor and dataspace references test-tree model'
190             setupSchemaSetMocks('test-tree.yang')
191         when: 'replace list data method is invoked with list element json data'
192             def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
193             objectUnderTest.replaceListContent(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
194         then: 'the persistence service method is invoked with correct parameters'
195             1 * mockCpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, '/test-tree',
196                 { dataNodeCollection ->
197                     {
198                         assert dataNodeCollection.size() == 2
199                         assert dataNodeCollection.collect { it.getXpath() }
200                             .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
201                     }
202                 }
203             )
204         and: 'data updated event is sent to notification service'
205             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE)
206     }
207
208     def 'Replace whole list content with empty list element.'() {
209         given: 'schema set for given anchor and dataspace references test-tree model'
210             setupSchemaSetMocks('test-tree.yang')
211         when: 'replace list data method is invoked with empty list'
212             def jsonData = '{"branch": []}'
213             objectUnderTest.replaceListContent(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
214         then: 'invalid data exception is thrown'
215             thrown(DataValidationException)
216     }
217
218     def 'Delete list element under existing node.'() {
219         given: 'schema set for given anchor and dataspace references test-tree model'
220             setupSchemaSetMocks('test-tree.yang')
221         when: 'delete list data method is invoked with list element json data'
222             objectUnderTest.deleteListOrListElement(dataspaceName, anchorName, '/test-tree/branch', observedTimestamp)
223         then: 'the persistence service method is invoked with correct parameters'
224             1 * mockCpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, '/test-tree/branch')
225         and: 'data updated event is sent to notification service'
226             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree/branch', Operation.DELETE)
227     }
228
229     def 'Delete data node under anchor and dataspace.'() {
230         given: 'schema set for given anchor and dataspace references test tree model'
231             setupSchemaSetMocks('test-tree.yang')
232         when: 'delete data node method is invoked with correct parameters'
233             objectUnderTest.deleteDataNode(dataspaceName, anchorName, '/data-node', observedTimestamp)
234         then: 'the persistence service method is invoked with the correct parameters'
235             1 * mockCpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, '/data-node')
236         and: 'data updated event is sent to notification service'
237             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/data-node', Operation.DELETE)
238     }
239
240     def setupSchemaSetMocks(String... yangResources) {
241         def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
242         mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
243         def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
244         mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
245         def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
246         def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
247         mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext
248     }
249 }