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 Long NEW_RESOURCE_ABSTRACT_ID = 0L
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' +
65 ' prefix book-store;\n' +
67 ' revision "2020-09-15" {\n' +
69 ' "Sample Model";\n' +
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 ModuleReference newModuleReference = ModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME)
76 .revision(NEW_RESOURCE_REVISION).build()
78 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
79 def allYangResourcesModuleAndRevisionList = [ModuleReference.builder().build(),ModuleReference.builder().build(),
80 ModuleReference.builder().build(),ModuleReference.builder().build(),
81 ModuleReference.builder().build(), newModuleReference]
85 dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
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
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_ABSTRACT_ID, NEW_RESOURCE_NAME,
106 NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM, NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION)
109 @Sql([CLEAR_DATA, SET_DATA])
110 def 'Retrieving schema set (resources) by anchor.'() {
111 given: 'a new schema set is stored'
112 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
113 and: 'an anchor is created with that schema set'
114 cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
115 when: 'the schema set resources for that anchor is retrieved'
116 def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
117 then: 'the correct resources are returned'
118 result == newYangResourcesNameToContentMap
121 @Sql([CLEAR_DATA, SET_DATA])
122 def 'Retrieving all yang resources module references.'() {
123 given: 'a new schema set is stored'
124 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
125 when: 'all yang resources module references are retrieved'
126 def result = objectUnderTest.getAllYangResourcesModuleReferences()
127 then: 'the correct resources are returned'
128 result.sort() == allYangResourcesModuleAndRevisionList.sort()
131 @Sql([CLEAR_DATA, SET_DATA])
132 def 'Storing duplicate schema content.'() {
133 given: 'a new schema set with a resource with the same content as an existing resource'
134 def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
135 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
136 when: 'the schema set with duplicate resource is stored'
137 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
138 then: 'the schema persisted (re)uses the existing id, name and has the same checksum'
139 def existingResourceId = 9L
140 def existingResourceName = 'module1@2020-02-02.yang'
141 def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
142 def existingResourceModelName = 'test'
143 def existingResourceRevision = '2020-09-15'
144 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
145 existingResourceId, existingResourceName, existingResourceContent, existingResourceChecksum,
146 existingResourceModelName, existingResourceRevision)
149 @Sql([CLEAR_DATA, SET_DATA])
150 def 'Delete schema set with cascade delete prohibited but no anchors using it'() {
151 when: 'a schema set is deleted with cascade-prohibited option'
152 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
153 CASCADE_DELETE_PROHIBITED)
154 then: 'the schema set has been deleted'
155 schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
156 and: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
157 def orphanedResourceId = 3100L
158 yangResourceRepository.findById(orphanedResourceId).isPresent() == false
159 and: 'any shared (still in use by other schema set) yang resources still persists'
160 def sharedResourceId = 3003L
161 yangResourceRepository.findById(sharedResourceId).isPresent()
164 @Sql([CLEAR_DATA, SET_DATA])
165 def 'Delete schema set with cascade allowed.'() {
166 when: 'a schema set is deleted with cascade-allowed option'
167 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA,
168 CASCADE_DELETE_ALLOWED)
169 then: 'the schema set has been deleted'
171 .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent() == false
172 and: 'the associated anchors are removed'
173 def associatedAnchorsIds = [ 6001, 6002 ]
174 associatedAnchorsIds.each {anchorRepository.findById(it).isPresent() == false }
175 and: 'the fragment(s) under those anchors are removed'
176 def fragmentUnderAnchor1Id = 7001L
177 fragmentRepository.findById(fragmentUnderAnchor1Id).isPresent() == false
178 and: 'the shared resources still persist'
179 def sharedResourceIds = [ 3003L, 3004L ]
180 sharedResourceIds.each {yangResourceRepository.findById(it).isPresent() }
183 @Sql([CLEAR_DATA, SET_DATA])
184 def 'Delete schema set error scenario: #scenario.'() {
185 when: 'attempt to delete a schema set where #scenario'
186 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
187 then: 'an #expectedException is thrown'
188 thrown(expectedException)
189 where: 'the following data is used'
190 scenario | dataspaceName | schemaSetName || expectedException
191 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
192 'schema set does not exists' | DATASPACE_NAME | 'unknown' || SchemaSetNotFoundException
193 'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
196 def assertSchemaSetPersisted(expectedDataspaceName,
197 expectedSchemaSetName,
198 expectedYangResourceId,
199 expectedYangResourceName,
200 expectedYangResourceContent,
201 expectedYangResourceChecksum,
202 expectedYangResourceModuleName,
203 expectedYangResourceRevision) {
204 // assert the schema set is persisted
205 def schemaSetEntity = schemaSetRepository
206 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
207 assert schemaSetEntity.name == expectedSchemaSetName
208 assert schemaSetEntity.dataspace.name == expectedDataspaceName
210 // assert the attached yang resource is persisted
211 def yangResourceEntities = schemaSetEntity.getYangResources()
212 yangResourceEntities.size() == 1
214 // assert the attached yang resource content
215 YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
216 assert yangResourceEntity.id != null
217 if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
218 // existing resource with known id
219 assert yangResourceEntity.id == expectedYangResourceId
221 yangResourceEntity.name == expectedYangResourceName
222 yangResourceEntity.content == expectedYangResourceContent
223 yangResourceEntity.checksum == expectedYangResourceChecksum
224 yangResourceEntity.moduleName == expectedYangResourceModuleName
225 yangResourceEntity.revision == expectedYangResourceRevision