Merge "Attach a (JSON) data instance for a container with children to a given Anchor"
[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.AnchorAlreadyDefinedException
24 import org.onap.cps.spi.exceptions.AnchorNotFoundException
25 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
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
32
33 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
34
35     @Autowired
36     CpsAdminPersistenceService objectUnderTest
37
38     static final String SET_DATA = '/data/anchor.sql'
39     static final String EMPTY_DATASPACE_NAME = 'DATASPACE-002'
40
41     @Sql(CLEAR_DATA)
42     def 'Create and retrieve a new dataspace.'() {
43         when: 'a new dataspace is created'
44             def dataspaceName = 'some new dataspace'
45             objectUnderTest.createDataspace(dataspaceName)
46         then: 'that dataspace can be retrieved from the dataspace repository'
47             def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
48             dataspaceEntity.id != null
49             dataspaceEntity.name == dataspaceName
50     }
51
52     @Sql([CLEAR_DATA, SET_DATA])
53     def 'Attempt to create a duplicate dataspace.'() {
54         when: 'an attempt is made to create an already existing dataspace'
55             objectUnderTest.createDataspace(DATASPACE_NAME)
56         then: 'an exception that is is already defined is thrown with the correct details'
57             def thrown = thrown(DataspaceAlreadyDefinedException)
58             thrown.details.contains(DATASPACE_NAME)
59     }
60
61     @Sql([CLEAR_DATA, SET_DATA])
62     def 'Create and retrieve a new anchor.'() {
63         when: 'a new anchor is created'
64             def newAnchorName = 'my new anchor'
65             objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
66         then: 'that anchor can be retrieved'
67             def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
68             anchor.name == newAnchorName
69             anchor.dataspaceName == DATASPACE_NAME
70             anchor.schemaSetName == SCHEMA_SET_NAME1
71     }
72
73     @Unroll
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   || AnchorAlreadyDefinedException
85     }
86
87     @Unroll
88     @Sql([CLEAR_DATA, SET_DATA])
89     def 'Get anchor error scenario: #scenario.'() {
90         when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
91             objectUnderTest.getAnchor(dataspaceName, anchorName)
92         then: 'an #expectedException is thrown'
93             thrown(expectedException)
94         where: 'the following data is used'
95             scenario                    | dataspaceName  | anchorName     || expectedException
96             'dataspace does not exist'  | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
97             'anchor does not exists'    | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
98     }
99
100     @Unroll
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 || []
113     }
114
115     @Sql(CLEAR_DATA)
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)
121     }
122 }