2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021-2022 Bell Canada.
5 * Modifications Copyright (C) 2022 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the 'License');
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an 'AS IS' BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.impl
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.AlreadyDefinedException
28 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
29 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
30 import org.onap.cps.spi.model.ModuleDefinition
31 import org.onap.cps.spi.model.ModuleReference
32 import org.onap.cps.spi.model.SchemaSet
33 import org.onap.cps.spi.repository.AnchorRepository
34 import org.onap.cps.spi.repository.SchemaSetRepository
35 import org.onap.cps.spi.repository.SchemaSetYangResourceRepositoryImpl
36 import org.onap.cps.spi.repository.YangResourceRepository
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.test.context.jdbc.Sql
39 import spock.lang.Ignore
41 class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
44 CpsModulePersistenceService objectUnderTest
47 AnchorRepository anchorRepository
50 SchemaSetRepository schemaSetRepository
53 CpsAdminPersistenceService cpsAdminPersistenceService
56 YangResourceRepository yangResourceRepository
58 final static String SET_DATA = '/data/schemaset.sql'
60 def static EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
61 def SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
62 def NEW_SCHEMA_SET_NAME = 'SCHEMA-SET-NEW'
63 def NEW_RESOURCE_NAME = 'some new resource'
64 def NEW_RESOURCE_CONTENT = 'module stores {\n' +
65 ' yang-version 1.1;\n' +
66 ' namespace "org:onap:ccsdk:sample";\n' +
68 ' prefix book-store;\n' +
70 ' revision "2020-09-15" {\n' +
72 ' "Sample Model";\n' +
75 def NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
76 def NEW_RESOURCE_MODULE_NAME = 'stores'
77 def NEW_RESOURCE_REVISION = '2020-09-15'
78 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
83 dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
86 @Sql([CLEAR_DATA, SET_DATA])
87 def 'Getting yang resource ids from module references'() {
88 when: 'getting yang resources for #scenario'
89 def result = yangResourceRepository.getResourceIdsByModuleReferences(moduleReferences)
90 then: 'the result contains the expected number entries'
91 assert result.size() == expectedResultSize
92 where: 'the following module references are provided'
93 scenario | moduleReferences || expectedResultSize
94 '2 valid module references' | [ new ModuleReference('MODULE-NAME-002','REVISION-002'), new ModuleReference('MODULE-NAME-003','REVISION-002') ] || 2
95 '1 invalid module reference' | [ new ModuleReference('NOT EXIST','IRRELEVANT') ] || 0
96 '1 valid and 1 invalid module reference' | [ new ModuleReference('MODULE-NAME-002','REVISION-002'), new ModuleReference('NOT EXIST','IRRELEVANT') ] || 1
97 'no module references' | [] || 0
100 @Sql([CLEAR_DATA, SET_DATA])
101 def 'Store schema set error scenario: #scenario.'() {
102 when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
103 objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
104 then: 'an #expectedException is thrown'
105 thrown(expectedException)
106 where: 'the following data is used'
107 scenario | dataspaceName | schemaSetName || expectedException
108 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
109 'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException
112 @Sql([CLEAR_DATA, SET_DATA])
113 def 'Store new schema set with one module'() {
114 when: 'a new schema set with one module is stored'
115 objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap)
116 then: 'the schema set is persisted correctly'
117 assertSchemaSetWithOneModuleIsPersistedCorrectly(NEW_RESOURCE_NAME,
118 NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM)
121 @Sql([CLEAR_DATA, SET_DATA])
122 def 'Store new schema set with multiple modules.'() {
123 given: 'a new schema set with #numberOfModules modules'
124 def numberOfModules = 2
125 String schemaSetName = "NewSchemaWith${numberOfModules}Modules"
126 def newYangResourcesNameToContentMap = [:]
127 (1..numberOfModules).each {
128 def uniqueRevision = String.valueOf(2000 + it) + '-01-01'
129 def uniqueContent = NEW_RESOURCE_CONTENT.replace(NEW_RESOURCE_REVISION, uniqueRevision)
130 newYangResourcesNameToContentMap.put(uniqueRevision, uniqueContent)
132 when: 'the new schema set is stored'
133 objectUnderTest.storeSchemaSet(DATASPACE_NAME, schemaSetName, newYangResourcesNameToContentMap)
134 then: 'the correct number of modules are persisted'
135 assert getYangResourceEntities(schemaSetName).size() == numberOfModules
138 @Sql([CLEAR_DATA, SET_DATA])
139 def 'Store and retrieve new schema set from new modules and existing modules.'() {
140 given: 'a new module'
141 def mapOfNewModules = [newModule: 'module newmodule { yang-version 1.1; revision "2022-08-19" { } }']
142 and: 'there are more existing modules in the db than the batch size (100)'
143 def listOfExistingModulesModuleReference = []
144 def mapOfExistingModule = [:]
145 def numberOfModules = 1 + SchemaSetYangResourceRepositoryImpl.MAX_INSERT_BATCH_SIZE
146 (1..numberOfModules).each {
147 def uniqueRevision = String.valueOf(2000 + it) + '-01-01'
148 def uniqueContent = "module test { yang-version 1.1; revision \"${uniqueRevision}\" { } }".toString()
149 mapOfNewModules.put( 'test' + it, uniqueContent)
150 listOfExistingModulesModuleReference.add(new ModuleReference('test',uniqueRevision))
152 objectUnderTest.storeSchemaSet(DATASPACE_NAME, 'existing schema set ', mapOfExistingModule)
153 when: 'a new schema set is created from these new modules and existing modules'
154 objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, NEW_SCHEMA_SET_NAME , mapOfNewModules, listOfExistingModulesModuleReference)
155 then: 'the schema set can be retrieved'
156 def actualYangResourcesMapAfterSchemaSetHasBeenCreated =
157 objectUnderTest.getYangSchemaResources(DATASPACE_NAME, NEW_SCHEMA_SET_NAME)
158 and: 'it has all the new and existing modules'
159 def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule
160 assert actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated
163 @Sql([CLEAR_DATA, SET_DATA])
164 def 'Retrieving schema set (resources) by anchor.'() {
165 given: 'a new schema set is stored'
166 objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap)
167 and: 'an anchor is created with that schema set'
168 cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, ANCHOR_NAME1)
169 when: 'the schema set resources for that anchor is retrieved'
170 def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
171 then: 'the correct resources are returned'
172 result == newYangResourcesNameToContentMap
175 @Sql([CLEAR_DATA, SET_DATA])
176 def 'Retrieving all yang resources module references for the given dataspace.'() {
177 given: 'a dataspace name'
178 def dataspaceName = 'DATASPACE-002'
179 when: 'all yang resources module references are retrieved for the given dataspace name'
180 def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName)
181 then: 'the correct resources are returned'
182 result.sort() == [new ModuleReference(moduleName: 'MODULE-NAME-005', revision: 'REVISION-002'),
183 new ModuleReference(moduleName: 'MODULE-NAME-006', revision: 'REVISION-006')]
186 @Sql([CLEAR_DATA, SET_DATA])
187 def 'Retrieving module names and revisions for the given anchor.'() {
188 given: 'a dataspace name and anchor name'
189 def dataspaceName = 'DATASPACE-001'
190 def anchorName = 'ANCHOR1'
191 when: 'all yang resources module references are retrieved for the given anchor'
192 def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName, anchorName)
193 then: 'the correct module names and revisions are returned'
194 result.sort() == [ new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'),
195 new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')]
198 @Sql([CLEAR_DATA, SET_DATA])
199 def 'Storing duplicate schema content.'() {
200 given: 'a new schema set with a resource with the same content as an existing resource'
201 def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
202 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
203 when: 'the schema set with duplicate resource is stored'
204 objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap)
205 then: 'the schema persisted has the new name and has the same checksum'
206 def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
207 def existingResourceModuleName = 'test'
208 def existingResourceRevision = '2020-09-15'
209 assertSchemaSetWithOneModuleIsPersistedCorrectly(NEW_RESOURCE_NAME, existingResourceModuleName,
210 existingResourceRevision, existingResourceContent, existingResourceChecksum)
213 @Sql([CLEAR_DATA, SET_DATA])
214 def 'Retrieve schema sets for a given dataspace name'() {
215 when: 'the schema set resources for a given dataspace name is retrieved'
216 def result = objectUnderTest.getSchemaSetsByDataspaceName(DATASPACE_NAME)
217 then: 'the correct resources are returned'
218 result.contains(new SchemaSet(name: 'SCHEMA-SET-001', dataspaceName: 'DATASPACE-001'))
221 @Sql([CLEAR_DATA, SET_DATA])
222 def 'Delete schema set'() {
223 when: 'a schema set is deleted with cascade-prohibited option'
224 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
225 then: 'the schema set has been deleted'
226 schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
229 @Sql([CLEAR_DATA, SET_DATA])
230 def 'Identifying new module references where #scenario'() {
231 when: 'identifyNewModuleReferences is called'
232 def result = objectUnderTest.identifyNewModuleReferences(moduleReferences)
233 then: 'the correct module references are returned'
234 assert result.size() == expectedResult.size()
235 assert result.containsAll(expectedResult)
236 where: 'the following data is used'
237 scenario | moduleReferences || expectedResult
238 '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']])
239 'no new module references exist' | [] || []
240 'module references collection is null'| null || []
243 @Sql([CLEAR_DATA, SET_DATA])
244 def 'Delete schema set error scenario: #scenario.'() {
245 when: 'attempt to delete a schema set where #scenario'
246 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName)
247 then: 'an #expectedException is thrown'
248 thrown(expectedException)
249 where: 'the following data is used'
250 scenario | dataspaceName | schemaSetName || expectedException
251 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
252 'schema set does not exists' | DATASPACE_NAME | 'unknown' || SchemaSetNotFoundException
255 @Sql([CLEAR_DATA, SET_DATA])
256 def 'Delete only orphan Yang Resources'() {
257 given: 'a schema set is deleted and and yang resource is not used anymore'
258 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
259 when: 'orphan yang resources are deleted'
260 objectUnderTest.deleteUnusedYangResourceModules()
261 then: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
262 def orphanedResourceId = 3100L
263 yangResourceRepository.findById(orphanedResourceId).isPresent() == false
264 and: 'any shared (still in use by other schema set) yang resources still persists'
265 def sharedResourceId = 3003L
266 yangResourceRepository.findById(sharedResourceId).isPresent()
269 @Sql([CLEAR_DATA, SET_DATA])
270 def 'Retrieving all yang resources module definition for the given dataspace and anchor name.'() {
271 when: 'all yang resources module definitions are retrieved for the given dataspace and anchor name'
272 def result = objectUnderTest.getYangResourceDefinitions('DATASPACE-001', 'ANCHOR3')
273 then: 'the correct module definitions (moduleName, revision and yang resource content) are returned'
274 result.sort() == [new ModuleDefinition('MODULE-NAME-004', 'REVISION-004', 'CONTENT-004')]
277 def assertSchemaSetWithOneModuleIsPersistedCorrectly(expectedYangResourceName,
278 expectedYangResourceModuleName,
279 expectedYangResourceRevision,
280 expectedYangResourceContent,
281 expectedYangResourceChecksum) {
283 // assert the attached yang resource is persisted
284 def yangResourceEntities = getYangResourceEntities(NEW_SCHEMA_SET_NAME)
285 assert yangResourceEntities.size() == 1
287 // assert the attached yang resource content
288 YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
289 assert yangResourceEntity.id != null
290 assert yangResourceEntity.fileName == expectedYangResourceName
291 assert yangResourceEntity.moduleName == expectedYangResourceModuleName
292 assert yangResourceEntity.revision == expectedYangResourceRevision
293 assert yangResourceEntity.content == expectedYangResourceContent
294 assert yangResourceEntity.checksum == expectedYangResourceChecksum
298 def getYangResourceEntities(schemaSetName) {
299 def schemaSetEntity = schemaSetRepository
300 .findByDataspaceAndName(dataspaceEntity, schemaSetName).orElseThrow()
301 return schemaSetEntity.getYangResources()
304 def toModuleReference(moduleReferenceAsMap) {
305 def moduleReferences = [].withDefault { [:] }
306 moduleReferenceAsMap.forEach(property ->
307 property.forEach((moduleName, revision) -> {
308 moduleReferences.add(new ModuleReference(moduleName, revision))
310 return moduleReferences