P2 - Get module names and revisions rest layer
[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 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: null, revision: null), new ModuleReference(moduleName: 'MODULE-NAME-002', revision: 'REVISION-002'),
158                               new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'),
159                               new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')]
160     }
161
162     @Sql([CLEAR_DATA, SET_DATA])
163     def 'Storing duplicate schema content.'() {
164         given: 'a new schema set with a resource with the same content as an existing resource'
165             def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }'
166             def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
167         when: 'the schema set with duplicate resource is stored'
168             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
169         then: 'the schema persisted (re)uses the existing name and has the same checksum'
170             def existingResourceName = 'module1@2020-02-02.yang'
171             def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a'
172             def existingResourceModelName = 'test'
173             def existingResourceRevision = '2020-09-15'
174             assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, existingResourceName,
175                     existingResourceContent, existingResourceChecksum,
176                     existingResourceModelName, existingResourceRevision)
177     }
178
179     @Sql([CLEAR_DATA, SET_DATA])
180     def 'Delete schema set with cascade delete prohibited but no anchors using it'() {
181         when: 'a schema set is deleted with cascade-prohibited option'
182             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
183                     CASCADE_DELETE_PROHIBITED)
184         then: 'the schema set has been deleted'
185             schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
186         and: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
187             def orphanedResourceId = 3100L
188             yangResourceRepository.findById(orphanedResourceId).isPresent() == false
189         and: 'any shared (still in use by other schema set) yang resources still persists'
190             def sharedResourceId = 3003L
191             yangResourceRepository.findById(sharedResourceId).isPresent()
192     }
193
194     @Sql([CLEAR_DATA, SET_DATA])
195     def 'Delete schema set with cascade allowed.'() {
196         when: 'a schema set is deleted with cascade-allowed option'
197             objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA,
198                     CASCADE_DELETE_ALLOWED)
199         then: 'the schema set has been deleted'
200             schemaSetRepository
201                     .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent() == false
202         and: 'the associated anchors are removed'
203             def associatedAnchorsIds = [ 6001, 6002 ]
204             associatedAnchorsIds.each {anchorRepository.findById(it).isPresent() == false }
205         and: 'the fragment(s) under those anchors are removed'
206             def fragmentUnderAnchor1Id = 7001L
207             fragmentRepository.findById(fragmentUnderAnchor1Id).isPresent() == false
208         and: 'the shared resources still persist'
209             def sharedResourceIds = [ 3003L, 3004L ]
210             sharedResourceIds.each {yangResourceRepository.findById(it).isPresent() }
211     }
212
213     @Sql([CLEAR_DATA, SET_DATA])
214     def 'Delete schema set error scenario: #scenario.'() {
215         when: 'attempt to delete a schema set where #scenario'
216             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
217         then: 'an #expectedException is thrown'
218             thrown(expectedException)
219         where: 'the following data is used'
220             scenario                                   | dataspaceName  | schemaSetName                         || expectedException
221             'dataspace does not exist'                 | 'unknown'      | 'not-relevant'                        || DataspaceNotFoundException
222             'schema set does not exists'               | DATASPACE_NAME | 'unknown'                             || SchemaSetNotFoundException
223             'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
224     }
225
226     def assertSchemaSetPersisted(expectedDataspaceName,
227                                  expectedSchemaSetName,
228                                  expectedYangResourceName,
229                                  expectedYangResourceContent,
230                                  expectedYangResourceChecksum,
231                                  expectedYangResourceModuleName,
232                                  expectedYangResourceRevision) {
233         // assert the schema set is persisted
234         def schemaSetEntity = schemaSetRepository
235                 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
236         assert schemaSetEntity.name == expectedSchemaSetName
237         assert schemaSetEntity.dataspace.name == expectedDataspaceName
238
239         // assert the attached yang resource is persisted
240         def yangResourceEntities = schemaSetEntity.getYangResources()
241         yangResourceEntities.size() == 1
242
243         // assert the attached yang resource content
244         YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
245         assert yangResourceEntity.id != null
246         yangResourceEntity.name == expectedYangResourceName
247         yangResourceEntity.content == expectedYangResourceContent
248         yangResourceEntity.checksum == expectedYangResourceChecksum
249         yangResourceEntity.moduleName == expectedYangResourceModuleName
250         yangResourceEntity.revision == expectedYangResourceRevision
251     }
252
253 }