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