063bd5b5ae0865171d8f80a2db63e026ab2edef1
[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 = 3002
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, SET_DATA])
118     def 'Get all anchors associated with schemaset in a dataspace.'() {
119         when: 'anchors are retrieved by dataspace and schema-set'
120             def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
121         then: ' the response contains expected anchors'
122             anchors == expectedAnchors
123         where:
124             scenario     | dataspace       | schemasetName               || expectedAnchors
125             'no-anchors' | 'DATASPACE-003' | 'SCHEMA-SET-002-NO-ANCHORS' || Collections.emptySet()
126             'one-anchor' | 'DATASPACE-001' | 'SCHEMA-SET-001'            || Set.of(new Anchor('ANCHOR-001', 'DATASPACE-001', 'SCHEMA-SET-001'))
127     }
128
129     @Sql([CLEAR_DATA, SET_DATA])
130     def 'Error Handling: Get all anchors associated with schemaset in a dataspace.'() {
131         when: 'anchors are retrieved by dataspace and schema-set'
132             def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
133         then: ' an expected expception is thrown'
134             thrown(expectedException)
135         where:
136             scenario            | dataspace       | schemasetName               || expectedException
137             'unknown-dataspace' | 'unknown'       | 'SCHEMA-SET-002-NO-ANCHORS' || DataspaceNotFoundException
138             'unknown-schemaset' | 'DATASPACE-001' | 'unknown-schema-set'        || SchemaSetNotFoundException
139     }
140
141     @Sql(CLEAR_DATA)
142     def 'Get all anchors in unknown dataspace.'() {
143         when: 'attempt to get all anchors in an unknown dataspace'
144             objectUnderTest.getAnchors('unknown dataspace')
145         then: 'an DataspaceNotFoundException is thrown'
146             thrown(DataspaceNotFoundException)
147     }
148
149     @Sql([CLEAR_DATA, SET_DATA])
150     def 'Delete anchor'() {
151         when: 'delete anchor action is invoked'
152             objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2)
153         then: 'anchor is deleted'
154             assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
155     }
156
157     @Sql([CLEAR_DATA, SET_DATA])
158     def 'delete anchor error scenario: #scenario'() {
159         when: 'delete anchor attempt is performed'
160             objectUnderTest.deleteAnchor(dataspaceName, anchorName)
161         then: 'an #expectedException is thrown'
162             thrown(expectedException)
163         where: 'the following data is used'
164             scenario                   | dataspaceName  | anchorName     || expectedException
165             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
166             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
167     }
168
169     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
170     def 'Query anchors that have #scenario.'() {
171         when: 'all anchor are retrieved for the given dataspace name and module names'
172             def anchors = objectUnderTest.queryAnchors('dataspace-1', inputModuleNames)
173         then: 'the expected anchors are returned'
174             anchors.size() == expectedAnchors.size()
175             anchors.containsAll(expectedAnchors)
176         where: 'the following data is used'
177             scenario                           | inputModuleNames                                    || expectedAnchors
178             'one module'                       | ['module-name-1']                                   || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
179             'two modules'                      | ['module-name-1', 'module-name-2']                  || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
180             'no anchors for all three modules' | ['module-name-1', 'module-name-2', 'module-name-3'] || []
181     }
182
183     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
184     def 'Query all anchors for an #scenario.'() {
185         when: 'attempt to query anchors'
186             objectUnderTest.queryAnchors(dataspaceName, moduleNames)
187         then: 'the correct exception is thrown with the relevant details'
188             def thrownException = thrown(expectedException)
189             thrownException.details.contains(expectedMessageDetails)
190         where: 'the following data is used'
191             scenario                          | dataspaceName       | moduleNames                                || expectedException            | expectedMessageDetails  | messageDoesNotContain
192             'unknown dataspace'               | 'db-does-not-exist' | ['does-not-matter']                        || DataspaceNotFoundException   | 'db-does-not-exist'     | 'does-not-matter'
193             'unknown module and known module' | 'dataspace-1'       | ['module-name-1', 'module-does-not-exist'] || ModuleNamesNotFoundException | 'module-does-not-exist' | 'module-name-1'
194     }
195
196     def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) {
197         return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build()
198     }
199
200     @Sql([CLEAR_DATA, SET_DATA])
201     def 'Delete dataspace.'() {
202         when: 'delete dataspace action is invoked'
203             objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA)
204         then: 'dataspace is deleted'
205             assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty();
206     }
207
208     @Sql([CLEAR_DATA, SET_DATA])
209     def 'Delete dataspace when #scenario.'() {
210         when: 'delete dataspace action is invoked'
211             objectUnderTest.deleteDataspace(dataspaceName)
212         then: 'the correct exception is thrown with the relevant details'
213             def thrownException = thrown(expectedException)
214             thrownException.details.contains(expectedMessageDetails)
215         where: 'the following data is used'
216             scenario                        | dataspaceName   || expectedException          | expectedMessageDetails
217             'dataspace name does not exist' | 'unknown'       || DataspaceNotFoundException | 'unknown does not exist'
218             'dataspace contains an anchor'  | 'DATASPACE-001' || DataspaceInUseException    | 'contains 2 anchor(s)'
219             'dataspace contains schemasets' | 'DATASPACE-003' || DataspaceInUseException    | 'contains 1 schemaset(s)'
220     }
221
222 }