b0c13af3dfad86929b19fda2c2c7352cd9815aa7
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceSpec.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 package org.onap.cps.spi.impl
20
21 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
22 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
23
24 import org.onap.cps.spi.CpsAdminPersistenceService
25 import org.onap.cps.spi.CpsModulePersistenceService
26 import org.onap.cps.spi.entities.YangResourceEntity
27 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
28 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException
29 import org.onap.cps.spi.exceptions.SchemaSetInUseException
30 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
31 import org.onap.cps.spi.repository.AnchorRepository
32 import org.onap.cps.spi.repository.SchemaSetRepository
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.test.context.jdbc.Sql
35 import spock.lang.Unroll
36
37 class CpsModulePersistenceServiceSpec extends CpsPersistenceSpecBase {
38
39     @Autowired
40     CpsModulePersistenceService objectUnderTest
41
42     @Autowired
43     AnchorRepository anchorRepository
44
45     @Autowired
46     SchemaSetRepository schemaSetRepository
47
48     @Autowired
49     CpsAdminPersistenceService cpsAdminPersistenceService
50
51     static final String SET_DATA = '/data/schemaset.sql'
52     static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
53     static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
54     static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = 'SCHEMA-SET-101'
55     static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
56
57     static final Long NEW_RESOURCE_ABSTRACT_ID = 0L
58     static final String NEW_RESOURCE_NAME = 'some new resource'
59     static final String NEW_RESOURCE_CONTENT = 'some resource content'
60     static final String NEW_RESOURCE_CHECKSUM = '8185b09f11e262f18043f0ea08803f46'
61
62     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
63     def dataspaceEntity
64
65     def setup() {
66         dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
67     }
68
69     @Unroll
70     @Sql([CLEAR_DATA, SET_DATA])
71     def 'Store schema set error scenario: #scenario.'() {
72         when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
73             objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
74         then: 'an #expectedException is thrown'
75             thrown(expectedException)
76         where: 'the following data is used'
77             scenario                    | dataspaceName  | schemaSetName            || expectedException
78             'dataspace does not exist'  | 'unknown'      | 'not-relevant'           || DataspaceNotFoundException
79             'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || SchemaSetAlreadyDefinedException
80     }
81
82     @Sql([CLEAR_DATA, SET_DATA])
83     def 'Store new schema set.'() {
84         when: 'a new schemaset is stored'
85             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
86         then: 'the schema set is persisted correctly'
87             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME,
88                     NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM)
89     }
90
91     @Sql([CLEAR_DATA, SET_DATA])
92     def 'Retrieving schema set (resources) by anchor.'() {
93         given: 'a new schema set is stored'
94             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
95         and: 'an anchor is created with that schema set'
96             cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
97         when: 'the schema set resources for that anchor is retrieved'
98             def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
99         then: 'the correct resources are returned'
100              result == newYangResourcesNameToContentMap
101     }
102
103     @Sql([CLEAR_DATA, SET_DATA])
104     def 'Storing duplicate schema content.'() {
105         given: 'a new schema set with a resource with the same content as an existing resource'
106             def existingResourceContent = 'CONTENT-001'
107             def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
108         when: 'the schema set with duplicate resource is stored'
109             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
110         then: 'the schema persisted (re)uses the existing id, name and has the same checksum'
111             def existingResourceId = 3001L
112             def existingResourceName = 'module1@2020-02-02.yang'
113             def existingResourceChecksum = '877e65a9f36d54e7702c3f073f6bc42b'
114             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
115                     existingResourceId, existingResourceName, existingResourceContent, existingResourceChecksum)
116     }
117
118     @Sql([CLEAR_DATA, SET_DATA])
119     def 'Delete schema set with cascade delete prohibited but no anchors using it'() {
120         when: 'a schema set is deleted with cascade-prohibited option'
121             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
122                     CASCADE_DELETE_PROHIBITED)
123         then: 'the schema set has been deleted'
124             schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
125         and: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
126             def orphanedResourceId = 3100L
127             yangResourceRepository.findById(orphanedResourceId).isPresent() == false
128         and: 'any shared (still in use by other schema set) yang resources still persists'
129             def sharedResourceId = 3003L
130             yangResourceRepository.findById(sharedResourceId).isPresent()
131     }
132
133     @Sql([CLEAR_DATA, SET_DATA])
134     def 'Delete schema set with cascade allowed.'() {
135         when: 'a schema set is deleted with cascade-allowed option'
136             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA,
137                     CASCADE_DELETE_ALLOWED)
138         then: 'the schema set has been deleted'
139             schemaSetRepository
140                     .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent() == false
141         and: 'the associated anchors are removed'
142             def associatedAnchorsIds = [ 6001, 6002 ]
143             associatedAnchorsIds.each {anchorRepository.findById(it).isPresent() == false }
144         and: 'the fragment(s) under those anchors are removed'
145             def fragmentUnderAnchor1Id = 7001L
146             fragmentRepository.findById(fragmentUnderAnchor1Id).isPresent() == false
147         and: 'the shared resources still persist'
148             def sharedResourceIds = [ 3003L, 3004L ]
149             sharedResourceIds.each {yangResourceRepository.findById(it).isPresent() }
150     }
151
152     @Unroll
153     @Sql([CLEAR_DATA, SET_DATA])
154     def 'Delete schema set error scenario: #scenario.'() {
155         when: 'attempt to delete a schema set where #scenario'
156             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
157         then: 'an #expectedException is thrown'
158             thrown(expectedException)
159         where: 'the following data is used'
160             scenario                                   | dataspaceName  | schemaSetName                         || expectedException
161             'dataspace does not exist'                 | 'unknown'      | 'not-relevant'                        || DataspaceNotFoundException
162             'schema set does not exists'               | DATASPACE_NAME | 'unknown'                             || SchemaSetNotFoundException
163             'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
164     }
165
166     def assertSchemaSetPersisted(expectedDataspaceName,
167                              expectedSchemaSetName,
168                              expectedYangResourceId,
169                              expectedYangResourceName,
170                              expectedYangResourceContent,
171                              expectedYangResourceChecksum) {
172         // assert the schema set is persisted
173         def schemaSetEntity = schemaSetRepository
174                 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
175         assert schemaSetEntity.name == expectedSchemaSetName
176         assert schemaSetEntity.dataspace.name == expectedDataspaceName
177
178         // assert the attached yang resource is persisted
179         def yangResourceEntities = schemaSetEntity.getYangResources()
180         yangResourceEntities.size() == 1
181
182         // assert the attached yang resource content
183         YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
184         assert yangResourceEntity.id != null
185         if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
186             // existing resource with known id
187             assert yangResourceEntity.id == expectedYangResourceId
188         }
189         yangResourceEntity.name == expectedYangResourceName
190         yangResourceEntity.content == expectedYangResourceContent
191         yangResourceEntity.checksum == expectedYangResourceChecksum
192     }
193
194 }