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
32 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
35 CpsAdminPersistenceService objectUnderTest
37 static final String SET_DATA = '/data/anchor.sql'
38 static final String EMPTY_DATASPACE_NAME = 'DATASPACE-002'
39 static final Integer DELETED_ANCHOR_ID = 3001
40 static final Long DELETED_FRAGMENT_ID = 4001
43 def 'Create and retrieve a new dataspace.'() {
44 when: 'a new dataspace is created'
45 def dataspaceName = 'some new dataspace'
46 objectUnderTest.createDataspace(dataspaceName)
47 then: 'that dataspace can be retrieved from the dataspace repository'
48 def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
49 dataspaceEntity.id != null
50 dataspaceEntity.name == dataspaceName
53 @Sql([CLEAR_DATA, SET_DATA])
54 def 'Attempt to create a duplicate dataspace.'() {
55 when: 'an attempt is made to create an already existing dataspace'
56 objectUnderTest.createDataspace(DATASPACE_NAME)
57 then: 'an exception that is is already defined is thrown with the correct details'
58 def thrown = thrown(AlreadyDefinedException)
59 thrown.details.contains(DATASPACE_NAME)
62 @Sql([CLEAR_DATA, SET_DATA])
63 def 'Create and retrieve a new anchor.'() {
64 when: 'a new anchor is created'
65 def newAnchorName = 'my new anchor'
66 objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
67 then: 'that anchor can be retrieved'
68 def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
69 anchor.name == newAnchorName
70 anchor.dataspaceName == DATASPACE_NAME
71 anchor.schemaSetName == SCHEMA_SET_NAME1
74 @Sql([CLEAR_DATA, SET_DATA])
75 def 'Create anchor error scenario: #scenario.'() {
76 when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
77 objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
78 then: 'an #expectedException is thrown'
79 thrown(expectedException)
80 where: 'the following data is used'
81 scenario | dataspaceName | schemaSetName | anchorName || expectedException
82 'dataspace does not exist' | 'unknown' | 'not-relevant' | 'not-relevant' || DataspaceNotFoundException
83 'schema set does not exist' | DATASPACE_NAME | 'unknown' | 'not-relevant' || SchemaSetNotFoundException
84 'anchor already exists' | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1 || AlreadyDefinedException
87 @Sql([CLEAR_DATA, SET_DATA])
88 def 'Get anchor error scenario: #scenario.'() {
89 when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
90 objectUnderTest.getAnchor(dataspaceName, anchorName)
91 then: 'an #expectedException is thrown'
92 thrown(expectedException)
93 where: 'the following data is used'
94 scenario | dataspaceName | anchorName || expectedException
95 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
96 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException
99 @Sql([CLEAR_DATA, SET_DATA])
100 def 'Get all anchors in dataspace #dataspaceName.'() {
101 when: 'all anchors are retrieved from #DATASPACE_NAME'
102 def result = objectUnderTest.getAnchors(dataspaceName)
103 then: 'the expected collection of anchors is returned'
104 result.size() == expectedAnchors.size()
105 result.containsAll(expectedAnchors)
106 where: 'the following data is used'
107 dataspaceName || expectedAnchors
108 DATASPACE_NAME || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
109 Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
110 EMPTY_DATASPACE_NAME || []
114 def 'Get all anchors in unknown dataspace.'() {
115 when: 'attempt to get all anchors in an unknown dataspace'
116 objectUnderTest.getAnchors('unknown dataspace')
117 then: 'an DataspaceNotFoundException is thrown'
118 thrown(DataspaceNotFoundException)
121 @Sql([CLEAR_DATA, SET_DATA])
122 def 'Delete anchor'() {
123 when: 'delete anchor action is invoked'
124 objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1)
125 then: 'anchor and associated data fragment are deleted'
126 assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
127 assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty()
130 @Sql([CLEAR_DATA, SET_DATA])
131 def 'delete anchor error scenario: #scenario'(){
132 when: 'delete anchor attempt is performed'
133 objectUnderTest.deleteAnchor(dataspaceName, anchorName)
134 then: 'an #expectedException is thrown'
135 thrown(expectedException)
136 where: 'the following data is used'
137 scenario | dataspaceName | anchorName || expectedException
138 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
139 'anchor does not exists' | DATASPACE_NAME | 'unknown' || AnchorNotFoundException