6cd7f21df3f143442cc87986ca695d247ee8d77f
[cps.git] /
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.cps
22
23 import org.onap.cps.api.CpsDataspaceService
24 import org.onap.cps.integration.base.FunctionalSpecBase
25 import org.onap.cps.api.exceptions.AlreadyDefinedException
26 import org.onap.cps.api.exceptions.DataspaceInUseException
27 import org.onap.cps.api.exceptions.DataspaceNotFoundException
28
29 class DataspaceServiceIntegrationSpec extends FunctionalSpecBase {
30
31     CpsDataspaceService objectUnderTest
32
33     def setup() { objectUnderTest = cpsDataspaceService }
34
35     def cleanup() { cpsModuleService.deleteUnusedYangResourceModules() }
36
37     def 'Dataspace CRUD operations.'() {
38         when: 'a dataspace is created'
39             objectUnderTest.createDataspace('newDataspace')
40         then: 'the dataspace can be read'
41             assert objectUnderTest.getDataspace('newDataspace').name == 'newDataspace'
42         and: 'it can be deleted'
43             objectUnderTest.deleteDataspace('newDataspace')
44         then: 'the dataspace no longer exists i.e. an exception is thrown if an attempt is made to retrieve it'
45             def thrown = null
46             try {
47                 objectUnderTest.getDataspace('newDataspace')
48             } catch(Exception exception) {
49                 thrown = exception
50             }
51            assert thrown instanceof DataspaceNotFoundException
52     }
53
54     def 'Attempt to delete a non-existing dataspace'() {
55         when: 'attempt to delete a non-existing dataspace'
56             objectUnderTest.deleteDataspace('non-existing-name')
57         then: 'a not found exception is thrown with the relevant dataspace name'
58             def thrownException = thrown(DataspaceNotFoundException)
59             assert thrownException.details.contains('non-existing-name does not exist')
60     }
61
62     def 'Attempt Delete dataspace with a schema set and anchor'() {
63         setup: 'a dataspace with a schema set and anchor'
64             objectUnderTest.createDataspace('targetDataspace')
65             cpsModuleService.createSchemaSet('targetDataspace','someSchemaSet',[:])
66             cpsAnchorService.createAnchor('targetDataspace', 'someSchemaSet', 'some_anchor')
67         when: 'attempt to delete dataspace'
68             objectUnderTest.deleteDataspace('targetDataspace')
69         then: 'an in-use exception is thrown mentioning anchors'
70             def thrownException = thrown(DataspaceInUseException)
71             assert thrownException.details.contains('contains 1 anchor(s)')
72         cleanup:
73             cpsModuleService.deleteSchemaSetsWithCascade('targetDataspace',['someSchemaSet'])
74             objectUnderTest.deleteDataspace('targetDataspace')
75     }
76
77     def 'Attempt to delete dataspace with just a schema set'() {
78         setup: 'a dataspace with a schema set'
79             objectUnderTest.createDataspace('targetDataspace')
80             cpsModuleService.createSchemaSet('targetDataspace','someSchemaSet',[:])
81         when: 'attempt to delete dataspace'
82             objectUnderTest.deleteDataspace('targetDataspace')
83         then: 'an in-use exception is thrown mentioning schemasets'
84             def thrownException = thrown(DataspaceInUseException)
85             assert thrownException.details.contains('contains 1 schemaset(s)')
86         cleanup:
87             cpsModuleService.deleteSchemaSetsWithCascade('targetDataspace',['someSchemaSet'])
88             objectUnderTest.deleteDataspace('targetDataspace')
89     }
90
91     def 'Retrieve all dataspaces (depends on total test suite).'() {
92         given: 'two addtional dataspaces are created'
93             objectUnderTest.createDataspace('dataspace1')
94             objectUnderTest.createDataspace('dataspace2')
95         when: 'all datespaces are retreived'
96             def result = objectUnderTest.getAllDataspaces()
97         then: 'there are at least 3 dataspaces (2 new ones plus the general test dataspace)'
98             result.size() >= 3
99             assert result.name.containsAll([GENERAL_TEST_DATASPACE, 'dataspace1', 'dataspace2'])
100     }
101
102     def 'Duplicate dataspaces.'() {
103         when: 'attempting to create a dataspace with the same name as an existing one'
104             objectUnderTest.createDataspace(GENERAL_TEST_DATASPACE)
105         then: 'an exception is thrown indicating the dataspace already exists'
106             thrown(AlreadyDefinedException)
107     }
108
109 }