CPS Validator Changes
[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.ModelValidationException
29 import org.onap.cps.spi.exceptions.SchemaSetInUseException
30 import org.onap.cps.spi.utils.CpsValidator
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     def mockCpsValidator = Mock(CpsValidator)
44
45     def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService, mockCpsValidator)
46
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)
54         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
55             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
56     }
57
58     def 'Create schema set from new modules and existing modules.'() {
59         given: 'a list of existing modules module reference'
60             def moduleReferenceForExistingModule = new ModuleReference("test",  "2021-10-12","test.org")
61             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
62         when: 'create schema set from modules method is invoked'
63             objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
64         then: 'processing is delegated to persistence service'
65             1 * mockCpsModulePersistenceService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
66         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
67             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someSchemaSetName')
68     }
69
70     def 'Create schema set from invalid resources'() {
71         given: 'Invalid yang resource as name-to-content map'
72             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
73         when: 'Create schema set method is invoked'
74             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
75         then: 'Model validation exception is thrown'
76             thrown(ModelValidationException.class)
77     }
78
79     def 'Get schema set by name and dataspace.'() {
80         given: 'an already present schema set'
81             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
82         and: 'yang resource cache returns the expected schema set'
83             mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
84         when: 'get schema set method is invoked'
85             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
86         then: 'the correct schema set is returned'
87             result.getName().contains('someSchemaSet')
88             result.getDataspaceName().contains('someDataspace')
89             result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
90         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
91             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
92     }
93
94     def 'Delete schema-set when cascade is allowed.'() {
95         given: '#numberOfAnchors anchors are associated with schemaset'
96             def associatedAnchors = createAnchors(numberOfAnchors)
97             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
98         when: 'schema set deletion is requested with cascade allowed'
99             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
100         then: 'anchor deletion is called #numberOfAnchors times'
101             numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
102         and: 'persistence service method is invoked with same parameters'
103             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
104         and: 'schema set will be removed from the cache'
105             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
106         and: 'orphan yang resources are deleted'
107             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
108         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
109             1 * mockCpsValidator.validateNameCharacters('my-dataspace', _)
110         where: 'following parameters are used'
111             numberOfAnchors << [0, 3]
112     }
113
114     def 'Delete schema-set when cascade is prohibited.'() {
115         given: 'no anchors are associated with schemaset'
116             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
117         when: 'schema set deletion is requested with cascade allowed'
118             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
119         then: 'no anchors are deleted'
120             0 * mockCpsAdminService.deleteAnchor(_, _)
121         and: 'persistence service method is invoked with same parameters'
122             1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
123         and: 'schema set will be removed from the cache'
124             1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
125         and: 'orphan yang resources are deleted'
126             1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
127         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
128             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
129     }
130
131     def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
132         given: '2 anchors are associated with schemaset'
133             mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
134         when: 'schema set deletion is requested with cascade allowed'
135             objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
136         then: 'Schema-Set in Use exception is thrown'
137             thrown(SchemaSetInUseException)
138     }
139
140     def createAnchors(int anchorCount) {
141         def anchors = []
142         (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
143         return anchors
144     }
145
146     def 'Get all yang resources module references.'() {
147         given: 'an already present module reference'
148             def moduleReferences = [new ModuleReference('some module name','some revision name')]
149             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
150         when: 'get yang resource module references is called'
151             def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName')
152         then: 'the list provided by persistence service is returned as result'
153             result == moduleReferences
154         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
155             1 * mockCpsValidator.validateNameCharacters('someDataspaceName')
156     }
157
158     def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
159         given: 'the module store service service returns a list module references'
160             def moduleReferences = [new ModuleReference()]
161             mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
162         when: 'get yang resource module references is called for dataspace name and anchor name'
163             def result = objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName')
164         then: 'the list provided by persistence service is returned as result'
165             result == moduleReferences
166         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
167             1 * mockCpsValidator.validateNameCharacters('someDataspaceName', 'someAnchorName')
168     }
169
170     def 'Identifying new module references'(){
171         given: 'module references from cm handle'
172             def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
173         when: 'identifyNewModuleReferences is called'
174             objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
175         then: 'cps module persistence service is called with module references to check'
176             1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
177     }
178
179     def 'Getting module definitions.'() {
180         when: 'get module definitions method is called with a valid dataspace and anchor name'
181             objectUnderTest.getModuleDefinitionsByAnchorName('some-dataspace-name', 'some-anchor-name')
182         then: 'CPS module persistence service is invoked the correct number of times'
183             1 * mockCpsModulePersistenceService.getYangResourceDefinitions('some-dataspace-name', 'some-anchor-name')
184         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
185             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name')
186     }
187 }