CPS-314: Delete Dataspace
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsAdminPersistenceServiceSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.impl
23
24 import org.onap.cps.spi.CpsAdminPersistenceService
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 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
30 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException
31 import org.onap.cps.spi.model.Anchor
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.test.context.jdbc.Sql
34
35 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
36
37     @Autowired
38     CpsAdminPersistenceService objectUnderTest
39
40     static final String SET_DATA = '/data/anchor.sql'
41     static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql'
42     static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002'
43     static final Integer DELETED_ANCHOR_ID = 3001
44     static final Long DELETED_FRAGMENT_ID = 4001
45
46     @Sql(CLEAR_DATA)
47     def 'Create and retrieve a new dataspace.'() {
48         when: 'a new dataspace is created'
49             def dataspaceName = 'some new dataspace'
50             objectUnderTest.createDataspace(dataspaceName)
51         then: 'that dataspace can be retrieved from the dataspace repository'
52             def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
53             dataspaceEntity.id != null
54             dataspaceEntity.name == dataspaceName
55     }
56
57     @Sql([CLEAR_DATA, SET_DATA])
58     def 'Attempt to create a duplicate dataspace.'() {
59         when: 'an attempt is made to create an already existing dataspace'
60             objectUnderTest.createDataspace(DATASPACE_NAME)
61         then: 'an exception that is is already defined is thrown with the correct details'
62             def thrown = thrown(AlreadyDefinedException)
63             thrown.details.contains(DATASPACE_NAME)
64     }
65
66     @Sql([CLEAR_DATA, SET_DATA])
67     def 'Create and retrieve a new anchor.'() {
68         when: 'a new anchor is created'
69             def newAnchorName = 'my new anchor'
70             objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
71         then: 'that anchor can be retrieved'
72             def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
73             anchor.name == newAnchorName
74             anchor.dataspaceName == DATASPACE_NAME
75             anchor.schemaSetName == SCHEMA_SET_NAME1
76     }
77
78     @Sql([CLEAR_DATA, SET_DATA])
79     def 'Create anchor error scenario: #scenario.'() {
80         when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
81             objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
82         then: 'an #expectedException is thrown'
83             thrown(expectedException)
84         where: 'the following data is used'
85             scenario                    | dataspaceName  | schemaSetName    | anchorName     || expectedException
86             'dataspace does not exist'  | 'unknown'      | 'not-relevant'   | 'not-relevant' || DataspaceNotFoundException
87             'schema set does not exist' | DATASPACE_NAME | 'unknown'        | 'not-relevant' || SchemaSetNotFoundException
88             'anchor already exists'     | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1   || AlreadyDefinedException
89     }
90
91     @Sql([CLEAR_DATA, SET_DATA])
92     def 'Get anchor error scenario: #scenario.'() {
93         when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
94             objectUnderTest.getAnchor(dataspaceName, anchorName)
95         then: 'an #expectedException is thrown'
96             thrown(expectedException)
97         where: 'the following data is used'
98             scenario                   | dataspaceName  | anchorName     || expectedException
99             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
100             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
101     }
102
103     @Sql([CLEAR_DATA, SET_DATA])
104     def 'Get all anchors in dataspace #dataspaceName.'() {
105         when: 'all anchors are retrieved from #DATASPACE_NAME'
106             def result = objectUnderTest.getAnchors(dataspaceName)
107         then: 'the expected collection of anchors is returned'
108             result.size() == expectedAnchors.size()
109             result.containsAll(expectedAnchors)
110         where: 'the following data is used'
111             dataspaceName        || expectedAnchors
112             DATASPACE_NAME       || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
113                                      Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
114             DATASPACE_WITH_NO_DATA || []
115     }
116
117     @Sql(CLEAR_DATA)
118     def 'Get all anchors in unknown dataspace.'() {
119         when: 'attempt to get all anchors in an unknown dataspace'
120             objectUnderTest.getAnchors('unknown dataspace')
121         then: 'an DataspaceNotFoundException is thrown'
122             thrown(DataspaceNotFoundException)
123     }
124
125     @Sql([CLEAR_DATA, SET_DATA])
126     def 'Delete anchor'() {
127         when: 'delete anchor action is invoked'
128             objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1)
129         then: 'anchor and associated data fragment are deleted'
130             assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
131             assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty()
132     }
133
134     @Sql([CLEAR_DATA, SET_DATA])
135     def 'delete anchor error scenario: #scenario'(){
136         when: 'delete anchor attempt is performed'
137             objectUnderTest.deleteAnchor(dataspaceName, anchorName)
138         then: 'an #expectedException is thrown'
139             thrown(expectedException)
140         where: 'the following data is used'
141             scenario                   | dataspaceName  | anchorName     || expectedException
142             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
143             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
144     }
145
146     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
147     def 'Query anchors that have #scenario.'() {
148         when: 'all anchor are retrieved for the given dataspace name and module names'
149             def anchors = objectUnderTest.queryAnchors('dataspace-1', inputModuleNames)
150         then: 'the expected anchors are returned'
151             anchors.size() == expectedAnchors.size()
152             anchors.containsAll(expectedAnchors)
153         where: 'the following data is used'
154             scenario                           | inputModuleNames                                    || expectedAnchors
155             'one module'                       | ['module-name-1']                                   || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
156             'two modules'                      | ['module-name-1', 'module-name-2']                  || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
157             'no anchors for all three modules' | ['module-name-1', 'module-name-2', 'module-name-3'] || []
158     }
159
160     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
161     def 'Query all anchors for an #scenario.'() {
162         when: 'attempt to query anchors'
163             objectUnderTest.queryAnchors(dataspaceName, moduleNames)
164         then: 'the correct exception is thrown with the relevant details'
165             def thrownException = thrown(expectedException)
166             thrownException.details.contains(expectedMessageDetails)
167         where: 'the following data is used'
168             scenario                          | dataspaceName       | moduleNames                                || expectedException            | expectedMessageDetails  | messageDoesNotContain
169             'unknown dataspace'               | 'db-does-not-exist' | ['does-not-matter']                        || DataspaceNotFoundException   | 'db-does-not-exist'     | 'does-not-matter'
170             'unknown module and known module' | 'dataspace-1'       | ['module-name-1', 'module-does-not-exist'] || ModuleNamesNotFoundException | 'module-does-not-exist' | 'module-name-1'
171     }
172
173     def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) {
174         return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build()
175     }
176
177     @Sql([CLEAR_DATA, SET_DATA])
178     def 'Delete dataspace.'() {
179         when: 'delete dataspace action is invoked'
180             objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA)
181         then: 'dataspace is deleted'
182             assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty();
183     }
184
185     @Sql([CLEAR_DATA, SET_DATA])
186     def 'Delete dataspace when #scenario.'() {
187         when: 'delete dataspace action is invoked'
188             objectUnderTest.deleteDataspace(dataspaceName)
189         then: 'the correct exception is thrown with the relevant details'
190             def thrownException = thrown(expectedException)
191             thrownException.details.contains(expectedMessageDetails)
192         where: 'the following data is used'
193             scenario                          | dataspaceName       || expectedException            | expectedMessageDetails
194             'dataspace name does not exist'   | 'unknown'           || DataspaceNotFoundException   | 'unknown does not exist'
195             'dataspace contains an anchor'    | 'DATASPACE-001'     || DataspaceInUseException      | 'contains 2 anchor(s)'
196             'dataspace contains schemasets'   | 'DATASPACE-003'     || DataspaceInUseException      | 'contains 1 schemaset(s)'
197     }
198
199 }