Merge "Merge 2 'query' end points in NCMP"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsAdminPersistenceServiceSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.mockito.Mock
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.exceptions.AlreadyDefinedException
28 import org.onap.cps.spi.exceptions.AnchorNotFoundException
29 import org.onap.cps.spi.exceptions.DataspaceInUseException
30 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
31 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
32 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException
33 import org.onap.cps.spi.model.Anchor
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.jdbc.Sql
36 import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper
37
38 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
39
40     @Autowired
41     CpsAdminPersistenceService objectUnderTest
42
43     @Mock
44     ObjectMapper objectMapper
45
46     static final String SET_DATA = '/data/anchor.sql'
47     static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql'
48     static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002-NO-DATA'
49     static final Integer DELETED_ANCHOR_ID = 3002
50
51     @Sql(CLEAR_DATA)
52     def 'Create and retrieve a new dataspace.'() {
53         when: 'a new dataspace is created'
54             def dataspaceName = 'some-new-dataspace'
55             objectUnderTest.createDataspace(dataspaceName)
56         then: 'that dataspace can be retrieved from the dataspace repository'
57             def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
58             dataspaceEntity.id != null
59             dataspaceEntity.name == dataspaceName
60     }
61
62     @Sql([CLEAR_DATA, SET_DATA])
63     def 'Attempt to create a duplicate dataspace.'() {
64         when: 'an attempt is made to create an already existing dataspace'
65             objectUnderTest.createDataspace(DATASPACE_NAME)
66         then: 'an exception that is is already defined is thrown with the correct details'
67             def thrown = thrown(AlreadyDefinedException)
68             thrown.details.contains(DATASPACE_NAME)
69     }
70
71     @Sql([CLEAR_DATA, SET_DATA])
72     def 'Create and retrieve a new anchor.'() {
73         when: 'a new anchor is created'
74             def newAnchorName = 'my-new-anchor'
75             objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
76         then: 'that anchor can be retrieved'
77             def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
78             anchor.name == newAnchorName
79             anchor.dataspaceName == DATASPACE_NAME
80             anchor.schemaSetName == SCHEMA_SET_NAME1
81     }
82
83     @Sql([CLEAR_DATA, SET_DATA])
84     def 'Create anchor error scenario: #scenario.'() {
85         when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
86             objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
87         then: 'an #expectedException is thrown'
88             thrown(expectedException)
89         where: 'the following data is used'
90             scenario                    | dataspaceName  | schemaSetName    | anchorName     || expectedException
91             'dataspace does not exist'  | 'unknown'      | 'not-relevant'   | 'not-relevant' || DataspaceNotFoundException
92             'schema set does not exist' | DATASPACE_NAME | 'unknown'        | 'not-relevant' || SchemaSetNotFoundException
93             'anchor already exists'     | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1   || AlreadyDefinedException
94     }
95
96     @Sql([CLEAR_DATA, SET_DATA])
97     def 'Get anchor error scenario: #scenario.'() {
98         when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
99             objectUnderTest.getAnchor(dataspaceName, anchorName)
100         then: 'an #expectedException is thrown'
101             thrown(expectedException)
102         where: 'the following data is used'
103             scenario                   | dataspaceName  | anchorName     || expectedException
104             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
105             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
106     }
107
108     @Sql([CLEAR_DATA, SET_DATA])
109     def 'Get all anchors in dataspace #dataspaceName.'() {
110         when: 'all anchors are retrieved from #DATASPACE_NAME'
111             def result = objectUnderTest.getAnchors(dataspaceName)
112         then: 'the expected collection of anchors is returned'
113             result.size() == expectedAnchors.size()
114             result.containsAll(expectedAnchors)
115         where: 'the following data is used'
116             dataspaceName          || expectedAnchors
117             DATASPACE_NAME         || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
118                                        Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
119             DATASPACE_WITH_NO_DATA || []
120     }
121
122     @Sql([CLEAR_DATA, SET_DATA])
123     def 'Get all anchors associated with schemaset in a dataspace.'() {
124         when: 'anchors are retrieved by dataspace and schema-set'
125             def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
126         then: ' the response contains expected anchors'
127             anchors == expectedAnchors
128         where:
129             scenario     | dataspace       | schemasetName               || expectedAnchors
130             'no-anchors' | 'DATASPACE-003' | 'SCHEMA-SET-002-NO-ANCHORS' || Collections.emptySet()
131             'one-anchor' | 'DATASPACE-001' | 'SCHEMA-SET-001'            || Set.of(new Anchor('ANCHOR-001', 'DATASPACE-001', 'SCHEMA-SET-001'))
132     }
133
134     @Sql([CLEAR_DATA, SET_DATA])
135     def 'Error Handling: Get all anchors associated with schemaset in a dataspace.'() {
136         when: 'anchors are retrieved by dataspace and schema-set'
137             def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
138         then: ' an expected expception is thrown'
139             thrown(expectedException)
140         where:
141             scenario            | dataspace       | schemasetName               || expectedException
142             'unknown-dataspace' | 'unknown'       | 'SCHEMA-SET-002-NO-ANCHORS' || DataspaceNotFoundException
143             'unknown-schemaset' | 'DATASPACE-001' | 'unknown-schema-set'        || SchemaSetNotFoundException
144     }
145
146     @Sql(CLEAR_DATA)
147     def 'Get all anchors in unknown dataspace.'() {
148         when: 'attempt to get all anchors in an unknown dataspace'
149             objectUnderTest.getAnchors('unknown-dataspace')
150         then: 'an DataspaceNotFoundException is thrown'
151             thrown(DataspaceNotFoundException)
152     }
153
154     @Sql([CLEAR_DATA, SET_DATA])
155     def 'Delete anchor'() {
156         when: 'delete anchor action is invoked'
157             objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2)
158         then: 'anchor is deleted'
159             assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
160     }
161
162     @Sql([CLEAR_DATA, SET_DATA])
163     def 'delete anchor error scenario: #scenario'() {
164         when: 'delete anchor attempt is performed'
165             objectUnderTest.deleteAnchor(dataspaceName, anchorName)
166         then: 'an #expectedException is thrown'
167             thrown(expectedException)
168         where: 'the following data is used'
169             scenario                   | dataspaceName  | anchorName     || expectedException
170             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
171             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
172     }
173
174     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
175     def 'Query anchors that have #scenario.'() {
176         when: 'all anchor are retrieved for the given dataspace name and module names'
177             def anchors = objectUnderTest.queryAnchors('dataspace-1', inputModuleNames)
178         then: 'the expected anchors are returned'
179             anchors.size() == expectedAnchors.size()
180             anchors.containsAll(expectedAnchors)
181         where: 'the following data is used'
182             scenario                           | inputModuleNames                                    || expectedAnchors
183             'one module'                       | ['module-name-1']                                   || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
184             'two modules'                      | ['module-name-1', 'module-name-2']                  || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
185             'no anchors for all three modules' | ['module-name-1', 'module-name-2', 'module-name-3'] || []
186     }
187
188     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
189     def 'Query all anchors for an #scenario.'() {
190         when: 'attempt to query anchors'
191             objectUnderTest.queryAnchors(dataspaceName, moduleNames)
192         then: 'the correct exception is thrown with the relevant details'
193             def thrownException = thrown(expectedException)
194             thrownException.details.contains(expectedMessageDetails)
195         where: 'the following data is used'
196             scenario                          | dataspaceName       | moduleNames                                || expectedException            | expectedMessageDetails  | messageDoesNotContain
197             'unknown dataspace'               | 'db-does-not-exist' | ['does-not-matter']                        || DataspaceNotFoundException   | 'db-does-not-exist'     | 'does-not-matter'
198             'unknown module and known module' | 'dataspace-1'       | ['module-name-1', 'module-does-not-exist'] || ModuleNamesNotFoundException | 'module-does-not-exist' | 'module-name-1'
199     }
200
201     def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) {
202         return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build()
203     }
204
205     @Sql([CLEAR_DATA, SET_DATA])
206     def 'Delete dataspace.'() {
207         when: 'delete dataspace action is invoked'
208             objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA)
209         then: 'dataspace is deleted'
210             assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty();
211     }
212
213     @Sql([CLEAR_DATA, SET_DATA])
214     def 'Delete dataspace when #scenario.'() {
215         when: 'delete dataspace action is invoked'
216             objectUnderTest.deleteDataspace(dataspaceName)
217         then: 'the correct exception is thrown with the relevant details'
218             def thrownException = thrown(expectedException)
219             thrownException.details.contains(expectedMessageDetails)
220         where: 'the following data is used'
221             scenario                        | dataspaceName   || expectedException          | expectedMessageDetails
222             'dataspace name does not exist' | 'unknown'       || DataspaceNotFoundException | 'unknown does not exist'
223             'dataspace contains an anchor'  | 'DATASPACE-001' || DataspaceInUseException    | 'contains 2 anchor(s)'
224             'dataspace contains schemasets' | 'DATASPACE-003' || DataspaceInUseException    | 'contains 1 schemaset(s)'
225     }
226 }