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