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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.api.impl
26 import org.onap.cps.api.CpsAnchorService
28 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
29 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
31 import org.onap.cps.TestUtils
32 import org.onap.cps.spi.CpsModulePersistenceService
33 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
34 import org.onap.cps.spi.exceptions.ModelValidationException
35 import org.onap.cps.spi.exceptions.SchemaSetInUseException
36 import org.onap.cps.spi.model.ModuleDefinition
37 import org.onap.cps.spi.utils.CpsValidator
38 import org.onap.cps.spi.model.Anchor
39 import org.onap.cps.spi.model.ModuleReference
40 import org.onap.cps.spi.model.SchemaSet
41 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder
42 import org.onap.cps.yang.YangTextSchemaSourceSet
43 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
44 import spock.lang.Specification
46 class CpsModuleServiceImplSpec extends Specification {
48 def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
49 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
50 def mockCpsAnchorService = Mock(CpsAnchorService)
51 def mockCpsValidator = Mock(CpsValidator)
52 def timedYangTextSchemaSourceSetBuilder = new TimedYangTextSchemaSourceSetBuilder()
54 def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAnchorService, mockCpsValidator,timedYangTextSchemaSourceSetBuilder)
56 def 'Create schema set.'() {
57 when: 'Create schema set method is invoked'
58 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', [:])
59 then: 'Parameters are validated and processing is delegated to persistence service'
60 1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', [:])
61 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
62 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
65 def 'Create schema set from new modules and existing modules.'() {
66 given: 'a list of existing modules module reference'
67 def moduleReferenceForExistingModule = new ModuleReference('test', '2021-10-12','test.org')
68 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
69 when: 'create schema set from modules method is invoked'
70 objectUnderTest.createSchemaSetFromModules('someDataspaceName', 'someSchemaSetName', [newModule: 'newContent'], listOfExistingModulesModuleReference)
71 then: 'processing is delegated to persistence service'
72 1 * mockCpsModulePersistenceService.storeSchemaSetFromModules('someDataspaceName', 'someSchemaSetName', [newModule: 'newContent'], listOfExistingModulesModuleReference)
73 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
74 1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someSchemaSetName')
77 def 'Create schema set from invalid resources'() {
78 given: 'Invalid yang resource as name-to-content map'
79 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
80 when: 'Create schema set method is invoked'
81 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
82 then: 'Model validation exception is thrown'
83 thrown(ModelValidationException)
86 def 'Create schema set with duplicate yang resource exception in persistence layer.'() {
87 given: 'the persistence layer throws an duplicated yang resource exception'
88 def originalException = new DuplicatedYangResourceException('name', '123', null)
89 mockCpsModulePersistenceService.storeSchemaSet(*_) >> { throw originalException }
90 when: 'attempt to create schema set'
91 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', [:])
92 then: 'the same duplicated yang resource exception is thrown (up)'
93 def thrownUp = thrown(DuplicatedYangResourceException)
94 assert thrownUp == originalException
95 and: 'the exception message contains the relevant data'
96 assert thrownUp.message.contains('name')
97 assert thrownUp.message.contains('123')
100 def 'Get schema set by name and dataspace.'() {
101 given: 'an already present schema set'
102 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
103 and: 'yang resource cache returns the expected schema set'
104 mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
105 when: 'get schema set method is invoked'
106 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
107 then: 'the correct schema set is returned'
108 result.getName().contains('someSchemaSet')
109 result.getDataspaceName().contains('someDataspace')
110 result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
111 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
112 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
115 def 'Get schema sets by dataspace name.'() {
116 given: 'two already present schema sets'
117 def moduleReference = new ModuleReference('sample1', '2022-12-07')
118 def sampleSchemaSet1 = new SchemaSet('testSchemaSet1', 'testDataspace', [moduleReference])
119 def sampleSchemaSet2 = new SchemaSet('testSchemaSet2', 'testDataspace', [moduleReference])
120 and: 'the persistence service returns the created schema sets'
121 mockCpsModulePersistenceService.getSchemaSetsByDataspaceName('testDataspace') >> [sampleSchemaSet1, sampleSchemaSet2]
122 and: 'yang resource cache always returns a schema source set'
123 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
124 mockYangTextSchemaSourceSetCache.get('testDataspace', _) >> mockYangTextSchemaSourceSet
125 when: 'get schema sets method is invoked'
126 def result = objectUnderTest.getSchemaSets('testDataspace')
127 then: 'the correct schema sets are returned'
128 assert result.size() == 2
129 assert result.containsAll(sampleSchemaSet1, sampleSchemaSet2)
130 and: 'the Cps Validator is called on the dataspaceName'
131 1 * mockCpsValidator.validateNameCharacters('testDataspace')
134 def 'Delete schema-set when cascade is allowed.'() {
135 given: '#numberOfAnchors anchors are associated with schemaset'
136 def associatedAnchors = createAnchors(numberOfAnchors)
137 mockCpsAnchorService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
138 when: 'schema set deletion is requested with cascade allowed'
139 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
140 then: 'anchor deletion is called #numberOfAnchors times'
141 numberOfAnchors * mockCpsAnchorService.deleteAnchor('my-dataspace', _)
142 and: 'persistence service method is invoked with same parameters'
143 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
144 and: 'schema set will be removed from the cache'
145 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
146 and: 'orphan yang resources are deleted'
147 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
148 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
149 1 * mockCpsValidator.validateNameCharacters('my-dataspace', _)
150 where: 'following parameters are used'
151 numberOfAnchors << [0, 3]
154 def 'Delete schema-set when cascade is prohibited.'() {
155 given: 'no anchors are associated with schemaset'
156 mockCpsAnchorService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
157 when: 'schema set deletion is requested with cascade allowed'
158 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
159 then: 'no anchors are deleted'
160 0 * mockCpsAnchorService.deleteAnchor(_, _)
161 and: 'persistence service method is invoked with same parameters'
162 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
163 and: 'schema set will be removed from the cache'
164 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
165 and: 'orphan yang resources are deleted'
166 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
167 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
168 1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
171 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
172 given: '2 anchors are associated with schemaset'
173 mockCpsAnchorService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
174 when: 'schema set deletion is requested with cascade allowed'
175 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
176 then: 'Schema-Set in Use exception is thrown'
177 thrown(SchemaSetInUseException)
180 def 'Delete multiple schema-sets when cascade is allowed.'() {
181 given: '#numberOfAnchors anchors are associated with each schemaset'
182 mockCpsAnchorService.getAnchors('my-dataspace', ['my-schemaset1', 'my-schemaset2']) >> createAnchors(numberOfAnchors * 2)
183 when: 'schema set deletion is requested with cascade allowed'
184 objectUnderTest.deleteSchemaSetsWithCascade('my-dataspace', ['my-schemaset1', 'my-schemaset2'])
185 then: 'anchor deletion is called #numberOfAnchors times'
186 mockCpsAnchorService.deleteAnchors('my-dataspace', _)
187 and: 'persistence service method is invoked with same parameters'
188 mockCpsModulePersistenceService.deleteSchemaSets('my-dataspace', _)
189 and: 'schema sets will be removed from the cache'
190 2 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', _)
191 and: 'orphan yang resources are deleted'
192 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
193 and: 'the CpsValidator is called on the dataspaceName'
194 1 * mockCpsValidator.validateNameCharacters('my-dataspace')
195 and: 'the CpsValidator is called on the schemaSetNames'
196 1 * mockCpsValidator.validateNameCharacters(_)
197 where: 'following parameters are used'
198 numberOfAnchors << [0, 3]
201 def 'Upgrade existing schema set'() {
202 when: 'schema set update is requested'
203 objectUnderTest.upgradeSchemaSetFromModules('my-dataspace', 'my-schemaset', [:], moduleReferences)
204 then: 'no exception is thrown '
208 def 'Get all yang resources module references.'() {
209 given: 'an already present module reference'
210 def moduleReferences = getModuleReferences()
211 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
212 when: 'get yang resource module references is called'
213 def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName')
214 then: 'the list provided by persistence service is returned as result'
215 result == moduleReferences
216 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
217 1 * mockCpsValidator.validateNameCharacters('someDataspaceName')
220 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
221 given: 'the module store service service returns a list module references'
222 def moduleReferences = [new ModuleReference()]
223 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
224 when: 'get yang resource module references is called for dataspace name and anchor name'
225 def result = objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName')
226 then: 'the list provided by persistence service is returned as result'
227 result == moduleReferences
228 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
229 1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someAnchorName')
232 def 'Identifying new module references.'(){
233 given: 'module references from cm handle'
234 def moduleReferencesToCheck = getModuleReferences()
235 when: 'identifyNewModuleReferences is called'
236 objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
237 then: 'cps module persistence service is called with module references to check'
238 1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck)
241 def 'Getting module definitions.'() {
242 given: 'the module persistence service returns a collection of module definitions'
243 def moduleDefinitionsFromPersistenceService = [ new ModuleDefinition('name', 'revision', 'content' ) ]
244 mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name') >> moduleDefinitionsFromPersistenceService
245 when: 'get module definitions method is called with a valid dataspace and anchor name'
246 def result = objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
247 then: 'the result is the same collection returned by the persistence service'
248 assert result == moduleDefinitionsFromPersistenceService
249 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
250 1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name')
253 def getModuleReferences() {
254 return [new ModuleReference('some module name','some revision name')]
257 def createAnchors(int anchorCount) {
259 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }