f8eca6199086bf514fa9f5e6bbfec46c5dcca02d
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / CpsAdminServiceIntegrationSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.functional
22
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
31
32 class CpsAdminServiceIntegrationSpec extends CpsIntegrationSpecBase {
33
34     CpsAdminService objectUnderTest
35
36     def setup() { objectUnderTest = cpsAdminService }
37
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'
46             def thrown = null
47             try {
48                 objectUnderTest.getDataspace('newDataspace')
49             } catch(Exception exception) {
50                 thrown = exception
51             }
52            assert thrown instanceof DataspaceNotFoundException
53     }
54
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)
59             }
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)
65         cleanup:
66             numberOfAnchors.times {
67                 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'anchor' + it)
68             }
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)'
74     }
75
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)'
83             result.size() >= 3
84             assert result.name.containsAll([GENERAL_TEST_DATASPACE, 'dataspace1', 'dataspace2'])
85     }
86
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)
92     }
93
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'
102             def thrown = null
103             try {
104                 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
105             } catch(Exception exception) {
106                 thrown = exception
107             }
108             assert thrown instanceof AnchorNotFoundException
109     }
110
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
125     }
126
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
132     }
133
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)
141         cleanup:
142             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
143     }
144
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)'
149             assert result == []
150         where:
151            scenario                 | dataspaceName
152            'non existing database'  | 'nonExistingDataspace'
153            'just unknown module(s)' | GENERAL_TEST_DATASPACE
154     }
155
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'] as List
178             assert birdsName.size() == 3
179             assert birdsName.containsAll('Night Owl', 'Raven', 'Crow')
180     }
181 }