Replace sleep with PollingConditions
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / CpsModuleServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.functional
22
23 import org.onap.cps.api.CpsModuleService
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
33
34 class CpsModuleServiceIntegrationSpec extends FunctionalSpecBase {
35
36     CpsModuleService objectUnderTest
37
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' +
44         '\n' +
45         '    prefix book-store;\n' +
46         '\n' +
47         '    revision "2023-05-10" {\n' +
48         '        description\n' +
49         '        "Sample Model";\n' +
50         '    }' +
51         '}'
52
53     def newYangResourcesNameToContentMap = [:]
54     def moduleReferences = []
55     def noNewModules = [:]
56
57     def setup() {
58         objectUnderTest = cpsModuleService
59     }
60
61     /*
62         C R E A T E   S C H E M A   S E T   U S E - C A S E S
63      */
64
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()
73         cleanup:
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
80     }
81
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)
89     }
90
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             cpsAnchorService.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()
104         cleanup:
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
114     }
115
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()
128         cleanup:
129             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, [ 'newSchema1', 'newSchema2'])
130     }
131
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
142     }
143
144     /*
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
146      */
147
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','')]
153     }
154
155     def 'Retrieving module definitions: #scenarios'() {
156         when: 'module definitions for module name are retrieved'
157             def result = objectUnderTest.getModuleDefinitionsByAnchorAndModule(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, moduleName, moduleRevision)
158         then: 'the correct module definitions are returned'
159             result == [new ModuleDefinition('stores','2020-09-15','')]
160         where: 'following parameters are used'
161             scenarios               | moduleName | moduleRevision
162             'both specified'        | 'stores'   | '2020-09-15'
163             'module name specified' | 'stores'   | null
164     }
165
166     def 'Retrieving yang resource module references by anchor.'() {
167         when: 'the yang resource module references for an anchor are retrieved'
168             def result = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1)
169         then: 'the correct module references are returned'
170             result == [ existingModuleReference ]
171     }
172
173     def 'Identifying new module references with #scenario'() {
174         when: 'identifyNewModuleReferences is called'
175             def result = objectUnderTest.identifyNewModuleReferences(moduleReferences)
176         then: 'the correct module references are returned'
177             assert result.size() == expectedResult.size()
178             assert result.containsAll(expectedResult)
179         where: 'the following data is used'
180             scenario                                | moduleReferences                                                       || expectedResult
181             'just new module references'            | [new ModuleReference('new1', 'r1'), new ModuleReference('new2', 'r1')] || [new ModuleReference('new1', 'r1'), new ModuleReference('new2', 'r1')]
182             'one new module,one existing reference' | [new ModuleReference('new1', 'r1'), existingModuleReference]           || [new ModuleReference('new1', 'r1')]
183             'no new module references'              | [existingModuleReference]                                              || []
184             'no module references'                  | []                                                                     || []
185             'module references collection is null'  | null                                                                   || []
186     }
187
188     def 'Retrieve schema set.'() {
189         when: 'a specific schema set is retreived'
190             def result = objectUnderTest.getSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_SCHEMA_SET)
191         then: 'the result has the correct name and module(s)'
192             assert result.name == 'bookstoreSchemaSet'
193             assert result.moduleReferences == [ new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample') ]
194     }
195
196     def 'Retrieve all schema sets.'() {
197         given: 'an extra schema set is stored'
198             populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
199             objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchema1', newYangResourcesNameToContentMap)
200         when: 'all schema sets are retrieved'
201             def result = objectUnderTest.getSchemaSets(FUNCTIONAL_TEST_DATASPACE_1)
202         then: 'the result contains all expected schema sets'
203             assert result.name == [ 'bookstoreSchemaSet', 'newSchema1' ]
204         cleanup:
205             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchema1'])
206     }
207
208     /*
209         D E L E T E   S C H E M A   S E T   U S E - C A S E S
210      */
211
212     def 'Delete schema sets with(out) cascade.'() {
213         given: 'a schema set'
214             populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
215             objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', newYangResourcesNameToContentMap)
216         and: 'optionally create anchor for the schema set'
217             if (associateWithAnchor) {
218                 cpsAnchorService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', 'newAnchor')
219             }
220         when: 'attempt to delete the schema set'
221             try {
222                 objectUnderTest.deleteSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet', cascadeDeleteAllowedOption)
223             }
224             catch (Exception e) {  // only accept correct exception when schema set cannot be deleted
225                 assert e instanceof SchemaSetInUseException && expectSchemaSetStillPresent
226             }
227         then: 'check if the dataspace still contains the new schema set or not'
228             def remainingSchemaSetNames = objectUnderTest.getSchemaSets(FUNCTIONAL_TEST_DATASPACE_1).name
229             assert remainingSchemaSetNames.contains('newSchemaSet') == expectSchemaSetStillPresent
230         cleanup:
231             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchemaSet'])
232         where: 'the following options are used'
233             associateWithAnchor | cascadeDeleteAllowedOption                     || expectSchemaSetStillPresent
234             false               | CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED    || false
235             false               | CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED || false
236             true                | CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED    || false
237             true                | CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED || true
238     }
239
240     def 'Delete schema sets with shared resources.'() {
241         given: 'a new schema set'
242             populateNewYangResourcesNameToContentMapAndAllModuleReferences(1)
243             objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet1', newYangResourcesNameToContentMap)
244         and: 'another schema set which shares one yang resource (module)'
245             populateNewYangResourcesNameToContentMapAndAllModuleReferences(2)
246             objectUnderTest.createSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'newSchemaSet2', newYangResourcesNameToContentMap)
247         when: 'all schema sets are retrieved'
248             def moduleRevisions = objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1).revision
249         then: 'both modules (revisions) are present'
250             assert moduleRevisions.containsAll(['2000-01-01', '2000-01-01'])
251         when: 'delete the second schema set that has two resources  one of which is a shared resource'
252             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchemaSet2'])
253         then: 'only the second schema set is deleted'
254             def remainingSchemaSetNames = objectUnderTest.getSchemaSets(FUNCTIONAL_TEST_DATASPACE_1).name
255             assert remainingSchemaSetNames.contains('newSchemaSet1')
256             assert !remainingSchemaSetNames.contains('newSchemaSet2')
257         and: 'only the shared module (revision) remains'
258             def remainingModuleRevisions = objectUnderTest.getYangResourceModuleReferences(FUNCTIONAL_TEST_DATASPACE_1).revision
259             assert remainingModuleRevisions.contains('2000-01-01')
260             assert !remainingModuleRevisions.contains('2001-01-01')
261         cleanup:
262             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['newSchemaSet1'])
263     }
264
265     def 'Delete schema set error scenario: #scenario.'() {
266         when: 'attempt to delete a schema set where #scenario'
267             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
268         then: 'an #expectedException is thrown'
269             thrown(expectedException)
270         where: 'the following data is used'
271             scenario                     | dataspaceName               | schemaSetName   || expectedException
272             'dataspace does not exist'   | 'unknown'                   | 'not-relevant'  || DataspaceNotFoundException
273             'schema set does not exists' | FUNCTIONAL_TEST_DATASPACE_1 | 'unknown'       || SchemaSetNotFoundException
274     }
275
276     /*
277         U P G R A D E
278      */
279
280     def 'Upgrade schema set (with existing and new modules, no matching module set tag in NCMP)'() {
281         given: 'an anchor and schema set with 2 modules (to be upgraded)'
282             populateNewYangResourcesNameToContentMapAndAllModuleReferences('original', 2)
283             objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', newYangResourcesNameToContentMap, [])
284             cpsAnchorService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', 'targetAnchor')
285             def yangResourceModuleReferencesBeforeUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
286             assert yangResourceModuleReferencesBeforeUpgrade.size() == 2
287             assert yangResourceModuleReferencesBeforeUpgrade.containsAll([new ModuleReference('original_0','2000-01-01'),new ModuleReference('original_1','2001-01-01')])
288         and: 'two new 2 modules (from node)'
289             populateNewYangResourcesNameToContentMapAndAllModuleReferences('new', 2)
290             def newModuleReferences = [new ModuleReference('new_0','2000-01-01'),new ModuleReference('new_1','2001-01-01')]
291         and: 'a list of all module references (normally retrieved from node)'
292             def allModuleReferences = []
293             allModuleReferences.add(existingModuleReference)
294             allModuleReferences.addAll(newModuleReferences)
295         when: 'the schema set is upgraded'
296             objectUnderTest.upgradeSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', newYangResourcesNameToContentMap, allModuleReferences)
297         then: 'the new anchor has the correct new and existing modules'
298             def yangResourceModuleReferencesAfterUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
299             assert yangResourceModuleReferencesAfterUpgrade.size() == 3
300             assert yangResourceModuleReferencesAfterUpgrade.contains(existingModuleReference)
301             assert yangResourceModuleReferencesAfterUpgrade.containsAll(newModuleReferences);
302         cleanup:
303             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['targetSchema'])
304     }
305
306     def 'Upgrade existing schema set from another anchor (used in NCMP for matching module set tag)'() {
307         given: 'an anchor and schema set with 1 module (target)'
308             populateNewYangResourcesNameToContentMapAndAllModuleReferences('target', 1)
309             objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', newYangResourcesNameToContentMap, [])
310             cpsAnchorService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', 'targetAnchor')
311             def moduleReferencesBeforeUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
312             assert moduleReferencesBeforeUpgrade.size() == 1
313         and: 'another anchor and schema set with 2 other modules (source for upgrade)'
314             populateNewYangResourcesNameToContentMapAndAllModuleReferences('source', 2)
315             objectUnderTest.createSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'sourceSchema', newYangResourcesNameToContentMap, [])
316             cpsAnchorService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, 'sourceSchema', 'sourceAnchor')
317             assert objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'sourceAnchor').size() == 2
318         when: 'the target schema is upgraded using the module references from the source anchor'
319             def moduleReferencesFromSourceAnchor = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'sourceAnchor')
320             objectUnderTest.upgradeSchemaSetFromModules(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema', noNewModules, moduleReferencesFromSourceAnchor)
321         then: 'the target schema now refers to the source modules (with namespace) modules'
322             def schemaSetModuleReferencesAfterUpgrade = getObjectUnderTest().getSchemaSet(FUNCTIONAL_TEST_DATASPACE_1, 'targetSchema').moduleReferences
323             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')]);
324         and: 'the associated target anchor has the same module references (without namespace but that is a legacy issue)'
325             def anchorModuleReferencesAfterUpgrade = objectUnderTest.getYangResourcesModuleReferences(FUNCTIONAL_TEST_DATASPACE_1, 'targetAnchor')
326             assert anchorModuleReferencesAfterUpgrade.containsAll([new ModuleReference('source_0','2000-01-01'),new ModuleReference('source_1','2001-01-01')]);
327         cleanup:
328             objectUnderTest.deleteSchemaSetsWithCascade(FUNCTIONAL_TEST_DATASPACE_1, ['sourceSchema', 'targetSchema'])
329     }
330
331     /*
332         H E L P E R   M E T H O D S
333      */
334
335     def populateNewYangResourcesNameToContentMapAndAllModuleReferences(numberOfModules) {
336         populateNewYangResourcesNameToContentMapAndAllModuleReferences('name', numberOfModules)
337     }
338
339     def populateNewYangResourcesNameToContentMapAndAllModuleReferences(namePrefix, numberOfModules) {
340         numberOfModules.times {
341             def uniqueName = namePrefix + '_' + it
342             def uniqueRevision = String.valueOf(2000 + it) + '-01-01'
343             moduleReferences.add(new ModuleReference(uniqueName, uniqueRevision))
344             def uniqueContent = NEW_RESOURCE_CONTENT.replace(NEW_RESOURCE_REVISION, uniqueRevision).replace('module test_module', 'module '+uniqueName)
345             newYangResourcesNameToContentMap.put(uniqueRevision, uniqueContent)
346         }
347     }
348
349 }