Merge "Don't handle ancestor-axis using regex quickfind (CPS-1664 #2)"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22 package org.onap.cps.spi.impl
23
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.SchemaSetRepository
34 import org.onap.cps.spi.repository.SchemaSetYangResourceRepositoryImpl
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.jdbc.Sql
37
38 class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
39
40     @Autowired
41     CpsModulePersistenceService objectUnderTest
42
43     @Autowired
44     SchemaSetRepository schemaSetRepository
45
46     @Autowired
47     CpsAdminPersistenceService cpsAdminPersistenceService
48
49     final static String SET_DATA = '/data/schemaset.sql'
50
51     def static EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
52     def SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
53     def NEW_SCHEMA_SET_NAME = 'SCHEMA-SET-NEW'
54     def NEW_RESOURCE_NAME = 'some new resource'
55     def NEW_RESOURCE_CONTENT = 'module stores {\n' +
56             '    yang-version 1.1;\n' +
57             '    namespace "org:onap:ccsdk:sample";\n' +
58             '\n' +
59             '    prefix book-store;\n' +
60             '\n' +
61             '    revision "2020-09-15" {\n' +
62             '        description\n' +
63             '        "Sample Model";\n' +
64             '    }' +
65             '}'
66     def NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
67     def NEW_RESOURCE_MODULE_NAME = 'stores'
68     def NEW_RESOURCE_REVISION = '2020-09-15'
69     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
70
71     def dataspaceEntity
72
73     def setup() {
74         dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
75     }
76
77     @Sql([CLEAR_DATA, SET_DATA])
78     def 'Getting yang resource ids from module references'() {
79         when: 'getting yang resources for #scenario'
80             def result = yangResourceRepository.getResourceIdsByModuleReferences(moduleReferences)
81         then: 'the result contains the expected number entries'
82             assert result.size() == expectedResultSize
83         where: 'the following module references are provided'
84             scenario                                 | moduleReferences                                                                                                 || expectedResultSize
85             '2 valid module references'              | [ new ModuleReference('MODULE-NAME-002','REVISION-002'), new ModuleReference('MODULE-NAME-003','REVISION-002') ] || 2
86             '1 invalid module reference'             | [ new ModuleReference('NOT EXIST','IRRELEVANT') ]                                                                || 0
87             '1 valid and 1 invalid module reference' | [ new ModuleReference('MODULE-NAME-002','REVISION-002'), new ModuleReference('NOT EXIST','IRRELEVANT') ]         || 1
88             'no module references'                   | []                                                                                                               || 0
89     }
90
91     @Sql([CLEAR_DATA, SET_DATA])
92     def 'Store schema set error scenario: #scenario.'() {
93         when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
94             objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
95         then: 'an #expectedException is thrown'
96             thrown(expectedException)
97         where: 'the following data is used'
98             scenario                    | dataspaceName  | schemaSetName            || expectedException
99             'dataspace does not exist'  | 'unknown'      | 'not-relevant'           || DataspaceNotFoundException
100             'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException
101     }
102
103     @Sql([CLEAR_DATA, SET_DATA])
104     def 'Store new schema set with one module'() {
105         when: 'a new schema set with one module is stored'
106             objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap)
107         then: 'the schema set is persisted correctly'
108            assertSchemaSetWithOneModuleIsPersistedCorrectly(NEW_RESOURCE_NAME,
109                NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM)
110     }
111
112     @Sql([CLEAR_DATA, SET_DATA])
113     def 'Store new schema set with multiple modules.'() {
114         given: 'a new schema set with #numberOfModules modules'
115             def numberOfModules =  2
116             String schemaSetName = "NewSchemaWith${numberOfModules}Modules"
117             def newYangResourcesNameToContentMap = [:]
118             (1..numberOfModules).each {
119                 def uniqueRevision = String.valueOf(2000 + it) + '-01-01'
120                 def uniqueContent = NEW_RESOURCE_CONTENT.replace(NEW_RESOURCE_REVISION, uniqueRevision)
121                 newYangResourcesNameToContentMap.put(uniqueRevision, uniqueContent)
122             }
123         when: 'the new schema set is stored'
124             objectUnderTest.storeSchemaSet(DATASPACE_NAME, schemaSetName, newYangResourcesNameToContentMap)
125         then: 'the correct number of modules are persisted'
126             assert getYangResourceEntities(schemaSetName).size() == numberOfModules
127     }
128
129     @Sql([CLEAR_DATA, SET_DATA])
130     def 'Store and retrieve new schema set from new modules and existing modules.'() {
131         given: 'a new module'
132             def mapOfNewModules = [newModule: 'module newmodule { yang-version 1.1; revision "2022-08-19" { } }']
133         and: 'there are more existing modules in the db than the batch size (100)'
134             def listOfExistingModulesModuleReference = []
135             def mapOfExistingModule = [:]
136             def numberOfModules =  1 + SchemaSetYangResourceRepositoryImpl.MAX_INSERT_BATCH_SIZE
137             (1..numberOfModules).each {
138                 def uniqueRevision = String.valueOf(2000 + it) + '-01-01'
139                 def uniqueContent = "module test { yang-version 1.1; revision \"${uniqueRevision}\" { } }".toString()
140                 mapOfNewModules.put( 'test' + it, uniqueContent)
141                 listOfExistingModulesModuleReference.add(new ModuleReference('test',uniqueRevision))
142             }
143             objectUnderTest.storeSchemaSet(DATASPACE_NAME, 'existing schema set ', mapOfExistingModule)
144         when: 'a new schema set is created from these new modules and existing modules'
145             objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, NEW_SCHEMA_SET_NAME , mapOfNewModules, listOfExistingModulesModuleReference)
146         then: 'the schema set can be retrieved'
147             def actualYangResourcesMapAfterSchemaSetHasBeenCreated =
148                     objectUnderTest.getYangSchemaResources(DATASPACE_NAME, NEW_SCHEMA_SET_NAME)
149         and: 'it has all the new and existing modules'
150             def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule
151             assert actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated
152     }
153
154     @Sql([CLEAR_DATA, SET_DATA])
155     def 'Retrieving schema set (resources) by anchor.'() {
156         given: 'a new schema set is stored'
157             objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap)
158         and: 'an anchor is created with that schema set'
159             cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, ANCHOR_NAME1)
160         when: 'the schema set resources for that anchor is retrieved'
161             def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
162         then: 'the correct resources are returned'
163              result == newYangResourcesNameToContentMap
164     }
165
166     @Sql([CLEAR_DATA, SET_DATA])
167     def 'Retrieving all yang resources module references for the given dataspace.'() {
168         given: 'a dataspace name'
169             def dataspaceName = 'DATASPACE-002'
170         when: 'all yang resources module references are retrieved for the given dataspace name'
171             def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName)
172         then: 'the correct resources are returned'
173             result.sort() == [new ModuleReference(moduleName: 'MODULE-NAME-005', revision: 'REVISION-002'),
174                               new ModuleReference(moduleName: 'MODULE-NAME-006', revision: 'REVISION-006')]
175     }
176
177     @Sql([CLEAR_DATA, SET_DATA])
178     def 'Retrieving module names and revisions for the given anchor.'() {
179         given: 'a dataspace name and anchor name'
180             def dataspaceName = 'DATASPACE-001'
181             def anchorName = 'ANCHOR1'
182         when: 'all yang resources module references are retrieved for the given anchor'
183             def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName, anchorName)
184         then: 'the correct module names and revisions are returned'
185             result.sort() == [ new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'),
186                               new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')]
187     }
188
189     @Sql([CLEAR_DATA, SET_DATA])
190     def 'Storing duplicate schema content.'() {
191         given: 'a new schema set with a resource with the same content as an existing resource'
192             def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
193             def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
194         when: 'the schema set with duplicate resource is stored'
195             objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap)
196         then: 'the schema persisted has the new name and has the same checksum'
197             def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
198             def existingResourceModuleName = 'test'
199             def existingResourceRevision = '2020-09-15'
200             assertSchemaSetWithOneModuleIsPersistedCorrectly(NEW_RESOURCE_NAME, existingResourceModuleName,
201                 existingResourceRevision, existingResourceContent, existingResourceChecksum)
202     }
203
204     @Sql([CLEAR_DATA, SET_DATA])
205     def 'Retrieve schema sets for a given dataspace name'() {
206         when: 'the schema set resources for a given dataspace name is retrieved'
207             def result = objectUnderTest.getSchemaSetsByDataspaceName(DATASPACE_NAME)
208         then: 'the correct resources are returned'
209              result.contains(new SchemaSet(name: 'SCHEMA-SET-001', dataspaceName: 'DATASPACE-001'))
210     }
211
212     @Sql([CLEAR_DATA, SET_DATA])
213     def 'Delete schema set'() {
214         when: 'a schema set is deleted with cascade-prohibited option'
215             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
216         then: 'the schema set has been deleted'
217             !schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent()
218     }
219
220     @Sql([CLEAR_DATA, SET_DATA])
221     def 'Delete schema sets'() {
222         when: 'schema sets are deleted'
223             objectUnderTest.deleteSchemaSets(DATASPACE_NAME, ['SCHEMA-SET-001', 'SCHEMA-SET-002'])
224         then: 'the schema sets have been deleted'
225             !schemaSetRepository.findByDataspaceAndName(dataspaceEntity, 'SCHEMA-SET-001').isPresent()
226             !schemaSetRepository.findByDataspaceAndName(dataspaceEntity, 'SCHEMA-SET-002').isPresent()
227     }
228
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                                                                                              || []
241     }
242
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
253     }
254
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()
267     }
268
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')]
275     }
276
277     def assertSchemaSetWithOneModuleIsPersistedCorrectly(expectedYangResourceName,
278                                                          expectedYangResourceModuleName,
279                                                          expectedYangResourceRevision,
280                                                          expectedYangResourceContent,
281                                                          expectedYangResourceChecksum) {
282
283         // assert the attached yang resource is persisted
284         def yangResourceEntities = getYangResourceEntities(NEW_SCHEMA_SET_NAME)
285         assert yangResourceEntities.size() == 1
286
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
295         return true
296     }
297
298     def getYangResourceEntities(schemaSetName) {
299         def schemaSetEntity = schemaSetRepository
300             .findByDataspaceAndName(dataspaceEntity, schemaSetName).orElseThrow()
301         return schemaSetEntity.getYangResources()
302     }
303
304     def toModuleReference(moduleReferenceAsMap) {
305         def moduleReferences = [].withDefault { [:] }
306         moduleReferenceAsMap.forEach(property ->
307             property.forEach((moduleName, revision) -> {
308                 moduleReferences.add(new ModuleReference(moduleName, revision))
309             }))
310         return moduleReferences
311     }
312
313 }