a223e7135fddd60d159b4634d563eda1aa4d3eef
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Bell Canada.
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 package org.onap.cps.spi.impl
22
23 import org.onap.cps.spi.CpsAdminPersistenceService
24 import org.onap.cps.spi.CpsModulePersistenceService
25 import org.onap.cps.spi.entities.YangResourceEntity
26 import org.onap.cps.spi.exceptions.AlreadyDefinedException
27 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
28 import org.onap.cps.spi.exceptions.SchemaSetInUseException
29 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
30 import org.onap.cps.spi.model.ModuleReference
31 import org.onap.cps.spi.model.ExtendedModuleReference
32 import org.onap.cps.spi.repository.AnchorRepository
33 import org.onap.cps.spi.repository.SchemaSetRepository
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.jdbc.Sql
36
37 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
38 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
39
40 class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
41
42     @Autowired
43     CpsModulePersistenceService objectUnderTest
44
45     @Autowired
46     AnchorRepository anchorRepository
47
48     @Autowired
49     SchemaSetRepository schemaSetRepository
50
51     @Autowired
52     CpsAdminPersistenceService cpsAdminPersistenceService
53
54     static final String SET_DATA = '/data/schemaset.sql'
55     static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
56     static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
57     static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = 'SCHEMA-SET-101'
58     static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
59
60     static final String NEW_RESOURCE_NAME = 'some new resource'
61     static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
62             '    yang-version 1.1;\n' +
63             '    namespace "org:onap:ccsdk:sample";\n' +
64             '\n' +
65             '    prefix book-store;\n' +
66             '\n' +
67             '    revision "2020-09-15" {\n' +
68             '        description\n' +
69             '        "Sample Model";\n' +
70             '    }' +
71             '}'
72     static final String NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
73     static final String NEW_RESOURCE_MODULE_NAME = 'stores'
74     static final String NEW_RESOURCE_REVISION = '2020-09-15'
75     static final ExtendedModuleReference newModuleReference = ExtendedModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME)
76             .revision(NEW_RESOURCE_REVISION).build()
77
78     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
79     def allYangResourcesModuleAndRevisionList = [new ExtendedModuleReference(name: 'MODULE-NAME-002',namespace:null, revision: 'REVISION-002'), new ExtendedModuleReference(name: 'MODULE-NAME-003',namespace:null, revision: 'REVISION-003'),
80                                                  new ExtendedModuleReference(name: 'MODULE-NAME-004',namespace:null, revision: 'REVISION-004'), ExtendedModuleReference.builder().build(),
81                                                  ExtendedModuleReference.builder().build(), newModuleReference]
82     def dataspaceEntity
83
84     def setup() {
85         dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
86     }
87
88     @Sql([CLEAR_DATA, SET_DATA])
89     def 'Store schema set error scenario: #scenario.'() {
90         when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
91             objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
92         then: 'an #expectedException is thrown'
93             thrown(expectedException)
94         where: 'the following data is used'
95             scenario                    | dataspaceName  | schemaSetName            || expectedException
96             'dataspace does not exist'  | 'unknown'      | 'not-relevant'           || DataspaceNotFoundException
97             'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException
98     }
99
100     @Sql([CLEAR_DATA, SET_DATA])
101     def 'Store new schema set.'() {
102         when: 'a new schemaset is stored'
103             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
104         then: 'the schema set is persisted correctly'
105             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_NAME,
106                     NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM, NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION)
107     }
108
109     @Sql([CLEAR_DATA, SET_DATA])
110     def 'Store and retrieve new schema set from new modules and existing modules.'() {
111         given: 'map of new modules, a list of existing modules, module reference'
112             def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }']
113             def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12")
114             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
115             def mapOfExistingModule = [test: 'module test { yang-version 1.1; revision "2021-10-12" { } }']
116             objectUnderTest.storeSchemaSet(DATASPACE_NAME, "someSchemaSetName", mapOfExistingModule)
117         when: 'a new schema set is created from these new modules and existing modules'
118             objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference)
119         then: 'the schema set can be retrieved'
120             def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule
121             def actualYangResourcesMapAfterSchemaSetHasBeenCreated =
122                     objectUnderTest.getYangSchemaResources(DATASPACE_NAME, "newSchemaSetName")
123         actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated
124     }
125
126     @Sql([CLEAR_DATA, SET_DATA])
127     def 'Retrieving schema set (resources) by anchor.'() {
128         given: 'a new schema set is stored'
129             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
130         and: 'an anchor is created with that schema set'
131             cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
132         when: 'the schema set resources for that anchor is retrieved'
133             def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
134         then: 'the correct resources are returned'
135              result == newYangResourcesNameToContentMap
136     }
137
138     @Sql([CLEAR_DATA, SET_DATA])
139     def 'Retrieving all yang resources module references for the given dataspace.'() {
140         given: 'a dataspace name'
141             def dataspaceName = 'DATASPACE-002'
142         when: 'all yang resources module references are retrieved for the given dataspace name'
143             def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName)
144         then: 'the correct resources are returned'
145             result.sort() == [new ModuleReference(moduleName: 'MODULE-NAME-005', revision: 'REVISION-002'),
146                               new ModuleReference(moduleName: 'MODULE-NAME-006', revision: 'REVISION-006')]
147     }
148
149     @Sql([CLEAR_DATA, SET_DATA])
150     def 'Retrieving module names and revisions for the given anchor.'() {
151         given: 'a dataspace name and anchor name'
152             def dataspaceName = 'DATASPACE-001'
153             def anchorName = 'ANCHOR1'
154         when: 'all yang resources module references are retrieved for the given anchor'
155             def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName, anchorName)
156         then: 'the correct module names and revisions are returned'
157             result.sort() == [ new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'),
158                               new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')]
159     }
160
161     @Sql([CLEAR_DATA, SET_DATA])
162     def 'Storing duplicate schema content.'() {
163         given: 'a new schema set with a resource with the same content as an existing resource'
164             def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
165             def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
166         when: 'the schema set with duplicate resource is stored'
167             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
168         then: 'the schema persisted (re)uses the existing name and has the same checksum'
169             def existingResourceName = 'module1@2020-02-02.yang'
170             def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
171             def existingResourceModelName = 'test'
172             def existingResourceRevision = '2020-09-15'
173             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, existingResourceName,
174                     existingResourceContent, existingResourceChecksum,
175                     existingResourceModelName, existingResourceRevision)
176     }
177
178     @Sql([CLEAR_DATA, SET_DATA])
179     def 'Delete schema set with cascade delete prohibited but no anchors using it'() {
180         when: 'a schema set is deleted with cascade-prohibited option'
181             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
182                     CASCADE_DELETE_PROHIBITED)
183         then: 'the schema set has been deleted'
184             schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
185         and: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
186             def orphanedResourceId = 3100L
187             yangResourceRepository.findById(orphanedResourceId).isPresent() == false
188         and: 'any shared (still in use by other schema set) yang resources still persists'
189             def sharedResourceId = 3003L
190             yangResourceRepository.findById(sharedResourceId).isPresent()
191     }
192
193     @Sql([CLEAR_DATA, SET_DATA])
194     def 'Delete schema set with cascade allowed.'() {
195         when: 'a schema set is deleted with cascade-allowed option'
196             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA,
197                     CASCADE_DELETE_ALLOWED)
198         then: 'the schema set has been deleted'
199             schemaSetRepository
200                     .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent() == false
201         and: 'the associated anchors are removed'
202             def associatedAnchorsIds = [ 6001, 6002 ]
203             associatedAnchorsIds.each {anchorRepository.findById(it).isPresent() == false }
204         and: 'the fragment(s) under those anchors are removed'
205             def fragmentUnderAnchor1Id = 7001L
206             fragmentRepository.findById(fragmentUnderAnchor1Id).isPresent() == false
207         and: 'the shared resources still persist'
208             def sharedResourceIds = [ 3003L, 3004L ]
209             sharedResourceIds.each {yangResourceRepository.findById(it).isPresent() }
210     }
211
212     @Sql([CLEAR_DATA, SET_DATA])
213     def 'Delete schema set error scenario: #scenario.'() {
214         when: 'attempt to delete a schema set where #scenario'
215             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
216         then: 'an #expectedException is thrown'
217             thrown(expectedException)
218         where: 'the following data is used'
219             scenario                                   | dataspaceName  | schemaSetName                         || expectedException
220             'dataspace does not exist'                 | 'unknown'      | 'not-relevant'                        || DataspaceNotFoundException
221             'schema set does not exists'               | DATASPACE_NAME | 'unknown'                             || SchemaSetNotFoundException
222             'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
223     }
224
225     def assertSchemaSetPersisted(expectedDataspaceName,
226                                  expectedSchemaSetName,
227                                  expectedYangResourceName,
228                                  expectedYangResourceContent,
229                                  expectedYangResourceChecksum,
230                                  expectedYangResourceModuleName,
231                                  expectedYangResourceRevision) {
232         // assert the schema set is persisted
233         def schemaSetEntity = schemaSetRepository
234                 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
235         assert schemaSetEntity.name == expectedSchemaSetName
236         assert schemaSetEntity.dataspace.name == expectedDataspaceName
237
238         // assert the attached yang resource is persisted
239         def yangResourceEntities = schemaSetEntity.getYangResources()
240         yangResourceEntities.size() == 1
241
242         // assert the attached yang resource content
243         YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
244         assert yangResourceEntity.id != null
245         yangResourceEntity.name == expectedYangResourceName
246         yangResourceEntity.content == expectedYangResourceContent
247         yangResourceEntity.checksum == expectedYangResourceChecksum
248         yangResourceEntity.moduleName == expectedYangResourceModuleName
249         yangResourceEntity.revision == expectedYangResourceRevision
250     }
251
252 }