2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.api.impl
23 import org.onap.cps.TestUtils
24 import org.onap.cps.api.CpsAdminService
25 import org.onap.cps.api.CpsModuleService
26 import org.onap.cps.spi.CpsDataPersistenceService
27 import org.onap.cps.spi.FetchDescendantsOption
28 import org.onap.cps.spi.exceptions.DataValidationException
29 import org.onap.cps.spi.model.Anchor
30 import org.onap.cps.spi.model.DataNodeBuilder
31 import org.onap.cps.yang.YangTextSchemaSourceSet
32 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
33 import spock.lang.Specification
35 class CpsDataServiceImplSpec extends Specification {
36 def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
37 def mockCpsAdminService = Mock(CpsAdminService)
38 def mockCpsModuleService = Mock(CpsModuleService)
39 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
41 def objectUnderTest = new CpsDataServiceImpl()
44 objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
45 objectUnderTest.cpsAdminService = mockCpsAdminService
46 objectUnderTest.cpsModuleService = mockCpsModuleService
47 objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
50 def dataspaceName = 'some dataspace'
51 def anchorName = 'some anchor'
52 def schemaSetName = 'some schema set'
54 def 'Saving json data.'() {
55 given: 'schema set for given anchor and dataspace references test-tree model'
56 setupSchemaSetMocks('test-tree.yang')
57 when: 'save data method is invoked with test-tree json data'
58 def jsonData = TestUtils.getResourceFileContent('test-tree.json')
59 objectUnderTest.saveData(dataspaceName, anchorName, jsonData)
60 then: 'the persistence service method is invoked with correct parameters'
61 1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
62 { dataNode -> dataNode.xpath == '/test-tree' })
65 def 'Saving child data fragment under existing node.'() {
66 given: 'schema set for given anchor and dataspace references test-tree model'
67 setupSchemaSetMocks('test-tree.yang')
68 when: 'save data method is invoked with test-tree json data'
69 def jsonData = '{"branch": [{"name": "New"}]}'
70 objectUnderTest.saveData(dataspaceName, anchorName, '/test-tree', jsonData)
71 then: 'the persistence service method is invoked with correct parameters'
72 1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
73 { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
76 def 'Saving list-node data fragment under existing node.'() {
77 given: 'schema set for given anchor and dataspace references test-tree model'
78 setupSchemaSetMocks('test-tree.yang')
79 when: 'save data method is invoked with list-node json data'
80 def jsonData = '{"branch": [{"name": "A"}, {"name": "B"}]}'
81 objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
82 then: 'the persistence service method is invoked with correct parameters'
83 1 * mockCpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, '/test-tree',
84 { dataNodeCollection ->
86 assert dataNodeCollection.size() == 2
87 assert dataNodeCollection.collect { it.getXpath() }
88 .containsAll(['/test-tree/branch[@name=\'A\']', '/test-tree/branch[@name=\'B\']'])
94 def 'Saving empty list-node data fragment.'() {
95 given: 'schema set for given anchor and dataspace references test-tree model'
96 setupSchemaSetMocks('test-tree.yang')
97 when: 'save data method is invoked with empty list-node data fragment'
98 def jsonData = '{"branch": []}'
99 objectUnderTest.saveListNodeData(dataspaceName, anchorName, '/test-tree', jsonData)
100 then: 'invalid data exception is thrown'
101 thrown(DataValidationException)
104 def 'Get data node with option #fetchDescendantsOption.'() {
106 def dataNode = new DataNodeBuilder().withXpath(xpath).build()
107 given: 'persistence service returns data for get data request'
108 mockCpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) >> dataNode
109 expect: 'service returns same data if uses same parameters'
110 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) == dataNode
111 where: 'all fetch options are supported'
112 fetchDescendantsOption << FetchDescendantsOption.values()
115 def 'Update data node leaves: #scenario.'() {
116 given: 'schema set for given anchor and dataspace references test-tree model'
117 setupSchemaSetMocks('test-tree.yang')
118 when: 'update data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
119 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData)
120 then: 'the persistence service method is invoked with correct parameters'
121 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
122 where: 'following parameters were used'
123 scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves
124 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap()
125 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']' | ['name': 'Name']
128 def 'Replace data node: #scenario.'() {
129 given: 'schema set for given anchor and dataspace references test-tree model'
130 setupSchemaSetMocks('test-tree.yang')
131 when: 'replace data method is invoked with json data #jsonData and parent node xpath #parentNodeXpath'
132 objectUnderTest.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData)
133 then: 'the persistence service method is invoked with correct parameters'
134 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
135 { dataNode -> dataNode.xpath == expectedNodeXpath })
136 where: 'following parameters were used'
137 scenario | parentNodeXpath | jsonData || expectedNodeXpath
138 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
139 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']'
142 def setupSchemaSetMocks(String... yangResources) {
143 def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
144 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
145 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
146 mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
147 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
148 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
149 mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext