2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.impl
23 import org.onap.cps.spi.CpsAdminPersistenceService
24 import org.onap.cps.spi.exceptions.AlreadyDefinedException
25 import org.onap.cps.spi.exceptions.AnchorNotFoundException
26 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
27 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
28 import org.onap.cps.spi.model.Anchor
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.test.context.jdbc.Sql
31 import spock.lang.Unroll
33 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
36 CpsAdminPersistenceService objectUnderTest
38 static final String SET_DATA = '/data/anchor.sql'
39 static final String EMPTY_DATASPACE_NAME = 'DATASPACE-002'
40 static final Integer DELETED_ANCHOR_ID = 3001
41 static final Long DELETED_FRAGMENT_ID = 4001
44 def 'Create and retrieve a new dataspace.'() {
45 when: 'a new dataspace is created'
46 def dataspaceName = 'some new dataspace'
47 objectUnderTest.createDataspace(dataspaceName)
48 then: 'that dataspace can be retrieved from the dataspace repository'
49 def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
50 dataspaceEntity.id != null
51 dataspaceEntity.name == dataspaceName
54 @Sql([CLEAR_DATA, SET_DATA])
55 def 'Attempt to create a duplicate dataspace.'() {
56 when: 'an attempt is made to create an already existing dataspace'
57 objectUnderTest.createDataspace(DATASPACE_NAME)
58 then: 'an exception that is is already defined is thrown with the correct details'
59 def thrown = thrown(AlreadyDefinedException)
60 thrown.details.contains(DATASPACE_NAME)
63 @Sql([CLEAR_DATA, SET_DATA])
64 def 'Create and retrieve a new anchor.'() {
65 when: 'a new anchor is created'
66 def newAnchorName = 'my new anchor'
67 objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
68 then: 'that anchor can be retrieved'
69 def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
70 anchor.name == newAnchorName
71 anchor.dataspaceName == DATASPACE_NAME
72 anchor.schemaSetName == SCHEMA_SET_NAME1
76 @Sql([CLEAR_DATA, SET_DATA])
77 def 'Create anchor error scenario: #scenario.'() {
78 when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
79 objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
80 then: 'an #expectedException is thrown'
81 thrown(expectedException)
82 where: 'the following data is used'
83 scenario | dataspaceName | schemaSetName | anchorName || expectedException
84 'dataspace does not exist' | 'unknown' | 'not-relevant' | 'not-relevant' || DataspaceNotFoundException
85 'schema set does not exist' | DATASPACE_NAME | 'unknown' | 'not-relevant' || SchemaSetNotFoundException
86 'anchor already exists' | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1 || AlreadyDefinedException
90 @Sql([CLEAR_DATA, SET_DATA])
91 def 'Get anchor error scenario: #scenario.'() {
92 when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
93 objectUnderTest.getAnchor(dataspaceName, anchorName)
94 then: 'an #expectedException is thrown'
95 thrown(expectedException)
96 where: 'the following data is used'
97 scenario | dataspaceName | anchorName || expectedException
98 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
99 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException
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 EMPTY_DATASPACE_NAME || []
118 def 'Get all anchors in unknown dataspace.'() {
119 when: 'attempt to get all anchors in an unknown dataspace'
120 objectUnderTest.getAnchors('unknown dataspace')
121 then: 'an DataspaceNotFoundException is thrown'
122 thrown(DataspaceNotFoundException)
125 @Sql([CLEAR_DATA, SET_DATA])
126 def 'Delete anchor'() {
127 when: 'delete anchor action is invoked'
128 objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1)
129 then: 'anchor and associated data fragment are deleted'
130 assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
131 assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty()
135 @Sql([CLEAR_DATA, SET_DATA])
136 def 'delete anchor error scenario: #scenario'(){
137 when: 'delete anchor attempt is performed'
138 objectUnderTest.deleteAnchor(dataspaceName, anchorName)
139 then: 'an #expectedException is thrown'
140 thrown(expectedException)
141 where: 'the following data is used'
142 scenario | dataspaceName | anchorName || expectedException
143 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
144 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException