Add method to get YANG module sources for CM handle
[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  *  ================================================================================
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl
24
25 import org.onap.cps.TestUtils
26 import org.onap.cps.api.CpsAdminService
27 import org.onap.cps.spi.CpsModulePersistenceService
28 import org.onap.cps.spi.exceptions.DataValidationException
29 import org.onap.cps.spi.exceptions.ModelValidationException
30 import org.onap.cps.spi.exceptions.SchemaSetInUseException
31 import org.onap.cps.spi.model.Anchor
32 import org.onap.cps.spi.model.ModuleReference
33 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
34 import spock.lang.Specification
35 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
36 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
37
38 class CpsModuleServiceImplSpec extends Specification {
39
40     def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
41     def mockCpsAdminService = Mock(CpsAdminService)
42     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
43
44     def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService)
45
46     def 'Create schema set.'() {
47         given: 'Valid yang resource as name-to-content map'
48             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
49         when: 'Create schema set method is invoked'
50             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
51         then: 'Parameters are validated and processing is delegated to persistence service'
52             1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
53     }
54
55     def 'Create a schema set with an invalid #scenario.'() {
56         when: 'create dataspace method is invoked with incorrectly named dataspace'
57             objectUnderTest.createSchemaSet(dataspaceName, schemaSetName, _ as Map<String, String>)
58         then: 'a data validation exception is thrown'
59             thrown(DataValidationException)
60         and: 'the persistence service method is not invoked'
61             0 * mockCpsModulePersistenceService.storeSchemaSet(_, _, _)
62         where: 'the following parameters are used'
63             scenario                         | dataspaceName                 | schemaSetName
64             'dataspace name'                 | 'dataspace names with spaces' | 'schemaSetName'
65             'schema set name name'           | 'dataspaceName'               | 'schema set name with spaces'
66             'dataspace and schema set name'  | 'dataspace name with spaces'  | 'schema set name with spaces'
67     }
68
69     def 'Create schema set from new modules and existing modules.'() {
70         given: 'a list of existing modules module reference'
71             def moduleReferenceForExistingModule = new ModuleReference("test",  "2021-10-12","test.org")
72             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
73         when: 'create schema set from modules method is invoked'
74             objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
75         then: 'processing is delegated to persistence service'
76             1 * mockCpsModulePersistenceService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
77     }
78
79     def 'Create schema set from new modules and existing modules with invalid #scenario.'() {
80         when: 'create dataspace method is invoked with incorrectly named dataspace'
81             objectUnderTest.createSchemaSetFromModules(dataspaceName, schemaSetName, _ as Map<String, String>, _ as Collection<ModuleReference>)
82         then: 'a data validation exception is thrown'
83             thrown(DataValidationException)
84         and: 'the persistence service method is not invoked'
85             0 * mockCpsModulePersistenceService.storeSchemaSetFromModules(_, _, _)
86         where: 'the following parameters are used'
87             scenario                         | dataspaceName                 | schemaSetName
88             'dataspace name'                 | 'dataspace names with spaces' | 'schemaSetName'
89             'schema set name name'           | 'dataspaceName'               | 'schema set name with spaces'
90             'dataspace and schema set name'  | 'dataspace name with spaces'  | 'schema set name with spaces'
91     }
92
93     def 'Create schema set from invalid resources'() {
94         given: 'Invalid yang resource as name-to-content map'
95             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
96         when: 'Create schema set method is invoked'
97             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
98         then: 'Model validation exception is thrown'
99             thrown(ModelValidationException.class)
100     }
101
102     def 'Get schema set by name and dataspace.'() {
103         given: 'an already present schema set'
104             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
105         and: 'yang resource cache returns the expected schema set'
106             mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
107         when: 'get schema set method is invoked'
108             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
109         then: 'the correct schema set is returned'
110             result.getName().contains('someSchemaSet')
111             result.getDataspaceName().contains('someDataspace')
112             result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
113     }
114
115     def 'Get a schema set with an invalid #scenario'() {
116         when: 'create dataspace method is invoked with incorrectly named dataspace'
117             objectUnderTest.getSchemaSet(dataspaceName, schemaSetName)
118         then: 'a data validation exception is thrown'
119             thrown(DataValidationException)
120         and: 'the yang resource cache is not invoked'
121             0 * mockYangTextSchemaSourceSetCache.get(_, _)
122         where: 'the following parameters are used'
123             scenario                        | dataspaceName                 | schemaSetName
124             'dataspace name'                | 'dataspace names with spaces' | 'schemaSetName'
125             'schema set name'               | 'dataspaceName'               | 'schema set name with spaces'
126             'dataspace and schema set name' | 'dataspace name with spaces'  | 'schema set name with spaces'
127     }
128
129     def 'Delete schema-set when cascade is allowed.'() {
130         given: '#numberOfAnchors anchors are associated with schemaset'
131             def associatedAnchors = createAnchors(numberOfAnchors)
132             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
133         when: 'schema set deletion is requested with cascade allowed'
134             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
135         then: 'anchor deletion is called #numberOfAnchors times'
136             numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
137         and: 'persistence service method is invoked with same parameters'
138             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
139         and: 'schema set will be removed from the cache'
140             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
141         and: 'orphan yang resources are deleted'
142             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
143         where: 'following parameters are used'
144             numberOfAnchors << [0, 3]
145     }
146
147     def 'Delete schema-set when cascade is prohibited.'() {
148         given: 'no anchors are associated with schemaset'
149             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
150         when: 'schema set deletion is requested with cascade allowed'
151             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
152         then: 'no anchors are deleted'
153             0 * mockCpsAdminService.deleteAnchor(_, _)
154         and: 'persistence service method is invoked with same parameters'
155             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
156         and: 'schema set will be removed from the cache'
157             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
158         and: 'orphan yang resources are deleted'
159             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
160     }
161
162     def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
163         given: '2 anchors are associated with schemaset'
164             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
165         when: 'schema set deletion is requested with cascade allowed'
166             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
167         then: 'Schema-Set in Use exception is thrown'
168             thrown(SchemaSetInUseException)
169     }
170
171     def 'Delete a schema set with an invalid #scenario.'() {
172         when: 'create dataspace method is invoked with incorrectly named dataspace'
173             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_ALLOWED)
174         then: 'a data validation exception is thrown'
175             thrown(DataValidationException)
176         and: 'anchor deletion is called 0 times'
177             0 * mockCpsAdminService.deleteAnchor(_, _)
178         and: 'the delete schema set persistence service method is not invoked'
179             0 * mockCpsModulePersistenceService.deleteSchemaSet(_, _, _)
180         and: 'schema set will be removed from the cache is not invoked'
181             0 * mockYangTextSchemaSourceSetCache.removeFromCache(_, _)
182         and: 'orphan yang resources are deleted is not invoked'
183             0 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
184         where: 'the following parameters are used'
185             scenario                         | dataspaceName                 | schemaSetName
186             'dataspace name'                 | 'dataspace names with spaces' | 'schemaSetName'
187             'schema set name name'           | 'dataspaceName'               | 'schema set name with spaces'
188             'dataspace and schema set name'  | 'dataspace name with spaces'  | 'schema set name with spaces'
189     }
190
191     def createAnchors(int anchorCount) {
192         def anchors = []
193         (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
194         return anchors
195     }
196
197     def 'Get all yang resources module references.'() {
198         given: 'an already present module reference'
199             def moduleReferences = [new ModuleReference('some module name','some revision name')]
200             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
201         expect: 'the list provided by persistence service is returned as result'
202             objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
203     }
204
205     def 'Get all yang resources module references given an invalid dataspace name.'() {
206         when: 'the get yang resources module references method is invoked with an invalid dataspace name'
207             objectUnderTest.getYangResourceModuleReferences('dataspace name with spaces')
208         then: 'a data validation exception is thrown'
209             thrown(DataValidationException)
210         and: 'the persistence service method is not invoked'
211             0 * mockCpsModulePersistenceService.getYangResourceModuleReferences(_)
212     }
213
214
215     def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
216         given: 'the module store service service returns a list module references'
217             def moduleReferences = [new ModuleReference()]
218             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
219         expect: 'the list provided by persistence service is returned as result'
220             objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences
221     }
222
223     def 'Get all yang resources module references given an invalid #scenario.'() {
224         when: 'the get yang resources module references method is invoked with invalid #scenario'
225             objectUnderTest.getYangResourcesModuleReferences(dataspaceName, anchorName)
226         then: 'a data validation exception is thrown'
227             thrown(DataValidationException)
228         and: 'the persistence service method is not invoked'
229             0 * mockCpsModulePersistenceService.getYangResourceModuleReferences(_, _)
230         where: 'the following parameters are used'
231             scenario                     | dataspaceName                 | anchorName
232             'dataspace name'             | 'dataspace names with spaces' | 'anchorName'
233             'anchor name'                | 'dataspaceName'               | 'anchor name with spaces'
234             'dataspace and anchor name'  | 'dataspace name with spaces'  | 'anchor name with spaces'
235     }
236
237     def 'Identifying new module references'(){
238         given: 'module references from cm handle'
239             def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
240         when: 'identifyNewModuleReferences is called'
241             objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
242         then: 'cps module persistence service is called with module references to check'
243             1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
244     }
245
246     def 'Getting module definitions.'() {
247         when: 'get module definitions method is called with a valid dataspace and anchor name'
248             objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
249         then: 'CPS module persistence service is invoked the correct number of times'
250             1 * mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name')
251     }
252 }