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
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.spi.CpsDataPersistenceService
30 import org.onap.cps.spi.FetchDescendantsOption
31 import org.onap.cps.spi.exceptions.CpsPathException
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 class CpsDataServiceImplSpec extends Specification {
40 def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
41 def mockCpsAdminService = Mock(CpsAdminService)
42 def mockCpsModuleService = Mock(CpsModuleService)
43 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
44 def mockNotificationService = Mock(NotificationService)
46 def objectUnderTest = new CpsDataServiceImpl()
49 objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
50 objectUnderTest.cpsAdminService = mockCpsAdminService
51 objectUnderTest.cpsModuleService = mockCpsModuleService
52 objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
53 objectUnderTest.notificationService = mockNotificationService
56 def dataspaceName = 'some dataspace'
57 def anchorName = 'some anchor'
58 def schemaSetName = 'some schema set'
60 def 'Saving json data.'() {
61 given: 'schema set for given anchor and dataspace references test-tree model'
62 setupSchemaSetMocks('test-tree.yang')
63 when: 'save data method is invoked with test-tree json data'
64 def jsonData = TestUtils.getResourceFileContent('test-tree.json')
65 objectUnderTest.saveData(dataspaceName, anchorName, jsonData)
66 then: 'the persistence service method is invoked with correct parameters'
67 1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
68 { dataNode -> dataNode.xpath == '/test-tree' })
69 and: 'data updated event is sent to notification service'
70 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
73 def 'Saving child data fragment under existing node.'() {
74 given: 'schema set for given anchor and dataspace references test-tree model'
75 setupSchemaSetMocks('test-tree.yang')
76 when: 'save data method is invoked with test-tree json data'
77 def jsonData = '{"branch": [{"name": "New"}]}'
78 objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData)
79 then: 'the persistence service method is invoked with correct parameters'
80 1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
81 { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
82 and: 'data updated event is sent to notification service'
83 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
86 def 'Saving list-node data fragment under existing node.'() {
87 given: 'schema set for given anchor and dataspace references test-tree model'
88 setupSchemaSetMocks('test-tree.yang')
89 when: 'save data method is invoked with list-node json data'
90 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
91 objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
92 then: 'the persistence service method is invoked with correct parameters'
93 1 * mockCpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, '/test-tree',
94 { dataNodeCollection ->
96 assert dataNodeCollection.size() == 2
97 assert dataNodeCollection.collect { it.getXpath() }
98 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
102 and: 'data updated event is sent to notification service'
103 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
106 def 'Saving empty list-node data fragment.'() {
107 given: 'schema set for given anchor and dataspace references test-tree model'
108 setupSchemaSetMocks('test-tree.yang')
109 when: 'save data method is invoked with empty list-node data fragment'
110 def jsonData = '{"branch": []}'
111 objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
112 then: 'invalid data exception is thrown'
113 thrown(DataValidationException)
116 def 'Get data node with option #fetchDescendantsOption.'() {
118 def dataNode = new DataNodeBuilder().withXpath(xpath).build()
119 given: 'persistence service returns data for get data request'
120 mockCpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) >> dataNode
121 expect: 'service returns same data if uses same parameters'
122 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) == dataNode
123 where: 'all fetch options are supported'
124 fetchDescendantsOption << FetchDescendantsOption.values()
127 def 'Update data node leaves: #scenario.'() {
128 given: 'schema set for given anchor and dataspace references test-tree model'
129 setupSchemaSetMocks('test-tree.yang')
130 when: 'update data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
131 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData)
132 then: 'the persistence service method is invoked with correct parameters'
133 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
134 and: 'data updated event is sent to notification service'
135 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
136 where: 'following parameters were used'
137 scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves
138 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap()
139 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']' | ['name': 'Name']
142 def 'Update list-element data node with : #scenario.'() {
143 given: 'schema set for given anchor and dataspace references bookstore model'
144 setupSchemaSetMocks('bookstore.yang')
145 when: 'update data method is invoked with json data #jsonData and parent node xpath'
146 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]', jsonData)
147 then: 'the persistence service method is invoked with correct parameters'
148 thrown(DataValidationException)
149 where: 'following parameters were used'
151 'multiple leaves' | '{"code": "01","name": "some-name"}'
152 'one leaf' | '{"name": "some-name"}'
155 def 'Replace data node: #scenario.'() {
156 given: 'schema set for given anchor and dataspace references test-tree model'
157 setupSchemaSetMocks('test-tree.yang')
158 when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
159 objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData)
160 then: 'the persistence service method is invoked with correct parameters'
161 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
162 { dataNode -> dataNode.xpath == expectedNodeXpath })
163 and: 'data updated event is sent to notification service'
164 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
165 where: 'following parameters were used'
166 scenario | parentNodeXpath | jsonData || expectedNodeXpath
167 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
168 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']'
171 def 'Replace list-node data fragment under existing node.'() {
172 given: 'schema set for given anchor and dataspace references test-tree model'
173 setupSchemaSetMocks('test-tree.yang')
174 when: 'replace list data method is invoked with list-node json data'
175 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
176 objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
177 then: 'the persistence service method is invoked with correct parameters'
178 1 * mockCpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, '/test-tree',
179 { dataNodeCollection ->
181 assert dataNodeCollection.size() == 2
182 assert dataNodeCollection.collect { it.getXpath() }
183 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
187 and: 'data updated event is sent to notification service'
188 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
191 def 'Replace with empty list-node data fragment.'() {
192 given: 'schema set for given anchor and dataspace references test-tree model'
193 setupSchemaSetMocks('test-tree.yang')
194 when: 'replace list data method is invoked with empty list-node data fragment'
195 def jsonData = '{"branch": []}'
196 objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
197 then: 'invalid data exception is thrown'
198 thrown(DataValidationException)
201 def 'Delete list-node data fragment under existing node.'() {
202 given: 'schema set for given anchor and dataspace references test-tree model'
203 setupSchemaSetMocks('test-tree.yang')
204 when: 'delete list data method is invoked with list-node json data'
205 objectUnderTest.deleteListNodeData(dataspaceName, anchorName, '/test-tree/branch')
206 then: 'the persistence service method is invoked with correct parameters'
207 1 * mockCpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, '/test-tree/branch')
208 and: 'data updated event is sent to notification service'
209 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
212 def setupSchemaSetMocks(String... yangResources) {
213 def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
214 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
215 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
216 mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
217 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
218 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
219 mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext