2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl
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.model.Anchor
31 import org.onap.cps.spi.model.ExtendedModuleReference
32 import org.onap.cps.spi.model.ModuleReference
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.cache.CacheManager
37 import org.springframework.cache.annotation.EnableCaching
38 import org.springframework.cache.caffeine.CaffeineCacheManager
39 import org.springframework.test.context.ContextConfiguration
40 import spock.lang.Specification
41 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
42 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
46 @ContextConfiguration(classes = [YangTextSchemaSourceSetCache, CpsModuleServiceImpl])
47 class CpsModuleServiceImplSpec extends Specification {
50 CpsModulePersistenceService mockModuleStoreService = Mock()
53 CpsAdminService mockCpsAdminService = Mock()
56 CacheManager cacheManager = new CaffeineCacheManager("yangSchema")
59 CpsModuleServiceImpl objectUnderTest
61 def 'Create schema set.'() {
62 given: 'Valid yang resource as name-to-content map'
63 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
64 when: 'Create schema set method is invoked'
65 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
66 then: 'Parameters are validated and processing is delegated to persistence service'
67 1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
70 def 'Create schema set from new modules and existing modules.'() {
71 given: 'a list of existing modules module reference'
72 def moduleReferenceForExistingModule = new ExtendedModuleReference("test", "test.org", "2021-10-12")
73 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
74 when: 'create schema set from modules method is invoked'
75 objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
76 then: 'processing is delegated to persistence service'
77 1 * mockModuleStoreService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
81 def 'Create schema set from invalid resources'() {
82 given: 'Invalid yang resource as name-to-content map'
83 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
84 when: 'Create schema set method is invoked'
85 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
86 then: 'Model validation exception is thrown'
87 thrown(ModelValidationException.class)
90 def 'Get schema set by name and dataspace.'() {
91 given: 'an already present schema set'
92 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
93 mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
94 when: 'get schema set method is invoked'
95 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
96 then: 'the correct schema set is returned'
97 result.getName().contains('someSchemaSet')
98 result.getDataspaceName().contains('someDataspace')
99 result.getExtendedModuleReferences().contains(new ExtendedModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
102 def 'Schema set caching.'() {
103 given: 'an schema set'
104 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
105 when: 'get schema set method is invoked twice'
107 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
109 then: 'the persistency service called only once'
110 1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
113 def 'Delete schema-set when cascade is allowed.'() {
114 given: '#numberOfAnchors anchors are associated with schemaset'
115 def associatedAnchors = createAnchors(numberOfAnchors)
116 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
117 when: 'schema set deletion is requested with cascade allowed'
118 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
119 then: 'anchor deletion is called #numberOfAnchors times'
120 numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
121 and: 'persistence service method is invoked with same parameters'
122 1 * mockModuleStoreService.deleteSchemaSet('my-dataspace', 'my-schemaset')
123 and: 'orphan yang resources are deleted'
124 1 * mockModuleStoreService.deleteUnusedYangResourceModules()
125 where: 'following parameters are used'
126 numberOfAnchors << [0, 3]
129 def 'Delete schema-set when cascade is prohibited.'() {
130 given: 'no anchors are associated with schemaset'
131 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
132 when: 'schema set deletion is requested with cascade allowed'
133 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
134 then: 'no anchors are deleted'
135 0 * mockCpsAdminService.deleteAnchor(_, _)
136 and: 'persistence service method is invoked with same parameters'
137 1 * mockModuleStoreService.deleteSchemaSet('my-dataspace', 'my-schemaset')
138 and: 'orphan yang resources are deleted'
139 1 * mockModuleStoreService.deleteUnusedYangResourceModules()
142 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
143 given: '2 anchors are associated with schemaset'
144 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
145 when: 'schema set deletion is requested with cascade allowed'
146 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
147 then: 'Schema-Set in Use exception is thrown'
148 thrown(SchemaSetInUseException)
151 def createAnchors(int anchorCount) {
153 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
157 def 'Get all yang resources module references.'() {
158 given: 'an already present module reference'
159 def moduleReferences = [new ExtendedModuleReference()]
160 mockModuleStoreService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
161 expect: 'the list provided by persistence service is returned as result'
162 objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
166 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
167 given: 'the module store service service returns a list module references'
168 def moduleReferences = [new ModuleReference()]
169 mockModuleStoreService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
170 expect: 'the list provided by persistence service is returned as result'
171 objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences