Merge "Define Initial Data Sync Enabled Flag and state"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsAdminPersistenceServiceSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2022 Bell Canada
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.spi.impl
24
25 import org.mockito.Mock
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.exceptions.AlreadyDefinedException
28 import org.onap.cps.spi.exceptions.AnchorNotFoundException
29 import org.onap.cps.spi.exceptions.DataspaceInUseException
30 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
31 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
32 import org.onap.cps.spi.model.Anchor
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.test.context.jdbc.Sql
35 import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper
36
37 class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase {
38
39     @Autowired
40     CpsAdminPersistenceService objectUnderTest
41
42     @Mock
43     ObjectMapper objectMapper
44
45     static final String SET_DATA = '/data/anchor.sql'
46     static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql'
47     static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002-NO-DATA'
48     static final Integer DELETED_ANCHOR_ID = 3002
49
50     @Sql(CLEAR_DATA)
51     def 'Create and retrieve a new dataspace.'() {
52         when: 'a new dataspace is created'
53             def dataspaceName = 'some-new-dataspace'
54             objectUnderTest.createDataspace(dataspaceName)
55         then: 'that dataspace can be retrieved from the dataspace repository'
56             def dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow()
57             dataspaceEntity.id != null
58             dataspaceEntity.name == dataspaceName
59     }
60
61     @Sql([CLEAR_DATA, SET_DATA])
62     def 'Attempt to create a duplicate dataspace.'() {
63         when: 'an attempt is made to create an already existing dataspace'
64             objectUnderTest.createDataspace(DATASPACE_NAME)
65         then: 'an exception that is is already defined is thrown with the correct details'
66             def thrown = thrown(AlreadyDefinedException)
67             thrown.details.contains(DATASPACE_NAME)
68     }
69
70     @Sql([CLEAR_DATA, SET_DATA])
71     def 'Create and retrieve a new anchor.'() {
72         when: 'a new anchor is created'
73             def newAnchorName = 'my-new-anchor'
74             objectUnderTest.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME1, newAnchorName)
75         then: 'that anchor can be retrieved'
76             def anchor = objectUnderTest.getAnchor(DATASPACE_NAME, newAnchorName)
77             anchor.name == newAnchorName
78             anchor.dataspaceName == DATASPACE_NAME
79             anchor.schemaSetName == SCHEMA_SET_NAME1
80     }
81
82     @Sql([CLEAR_DATA, SET_DATA])
83     def 'Create anchor error scenario: #scenario.'() {
84         when: 'attempt to create new anchor named #anchorName in dataspace #dataspaceName with #schemaSetName'
85             objectUnderTest.createAnchor(dataspaceName, schemaSetName, anchorName)
86         then: 'an #expectedException is thrown'
87             thrown(expectedException)
88         where: 'the following data is used'
89             scenario                    | dataspaceName  | schemaSetName    | anchorName     || expectedException
90             'dataspace does not exist'  | 'unknown'      | 'not-relevant'   | 'not-relevant' || DataspaceNotFoundException
91             'schema set does not exist' | DATASPACE_NAME | 'unknown'        | 'not-relevant' || SchemaSetNotFoundException
92             'anchor already exists'     | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1   || AlreadyDefinedException
93     }
94
95     @Sql([CLEAR_DATA, SET_DATA])
96     def 'Get anchor error scenario: #scenario.'() {
97         when: 'attempt to get anchor named #anchorName in dataspace #dataspaceName'
98             objectUnderTest.getAnchor(dataspaceName, anchorName)
99         then: 'an #expectedException is thrown'
100             thrown(expectedException)
101         where: 'the following data is used'
102             scenario                   | dataspaceName  | anchorName     || expectedException
103             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
104             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
105     }
106
107     @Sql([CLEAR_DATA, SET_DATA])
108     def 'Get all anchors in dataspace #dataspaceName.'() {
109         when: 'all anchors are retrieved from #DATASPACE_NAME'
110             def result = objectUnderTest.getAnchors(dataspaceName)
111         then: 'the expected collection of anchors is returned'
112             result.size() == expectedAnchors.size()
113             result.containsAll(expectedAnchors)
114         where: 'the following data is used'
115             dataspaceName          || expectedAnchors
116             DATASPACE_NAME         || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(),
117                                        Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()]
118             DATASPACE_WITH_NO_DATA || []
119     }
120
121     @Sql([CLEAR_DATA, SET_DATA])
122     def 'Get all anchors associated with schemaset in a dataspace.'() {
123         when: 'anchors are retrieved by dataspace and schema-set'
124             def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
125         then: ' the response contains expected anchors'
126             anchors == expectedAnchors
127         where:
128             scenario     | dataspace       | schemasetName               || expectedAnchors
129             'no-anchors' | 'DATASPACE-003' | 'SCHEMA-SET-002-NO-ANCHORS' || Collections.emptySet()
130             'one-anchor' | 'DATASPACE-001' | 'SCHEMA-SET-001'            || Set.of(new Anchor('ANCHOR-001', 'DATASPACE-001', 'SCHEMA-SET-001'))
131     }
132
133     @Sql([CLEAR_DATA, SET_DATA])
134     def 'Error Handling: Get all anchors associated with schemaset in a dataspace.'() {
135         when: 'anchors are retrieved by dataspace and schema-set'
136             def anchors = objectUnderTest.getAnchors(dataspace, schemasetName)
137         then: ' an expected expception is thrown'
138             thrown(expectedException)
139         where:
140             scenario            | dataspace       | schemasetName               || expectedException
141             'unknown-dataspace' | 'unknown'       | 'SCHEMA-SET-002-NO-ANCHORS' || DataspaceNotFoundException
142             'unknown-schemaset' | 'DATASPACE-001' | 'unknown-schema-set'        || SchemaSetNotFoundException
143     }
144
145     @Sql(CLEAR_DATA)
146     def 'Get all anchors in unknown dataspace.'() {
147         when: 'attempt to get all anchors in an unknown dataspace'
148             objectUnderTest.getAnchors('unknown-dataspace')
149         then: 'an DataspaceNotFoundException is thrown'
150             thrown(DataspaceNotFoundException)
151     }
152
153     @Sql([CLEAR_DATA, SET_DATA])
154     def 'Delete anchor'() {
155         when: 'delete anchor action is invoked'
156             objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2)
157         then: 'anchor is deleted'
158             assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
159     }
160
161     @Sql([CLEAR_DATA, SET_DATA])
162     def 'delete anchor error scenario: #scenario'() {
163         when: 'delete anchor attempt is performed'
164             objectUnderTest.deleteAnchor(dataspaceName, anchorName)
165         then: 'an #expectedException is thrown'
166             thrown(expectedException)
167         where: 'the following data is used'
168             scenario                   | dataspaceName  | anchorName     || expectedException
169             'dataspace does not exist' | 'unknown'      | 'not-relevant' || DataspaceNotFoundException
170             'anchor does not exists'   | DATASPACE_NAME | 'unknown'      || AnchorNotFoundException
171     }
172
173     @Sql([CLEAR_DATA, SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES])
174     def 'Query anchors that have #scenario.'() {
175         when: 'all anchor are retrieved for the given dataspace name and module names'
176             def anchors = objectUnderTest.queryAnchors(inputDataspaceName, inputModuleNames)
177         then: 'the expected anchors are returned'
178             anchors.size() == expectedAnchors.size()
179             anchors.containsAll(expectedAnchors)
180         where: 'the following data is used'
181             scenario                           | inputDataspaceName  | inputModuleNames                                    || expectedAnchors
182             'one module'                       | 'dataspace-1'       | ['module-name-1']                                   || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
183             'two modules'                      | 'dataspace-1'       | ['module-name-1', 'module-name-2']                  || [buildAnchor('anchor-2', 'dataspace-1', 'schema-set-2'), buildAnchor('anchor-1', 'dataspace-1', 'schema-set-1')]
184             'no anchors for all three modules' | 'dataspace-1'       | ['module-name-1', 'module-name-2', 'module-name-3'] || []
185             'unknown dataspace'                | 'db-does-not-exist' | ['does-not-matter']                                 || []
186             'unknown module and known module'  | 'dataspace-1'       | ['module-name-1', 'module-does-not-exist']          || []
187     }
188
189     def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) {
190         return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build()
191     }
192
193     @Sql([CLEAR_DATA, SET_DATA])
194     def 'Delete dataspace.'() {
195         when: 'delete dataspace action is invoked'
196             objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA)
197         then: 'dataspace is deleted'
198             assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty();
199     }
200
201     @Sql([CLEAR_DATA, SET_DATA])
202     def 'Delete dataspace when #scenario.'() {
203         when: 'delete dataspace action is invoked'
204             objectUnderTest.deleteDataspace(dataspaceName)
205         then: 'the correct exception is thrown with the relevant details'
206             def thrownException = thrown(expectedException)
207             thrownException.details.contains(expectedMessageDetails)
208         where: 'the following data is used'
209             scenario                        | dataspaceName   || expectedException          | expectedMessageDetails
210             'dataspace name does not exist' | 'unknown'       || DataspaceNotFoundException | 'unknown does not exist'
211             'dataspace contains an anchor'  | 'DATASPACE-001' || DataspaceInUseException    | 'contains 2 anchor(s)'
212             'dataspace contains schemasets' | 'DATASPACE-003' || DataspaceInUseException    | 'contains 1 schemaset(s)'
213     }
214 }