Merge "Delete list-node p1 service and persistence layers"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsDataServiceImplSpec.groovy
1 /*
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
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.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
38
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)
45
46     def objectUnderTest = new CpsDataServiceImpl()
47
48     def setup() {
49         objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
50         objectUnderTest.cpsAdminService = mockCpsAdminService
51         objectUnderTest.cpsModuleService = mockCpsModuleService
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
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)
71     }
72
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)
84     }
85
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 ->
95                         {
96                             assert dataNodeCollection.size() == 2
97                             assert dataNodeCollection.collect { it.getXpath() }
98                                     .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
99                         }
100                     }
101             )
102         and: 'data updated event is sent to notification service'
103             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
104     }
105
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)
114     }
115
116     def 'Get data node with option #fetchDescendantsOption.'() {
117         def xpath = '/xpath'
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()
125     }
126
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']
140     }
141
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'
150             scenario          | jsonData
151             'multiple leaves' | '{"code": "01","name": "some-name"}'
152             'one leaf'        | '{"name": "some-name"}'
153     }
154
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\']'
169     }
170
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 ->
180                         {
181                             assert dataNodeCollection.size() == 2
182                             assert dataNodeCollection.collect { it.getXpath() }
183                                     .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
184                         }
185                     }
186             )
187         and: 'data updated event is sent to notification service'
188             1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName)
189     }
190
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)
199     }
200
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)
210     }
211
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
220     }
221 }