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