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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl
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
39 import java.time.OffsetDateTime
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)
47 def objectUnderTest = new CpsDataServiceImpl(mockCpsDataPersistenceService, mockCpsAdminService,
48 mockYangTextSchemaSourceSetCache, mockNotificationService)
50 def dataspaceName = 'some dataspace'
51 def anchorName = 'some anchor'
52 def schemaSetName = 'some schema set'
53 def observedTimestamp = OffsetDateTime.now()
55 def 'Saving json data.'() {
56 given: 'schema set for given anchor and dataspace references test-tree model'
57 setupSchemaSetMocks('test-tree.yang')
58 when: 'save data method is invoked with test-tree json data'
59 def jsonData = TestUtils.getResourceFileContent('test-tree.json')
60 objectUnderTest.saveData(dataspaceName, anchorName, jsonData, observedTimestamp)
61 then: 'the persistence service method is invoked with correct parameters'
62 1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
63 { dataNode -> dataNode.xpath == '/test-tree' })
64 and: 'data updated event is sent to notification service'
65 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/', Operation.CREATE)
68 def 'Saving child data fragment under existing node.'() {
69 given: 'schema set for given anchor and dataspace references test-tree model'
70 setupSchemaSetMocks('test-tree.yang')
71 when: 'save data method is invoked with test-tree json data'
72 def jsonData = '{"branch": [{"name": "New"}]}'
73 objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
74 then: 'the persistence service method is invoked with correct parameters'
75 1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
76 { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
77 and: 'data updated event is sent to notification service'
78 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.CREATE)
81 def 'Saving list element data fragment under existing node.'() {
82 given: 'schema set for given anchor and dataspace references test-tree model'
83 setupSchemaSetMocks('test-tree.yang')
84 when: 'save data method is invoked with list element json data'
85 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
86 objectUnderTest.saveListElements(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
87 then: 'the persistence service method is invoked with correct parameters'
88 1 * mockCpsDataPersistenceService.addListElements(dataspaceName, anchorName, '/test-tree',
89 { dataNodeCollection ->
91 assert dataNodeCollection.size() == 2
92 assert dataNodeCollection.collect { it.getXpath() }
93 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
97 and: 'data updated event is sent to notification service'
98 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE)
101 def 'Saving empty list element data fragment.'() {
102 given: 'schema set for given anchor and dataspace references test-tree model'
103 setupSchemaSetMocks('test-tree.yang')
104 when: 'save data method is invoked with an empty list'
105 def jsonData = '{"branch": []}'
106 objectUnderTest.saveListElements(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
107 then: 'invalid data exception is thrown'
108 thrown(DataValidationException)
111 def 'Get data node with option #fetchDescendantsOption.'() {
113 def dataNode = new DataNodeBuilder().withXpath(xpath).build()
114 given: 'persistence service returns data for get data request'
115 mockCpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) >> dataNode
116 expect: 'service returns same data if uses same parameters'
117 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) == dataNode
118 where: 'all fetch options are supported'
119 fetchDescendantsOption << FetchDescendantsOption.values()
122 def 'Update data node leaves: #scenario.'() {
123 given: 'schema set for given anchor and dataspace references test-tree model'
124 setupSchemaSetMocks('test-tree.yang')
125 when: 'update data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
126 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData, observedTimestamp)
127 then: 'the persistence service method is invoked with correct parameters'
128 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
129 and: 'data updated event is sent to notification service'
130 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE)
131 where: 'following parameters were used'
132 scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves
133 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap()
134 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']' | ['name': 'Name']
137 def 'Update list-element data node with : #scenario.'() {
138 given: 'schema set for given anchor and dataspace references bookstore model'
139 setupSchemaSetMocks('bookstore.yang')
140 when: 'update data method is invoked with json data #jsonData and parent node xpath'
141 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]',
142 jsonData, observedTimestamp)
143 then: 'the persistence service method is invoked with correct parameters'
144 thrown(DataValidationException)
145 where: 'following parameters were used'
147 'multiple expectedLeaves' | '{"code": "01","name": "some-name"}'
148 'one leaf' | '{"name": "some-name"}'
151 def 'Update Bookstore node leaves' () {
152 given: 'a DMI registry model'
153 setupSchemaSetMocks('bookstore.yang')
154 and: 'the expected json string'
155 def jsonData = '{"categories":[{"code":01,"name":"Romance"}]}'
156 when: 'update data method is invoked with json data and parent node xpath'
157 objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName,
158 '/bookstore', jsonData, observedTimestamp)
159 then: 'the persistence service method is invoked with correct parameters'
160 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName,
161 "/bookstore/categories[@code='01']", ['name':'Romance', 'code': '01'])
162 and: 'the data updated event is sent to the notification service'
163 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/bookstore', Operation.UPDATE)
166 def 'Replace data node: #scenario.'() {
167 given: 'schema set for given anchor and dataspace references test-tree model'
168 setupSchemaSetMocks('test-tree.yang')
169 when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
170 objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, observedTimestamp)
171 then: 'the persistence service method is invoked with correct parameters'
172 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
173 { dataNode -> dataNode.xpath == expectedNodeXpath })
174 and: 'data updated event is sent to notification service'
175 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE)
176 where: 'following parameters were used'
177 scenario | parentNodeXpath | jsonData || expectedNodeXpath
178 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
179 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']'
182 def 'Replace list content data fragment under parent node.'() {
183 given: 'schema set for given anchor and dataspace references test-tree model'
184 setupSchemaSetMocks('test-tree.yang')
185 when: 'replace list data method is invoked with list element json data'
186 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
187 objectUnderTest.replaceListContent(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
188 then: 'the persistence service method is invoked with correct parameters'
189 1 * mockCpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, '/test-tree',
190 { dataNodeCollection ->
192 assert dataNodeCollection.size() == 2
193 assert dataNodeCollection.collect { it.getXpath() }
194 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
198 and: 'data updated event is sent to notification service'
199 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE)
202 def 'Replace whole list content with empty list element.'() {
203 given: 'schema set for given anchor and dataspace references test-tree model'
204 setupSchemaSetMocks('test-tree.yang')
205 when: 'replace list data method is invoked with empty list'
206 def jsonData = '{"branch": []}'
207 objectUnderTest.replaceListContent(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
208 then: 'invalid data exception is thrown'
209 thrown(DataValidationException)
212 def 'Delete list element under existing node.'() {
213 given: 'schema set for given anchor and dataspace references test-tree model'
214 setupSchemaSetMocks('test-tree.yang')
215 when: 'delete list data method is invoked with list element json data'
216 objectUnderTest.deleteListOrListElement(dataspaceName, anchorName, '/test-tree/branch', observedTimestamp)
217 then: 'the persistence service method is invoked with correct parameters'
218 1 * mockCpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, '/test-tree/branch')
219 and: 'data updated event is sent to notification service'
220 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree/branch', Operation.DELETE)
223 def 'Delete data node under anchor and dataspace.'() {
224 given: 'schema set for given anchor and dataspace references test tree model'
225 setupSchemaSetMocks('test-tree.yang')
226 when: 'delete data node method is invoked with correct parameters'
227 objectUnderTest.deleteDataNode(dataspaceName, anchorName, '/data-node', observedTimestamp)
228 then: 'the persistence service method is invoked with the correct parameters'
229 1 * mockCpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, '/data-node')
230 and: 'data updated event is sent to notification service'
231 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/data-node', Operation.DELETE)
234 def 'Delete all data nodes for a given anchor and dataspace.'() {
235 given: 'schema set for given anchor and dataspace references test tree model'
236 setupSchemaSetMocks('test-tree.yang')
237 when: 'delete data node method is invoked with correct parameters'
238 objectUnderTest.deleteDataNodes(dataspaceName, anchorName, observedTimestamp)
239 then: 'the persistence service method is invoked with the correct parameters'
240 1 * mockCpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName)
243 def setupSchemaSetMocks(String... yangResources) {
244 def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
245 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
246 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
247 mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
248 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
249 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
250 mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext