358a6fb3f3db1952d0e5dcb3b821594c529bede5
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsModuleServiceImplSpec.groovy
1 /*
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  *  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.YangTextSchemaSourceSet
36 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
37 import spock.lang.Specification
38 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
40
41 class CpsModuleServiceImplSpec extends Specification {
42
43     def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
44     def mockCpsAdminService = Mock(CpsAdminService)
45     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
46     def mockCpsValidator = Mock(CpsValidator)
47
48     def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService, mockCpsValidator)
49
50     def 'Create schema set.'() {
51         given: 'Valid yang resource as name-to-content map'
52             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
53         when: 'Create schema set method is invoked'
54             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
55         then: 'Parameters are validated and processing is delegated to persistence service'
56             1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
57         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
58             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
59     }
60
61     def 'Create schema set from new modules and existing modules.'() {
62         given: 'a list of existing modules module reference'
63             def moduleReferenceForExistingModule = new ModuleReference("test",  "2021-10-12","test.org")
64             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
65         when: 'create schema set from modules method is invoked'
66             objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
67         then: 'processing is delegated to persistence service'
68             1 * mockCpsModulePersistenceService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
69         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
70             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someSchemaSetName')
71     }
72
73     def 'Create schema set from invalid resources'() {
74         given: 'Invalid yang resource as name-to-content map'
75             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
76         when: 'Create schema set method is invoked'
77             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
78         then: 'Model validation exception is thrown'
79             thrown(ModelValidationException.class)
80     }
81
82     def 'Get schema set by name and dataspace.'() {
83         given: 'an already present schema set'
84             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
85         and: 'yang resource cache returns the expected schema set'
86             mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
87         when: 'get schema set method is invoked'
88             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
89         then: 'the correct schema set is returned'
90             result.getName().contains('someSchemaSet')
91             result.getDataspaceName().contains('someDataspace')
92             result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
93         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
94             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
95     }
96
97     def 'Get schema sets by dataspace name.'() {
98         given: 'two already present schema sets'
99             def moduleReference = new ModuleReference('sample1', '2022-12-07')
100             def sampleSchemaSet1 = new SchemaSet('testSchemaSet1', 'testDataspace', [moduleReference])
101             def sampleSchemaSet2 = new SchemaSet('testSchemaSet2', 'testDataspace', [moduleReference])
102         and: 'the persistence service returns the created schema sets'
103             mockCpsModulePersistenceService.getSchemaSetsByDataspaceName('testDataspace') >> [sampleSchemaSet1, sampleSchemaSet2]
104         and: 'yang resource cache always returns a schema source set'
105             def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
106             mockYangTextSchemaSourceSetCache.get('testDataspace', _) >> mockYangTextSchemaSourceSet
107         when: 'get schema sets method is invoked'
108             def result = objectUnderTest.getSchemaSets('testDataspace')
109         then: 'the correct schema sets are returned'
110             assert result.size() == 2
111             assert result.containsAll(sampleSchemaSet1, sampleSchemaSet2)
112         and: 'the Cps Validator is called on the dataspaceName'
113             1 * mockCpsValidator.validateNameCharacters('testDataspace')
114     }
115
116     def 'Delete schema-set when cascade is allowed.'() {
117         given: '#numberOfAnchors anchors are associated with schemaset'
118             def associatedAnchors = createAnchors(numberOfAnchors)
119             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
120         when: 'schema set deletion is requested with cascade allowed'
121             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
122         then: 'anchor deletion is called #numberOfAnchors times'
123             numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
124         and: 'persistence service method is invoked with same parameters'
125             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
126         and: 'schema set will be removed from the cache'
127             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
128         and: 'orphan yang resources are deleted'
129             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
130         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
131             1 * mockCpsValidator.validateNameCharacters('my-dataspace', _)
132         where: 'following parameters are used'
133             numberOfAnchors << [0, 3]
134     }
135
136     def 'Delete schema-set when cascade is prohibited.'() {
137         given: 'no anchors are associated with schemaset'
138             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
139         when: 'schema set deletion is requested with cascade allowed'
140             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
141         then: 'no anchors are deleted'
142             0 * mockCpsAdminService.deleteAnchor(_, _)
143         and: 'persistence service method is invoked with same parameters'
144             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
145         and: 'schema set will be removed from the cache'
146             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
147         and: 'orphan yang resources are deleted'
148             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
149         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
150             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
151     }
152
153     def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
154         given: '2 anchors are associated with schemaset'
155             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
156         when: 'schema set deletion is requested with cascade allowed'
157             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
158         then: 'Schema-Set in Use exception is thrown'
159             thrown(SchemaSetInUseException)
160     }
161
162     def createAnchors(int anchorCount) {
163         def anchors = []
164         (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
165         return anchors
166     }
167
168     def 'Get all yang resources module references.'() {
169         given: 'an already present module reference'
170             def moduleReferences = [new ModuleReference('some module name','some revision name')]
171             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
172         when: 'get yang resource module references is called'
173             def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName')
174         then: 'the list provided by persistence service is returned as result'
175             result == moduleReferences
176         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
177             1 * mockCpsValidator.validateNameCharacters('someDataspaceName')
178     }
179
180     def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
181         given: 'the module store service service returns a list module references'
182             def moduleReferences = [new ModuleReference()]
183             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
184         when: 'get yang resource module references is called for dataspace name and anchor name'
185             def result = objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName')
186         then: 'the list provided by persistence service is returned as result'
187             result == moduleReferences
188         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
189             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someAnchorName')
190     }
191
192     def 'Identifying new module references'(){
193         given: 'module references from cm handle'
194             def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
195         when: 'identifyNewModuleReferences is called'
196             objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
197         then: 'cps module persistence service is called with module references to check'
198             1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
199     }
200
201     def 'Getting module definitions.'() {
202         when: 'get module definitions method is called with a valid dataspace and anchor name'
203             objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
204         then: 'CPS module persistence service is invoked the correct number of times'
205             1 * mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name')
206         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
207             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name')
208     }
209 }