2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 Nordix Foundation
4 * Modifications Copyright (C) 2020-2021 Pantheon.tech
5 * Modifications Copyright (C) 2020-2022 Bell Canada.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl
25 import org.onap.cps.TestUtils
26 import org.onap.cps.api.CpsAdminService
27 import org.onap.cps.spi.CascadeDeleteAllowed
28 import org.onap.cps.spi.CpsModulePersistenceService
29 import org.onap.cps.spi.exceptions.DataValidationException
30 import org.onap.cps.spi.exceptions.ModelValidationException
31 import org.onap.cps.spi.exceptions.SchemaSetInUseException
32 import org.onap.cps.spi.model.Anchor
33 import org.onap.cps.spi.model.ModuleReference
34 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
35 import spock.lang.Specification
36 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
37 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
39 class CpsModuleServiceImplSpec extends Specification {
41 def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
42 def mockCpsAdminService = Mock(CpsAdminService)
43 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
45 def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService)
47 def 'Create schema set.'() {
48 given: 'Valid yang resource as name-to-content map'
49 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
50 when: 'Create schema set method is invoked'
51 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
52 then: 'Parameters are validated and processing is delegated to persistence service'
53 1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
56 def 'Create a schema set with an invalid #scenario.'() {
57 when: 'create dataspace method is invoked with incorrectly named dataspace'
58 objectUnderTest.createSchemaSet(dataspaceName, schemaSetName, _ as Map<String, String>)
59 then: 'a data validation exception is thrown'
60 thrown(DataValidationException)
61 and: 'the persistence service method is not invoked'
62 0 * mockCpsModulePersistenceService.storeSchemaSet(_, _, _)
63 where: 'the following parameters are used'
64 scenario | dataspaceName | schemaSetName
65 'dataspace name' | 'dataspace names with spaces' | 'schemaSetName'
66 'schema set name name' | 'dataspaceName' | 'schema set name with spaces'
67 'dataspace and schema set name' | 'dataspace name with spaces' | 'schema set name with spaces'
70 def 'Create schema set from new modules and existing modules.'() {
71 given: 'a list of existing modules module reference'
72 def moduleReferenceForExistingModule = new ModuleReference("test", "2021-10-12","test.org")
73 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
74 when: 'create schema set from modules method is invoked'
75 objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
76 then: 'processing is delegated to persistence service'
77 1 * mockCpsModulePersistenceService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
80 def 'Create schema set from new modules and existing modules with invalid #scenario.'() {
81 when: 'create dataspace method is invoked with incorrectly named dataspace'
82 objectUnderTest.createSchemaSetFromModules(dataspaceName, schemaSetName, _ as Map<String, String>, _ as Collection<ModuleReference>)
83 then: 'a data validation exception is thrown'
84 thrown(DataValidationException)
85 and: 'the persistence service method is not invoked'
86 0 * mockCpsModulePersistenceService.storeSchemaSetFromModules(_, _, _)
87 where: 'the following parameters are used'
88 scenario | dataspaceName | schemaSetName
89 'dataspace name' | 'dataspace names with spaces' | 'schemaSetName'
90 'schema set name name' | 'dataspaceName' | 'schema set name with spaces'
91 'dataspace and schema set name' | 'dataspace name with spaces' | 'schema set name with spaces'
94 def 'Create schema set from invalid resources'() {
95 given: 'Invalid yang resource as name-to-content map'
96 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
97 when: 'Create schema set method is invoked'
98 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
99 then: 'Model validation exception is thrown'
100 thrown(ModelValidationException.class)
103 def 'Get schema set by name and dataspace.'() {
104 given: 'an already present schema set'
105 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
106 and: 'yang resource cache returns the expected schema set'
107 mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
108 when: 'get schema set method is invoked'
109 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
110 then: 'the correct schema set is returned'
111 result.getName().contains('someSchemaSet')
112 result.getDataspaceName().contains('someDataspace')
113 result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
116 def 'Get a schema set with an invalid #scenario'() {
117 when: 'create dataspace method is invoked with incorrectly named dataspace'
118 objectUnderTest.getSchemaSet(dataspaceName, schemaSetName)
119 then: 'a data validation exception is thrown'
120 thrown(DataValidationException)
121 and: 'the yang resource cache is not invoked'
122 0 * mockYangTextSchemaSourceSetCache.get(_, _)
123 where: 'the following parameters are used'
124 scenario | dataspaceName | schemaSetName
125 'dataspace name' | 'dataspace names with spaces' | 'schemaSetName'
126 'schema set name' | 'dataspaceName' | 'schema set name with spaces'
127 'dataspace and schema set name' | 'dataspace name with spaces' | 'schema set name with spaces'
130 def 'Delete schema-set when cascade is allowed.'() {
131 given: '#numberOfAnchors anchors are associated with schemaset'
132 def associatedAnchors = createAnchors(numberOfAnchors)
133 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
134 when: 'schema set deletion is requested with cascade allowed'
135 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
136 then: 'anchor deletion is called #numberOfAnchors times'
137 numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
138 and: 'persistence service method is invoked with same parameters'
139 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
140 and: 'schema set will be removed from the cache'
141 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
142 and: 'orphan yang resources are deleted'
143 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
144 where: 'following parameters are used'
145 numberOfAnchors << [0, 3]
148 def 'Delete schema-set when cascade is prohibited.'() {
149 given: 'no anchors are associated with schemaset'
150 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
151 when: 'schema set deletion is requested with cascade allowed'
152 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
153 then: 'no anchors are deleted'
154 0 * mockCpsAdminService.deleteAnchor(_, _)
155 and: 'persistence service method is invoked with same parameters'
156 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
157 and: 'schema set will be removed from the cache'
158 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
159 and: 'orphan yang resources are deleted'
160 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
163 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
164 given: '2 anchors are associated with schemaset'
165 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
166 when: 'schema set deletion is requested with cascade allowed'
167 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
168 then: 'Schema-Set in Use exception is thrown'
169 thrown(SchemaSetInUseException)
172 def 'Delete a schema set with an invalid #scenario.'() {
173 when: 'create dataspace method is invoked with incorrectly named dataspace'
174 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_ALLOWED)
175 then: 'a data validation exception is thrown'
176 thrown(DataValidationException)
177 and: 'anchor deletion is called 0 times'
178 0 * mockCpsAdminService.deleteAnchor(_, _)
179 and: 'the delete schema set persistence service method is not invoked'
180 0 * mockCpsModulePersistenceService.deleteSchemaSet(_, _, _)
181 and: 'schema set will be removed from the cache is not invoked'
182 0 * mockYangTextSchemaSourceSetCache.removeFromCache(_, _)
183 and: 'orphan yang resources are deleted is not invoked'
184 0 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
185 where: 'the following parameters are used'
186 scenario | dataspaceName | schemaSetName
187 'dataspace name' | 'dataspace names with spaces' | 'schemaSetName'
188 'schema set name name' | 'dataspaceName' | 'schema set name with spaces'
189 'dataspace and schema set name' | 'dataspace name with spaces' | 'schema set name with spaces'
192 def createAnchors(int anchorCount) {
194 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
198 def 'Get all yang resources module references.'() {
199 given: 'an already present module reference'
200 def moduleReferences = [new ModuleReference('some module name','some revision name')]
201 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
202 expect: 'the list provided by persistence service is returned as result'
203 objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
206 def 'Get all yang resources module references given an invalid dataspace name.'() {
207 when: 'the get yang resources module references method is invoked with an invalid dataspace name'
208 objectUnderTest.getYangResourceModuleReferences('dataspace name with spaces')
209 then: 'a data validation exception is thrown'
210 thrown(DataValidationException)
211 and: 'the persistence service method is not invoked'
212 0 * mockCpsModulePersistenceService.getYangResourceModuleReferences(_)
216 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
217 given: 'the module store service service returns a list module references'
218 def moduleReferences = [new ModuleReference()]
219 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
220 expect: 'the list provided by persistence service is returned as result'
221 objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences
224 def 'Get all yang resources module references given an invalid #scenario.'() {
225 when: 'the get yang resources module references method is invoked with invalid #scenario'
226 objectUnderTest.getYangResourcesModuleReferences(dataspaceName, anchorName)
227 then: 'a data validation exception is thrown'
228 thrown(DataValidationException)
229 and: 'the persistence service method is not invoked'
230 0 * mockCpsModulePersistenceService.getYangResourceModuleReferences(_, _)
231 where: 'the following parameters are used'
232 scenario | dataspaceName | anchorName
233 'dataspace name' | 'dataspace names with spaces' | 'anchorName'
234 'anchor name' | 'dataspaceName' | 'anchor name with spaces'
235 'dataspace and anchor name' | 'dataspace name with spaces' | 'anchor name with spaces'
238 def 'Identifying new module references'(){
239 given: 'module references from cm handle'
240 def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
241 when: 'identifyNewModuleReferences is called'
242 objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
243 then: 'cps module persistence service is called with module references to check'
244 1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);