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.ModelValidationException
30 import org.onap.cps.spi.exceptions.SchemaSetInUseException
31 import org.onap.cps.spi.utils.CpsValidator
32 import org.onap.cps.spi.model.Anchor
33 import org.onap.cps.spi.model.ModuleReference
34 import org.onap.cps.spi.model.SchemaSet
35 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder
36 import org.onap.cps.yang.YangTextSchemaSourceSet
37 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
38 import spock.lang.Specification
39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
40 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
42 class CpsModuleServiceImplSpec extends Specification {
44 def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
45 def mockCpsAdminService = Mock(CpsAdminService)
46 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
47 def mockCpsValidator = Mock(CpsValidator)
48 def timedYangTextSchemaSourceSetBuilder = new TimedYangTextSchemaSourceSetBuilder()
50 def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService, mockCpsValidator,timedYangTextSchemaSourceSetBuilder)
52 def 'Create schema set.'() {
53 given: 'Valid yang resource as name-to-content map'
54 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
55 when: 'Create schema set method is invoked'
56 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
57 then: 'Parameters are validated and processing is delegated to persistence service'
58 1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
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.class)
84 def 'Get schema set by name and dataspace.'() {
85 given: 'an already present schema set'
86 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
87 and: 'yang resource cache returns the expected schema set'
88 mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
89 when: 'get schema set method is invoked'
90 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
91 then: 'the correct schema set is returned'
92 result.getName().contains('someSchemaSet')
93 result.getDataspaceName().contains('someDataspace')
94 result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
95 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
96 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
99 def 'Get schema sets by dataspace name.'() {
100 given: 'two already present schema sets'
101 def moduleReference = new ModuleReference('sample1', '2022-12-07')
102 def sampleSchemaSet1 = new SchemaSet('testSchemaSet1', 'testDataspace', [moduleReference])
103 def sampleSchemaSet2 = new SchemaSet('testSchemaSet2', 'testDataspace', [moduleReference])
104 and: 'the persistence service returns the created schema sets'
105 mockCpsModulePersistenceService.getSchemaSetsByDataspaceName('testDataspace') >> [sampleSchemaSet1, sampleSchemaSet2]
106 and: 'yang resource cache always returns a schema source set'
107 def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
108 mockYangTextSchemaSourceSetCache.get('testDataspace', _) >> mockYangTextSchemaSourceSet
109 when: 'get schema sets method is invoked'
110 def result = objectUnderTest.getSchemaSets('testDataspace')
111 then: 'the correct schema sets are returned'
112 assert result.size() == 2
113 assert result.containsAll(sampleSchemaSet1, sampleSchemaSet2)
114 and: 'the Cps Validator is called on the dataspaceName'
115 1 * mockCpsValidator.validateNameCharacters('testDataspace')
118 def 'Delete schema-set when cascade is allowed.'() {
119 given: '#numberOfAnchors anchors are associated with schemaset'
120 def associatedAnchors = createAnchors(numberOfAnchors)
121 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
122 when: 'schema set deletion is requested with cascade allowed'
123 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
124 then: 'anchor deletion is called #numberOfAnchors times'
125 numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
126 and: 'persistence service method is invoked with same parameters'
127 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
128 and: 'schema set will be removed from the cache'
129 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
130 and: 'orphan yang resources are deleted'
131 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
132 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
133 1 * mockCpsValidator.validateNameCharacters('my-dataspace', _)
134 where: 'following parameters are used'
135 numberOfAnchors << [0, 3]
138 def 'Delete schema-set when cascade is prohibited.'() {
139 given: 'no anchors are associated with schemaset'
140 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
141 when: 'schema set deletion is requested with cascade allowed'
142 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
143 then: 'no anchors are deleted'
144 0 * mockCpsAdminService.deleteAnchor(_, _)
145 and: 'persistence service method is invoked with same parameters'
146 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
147 and: 'schema set will be removed from the cache'
148 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
149 and: 'orphan yang resources are deleted'
150 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
151 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
152 1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
155 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
156 given: '2 anchors are associated with schemaset'
157 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
158 when: 'schema set deletion is requested with cascade allowed'
159 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
160 then: 'Schema-Set in Use exception is thrown'
161 thrown(SchemaSetInUseException)
164 def createAnchors(int anchorCount) {
166 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
170 def 'Delete multiple schema-sets when cascade is allowed.'() {
171 given: '#numberOfAnchors anchors are associated with each schemaset'
172 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset1') >> createAnchors(numberOfAnchors)
173 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset2') >> createAnchors(numberOfAnchors)
174 when: 'schema set deletion is requested with cascade allowed'
175 objectUnderTest.deleteSchemaSetsWithCascade('my-dataspace', ['my-schemaset1', 'my-schemaset2'])
176 then: 'anchor deletion is called 2 * #numberOfAnchors times'
177 (2 * numberOfAnchors) * mockCpsAdminService.deleteAnchor('my-dataspace', _)
178 and: 'persistence service method is invoked with same parameters'
179 mockCpsModulePersistenceService.deleteSchemaSets('my-dataspace', _)
180 and: 'schema sets will be removed from the cache'
181 2 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', _)
182 and: 'orphan yang resources are deleted'
183 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
184 and: 'the CpsValidator is called on the dataspaceName'
185 1 * mockCpsValidator.validateNameCharacters('my-dataspace')
186 and: 'the CpsValidator is called on the schemaSetNames'
187 1 * mockCpsValidator.validateNameCharacters(_)
188 where: 'following parameters are used'
189 numberOfAnchors << [0, 3]
192 def 'Get all yang resources module references.'() {
193 given: 'an already present module reference'
194 def moduleReferences = [new ModuleReference('some module name','some revision name')]
195 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
196 when: 'get yang resource module references is called'
197 def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName')
198 then: 'the list provided by persistence service is returned as result'
199 result == moduleReferences
200 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
201 1 * mockCpsValidator.validateNameCharacters('someDataspaceName')
204 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
205 given: 'the module store service service returns a list module references'
206 def moduleReferences = [new ModuleReference()]
207 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
208 when: 'get yang resource module references is called for dataspace name and anchor name'
209 def result = objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName')
210 then: 'the list provided by persistence service is returned as result'
211 result == moduleReferences
212 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
213 1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someAnchorName')
216 def 'Identifying new module references'(){
217 given: 'module references from cm handle'
218 def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
219 when: 'identifyNewModuleReferences is called'
220 objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
221 then: 'cps module persistence service is called with module references to check'
222 1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
225 def 'Getting module definitions.'() {
226 when: 'get module definitions method is called with a valid dataspace and anchor name'
227 objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
228 then: 'CPS module persistence service is invoked the correct number of times'
229 1 * mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name')
230 and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
231 1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name')