2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021-2022 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 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.SchemaSetNotFoundException
29 import org.onap.cps.spi.model.ModuleReference
30 import org.onap.cps.spi.model.ExtendedModuleReference
31 import org.onap.cps.spi.repository.AnchorRepository
32 import org.onap.cps.spi.repository.ModuleReferenceRepository
33 import org.onap.cps.spi.repository.SchemaSetRepository
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.jdbc.Sql
37 class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
40 CpsModulePersistenceService objectUnderTest
43 AnchorRepository anchorRepository
46 SchemaSetRepository schemaSetRepository
49 CpsAdminPersistenceService cpsAdminPersistenceService
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_NEW = 'SCHEMA-SET-NEW'
56 static final String NEW_RESOURCE_NAME = 'some new resource'
57 static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
58 ' yang-version 1.1;\n' +
59 ' namespace "org:onap:ccsdk:sample";\n' +
61 ' prefix book-store;\n' +
63 ' revision "2020-09-15" {\n' +
65 ' "Sample Model";\n' +
68 static final String NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
69 static final String NEW_RESOURCE_MODULE_NAME = 'stores'
70 static final String NEW_RESOURCE_REVISION = '2020-09-15'
71 static final ExtendedModuleReference newModuleReference = ExtendedModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME)
72 .revision(NEW_RESOURCE_REVISION).build()
74 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
79 dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
82 @Sql([CLEAR_DATA, SET_DATA])
83 def 'Store schema set error scenario: #scenario.'() {
84 when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
85 objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
86 then: 'an #expectedException is thrown'
87 thrown(expectedException)
88 where: 'the following data is used'
89 scenario | dataspaceName | schemaSetName || expectedException
90 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
91 'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException
94 @Sql([CLEAR_DATA, SET_DATA])
95 def 'Store new schema set.'() {
96 when: 'a new schemaset is stored'
97 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
98 then: 'the schema set is persisted correctly'
99 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_NAME,
100 NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM, NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION)
103 @Sql([CLEAR_DATA, SET_DATA])
104 def 'Store and retrieve new schema set from new modules and existing modules.'() {
105 given: 'map of new modules, a list of existing modules, module reference'
106 def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }']
107 def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12")
108 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
109 def mapOfExistingModule = [test: 'module test { yang-version 1.1; revision "2021-10-12" { } }']
110 objectUnderTest.storeSchemaSet(DATASPACE_NAME, "someSchemaSetName", mapOfExistingModule)
111 when: 'a new schema set is created from these new modules and existing modules'
112 objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference)
113 then: 'the schema set can be retrieved'
114 def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule
115 def actualYangResourcesMapAfterSchemaSetHasBeenCreated =
116 objectUnderTest.getYangSchemaResources(DATASPACE_NAME, "newSchemaSetName")
117 actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated
120 @Sql([CLEAR_DATA, SET_DATA])
121 def 'Retrieving schema set (resources) by anchor.'() {
122 given: 'a new schema set is stored'
123 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
124 and: 'an anchor is created with that schema set'
125 cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
126 when: 'the schema set resources for that anchor is retrieved'
127 def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
128 then: 'the correct resources are returned'
129 result == newYangResourcesNameToContentMap
132 @Sql([CLEAR_DATA, SET_DATA])
133 def 'Retrieving all yang resources module references for the given dataspace.'() {
134 given: 'a dataspace name'
135 def dataspaceName = 'DATASPACE-002'
136 when: 'all yang resources module references are retrieved for the given dataspace name'
137 def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName)
138 then: 'the correct resources are returned'
139 result.sort() == [new ModuleReference(moduleName: 'MODULE-NAME-005', revision: 'REVISION-002'),
140 new ModuleReference(moduleName: 'MODULE-NAME-006', revision: 'REVISION-006')]
143 @Sql([CLEAR_DATA, SET_DATA])
144 def 'Retrieving module names and revisions for the given anchor.'() {
145 given: 'a dataspace name and anchor name'
146 def dataspaceName = 'DATASPACE-001'
147 def anchorName = 'ANCHOR1'
148 when: 'all yang resources module references are retrieved for the given anchor'
149 def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName, anchorName)
150 then: 'the correct module names and revisions are returned'
151 result.sort() == [ new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'),
152 new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')]
155 @Sql([CLEAR_DATA, SET_DATA])
156 def 'Storing duplicate schema content.'() {
157 given: 'a new schema set with a resource with the same content as an existing resource'
158 def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
159 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
160 when: 'the schema set with duplicate resource is stored'
161 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
162 then: 'the schema persisted (re)uses the existing name and has the same checksum'
163 def existingResourceName = 'module1@2020-02-02.yang'
164 def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
165 def existingResourceModelName = 'test'
166 def existingResourceRevision = '2020-09-15'
167 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, existingResourceName,
168 existingResourceContent, existingResourceChecksum,
169 existingResourceModelName, existingResourceRevision)
172 @Sql([CLEAR_DATA, SET_DATA])
173 def 'Delete schema set'() {
174 when: 'a schema set is deleted with cascade-prohibited option'
175 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
176 then: 'the schema set has been deleted'
177 schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
180 @Sql([CLEAR_DATA, SET_DATA])
181 def 'Identifying new module references where #scenario'() {
182 when: 'identifyNewModuleReferences is called'
183 def result = objectUnderTest.identifyNewModuleReferences(moduleReferences)
184 then: 'the correct module reference collection is returned'
185 assert result == expectedResult
186 where: 'the following data is used'
187 scenario | moduleReferences || expectedResult
188 'new module references exist' | toModuleReference([['some module 1' : 'some revision 1'], ['some module 2' : 'some revision 2']]) || toModuleReference([['some module 1' : 'some revision 1'], ['some module 2' : 'some revision 2']])
189 'no new module references exist' | [] || []
190 'module references collection is null'| null || []
193 @Sql([CLEAR_DATA, SET_DATA])
194 def 'Delete schema set error scenario: #scenario.'() {
195 when: 'attempt to delete a schema set where #scenario'
196 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName)
197 then: 'an #expectedException is thrown'
198 thrown(expectedException)
199 where: 'the following data is used'
200 scenario | dataspaceName | schemaSetName || expectedException
201 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
202 'schema set does not exists' | DATASPACE_NAME | 'unknown' || SchemaSetNotFoundException
205 @Sql([CLEAR_DATA, SET_DATA])
206 def 'Delete only orphan Yang Resources'() {
207 given: 'a schema set is deleted and and yang resource is not used anymore'
208 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
209 when: 'orphan yang resources are deleted'
210 objectUnderTest.deleteUnusedYangResourceModules()
211 then: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
212 def orphanedResourceId = 3100L
213 yangResourceRepository.findById(orphanedResourceId).isPresent() == false
214 and: 'any shared (still in use by other schema set) yang resources still persists'
215 def sharedResourceId = 3003L
216 yangResourceRepository.findById(sharedResourceId).isPresent()
219 def assertSchemaSetPersisted(expectedDataspaceName,
220 expectedSchemaSetName,
221 expectedYangResourceName,
222 expectedYangResourceContent,
223 expectedYangResourceChecksum,
224 expectedYangResourceModuleName,
225 expectedYangResourceRevision) {
226 // assert the schema set is persisted
227 def schemaSetEntity = schemaSetRepository
228 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
229 assert schemaSetEntity.name == expectedSchemaSetName
230 assert schemaSetEntity.dataspace.name == expectedDataspaceName
232 // assert the attached yang resource is persisted
233 def yangResourceEntities = schemaSetEntity.getYangResources()
234 yangResourceEntities.size() == 1
236 // assert the attached yang resource content
237 YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
238 assert yangResourceEntity.id != null
239 yangResourceEntity.name == expectedYangResourceName
240 yangResourceEntity.content == expectedYangResourceContent
241 yangResourceEntity.checksum == expectedYangResourceChecksum
242 yangResourceEntity.moduleName == expectedYangResourceModuleName
243 yangResourceEntity.revision == expectedYangResourceRevision
246 def toModuleReference(moduleReferenceAsMap) {
247 def moduleReferences = [].withDefault { [:] }
248 moduleReferenceAsMap.forEach(property ->
249 property.forEach((moduleName, revision) -> {
250 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
252 return moduleReferences