Internal Server Error when creating the same data node twice
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsAdminPersistenceServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.spi.impl
21
22 import org.onap.cps.spi.CpsAdminPersistenceService
23 import org.onap.cps.spi.exceptions.AlreadyDefinedException
24 import org.onap.cps.spi.exceptions.AnchorNotFoundException
25 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
26 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
27 import org.onap.cps.spi.model.Anchor
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.jdbc.Sql
30 import spock.lang.Unroll
31
32 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
33
34     @Autowired
35     CpsAdminPersistenceService objectUnderTest
36
37     static final String SET_DATA = '/data/anchor.sql'
38     static final String EMPTY_DATASPACE_NAME = 'DATASPACE-002'
39
40     @Sql(CLEAR_DATA)
41     def 'Create and retrieve a new dataspace.'() {
42         when: 'a new dataspace is created'
43             def dataspaceName = 'some new dataspace'
44             objectUnderTest.createDataspace(dataspaceName)
45         then: 'that dataspace can be retrieved from the dataspace repository'
46             def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
47             dataspaceEntity.id != null
48             dataspaceEntity.name == dataspaceName
49     }
50
51     @Sql([CLEAR_DATA, SET_DATA])
52     def 'Attempt to create a duplicate dataspace.'() {
53         when: 'an attempt is made to create an already existing dataspace'
54             objectUnderTest.createDataspace(DATASPACE_NAME)
55         then: 'an exception that is is already defined is thrown with the correct details'
56             def thrown = thrown(AlreadyDefinedException)
57             thrown.details.contains(DATASPACE_NAME)
58     }
59
60     @Sql([CLEAR_DATA, SET_DATA])
61     def 'Create and retrieve a new anchor.'() {
62         when: 'a new anchor is created'
63             def newAnchorName = 'my new anchor'
64             objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
65         then: 'that anchor can be retrieved'
66             def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
67             anchor.name == newAnchorName
68             anchor.dataspaceName == DATASPACE_NAME
69             anchor.schemaSetName == SCHEMA_SET_NAME1
70     }
71
72     @Unroll
73     @Sql([CLEAR_DATA, SET_DATA])
74     def 'Create anchor error scenario: #scenario.'() {
75         when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
76             objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
77         then: 'an #expectedException is thrown'
78             thrown(expectedException)
79         where: 'the following data is used'
80             scenario                    | dataspaceName  | schemaSetName     | anchorName     || expectedException
81             'dataspace does not exist'  | 'unknown'      | 'not-relevant'    | 'not-relevant' || DataspaceNotFoundException
82             'schema set does not exist' | DATASPACE_NAME | 'unknown'         | 'not-relevant' || SchemaSetNotFoundException
83             'anchor already exists'     | DATASPACE_NAME |  SCHEMA_SET_NAME1 | ANCHOR_NAME1   || AlreadyDefinedException
84     }
85
86     @Unroll
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
97     }
98
99     @Unroll
100     @Sql([CLEAR_DATA, SET_DATA])
101     def 'Get all anchors in dataspace #dataspaceName.'() {
102         when: 'all anchors are retrieved from #DATASPACE_NAME'
103             def result = objectUnderTest.getAnchors(dataspaceName)
104         then: 'the expected collection of anchors is returned'
105             result.size() == expectedAnchors.size()
106             result.containsAll(expectedAnchors)
107         where: 'the following data is used'
108             dataspaceName        || expectedAnchors
109             DATASPACE_NAME       || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
110                                      Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
111             EMPTY_DATASPACE_NAME || []
112     }
113
114     @Sql(CLEAR_DATA)
115     def 'Get all anchors in unknown dataspace.'() {
116         when: 'attempt to get all anchors in an unknown dataspace'
117             objectUnderTest.getAnchors('unknown dataspace')
118         then: 'an DataspaceNotFoundException is thrown'
119             thrown(DataspaceNotFoundException)
120     }
121 }