d8edb02abdc98027eddadea5f4a13624df54670b
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsModuleServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2021 Pantheon.tech
5  *  Modifications Copyright (C) 2020-2022 Bell Canada.
6  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.api.impl
25
26 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
27 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
28
29 import org.onap.cps.TestUtils
30 import org.onap.cps.api.CpsAdminService
31 import org.onap.cps.spi.CpsModulePersistenceService
32 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
33 import org.onap.cps.spi.exceptions.ModelValidationException
34 import org.onap.cps.spi.exceptions.SchemaSetInUseException
35 import org.onap.cps.spi.model.ModuleDefinition
36 import org.onap.cps.spi.utils.CpsValidator
37 import org.onap.cps.spi.model.Anchor
38 import org.onap.cps.spi.model.ModuleReference
39 import org.onap.cps.spi.model.SchemaSet
40 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder
41 import org.onap.cps.yang.YangTextSchemaSourceSet
42 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
43 import spock.lang.Specification
44
45 class CpsModuleServiceImplSpec extends Specification {
46
47     def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
48     def mockCpsAdminService = Mock(CpsAdminService)
49     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
50     def mockCpsValidator = Mock(CpsValidator)
51     def timedYangTextSchemaSourceSetBuilder = new TimedYangTextSchemaSourceSetBuilder()
52
53     def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService, mockCpsValidator,timedYangTextSchemaSourceSetBuilder)
54
55     def 'Create schema set.'() {
56         when: 'Create schema set method is invoked'
57             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', [:])
58         then: 'Parameters are validated and processing is delegated to persistence service'
59             1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', [:])
60         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
61             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
62     }
63
64     def 'Create schema set from new modules and existing modules.'() {
65         given: 'a list of existing modules module reference'
66             def moduleReferenceForExistingModule = new ModuleReference('test',  '2021-10-12','test.org')
67             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
68         when: 'create schema set from modules method is invoked'
69             objectUnderTest.createSchemaSetFromModules('someDataspaceName', 'someSchemaSetName', [newModule: 'newContent'], listOfExistingModulesModuleReference)
70         then: 'processing is delegated to persistence service'
71             1 * mockCpsModulePersistenceService.storeSchemaSetFromModules('someDataspaceName', 'someSchemaSetName', [newModule: 'newContent'], listOfExistingModulesModuleReference)
72         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
73             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someSchemaSetName')
74     }
75
76     def 'Create schema set from invalid resources'() {
77         given: 'Invalid yang resource as name-to-content map'
78             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
79         when: 'Create schema set method is invoked'
80             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
81         then: 'Model validation exception is thrown'
82             thrown(ModelValidationException)
83     }
84
85     def 'Create schema set with duplicate yang resource exception in persistence layer.'() {
86         given: 'the persistence layer throws an duplicated yang resource exception'
87             def originalException = new DuplicatedYangResourceException('name', '123', null)
88             mockCpsModulePersistenceService.storeSchemaSet(*_) >> { throw originalException }
89         when: 'attempt to create schema set'
90             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', [:])
91         then: 'the same duplicated yang resource exception is thrown (up)'
92             def thrownUp = thrown(DuplicatedYangResourceException)
93             assert thrownUp == originalException
94         and: 'the exception message contains the relevant data'
95             assert thrownUp.message.contains('name')
96             assert thrownUp.message.contains('123')
97     }
98
99     def 'Get schema set by name and dataspace.'() {
100         given: 'an already present schema set'
101             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
102         and: 'yang resource cache returns the expected schema set'
103             mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
104         when: 'get schema set method is invoked'
105             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
106         then: 'the correct schema set is returned'
107             result.getName().contains('someSchemaSet')
108             result.getDataspaceName().contains('someDataspace')
109             result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
110         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
111             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
112     }
113
114     def 'Get schema sets by dataspace name.'() {
115         given: 'two already present schema sets'
116             def moduleReference = new ModuleReference('sample1', '2022-12-07')
117             def sampleSchemaSet1 = new SchemaSet('testSchemaSet1', 'testDataspace', [moduleReference])
118             def sampleSchemaSet2 = new SchemaSet('testSchemaSet2', 'testDataspace', [moduleReference])
119         and: 'the persistence service returns the created schema sets'
120             mockCpsModulePersistenceService.getSchemaSetsByDataspaceName('testDataspace') >> [sampleSchemaSet1, sampleSchemaSet2]
121         and: 'yang resource cache always returns a schema source set'
122             def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
123             mockYangTextSchemaSourceSetCache.get('testDataspace', _) >> mockYangTextSchemaSourceSet
124         when: 'get schema sets method is invoked'
125             def result = objectUnderTest.getSchemaSets('testDataspace')
126         then: 'the correct schema sets are returned'
127             assert result.size() == 2
128             assert result.containsAll(sampleSchemaSet1, sampleSchemaSet2)
129         and: 'the Cps Validator is called on the dataspaceName'
130             1 * mockCpsValidator.validateNameCharacters('testDataspace')
131     }
132
133     def 'Delete schema-set when cascade is allowed.'() {
134         given: '#numberOfAnchors anchors are associated with schemaset'
135             def associatedAnchors = createAnchors(numberOfAnchors)
136             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
137         when: 'schema set deletion is requested with cascade allowed'
138             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
139         then: 'anchor deletion is called #numberOfAnchors times'
140             numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
141         and: 'persistence service method is invoked with same parameters'
142             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
143         and: 'schema set will be removed from the cache'
144             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
145         and: 'orphan yang resources are deleted'
146             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
147         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
148             1 * mockCpsValidator.validateNameCharacters('my-dataspace', _)
149         where: 'following parameters are used'
150             numberOfAnchors << [0, 3]
151     }
152
153     def 'Delete schema-set when cascade is prohibited.'() {
154         given: 'no anchors are associated with schemaset'
155             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
156         when: 'schema set deletion is requested with cascade allowed'
157             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
158         then: 'no anchors are deleted'
159             0 * mockCpsAdminService.deleteAnchor(_, _)
160         and: 'persistence service method is invoked with same parameters'
161             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
162         and: 'schema set will be removed from the cache'
163             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
164         and: 'orphan yang resources are deleted'
165             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
166         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
167             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
168     }
169
170     def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
171         given: '2 anchors are associated with schemaset'
172             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
173         when: 'schema set deletion is requested with cascade allowed'
174             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
175         then: 'Schema-Set in Use exception is thrown'
176             thrown(SchemaSetInUseException)
177     }
178
179     def 'Delete multiple schema-sets when cascade is allowed.'() {
180         given: '#numberOfAnchors anchors are associated with each schemaset'
181             mockCpsAdminService.getAnchors('my-dataspace', ['my-schemaset1', 'my-schemaset2']) >> createAnchors(numberOfAnchors * 2)
182         when: 'schema set deletion is requested with cascade allowed'
183             objectUnderTest.deleteSchemaSetsWithCascade('my-dataspace', ['my-schemaset1', 'my-schemaset2'])
184         then: 'anchor deletion is called #numberOfAnchors times'
185             mockCpsAdminService.deleteAnchors('my-dataspace', _)
186         and: 'persistence service method is invoked with same parameters'
187             mockCpsModulePersistenceService.deleteSchemaSets('my-dataspace', _)
188         and: 'schema sets will be removed from the cache'
189             2 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', _)
190         and: 'orphan yang resources are deleted'
191             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
192         and: 'the CpsValidator is called on the dataspaceName'
193             1 * mockCpsValidator.validateNameCharacters('my-dataspace')
194         and: 'the CpsValidator is called on the schemaSetNames'
195             1 * mockCpsValidator.validateNameCharacters(_)
196         where: 'following parameters are used'
197             numberOfAnchors << [0, 3]
198     }
199
200     def 'Upgrade existing schema set'() {
201         when: 'schema set update is requested'
202         objectUnderTest.upgradeSchemaSetFromModules('my-dataspace', 'my-schemaset', [:], moduleReferences)
203         then: 'no exception is thrown '
204         noExceptionThrown()
205     }
206
207     def 'Get all yang resources module references.'() {
208         given: 'an already present module reference'
209             def moduleReferences = getModuleReferences()
210             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
211         when: 'get yang resource module references is called'
212             def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName')
213         then: 'the list provided by persistence service is returned as result'
214             result == moduleReferences
215         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
216             1 * mockCpsValidator.validateNameCharacters('someDataspaceName')
217     }
218
219     def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
220         given: 'the module store service service returns a list module references'
221             def moduleReferences = [new ModuleReference()]
222             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
223         when: 'get yang resource module references is called for dataspace name and anchor name'
224             def result = objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName')
225         then: 'the list provided by persistence service is returned as result'
226             result == moduleReferences
227         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
228             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someAnchorName')
229     }
230
231     def 'Identifying new module references.'(){
232         given: 'module references from cm handle'
233             def moduleReferencesToCheck = getModuleReferences()
234         when: 'identifyNewModuleReferences is called'
235             objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
236         then: 'cps module persistence service is called with module references to check'
237             1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck)
238     }
239
240     def 'Getting module definitions.'() {
241         given: 'the module persistence service returns a collection of module definitions'
242             def moduleDefinitionsFromPersistenceService = [ new ModuleDefinition('name', 'revision', 'content' ) ]
243             mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name')  >> moduleDefinitionsFromPersistenceService
244         when: 'get module definitions method is called with a valid dataspace and anchor name'
245             def result = objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
246         then: 'the result is the same collection returned by the persistence service'
247             assert result == moduleDefinitionsFromPersistenceService
248         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
249             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name')
250     }
251
252     def getModuleReferences() {
253         return [new ModuleReference('some module name','some revision name')]
254     }
255
256     def createAnchors(int anchorCount) {
257         def anchors = []
258         (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
259         return anchors
260     }
261 }