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