2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the 'License');
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * 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.integration.functional
23 import org.onap.cps.api.CpsAdminService
24 import org.onap.cps.integration.base.CpsIntegrationSpecBase
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.exceptions.AlreadyDefinedException
27 import org.onap.cps.spi.exceptions.AnchorNotFoundException
28 import org.onap.cps.spi.exceptions.DataspaceInUseException
29 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
30 import java.time.OffsetDateTime
32 class CpsAdminServiceIntegrationSpec extends CpsIntegrationSpecBase {
34 CpsAdminService objectUnderTest
36 def setup() { objectUnderTest = cpsAdminService }
38 def 'Dataspace CRUD operations.'() {
39 when: 'a dataspace is created'
40 objectUnderTest.createDataspace('newDataspace')
41 then: 'the dataspace can be read'
42 assert objectUnderTest.getDataspace('newDataspace').name == 'newDataspace'
43 and: 'it can be deleted'
44 objectUnderTest.deleteDataspace('newDataspace')
45 then: 'the dataspace no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
48 objectUnderTest.getDataspace('newDataspace')
49 } catch(Exception exception) {
52 assert thrown instanceof DataspaceNotFoundException
55 def 'Delete dataspace with error; #scenario.'() {
56 setup: 'add some anchors if needed'
57 numberOfAnchors.times {
58 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor' + it)
60 when: 'attempt to delete dataspace'
61 objectUnderTest.deleteDataspace(dataspaceName)
62 then: 'the correct exception is thrown with the relevant details'
63 def thrownException = thrown(expectedException)
64 thrownException.details.contains(expectedMessageDetails)
66 numberOfAnchors.times {
67 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'anchor' + it)
69 where: 'the following data is used'
70 scenario | dataspaceName | numberOfAnchors || expectedException | expectedMessageDetails
71 'dataspace name does not exist' | 'unknown' | 0 || DataspaceNotFoundException | 'unknown does not exist'
72 'dataspace contains schemasets' | GENERAL_TEST_DATASPACE | 0 || DataspaceInUseException | 'contains 1 schemaset(s)'
73 'dataspace contains anchors' | GENERAL_TEST_DATASPACE | 2 || DataspaceInUseException | 'contains 2 anchor(s)'
76 def 'Retrieve all dataspaces (depends on total test suite).'() {
77 given: 'two addtional dataspaces are created'
78 objectUnderTest.createDataspace('dataspace1')
79 objectUnderTest.createDataspace('dataspace2')
80 when: 'all datespaces are retreived'
81 def result = objectUnderTest.getAllDataspaces()
82 then: 'there are at least 3 dataspaces (2 new ones plus the general test dataspace)'
84 assert result.name.containsAll([GENERAL_TEST_DATASPACE, 'dataspace1', 'dataspace2'])
87 def 'Duplicate dataspaces.'() {
88 when: 'attempting to create a dataspace with the same name as an existing one'
89 objectUnderTest.createDataspace(GENERAL_TEST_DATASPACE)
90 then: 'an exception is thrown indicating the dataspace already exists'
91 thrown(AlreadyDefinedException)
94 def 'Anchor CRUD operations.'() {
95 when: 'an anchor is created'
96 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
97 then: 'the anchor be read'
98 assert objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor').name == 'newAnchor'
99 and: 'it can be deleted'
100 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE,'newAnchor')
101 then: 'the anchor no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
104 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
105 } catch(Exception exception) {
108 assert thrown instanceof AnchorNotFoundException
111 def 'Filtering multiple anchors.'() {
112 when: '2 anchors with bookstore schema set are created'
113 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor1')
114 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor2')
115 and: '1 anchor with "other" schema set is created'
116 def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
117 cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'otherSchemaSet', [someFileName: bookstoreModelFileContent])
118 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'otherSchemaSet', 'anchor3')
119 then: 'there are 3 anchors in the general test database'
120 assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE).size() == 3
121 and: 'there are 2 anchors associated with bookstore schema set'
122 assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET).size() == 2
123 and: 'there is 1 anchor associated with other schema set'
124 assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, 'otherSchemaSet').size() == 1
127 def 'Querying anchor(name)s (depends on previous test!).'() {
128 expect: 'there are now 3 anchors using the "stores" module (both schema sets use the same modules) '
129 assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores']).size() == 3
130 and: 'there are no anchors using both "stores" and a "unused-model"'
131 assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'unused-model']).size() == 0
134 def 'Duplicate anchors.'() {
135 given: 'an anchor is created'
136 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
137 when: 'attempt to create another anchor with the same name'
138 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
139 then: 'an exception is thrown that the anchor already is defined'
140 thrown(AlreadyDefinedException)
142 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
145 def 'Query anchors without any known modules and #scenario'() {
146 when: 'querying for anchors with #scenario'
147 def result = objectUnderTest.queryAnchorNames(dataspaceName, ['unknownModule'])
148 then: 'an empty result is returned (no error)'
151 scenario | dataspaceName
152 'non existing database' | 'nonExistingDataspace'
153 'just unknown module(s)' | GENERAL_TEST_DATASPACE
156 def 'Update anchor schema set.'() {
157 when: 'a new schema set with tree yang model is created'
158 def newTreeYangModelAsString = readResourceDataFile('tree/new-test-tree.yang')
159 cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', [tree: newTreeYangModelAsString])
160 then: 'an anchor with new schema set is created'
161 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'newTreeSchemaSet', 'anchor4')
162 and: 'the new tree datanode is saved'
163 def treeJsonData = readResourceDataFile('tree/new-test-tree.json')
164 cpsDataService.saveData(GENERAL_TEST_DATASPACE, 'anchor4', treeJsonData, OffsetDateTime.now())
165 and: 'saved tree data node can be retrieved by its normalized xpath'
166 def branchName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4', "/test-tree/branch", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['name']
167 assert branchName == 'left'
168 and: 'a another schema set with updated tree yang model is created'
169 def updatedTreeYangModelAsString = readResourceDataFile('tree/updated-test-tree.yang')
170 cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'anotherTreeSchemaSet', [tree: updatedTreeYangModelAsString])
171 and: 'anchor4 schema set is updated with another schema set successfully'
172 objectUnderTest.updateAnchorSchemaSet(GENERAL_TEST_DATASPACE, 'anchor4', 'anotherTreeSchemaSet')
173 when: 'updated tree data node with new leaves'
174 def updatedTreeJsonData = readResourceDataFile('tree/updated-test-tree.json')
175 cpsDataService.updateNodeLeaves(GENERAL_TEST_DATASPACE, "anchor4", "/test-tree/branch[@name='left']", updatedTreeJsonData, OffsetDateTime.now())
176 then: 'updated tree data node can be retrieved by its normalized xpath'
177 def birdsName = cpsDataService.getDataNodes(GENERAL_TEST_DATASPACE, 'anchor4',"/test-tree/branch[@name='left']/nest", FetchDescendantsOption.DIRECT_CHILDREN_ONLY)[0].leaves['birds']
178 assert birdsName as String == '[Raven, Night Owl, Crow]'