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.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
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)
45 def objectUnderTest = new CpsDataServiceImpl()
48 objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
49 objectUnderTest.cpsAdminService = mockCpsAdminService
50 objectUnderTest.cpsModuleService = mockCpsModuleService
51 objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
52 objectUnderTest.notificationService = mockNotificationService
55 def dataspaceName = 'some dataspace'
56 def anchorName = 'some anchor'
57 def schemaSetName = 'some schema set'
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)
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)
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 ->
95 assert dataNodeCollection.size() == 2
96 assert dataNodeCollection.collect { it.getXpath() }
97 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
101 and: 'data updated event is sent to notification service'
102 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
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)
115 def 'Get data node with option #fetchDescendantsOption.'() {
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()
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']
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'
150 'multiple leaves' | '{"code": "01","name": "some-name"}'
151 'one leaf' | '{"name": "some-name"}'
154 def 'Replace data node: #scenario.'() {
155 given: 'schema set for given anchor and dataspace references test-tree model'
156 setupSchemaSetMocks('test-tree.yang')
157 when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
158 objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData)
159 then: 'the persistence service method is invoked with correct parameters'
160 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
161 { dataNode -> dataNode.xpath == expectedNodeXpath })
162 and: 'data updated event is sent to notification service'
163 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
164 where: 'following parameters were used'
165 scenario | parentNodeXpath | jsonData || expectedNodeXpath
166 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
167 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']'
170 def 'Replace list-node data fragment under existing node.'() {
171 given: 'schema set for given anchor and dataspace references test-tree model'
172 setupSchemaSetMocks('test-tree.yang')
173 when: 'replace list data method is invoked with list-node json data'
174 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
175 objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
176 then: 'the persistence service method is invoked with correct parameters'
177 1 * mockCpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, '/test-tree',
178 { dataNodeCollection ->
180 assert dataNodeCollection.size() == 2
181 assert dataNodeCollection.collect { it.getXpath() }
182 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
186 and: 'data updated event is sent to notification service'
187 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
190 def 'Replace with empty list-node data fragment.'() {
191 given: 'schema set for given anchor and dataspace references test-tree model'
192 setupSchemaSetMocks('test-tree.yang')
193 when: 'replace list data method is invoked with empty list-node data fragment'
194 def jsonData = '{"branch": []}'
195 objectUnderTest.replaceListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
196 then: 'invalid data exception is thrown'
197 thrown(DataValidationException)
200 def setupSchemaSetMocks(String... yangResources) {
201 def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
202 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
203 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
204 mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
205 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
206 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
207 mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext