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