Use constants for magic numbers in perf tests
[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 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
41
42 class CpsModuleServiceImplSpec extends Specification {
43
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()
49
50     def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService, mockCpsValidator,timedYangTextSchemaSourceSetBuilder)
51
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')
61     }
62
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')
73     }
74
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)
82     }
83
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')
97     }
98
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')
116     }
117
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]
136     }
137
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')
153     }
154
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)
162     }
163
164     def createAnchors(int anchorCount) {
165         def anchors = []
166         (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
167         return anchors
168     }
169
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', 'my-schemaset2']) >> createAnchors(numberOfAnchors * 2)
173         when: 'schema set deletion is requested with cascade allowed'
174             objectUnderTest.deleteSchemaSetsWithCascade('my-dataspace', ['my-schemaset1', 'my-schemaset2'])
175         then: 'anchor deletion is called #numberOfAnchors times'
176             mockCpsAdminService.deleteAnchors('my-dataspace', _)
177         and: 'persistence service method is invoked with same parameters'
178             mockCpsModulePersistenceService.deleteSchemaSets('my-dataspace', _)
179         and: 'schema sets will be removed from the cache'
180             2 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', _)
181         and: 'orphan yang resources are deleted'
182             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
183         and: 'the CpsValidator is called on the dataspaceName'
184             1 * mockCpsValidator.validateNameCharacters('my-dataspace')
185         and: 'the CpsValidator is called on the schemaSetNames'
186             1 * mockCpsValidator.validateNameCharacters(_)
187         where: 'following parameters are used'
188             numberOfAnchors << [0, 3]
189     }
190
191     def 'Get all yang resources module references.'() {
192         given: 'an already present module reference'
193             def moduleReferences = [new ModuleReference('some module name','some revision name')]
194             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
195         when: 'get yang resource module references is called'
196             def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName')
197         then: 'the list provided by persistence service is returned as result'
198             result == moduleReferences
199         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
200             1 * mockCpsValidator.validateNameCharacters('someDataspaceName')
201     }
202
203     def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
204         given: 'the module store service service returns a list module references'
205             def moduleReferences = [new ModuleReference()]
206             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
207         when: 'get yang resource module references is called for dataspace name and anchor name'
208             def result = objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName')
209         then: 'the list provided by persistence service is returned as result'
210             result == moduleReferences
211         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
212             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someAnchorName')
213     }
214
215     def 'Identifying new module references'(){
216         given: 'module references from cm handle'
217             def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
218         when: 'identifyNewModuleReferences is called'
219             objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
220         then: 'cps module persistence service is called with module references to check'
221             1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
222     }
223
224     def 'Getting module definitions.'() {
225         when: 'get module definitions method is called with a valid dataspace and anchor name'
226             objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
227         then: 'CPS module persistence service is invoked the correct number of times'
228             1 * mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name')
229         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
230             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name')
231     }
232 }