2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the 'License');
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an 'AS IS' BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration.functional
23 import org.onap.cps.api.impl.CpsModuleServiceImpl
24 import org.onap.cps.integration.base.FunctionalSpecBase
25 import org.onap.cps.spi.CascadeDeleteAllowed
26 import org.onap.cps.spi.exceptions.AlreadyDefinedException
27 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
28 import org.onap.cps.spi.exceptions.ModelValidationException
29 import org.onap.cps.spi.exceptions.SchemaSetInUseException
30 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
31 import org.onap.cps.spi.model.ModuleDefinition
32 import org.onap.cps.spi.model.ModuleReference
34 class CpsModuleServiceIntegrationSpec extends FunctionalSpecBase {
36 CpsModuleServiceImpl objectUnderTest
38 private static def originalNumberOfModuleReferences = 1
39 private static def existingModuleReference = new ModuleReference('stores','2020-09-15')
40 static def NEW_RESOURCE_REVISION = '2023-05-10'
41 static def NEW_RESOURCE_CONTENT = 'module test_module {\n' +
42 ' yang-version 1.1;\n' +
43 ' namespace "org:onap:ccsdk:sample";\n' +
45 ' prefix book-store;\n' +
47 ' revision "2023-05-10" {\n' +
49 ' "Sample Model";\n' +
53 def newYangResourcesNameToContentMap = [:]
54 def moduleReferences = []
55 def noNewModules = [:]
58 objectUnderTest = cpsModuleService
62 C R E A T E S C H E M A S E T U S E - C A S E S
65 def 'Create new schema set from yang resources with #scenario'() {
66 given: 'a new schema set with #numberOfModules modules'
67 populateNewYangResourcesNameToContentMapAndAllModuleReferences(numberOfNewModules)
68 when: 'the new schema set is created'
69 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', newYangResourcesNameToContentMap)
70 then: 'the number of module references has increased by #expectedIncrease'
71 def yangResourceModuleReferences = objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1)
72 originalNumberOfModuleReferences + numberOfNewModules == yangResourceModuleReferences.size()
74 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, [ 'newSchemaSet' ])
75 where: 'the following parameters are use'
76 scenario | numberOfNewModules
77 'two valid new modules' | 2
78 'empty schema set' | 0
79 'over max batch size #modules' | 101
82 def 'Create new schema set with recommended filename format but invalid yang'() {
83 given: 'a filename using RFC6020 recommended format (for coverage only)'
84 def fileName = 'test@2023-05-11.yang'
85 when: 'attempt to create a schema set with invalid Yang'
86 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', [(fileName) :'invalid yang'])
87 then: 'a model validation exception'
88 thrown(ModelValidationException)
91 def 'Create new schema set from modules with #scenario'() {
92 given: 'a new schema set with #numberOfNewModules modules'
93 populateNewYangResourcesNameToContentMapAndAllModuleReferences(numberOfNewModules)
94 and: 'add existing module references (optional)'
95 moduleReferences.addAll(existingModuleReferences)
96 when: 'the new schema set is created'
97 def schemaSetName = "NewSchemaWith${numberOfNewModules}Modules"
98 objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, schemaSetName, newYangResourcesNameToContentMap, moduleReferences)
99 and: 'associated with a new anchor'
100 cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, schemaSetName, 'newAnchor')
101 then: 'the new anchor has the correct number of modules'
102 def yangResourceModuleReferences = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'newAnchor')
103 assert expectedNumberOfModulesForAnchor == yangResourceModuleReferences.size()
105 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, [ schemaSetName.toString() ])
106 where: 'the following module references are provided'
107 scenario | numberOfNewModules | existingModuleReferences || expectedNumberOfModulesForAnchor
108 'empty schema set' | 0 | [ ] || 0
109 'one existing module' | 0 | [ existingModuleReference ] || 1
110 'two new modules' | 2 | [ ] || 2
111 'two new modules, one existing' | 2 | [ existingModuleReference ] || 3
112 'over max batch size #modules' | 101 | [ ] || 101
113 'two valid, one invalid module' | 2 | [ new ModuleReference('NOT EXIST','IRRELEVANT') ] || 2
116 def 'Duplicate schema content.'() {
117 given: 'a map of yang resources'
118 populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
119 when: 'a new schema set is created'
120 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchema1', newYangResourcesNameToContentMap)
121 then: 'the dataspace has one new module (reference)'
122 def numberOfModuleReferencesAfterFirstSchemaSetHasBeenAdded = objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1).size()
123 assert numberOfModuleReferencesAfterFirstSchemaSetHasBeenAdded == originalNumberOfModuleReferences + 1
124 when: 'a second new schema set is created'
125 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchema2', newYangResourcesNameToContentMap)
126 then: 'the dataspace has no additional module (reference)'
127 assert numberOfModuleReferencesAfterFirstSchemaSetHasBeenAdded == objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1).size()
129 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, [ 'newSchema1', 'newSchema2'])
132 def 'Create schema set error scenario: #scenario.'() {
133 when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
134 populateNewYangResourcesNameToContentMapAndAllModuleReferences(0)
135 objectUnderTest.createSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
136 then: 'an #expectedException is thrown'
137 thrown(expectedException)
138 where: 'the following data is used'
139 scenario | dataspaceName | schemaSetName || expectedException
140 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
141 'schema set already exists' | FUNCTIONAL_TEST_DATASPACE_1 | BOOKSTORE_SCHEMA_SET || AlreadyDefinedException
145 R E A D S C H E M A S E T I N F O U S E - C A S E S
148 def 'Retrieving module definitions by anchor.'() {
149 when: 'the module definitions for an anchor are retrieved'
150 def result = objectUnderTest.getModuleDefinitionsByAnchorName(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1)
151 then: 'the correct module definitions are returned'
152 result == [new ModuleDefinition('stores','2020-09-15','')]
155 def 'Retrieving yang resource module references by anchor.'() {
156 when: 'the yang resource module references for an anchor are retrieved'
157 def result = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1)
158 then: 'the correct module references are returned'
159 result == [ existingModuleReference ]
162 def 'Identifying new module references with #scenario'() {
163 when: 'identifyNewModuleReferences is called'
164 def result = objectUnderTest.identifyNewModuleReferences(moduleReferences)
165 then: 'the correct module references are returned'
166 assert result.size() == expectedResult.size()
167 assert result.containsAll(expectedResult)
168 where: 'the following data is used'
169 scenario | moduleReferences || expectedResult
170 'just new module references' | [new ModuleReference('new1', 'r1'), new ModuleReference('new2', 'r1')] || [new ModuleReference('new1', 'r1'), new ModuleReference('new2', 'r1')]
171 'one new module,one existing reference' | [new ModuleReference('new1', 'r1'), existingModuleReference] || [new ModuleReference('new1', 'r1')]
172 'no new module references' | [existingModuleReference] || []
173 'no module references' | [] || []
174 'module references collection is null' | null || []
177 def 'Retrieve schema set.'() {
178 when: 'a specific schema set is retreived'
179 def result = objectUnderTest.getSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_SCHEMA_SET)
180 then: 'the result has the correct name and module(s)'
181 assert result.name == 'bookstoreSchemaSet'
182 assert result.moduleReferences == [ new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample') ]
185 def 'Retrieve all schema sets.'() {
186 given: 'an extra schema set is stored'
187 populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
188 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchema1', newYangResourcesNameToContentMap)
189 when: 'all schema sets are retrieved'
190 def result = objectUnderTest.getSchemaSets(FUNCTIONAL_TEST_DATASPACE_1)
191 then: 'the result contains all expected schema sets'
192 assert result.name == [ 'bookstoreSchemaSet', 'newSchema1' ]
194 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchema1'])
198 D E L E T E S C H E M A S E T U S E - C A S E S
201 def 'Delete schema sets with(out) cascade.'() {
202 given: 'a schema set'
203 populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
204 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', newYangResourcesNameToContentMap)
205 and: 'optionally create anchor for the schema set'
206 if (associateWithAnchor) {
207 cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', 'newAnchor')
209 when: 'attempt to delete the schema set'
211 objectUnderTest.deleteSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', cascadeDeleteAllowedOption)
213 catch (Exception e) { // only accept correct exception when schema set cannot be deleted
214 assert e instanceof SchemaSetInUseException && expectSchemaSetStillPresent
216 then: 'check if the dataspace still contains the new schema set or not'
217 def remainingSchemaSetNames = objectUnderTest.getSchemaSets(FUNCTIONAL_TEST_DATASPACE_1).name
218 assert remainingSchemaSetNames.contains('newSchemaSet') == expectSchemaSetStillPresent
220 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchemaSet'])
221 where: 'the following options are used'
222 associateWithAnchor | cascadeDeleteAllowedOption || expectSchemaSetStillPresent
223 false | CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED || false
224 false | CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED || false
225 true | CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED || false
226 true | CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED || true
229 def 'Delete schema sets with shared resources.'() {
230 given: 'a new schema set'
231 populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
232 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet1', newYangResourcesNameToContentMap)
233 and: 'another schema set which shares one yang resource (module)'
234 populateNewYangResourcesNameToContentMapAndAllModuleReferences(2)
235 objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet2', newYangResourcesNameToContentMap)
236 when: 'all schema sets are retrieved'
237 def moduleRevisions = objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1).revision
238 then: 'both modules (revisions) are present'
239 assert moduleRevisions.containsAll(['2000-01-01', '2000-01-01'])
240 when: 'delete the second schema set that has two resources one of which is a shared resource'
241 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchemaSet2'])
242 then: 'only the second schema set is deleted'
243 def remainingSchemaSetNames = objectUnderTest.getSchemaSets(FUNCTIONAL_TEST_DATASPACE_1).name
244 assert remainingSchemaSetNames.contains('newSchemaSet1')
245 assert !remainingSchemaSetNames.contains('newSchemaSet2')
246 and: 'only the shared module (revision) remains'
247 def remainingModuleRevisions = objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1).revision
248 assert remainingModuleRevisions.contains('2000-01-01')
249 assert !remainingModuleRevisions.contains('2001-01-01')
251 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchemaSet1'])
254 def 'Delete schema set error scenario: #scenario.'() {
255 when: 'attempt to delete a schema set where #scenario'
256 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
257 then: 'an #expectedException is thrown'
258 thrown(expectedException)
259 where: 'the following data is used'
260 scenario | dataspaceName | schemaSetName || expectedException
261 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
262 'schema set does not exists' | FUNCTIONAL_TEST_DATASPACE_1 | 'unknown' || SchemaSetNotFoundException
269 def 'Upgrade schema set (with existing and new modules, no matching module set tag in NCMP)'() {
270 given: 'an anchor and schema set with 2 modules (to be upgraded)'
271 populateNewYangResourcesNameToContentMapAndAllModuleReferences('original', 2)
272 objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', newYangResourcesNameToContentMap, [])
273 cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', 'targetAnchor')
274 def yangResourceModuleReferencesBeforeUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
275 assert yangResourceModuleReferencesBeforeUpgrade.size() == 2
276 assert yangResourceModuleReferencesBeforeUpgrade.containsAll([new ModuleReference('original_0','2000-01-01'),new ModuleReference('original_1','2001-01-01')])
277 and: 'two new 2 modules (from node)'
278 populateNewYangResourcesNameToContentMapAndAllModuleReferences('new', 2)
279 def newModuleReferences = [new ModuleReference('new_0','2000-01-01'),new ModuleReference('new_1','2001-01-01')]
280 and: 'a list of all module references (normally retrieved from node)'
281 def allModuleReferences = []
282 allModuleReferences.add(existingModuleReference)
283 allModuleReferences.addAll(newModuleReferences)
284 when: 'the schema set is upgraded'
285 objectUnderTest.upgradeSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', newYangResourcesNameToContentMap, allModuleReferences)
286 then: 'the new anchor has the correct new and existing modules'
287 def yangResourceModuleReferencesAfterUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
288 assert yangResourceModuleReferencesAfterUpgrade.size() == 3
289 assert yangResourceModuleReferencesAfterUpgrade.contains(existingModuleReference)
290 assert yangResourceModuleReferencesAfterUpgrade.containsAll(newModuleReferences);
292 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['targetSchema'])
295 def 'Upgrade existing schema set from another anchor (used in NCMP for matching module set tag)'() {
296 given: 'an anchor and schema set with 1 module (target)'
297 populateNewYangResourcesNameToContentMapAndAllModuleReferences('target', 1)
298 objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', newYangResourcesNameToContentMap, [])
299 cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', 'targetAnchor')
300 def moduleReferencesBeforeUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
301 assert moduleReferencesBeforeUpgrade.size() == 1
302 and: 'another anchor and schema set with 2 other modules (source for upgrade)'
303 populateNewYangResourcesNameToContentMapAndAllModuleReferences('source', 2)
304 objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'sourceSchema', newYangResourcesNameToContentMap, [])
305 cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'sourceSchema', 'sourceAnchor')
306 assert objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'sourceAnchor').size() == 2
307 when: 'the target schema is upgraded using the module references from the source anchor'
308 def moduleReferencesFromSourceAnchor = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'sourceAnchor')
309 objectUnderTest.upgradeSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', noNewModules, moduleReferencesFromSourceAnchor)
310 then: 'the target schema now refers to the source modules (with namespace) modules'
311 def schemaSetModuleReferencesAfterUpgrade = getObjectUnderTest().getSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema').moduleReferences
312 assert schemaSetModuleReferencesAfterUpgrade.containsAll([new ModuleReference('source_0','2000-01-01','org:onap:ccsdk:sample'),new ModuleReference('source_1','2001-01-01','org:onap:ccsdk:sample')]);
313 and: 'the associated target anchor has the same module references (without namespace but that is a legacy issue)'
314 def anchorModuleReferencesAfterUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
315 assert anchorModuleReferencesAfterUpgrade.containsAll([new ModuleReference('source_0','2000-01-01'),new ModuleReference('source_1','2001-01-01')]);
317 objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['sourceSchema', 'targetSchema'])
321 H E L P E R M E T H O D S
324 def populateNewYangResourcesNameToContentMapAndAllModuleReferences(numberOfModules) {
325 populateNewYangResourcesNameToContentMapAndAllModuleReferences('name', numberOfModules)
328 def populateNewYangResourcesNameToContentMapAndAllModuleReferences(namePrefix, numberOfModules) {
329 numberOfModules.times {
330 def uniqueName = namePrefix + '_' + it
331 def uniqueRevision = String.valueOf(2000 + it) + '-01-01'
332 moduleReferences.add(new ModuleReference(uniqueName, uniqueRevision))
333 def uniqueContent = NEW_RESOURCE_CONTENT.replace(NEW_RESOURCE_REVISION, uniqueRevision).replace('module test_module', 'module '+uniqueName)
334 newYangResourcesNameToContentMap.put(uniqueRevision, uniqueContent)