Editing of Nordix Licenses to ONAP guidelines
[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  *  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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.impl
23
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
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     static final Integer DELETED_ANCHOR_ID = 3001
41     static final Long DELETED_FRAGMENT_ID = 4001
42
43     @Sql(CLEAR_DATA)
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
52     }
53
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)
61     }
62
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
73     }
74
75     @Sql([CLEAR_DATA, SET_DATA])
76     def 'Create anchor error scenario: #scenario.'() {
77         when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
78             objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
79         then: 'an #expectedException is thrown'
80             thrown(expectedException)
81         where: 'the following data is used'
82             scenario                    | dataspaceName  | schemaSetName    | anchorName     || expectedException
83             'dataspace does not exist'  | 'unknown'      | 'not-relevant'   | 'not-relevant' || DataspaceNotFoundException
84             'schema set does not exist' | DATASPACE_NAME | 'unknown'        | 'not-relevant' || SchemaSetNotFoundException
85             'anchor already exists'     | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1   || AlreadyDefinedException
86     }
87
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     @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
122     @Sql([CLEAR_DATA, SET_DATA])
123     def 'Delete anchor'() {
124         when: 'delete anchor action is invoked'
125             objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1)
126         then: 'anchor and associated data fragment are deleted'
127             assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
128             assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty()
129     }
130
131     @Sql([CLEAR_DATA, SET_DATA])
132     def 'delete anchor error scenario: #scenario'(){
133         when: 'delete anchor attempt is performed'
134             objectUnderTest.deleteAnchor(dataspaceName, anchorName)
135         then: 'an #expectedException is thrown'
136             thrown(expectedException)
137         where: 'the following data is used'
138             scenario                   | dataspaceName  | anchorName     || expectedException
139             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
140             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
141     }
142 }