75d63302657fe14754540080cf3b5cf72be0fa5b
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.onap.cps.spi.impl
22
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.SchemaSetInUseException
29 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
30 import org.onap.cps.spi.model.ModuleReference
31 import org.onap.cps.spi.model.ExtendedModuleReference
32 import org.onap.cps.spi.repository.AnchorRepository
33 import org.onap.cps.spi.repository.SchemaSetRepository
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.jdbc.Sql
36
37 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
38 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
39
40 class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
41
42     @Autowired
43     CpsModulePersistenceService objectUnderTest
44
45     @Autowired
46     AnchorRepository anchorRepository
47
48     @Autowired
49     SchemaSetRepository schemaSetRepository
50
51     @Autowired
52     CpsAdminPersistenceService cpsAdminPersistenceService
53
54     static final String SET_DATA = '/data/schemaset.sql'
55     static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
56     static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
57     static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = 'SCHEMA-SET-101'
58     static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
59
60     static final String NEW_RESOURCE_NAME = 'some new resource'
61     static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
62             '    yang-version 1.1;\n' +
63             '    namespace "org:onap:ccsdk:sample";\n' +
64             '\n' +
65             '    prefix book-store;\n' +
66             '\n' +
67             '    revision "2020-09-15" {\n' +
68             '        description\n' +
69             '        "Sample Model";\n' +
70             '    }' +
71             '}'
72     static final String NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
73     static final String NEW_RESOURCE_MODULE_NAME = 'stores'
74     static final String NEW_RESOURCE_REVISION = '2020-09-15'
75     static final ExtendedModuleReference newModuleReference = ExtendedModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME)
76             .revision(NEW_RESOURCE_REVISION).build()
77
78     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
79     def allYangResourcesModuleAndRevisionList = [new ExtendedModuleReference(name: 'MODULE-NAME-002',namespace:null, revision: 'REVISION-002'), new ExtendedModuleReference(name: 'MODULE-NAME-003',namespace:null, revision: 'REVISION-003'),
80                                                  new ExtendedModuleReference(name: 'MODULE-NAME-004',namespace:null, revision: 'REVISION-004'), ExtendedModuleReference.builder().build(),
81                                                  ExtendedModuleReference.builder().build(), newModuleReference]
82     def dataspaceEntity
83
84     def setup() {
85         dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
86     }
87
88     @Sql([CLEAR_DATA, SET_DATA])
89     def 'Store schema set error scenario: #scenario.'() {
90         when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
91             objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
92         then: 'an #expectedException is thrown'
93             thrown(expectedException)
94         where: 'the following data is used'
95             scenario                    | dataspaceName  | schemaSetName            || expectedException
96             'dataspace does not exist'  | 'unknown'      | 'not-relevant'           || DataspaceNotFoundException
97             'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException
98     }
99
100     @Sql([CLEAR_DATA, SET_DATA])
101     def 'Store new schema set.'() {
102         when: 'a new schemaset is stored'
103             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
104         then: 'the schema set is persisted correctly'
105             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_NAME,
106                     NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM, NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION)
107     }
108
109     @Sql([CLEAR_DATA, SET_DATA])
110     def 'Store and retrieve new schema set from new modules and existing modules.'() {
111         given: 'map of new modules, a list of existing modules, module reference'
112             def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }']
113             def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12")
114             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
115             def mapOfExistingModule = [test: 'module test { yang-version 1.1; revision "2021-10-12" { } }']
116             objectUnderTest.storeSchemaSet(DATASPACE_NAME, "someSchemaSetName", mapOfExistingModule)
117         when: 'a new schema set is created from these new modules and existing modules'
118             objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference)
119         then: 'the schema set can be retrieved'
120             def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule
121             def actualYangResourcesMapAfterSchemaSetHasBeenCreated =
122                     objectUnderTest.getYangSchemaResources(DATASPACE_NAME, "newSchemaSetName")
123         actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated
124     }
125
126     @Sql([CLEAR_DATA, SET_DATA])
127     def 'Retrieving schema set (resources) by anchor.'() {
128         given: 'a new schema set is stored'
129             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
130         and: 'an anchor is created with that schema set'
131             cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
132         when: 'the schema set resources for that anchor is retrieved'
133             def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
134         then: 'the correct resources are returned'
135              result == newYangResourcesNameToContentMap
136     }
137
138     @Sql([CLEAR_DATA, SET_DATA])
139     def 'Retrieving all yang resources module references for the given dataspace.'() {
140         given: 'a dataspace name'
141             def dataspaceName = 'DATASPACE-002'
142         when: 'all yang resources module references are retrieved for the given dataspace name'
143             def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName)
144         then: 'the correct resources are returned'
145             result.sort() == [new ModuleReference(moduleName: 'MODULE-NAME-005', revision: 'REVISION-002'),
146                               new ModuleReference(moduleName: 'MODULE-NAME-006', revision: 'REVISION-006')]
147     }
148
149     @Sql([CLEAR_DATA, SET_DATA])
150     def 'Retrieving module names and revisions for the given anchor.'() {
151         given: 'a dataspace name and anchor name'
152             def dataspaceName = 'DATASPACE-001'
153             def anchorName = 'ANCHOR1'
154         when: 'all yang resources module references are retrieved for the given anchor'
155             def result = objectUnderTest.getYangResourceModuleReferences(dataspaceName, anchorName)
156         then: 'the correct module names and revisions are returned'
157             result.sort() == [ new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'),
158                               new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')]
159     }
160
161     @Sql([CLEAR_DATA, SET_DATA])
162     def 'Storing duplicate schema content.'() {
163         given: 'a new schema set with a resource with the same content as an existing resource'
164             def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
165             def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
166         when: 'the schema set with duplicate resource is stored'
167             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
168         then: 'the schema persisted (re)uses the existing name and has the same checksum'
169             def existingResourceName = 'module1@2020-02-02.yang'
170             def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
171             def existingResourceModelName = 'test'
172             def existingResourceRevision = '2020-09-15'
173             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, existingResourceName,
174                     existingResourceContent, existingResourceChecksum,
175                     existingResourceModelName, existingResourceRevision)
176     }
177
178     @Sql([CLEAR_DATA, SET_DATA])
179     def 'Delete schema set'() {
180         when: 'a schema set is deleted with cascade-prohibited option'
181             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
182         then: 'the schema set has been deleted'
183             schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
184     }
185
186     @Sql([CLEAR_DATA, SET_DATA])
187     def 'Delete schema set error scenario: #scenario.'() {
188         when: 'attempt to delete a schema set where #scenario'
189             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName)
190         then: 'an #expectedException is thrown'
191             thrown(expectedException)
192         where: 'the following data is used'
193             scenario                                   | dataspaceName  | schemaSetName                         || expectedException
194             'dataspace does not exist'                 | 'unknown'      | 'not-relevant'                        || DataspaceNotFoundException
195             'schema set does not exists'               | DATASPACE_NAME | 'unknown'                             || SchemaSetNotFoundException
196     }
197
198     @Sql([CLEAR_DATA, SET_DATA])
199     def 'Delete only orphan Yang Resources'() {
200         given: 'a schema set is deleted and and yang resource is not used anymore'
201             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS)
202         when: 'orphan yang resources are deleted'
203             objectUnderTest.deleteUnusedYangResourceModules()
204         then: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
205             def orphanedResourceId = 3100L
206             yangResourceRepository.findById(orphanedResourceId).isPresent() == false
207         and: 'any shared (still in use by other schema set) yang resources still persists'
208             def sharedResourceId = 3003L
209             yangResourceRepository.findById(sharedResourceId).isPresent()
210     }
211
212     def assertSchemaSetPersisted(expectedDataspaceName,
213                                  expectedSchemaSetName,
214                                  expectedYangResourceName,
215                                  expectedYangResourceContent,
216                                  expectedYangResourceChecksum,
217                                  expectedYangResourceModuleName,
218                                  expectedYangResourceRevision) {
219         // assert the schema set is persisted
220         def schemaSetEntity = schemaSetRepository
221                 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
222         assert schemaSetEntity.name == expectedSchemaSetName
223         assert schemaSetEntity.dataspace.name == expectedDataspaceName
224
225         // assert the attached yang resource is persisted
226         def yangResourceEntities = schemaSetEntity.getYangResources()
227         yangResourceEntities.size() == 1
228
229         // assert the attached yang resource content
230         YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
231         assert yangResourceEntity.id != null
232         yangResourceEntity.name == expectedYangResourceName
233         yangResourceEntity.content == expectedYangResourceContent
234         yangResourceEntity.checksum == expectedYangResourceChecksum
235         yangResourceEntity.moduleName == expectedYangResourceModuleName
236         yangResourceEntity.revision == expectedYangResourceRevision
237     }
238
239 }