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 java.time.OffsetDateTime
26 import org.onap.cps.TestUtils
27 import org.onap.cps.api.CpsAdminService
28 import org.onap.cps.api.CpsModuleService
29 import org.onap.cps.notification.NotificationService
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 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'
59 def observedTimestamp = OffsetDateTime.now()
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)
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)
87 def 'Saving list-node 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-node json data'
91 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
92 objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
93 then: 'the persistence service method is invoked with correct parameters'
94 1 * mockCpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, '/test-tree',
95 { dataNodeCollection ->
97 assert dataNodeCollection.size() == 2
98 assert dataNodeCollection.collect { it.getXpath() }
99 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
103 and: 'data updated event is sent to notification service'
104 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
107 def 'Saving empty list-node 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 empty list-node data fragment'
111 def jsonData = '{"branch": []}'
112 objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
113 then: 'invalid data exception is thrown'
114 thrown(DataValidationException)
117 def 'Get data node with option #fetchDescendantsOption.'() {
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()
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)
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']
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'
153 'multiple expectedLeaves' | '{"code": "01","name": "some-name"}'
154 'one leaf' | '{"name": "some-name"}'
157 def 'Update cm-handle properties' () {
158 given: 'a dmi registry model'
159 setupSchemaSetMocks('dmi-registry.yang')
160 and: 'the expected json string'
161 def jsonData = '{"cm-handles":[{"id":"cmHandle001", "additional-properties":[{"name":"P1"}]}]}'
162 when: 'update data method is invoked with json data and parent node xpath'
163 objectUnderTest.updateNodeLeavesAndExistingDescendantLeaves(dataspaceName, anchorName,
164 '/dmi-registry', jsonData, observedTimestamp)
165 then: 'the persistence service method is invoked with correct parameters'
166 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName,
167 "/dmi-registry/cm-handles[@id='cmHandle001']", ['id': 'cmHandle001'])
168 and: 'the data updated event is sent to the notification service'
169 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
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)
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\']'
188 def 'Replace list-node data fragment under existing 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-node json data'
192 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
193 objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
194 then: 'the persistence service method is invoked with correct parameters'
195 1 * mockCpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, '/test-tree',
196 { dataNodeCollection ->
198 assert dataNodeCollection.size() == 2
199 assert dataNodeCollection.collect { it.getXpath() }
200 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
204 and: 'data updated event is sent to notification service'
205 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
208 def 'Replace with empty list-node data fragment.'() {
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-node data fragment'
212 def jsonData = '{"branch": []}'
213 objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData, observedTimestamp)
214 then: 'invalid data exception is thrown'
215 thrown(DataValidationException)
218 def 'Delete list-node data fragment 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-node json data'
222 objectUnderTest.deleteListNodeData(dataspaceName, anchorName, '/test-tree/branch', observedTimestamp)
223 then: 'the persistence service method is invoked with correct parameters'
224 1 * mockCpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, '/test-tree/branch')
225 and: 'data updated event is sent to notification service'
226 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp)
229 def setupSchemaSetMocks(String... yangResources) {
230 def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
231 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
232 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
233 mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
234 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
235 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
236 mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext