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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.spi.impl
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
38 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
41 CpsAdminPersistenceService objectUnderTest
44 ObjectMapper objectMapper
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
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
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)
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
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
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
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 || []
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
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'))
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)
141 scenario | dataspace | schemasetName || expectedException
142 'unknown-dataspace' | 'unknown' | 'SCHEMA-SET-002-NO-ANCHORS' || DataspaceNotFoundException
143 'unknown-schemaset' | 'DATASPACE-001' | 'unknown-schema-set' || SchemaSetNotFoundException
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)
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()
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
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(inputDataspaceName, 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 | inputDataspaceName | inputModuleNames || expectedAnchors
183 'one module' | 'dataspace-1' | ['module-name-1'] || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
184 'two modules' | 'dataspace-1' | ['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' | 'dataspace-1' | ['module-name-1', 'module-name-2', 'module-name-3'] || []
186 'unknown dataspace' | 'db-does-not-exist' | ['does-not-matter'] || []
187 'unknown module and known module' | 'dataspace-1' | ['module-name-1', 'module-does-not-exist'] || []
190 def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) {
191 return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build()
194 @Sql([CLEAR_DATA, SET_DATA])
195 def 'Delete dataspace.'() {
196 when: 'delete dataspace action is invoked'
197 objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA)
198 then: 'dataspace is deleted'
199 assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty();
202 @Sql([CLEAR_DATA, SET_DATA])
203 def 'Delete dataspace when #scenario.'() {
204 when: 'delete dataspace action is invoked'
205 objectUnderTest.deleteDataspace(dataspaceName)
206 then: 'the correct exception is thrown with the relevant details'
207 def thrownException = thrown(expectedException)
208 thrownException.details.contains(expectedMessageDetails)
209 where: 'the following data is used'
210 scenario | dataspaceName || expectedException | expectedMessageDetails
211 'dataspace name does not exist' | 'unknown' || DataspaceNotFoundException | 'unknown does not exist'
212 'dataspace contains an anchor' | 'DATASPACE-001' || DataspaceInUseException | 'contains 2 anchor(s)'
213 'dataspace contains schemasets' | 'DATASPACE-003' || DataspaceInUseException | 'contains 1 schemaset(s)'