Merge "LcmEvent to have header now"
[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.exceptions.AlreadyDefinedException
26 import org.onap.cps.spi.exceptions.AnchorNotFoundException
27 import org.onap.cps.spi.exceptions.DataspaceInUseException
28 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
29
30 class CpsAdminServiceIntegrationSpec extends CpsIntegrationSpecBase {
31
32     CpsAdminService objectUnderTest
33
34     def setup() { objectUnderTest = cpsAdminService }
35
36     def 'Dataspace CRUD operations.'() {
37         when: 'a dataspace is created'
38             objectUnderTest.createDataspace('newDataspace')
39         then: 'the dataspace can be read'
40             assert objectUnderTest.getDataspace('newDataspace').name == 'newDataspace'
41         and: 'it can be deleted'
42             objectUnderTest.deleteDataspace('newDataspace')
43         then: 'the dataspace no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
44             def thrown = null
45             try {
46                 objectUnderTest.getDataspace('newDataspace')
47             } catch(Exception e) {
48                 thrown = e
49             }
50            assert thrown instanceof DataspaceNotFoundException
51     }
52
53     def 'Delete dataspace with error; #scenario.'() {
54         setup: 'add some anchors if needed'
55             numberOfAnchors.times {
56                 objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor' + it)
57             }
58         when: 'attempt to delete dataspace'
59             objectUnderTest.deleteDataspace(dataspaceName)
60         then: 'the correct exception is thrown with the relevant details'
61             def thrownException = thrown(expectedException)
62             thrownException.details.contains(expectedMessageDetails)
63         cleanup:
64             numberOfAnchors.times {
65                 objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'anchor' + it)
66             }
67         where: 'the following data is used'
68             scenario                        | dataspaceName          | numberOfAnchors || expectedException          | expectedMessageDetails
69             'dataspace name does not exist' | 'unknown'              | 0               || DataspaceNotFoundException | 'unknown does not exist'
70             'dataspace contains schemasets' | GENERAL_TEST_DATASPACE | 0               || DataspaceInUseException    | 'contains 1 schemaset(s)'
71             'dataspace contains anchors'    | GENERAL_TEST_DATASPACE | 2               || DataspaceInUseException    | 'contains 2 anchor(s)'
72     }
73
74     def 'Retrieve all dataspaces (depends on total test suite).'() {
75         given: 'two addtional dataspaces are created'
76             objectUnderTest.createDataspace('dataspace1')
77             objectUnderTest.createDataspace('dataspace2')
78         when: 'all datespaces are retreived'
79             def result = objectUnderTest.getAllDataspaces()
80         then: 'there are at least 3 dataspaces (2 new ones plus the general test dataspace)'
81             result.size() >= 3
82             assert result.name.containsAll([GENERAL_TEST_DATASPACE, 'dataspace1', 'dataspace2'])
83     }
84
85     def 'Duplicate dataspaces.'() {
86         when: 'attempting to create a dataspace with the same name as an existing one'
87             objectUnderTest.createDataspace(GENERAL_TEST_DATASPACE)
88         then: 'an exception is thrown indicating the dataspace already exists'
89             thrown(AlreadyDefinedException)
90     }
91
92     def 'Anchor CRUD operations.'() {
93         when: 'an anchor is created'
94             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
95         then: 'the anchor be read'
96             assert objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor').name == 'newAnchor'
97         and: 'it can be deleted'
98             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE,'newAnchor')
99         then: 'the anchor no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
100             def thrown = null
101             try {
102                 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
103             } catch(Exception e) {
104                 thrown = e
105             }
106             assert thrown instanceof AnchorNotFoundException
107     }
108
109     def 'Filtering multiple anchors.'() {
110         when: '2 anchors with bookstore schema set are created'
111             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor1')
112             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor2')
113         and: '1 anchor with "other" schema set is created'
114             def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
115             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'otherSchemaSet', [someFileName: bookstoreModelFileContent])
116             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'otherSchemaSet', 'anchor3')
117         then: 'there are 3 anchors in the general test database'
118             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE).size() == 3
119         and: 'there are 2 anchors associated with bookstore schema set'
120             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET).size() == 2
121         and: 'there is 1 anchor associated with other schema set'
122             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, 'otherSchemaSet').size() == 1
123     }
124
125     def 'Querying anchor(name)s (depends on previous test!).'() {
126         expect: 'there are now 3 anchors using the "stores" module (both schema sets use the same modules) '
127             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores']).size() == 3
128         and: 'there are no anchors using both "stores" and a "unused-model"'
129             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'unused-model']).size() == 0
130     }
131
132     def 'Duplicate anchors.'() {
133         given: 'an anchor is created'
134             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
135         when: 'attempt to create another anchor with the same name'
136             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
137         then: 'an exception is thrown that the anchor already is defined'
138             thrown(AlreadyDefinedException)
139         cleanup:
140             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
141     }
142
143     def 'Query anchors without any known modules and #scenario'() {
144         when: 'querying for anchors with #scenario'
145             def result = objectUnderTest.queryAnchorNames(dataspaceName, ['unknownModule'])
146         then: 'an empty result is returned (no error)'
147             assert result == []
148         where:
149            scenario                 | dataspaceName
150            'non existing database'  | 'nonExistingDataspace'
151            'just unknown module(s)' | GENERAL_TEST_DATASPACE
152     }
153
154 }