Use exact type for objectUnderTest in groovy tests
[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.DataspaceNotFoundException
28
29 class CpsAdminServiceIntegrationSpec extends CpsIntegrationSpecBase {
30
31     CpsAdminService objectUnderTest
32
33     def setup() { objectUnderTest = cpsAdminService }
34
35     def 'Dataspace CRUD operations.'() {
36         when: 'a dataspace is created'
37             objectUnderTest.createDataspace('newDataspace')
38         then: 'the dataspace can be read'
39             assert objectUnderTest.getDataspace('newDataspace').name == 'newDataspace'
40         and: 'it can be deleted'
41             objectUnderTest.deleteDataspace('newDataspace')
42         then: 'the dataspace no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
43             def thrown = null
44             try {
45                 objectUnderTest.getDataspace('newDataspace')
46             } catch(Exception e) {
47                 thrown = e
48             }
49            assert thrown instanceof DataspaceNotFoundException
50     }
51
52     def 'Retrieve all dataspaces (depends on total test suite).'() {
53         given: 'two addtional dataspaces are created'
54             objectUnderTest.createDataspace('dataspace1')
55             objectUnderTest.createDataspace('dataspace2')
56         when: 'all datespaces are retreived'
57             def result = objectUnderTest.getAllDataspaces()
58         then: 'there are at least 3 dataspaces (2 new ones plus the general test dataspace)'
59             result.size() >= 3
60             assert result.name.containsAll([GENERAL_TEST_DATASPACE, 'dataspace1', 'dataspace2'])
61     }
62
63     def 'Duplicate dataspaces.'() {
64         when: 'attempting to create a dataspace with the same name as an existing one'
65             objectUnderTest.createDataspace(GENERAL_TEST_DATASPACE)
66         then: 'an exception is thrown indicating the dataspace already exists'
67             thrown(AlreadyDefinedException)
68     }
69
70     def 'Anchor CRUD operations.'() {
71         when: 'a anchor is created'
72             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'newAnchor')
73         then: 'the anchor be read'
74             assert objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor').name == 'newAnchor'
75         and: 'it can be deleted'
76             objectUnderTest.deleteAnchor(GENERAL_TEST_DATASPACE,'newAnchor')
77         then: 'the anchor no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
78             def thrown = null
79             try {
80                 objectUnderTest.getAnchor(GENERAL_TEST_DATASPACE, 'newAnchor')
81             } catch(Exception e) {
82                 thrown = e
83             }
84             assert thrown instanceof AnchorNotFoundException
85     }
86
87     def 'Filtering multiple anchors.'() {
88         when: '2 anchors with bookstore schema set are created'
89             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor1')
90             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'anchor2')
91         and: '1 anchor with "other" schema set is created'
92             def bookstoreModelFileContent = readResourceDataFile('bookstore/bookstore.yang')
93             cpsModuleService.createSchemaSet(GENERAL_TEST_DATASPACE, 'otherSchemaSet', [someFileName: bookstoreModelFileContent])
94             objectUnderTest.createAnchor(GENERAL_TEST_DATASPACE, 'otherSchemaSet', 'anchor3')
95         then: 'there are 3 anchors in the general test database'
96             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE).size() == 3
97         and: 'there are 2 anchors associated with bookstore schema set'
98             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET).size() == 2
99         and: 'there is 1 anchor associated with other schema set'
100             assert objectUnderTest.getAnchors(GENERAL_TEST_DATASPACE, 'otherSchemaSet').size() == 1
101     }
102
103     def 'Querying anchor(name)s (depends on previous test!).'() {
104         expect: 'there are now 3 anchors using the "stores" module (both schema sets use the same modules) '
105             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores']).size() == 3
106         and: 'there are no anchors using both "stores" and a "unused-model"'
107             assert objectUnderTest.queryAnchorNames(GENERAL_TEST_DATASPACE, ['stores', 'unused-model']).size() == 0
108     }
109
110 }