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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.impl
24 import org.onap.cps.spi.CpsAdminPersistenceService
25 import org.onap.cps.spi.exceptions.AlreadyDefinedException
26 import org.onap.cps.spi.exceptions.AnchorNotFoundException
27 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
28 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
29 import org.onap.cps.spi.model.Anchor
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.test.context.jdbc.Sql
33 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
36 CpsAdminPersistenceService objectUnderTest
39 static final String SET_DATA = '/data/anchor.sql'
40 static final String EMPTY_DATASPACE_NAME = 'DATASPACE-002'
41 static final Integer DELETED_ANCHOR_ID = 3001
42 static final Long DELETED_FRAGMENT_ID = 4001
45 def 'Create and retrieve a new dataspace.'() {
46 when: 'a new dataspace is created'
47 def dataspaceName = 'some new dataspace'
48 objectUnderTest.createDataspace(dataspaceName)
49 then: 'that dataspace can be retrieved from the dataspace repository'
50 def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
51 dataspaceEntity.id != null
52 dataspaceEntity.name == dataspaceName
55 @Sql([CLEAR_DATA, SET_DATA])
56 def 'Attempt to create a duplicate dataspace.'() {
57 when: 'an attempt is made to create an already existing dataspace'
58 objectUnderTest.createDataspace(DATASPACE_NAME)
59 then: 'an exception that is is already defined is thrown with the correct details'
60 def thrown = thrown(AlreadyDefinedException)
61 thrown.details.contains(DATASPACE_NAME)
64 @Sql([CLEAR_DATA, SET_DATA])
65 def 'Create and retrieve a new anchor.'() {
66 when: 'a new anchor is created'
67 def newAnchorName = 'my new anchor'
68 objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
69 then: 'that anchor can be retrieved'
70 def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
71 anchor.name == newAnchorName
72 anchor.dataspaceName == DATASPACE_NAME
73 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
89 @Sql([CLEAR_DATA, SET_DATA])
90 def 'Get anchor error scenario: #scenario.'() {
91 when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
92 objectUnderTest.getAnchor(dataspaceName, anchorName)
93 then: 'an #expectedException is thrown'
94 thrown(expectedException)
95 where: 'the following data is used'
96 scenario | dataspaceName | anchorName || expectedException
97 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
98 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException
101 @Sql([CLEAR_DATA, SET_DATA])
102 def 'Get all anchors in dataspace #dataspaceName.'() {
103 when: 'all anchors are retrieved from #DATASPACE_NAME'
104 def result = objectUnderTest.getAnchors(dataspaceName)
105 then: 'the expected collection of anchors is returned'
106 result.size() == expectedAnchors.size()
107 result.containsAll(expectedAnchors)
108 where: 'the following data is used'
109 dataspaceName || expectedAnchors
110 DATASPACE_NAME || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
111 Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
112 EMPTY_DATASPACE_NAME || []
116 def 'Get all anchors in unknown dataspace.'() {
117 when: 'attempt to get all anchors in an unknown dataspace'
118 objectUnderTest.getAnchors('unknown dataspace')
119 then: 'an DataspaceNotFoundException is thrown'
120 thrown(DataspaceNotFoundException)
123 @Sql([CLEAR_DATA, SET_DATA])
124 def 'Delete anchor'() {
125 when: 'delete anchor action is invoked'
126 objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1)
127 then: 'anchor and associated data fragment are deleted'
128 assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
129 assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty()
132 @Sql([CLEAR_DATA, SET_DATA])
133 def 'delete anchor error scenario: #scenario'(){
134 when: 'delete anchor attempt is performed'
135 objectUnderTest.deleteAnchor(dataspaceName, anchorName)
136 then: 'an #expectedException is thrown'
137 thrown(expectedException)
138 where: 'the following data is used'
139 scenario | dataspaceName | anchorName || expectedException
140 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
141 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException