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.onap.cps.spi.model.CmHandleQueryParameters
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.jdbc.Sql
37 import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper
39 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
42 CpsAdminPersistenceService objectUnderTest
45 ObjectMapper objectMapper
47 static final String SET_DATA = '/data/anchor.sql'
48 static final String SET_FRAGMENT_DATA = '/data/fragment.sql'
49 static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql'
50 static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002-NO-DATA'
51 static final Integer DELETED_ANCHOR_ID = 3002
54 def 'Create and retrieve a new dataspace.'() {
55 when: 'a new dataspace is created'
56 def dataspaceName = 'some-new-dataspace'
57 objectUnderTest.createDataspace(dataspaceName)
58 then: 'that dataspace can be retrieved from the dataspace repository'
59 def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
60 dataspaceEntity.id != null
61 dataspaceEntity.name == dataspaceName
64 @Sql([CLEAR_DATA, SET_DATA])
65 def 'Attempt to create a duplicate dataspace.'() {
66 when: 'an attempt is made to create an already existing dataspace'
67 objectUnderTest.createDataspace(DATASPACE_NAME)
68 then: 'an exception that is is already defined is thrown with the correct details'
69 def thrown = thrown(AlreadyDefinedException)
70 thrown.details.contains(DATASPACE_NAME)
73 @Sql([CLEAR_DATA, SET_DATA])
74 def 'Create and retrieve a new anchor.'() {
75 when: 'a new anchor is created'
76 def newAnchorName = 'my-new-anchor'
77 objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
78 then: 'that anchor can be retrieved'
79 def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
80 anchor.name == newAnchorName
81 anchor.dataspaceName == DATASPACE_NAME
82 anchor.schemaSetName == SCHEMA_SET_NAME1
85 @Sql([CLEAR_DATA, SET_DATA])
86 def 'Create anchor error scenario: #scenario.'() {
87 when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
88 objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
89 then: 'an #expectedException is thrown'
90 thrown(expectedException)
91 where: 'the following data is used'
92 scenario | dataspaceName | schemaSetName | anchorName || expectedException
93 'dataspace does not exist' | 'unknown' | 'not-relevant' | 'not-relevant' || DataspaceNotFoundException
94 'schema set does not exist' | DATASPACE_NAME | 'unknown' | 'not-relevant' || SchemaSetNotFoundException
95 'anchor already exists' | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1 || AlreadyDefinedException
98 @Sql([CLEAR_DATA, SET_DATA])
99 def 'Get anchor error scenario: #scenario.'() {
100 when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
101 objectUnderTest.getAnchor(dataspaceName, anchorName)
102 then: 'an #expectedException is thrown'
103 thrown(expectedException)
104 where: 'the following data is used'
105 scenario | dataspaceName | anchorName || expectedException
106 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
107 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException
110 @Sql([CLEAR_DATA, SET_DATA])
111 def 'Get all anchors in dataspace #dataspaceName.'() {
112 when: 'all anchors are retrieved from #DATASPACE_NAME'
113 def result = objectUnderTest.getAnchors(dataspaceName)
114 then: 'the expected collection of anchors is returned'
115 result.size() == expectedAnchors.size()
116 result.containsAll(expectedAnchors)
117 where: 'the following data is used'
118 dataspaceName || expectedAnchors
119 DATASPACE_NAME || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
120 Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
121 DATASPACE_WITH_NO_DATA || []
124 @Sql([CLEAR_DATA, SET_DATA])
125 def 'Get all anchors associated with schemaset in a dataspace.'() {
126 when: 'anchors are retrieved by dataspace and schema-set'
127 def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
128 then: ' the response contains expected anchors'
129 anchors == expectedAnchors
131 scenario | dataspace | schemasetName || expectedAnchors
132 'no-anchors' | 'DATASPACE-003' | 'SCHEMA-SET-002-NO-ANCHORS' || Collections.emptySet()
133 'one-anchor' | 'DATASPACE-001' | 'SCHEMA-SET-001' || Set.of(new Anchor('ANCHOR-001', 'DATASPACE-001', 'SCHEMA-SET-001'))
136 @Sql([CLEAR_DATA, SET_DATA])
137 def 'Error Handling: Get all anchors associated with schemaset in a dataspace.'() {
138 when: 'anchors are retrieved by dataspace and schema-set'
139 def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
140 then: ' an expected expception is thrown'
141 thrown(expectedException)
143 scenario | dataspace | schemasetName || expectedException
144 'unknown-dataspace' | 'unknown' | 'SCHEMA-SET-002-NO-ANCHORS' || DataspaceNotFoundException
145 'unknown-schemaset' | 'DATASPACE-001' | 'unknown-schema-set' || SchemaSetNotFoundException
149 def 'Get all anchors in unknown dataspace.'() {
150 when: 'attempt to get all anchors in an unknown dataspace'
151 objectUnderTest.getAnchors('unknown-dataspace')
152 then: 'an DataspaceNotFoundException is thrown'
153 thrown(DataspaceNotFoundException)
156 @Sql([CLEAR_DATA, SET_DATA])
157 def 'Delete anchor'() {
158 when: 'delete anchor action is invoked'
159 objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2)
160 then: 'anchor is deleted'
161 assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
164 @Sql([CLEAR_DATA, SET_DATA])
165 def 'delete anchor error scenario: #scenario'() {
166 when: 'delete anchor attempt is performed'
167 objectUnderTest.deleteAnchor(dataspaceName, anchorName)
168 then: 'an #expectedException is thrown'
169 thrown(expectedException)
170 where: 'the following data is used'
171 scenario | dataspaceName | anchorName || expectedException
172 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
173 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException
176 @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
177 def 'Query anchors that have #scenario.'() {
178 when: 'all anchor are retrieved for the given dataspace name and module names'
179 def anchors = objectUnderTest.queryAnchors('dataspace-1', inputModuleNames)
180 then: 'the expected anchors are returned'
181 anchors.size() == expectedAnchors.size()
182 anchors.containsAll(expectedAnchors)
183 where: 'the following data is used'
184 scenario | inputModuleNames || expectedAnchors
185 'one module' | ['module-name-1'] || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
186 'two modules' | ['module-name-1', 'module-name-2'] || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
187 'no anchors for all three modules' | ['module-name-1', 'module-name-2', 'module-name-3'] || []
190 @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
191 def 'Query all anchors for an #scenario.'() {
192 when: 'attempt to query anchors'
193 objectUnderTest.queryAnchors(dataspaceName, moduleNames)
194 then: 'the correct exception is thrown with the relevant details'
195 def thrownException = thrown(expectedException)
196 thrownException.details.contains(expectedMessageDetails)
197 where: 'the following data is used'
198 scenario | dataspaceName | moduleNames || expectedException | expectedMessageDetails | messageDoesNotContain
199 'unknown dataspace' | 'db-does-not-exist' | ['does-not-matter'] || DataspaceNotFoundException | 'db-does-not-exist' | 'does-not-matter'
200 'unknown module and known module' | 'dataspace-1' | ['module-name-1', 'module-does-not-exist'] || ModuleNamesNotFoundException | 'module-does-not-exist' | 'module-name-1'
203 def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) {
204 return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build()
207 @Sql([CLEAR_DATA, SET_DATA])
208 def 'Delete dataspace.'() {
209 when: 'delete dataspace action is invoked'
210 objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA)
211 then: 'dataspace is deleted'
212 assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty();
215 @Sql([CLEAR_DATA, SET_DATA])
216 def 'Delete dataspace when #scenario.'() {
217 when: 'delete dataspace action is invoked'
218 objectUnderTest.deleteDataspace(dataspaceName)
219 then: 'the correct exception is thrown with the relevant details'
220 def thrownException = thrown(expectedException)
221 thrownException.details.contains(expectedMessageDetails)
222 where: 'the following data is used'
223 scenario | dataspaceName || expectedException | expectedMessageDetails
224 'dataspace name does not exist' | 'unknown' || DataspaceNotFoundException | 'unknown does not exist'
225 'dataspace contains an anchor' | 'DATASPACE-001' || DataspaceInUseException | 'contains 2 anchor(s)'
226 'dataspace contains schemasets' | 'DATASPACE-003' || DataspaceInUseException | 'contains 1 schemaset(s)'
229 @Sql([CLEAR_DATA, SET_FRAGMENT_DATA])
230 def 'Retrieve cm handle ids when #scenario.'() {
231 when: 'the service is invoked'
232 def cmHandleQueryParameters = new CmHandleQueryParameters()
233 cmHandleQueryParameters.setPublicProperties(publicProperties)
234 def returnedCmHandles = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
235 then: 'the correct expected cm handles are returned'
236 returnedCmHandles == expectedCmHandleIds
237 where: 'the following data is used'
238 scenario | publicProperties || expectedCmHandleIds
239 'single matching property' | ['Contact' : 'newemailforstore@bookstore.com'] || ['PNFDemo2', 'PNFDemo', 'PNFDemo4'] as Set
240 'public property dont match' | ['wont_match' : 'wont_match'] || [] as Set
241 '2 properties, only one match (and)' | ['Contact' : 'newemailforstore@bookstore.com', 'Contact2': 'newemailforstore2@bookstore.com'] || ['PNFDemo4'] as Set
242 '2 properties, no match (and)' | ['Contact' : 'newemailforstore@bookstore.com', 'Contact2': ''] || [] as Set
243 'No public properties - return all cm handles' | [ : ] || ['PNFDemo3', 'PNFDemo', 'PNFDemo2', 'PNFDemo4'] as Set