2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Nordix Foundation
4 * Modifications Copyright (C) 2020-2021 Pantheon.tech
5 * Modifications Copyright (C) 2020-2021 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.spi.CpsModulePersistenceService
27 import org.onap.cps.spi.exceptions.ModelValidationException
28 import org.onap.cps.spi.model.ExtendedModuleReference
29 import org.spockframework.spring.SpringBean
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.test.context.SpringBootTest
32 import org.springframework.cache.CacheManager
33 import org.springframework.cache.annotation.EnableCaching
34 import org.springframework.cache.caffeine.CaffeineCacheManager
35 import org.springframework.test.context.ContextConfiguration
36 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
43 @ContextConfiguration(classes = [YangTextSchemaSourceSetCache.class, CpsModuleServiceImpl.class])
44 class CpsModuleServiceImplSpec extends Specification {
47 CpsModulePersistenceService mockModuleStoreService = Mock()
50 CacheManager cacheManager = new CaffeineCacheManager("yangSchema")
53 CpsModuleServiceImpl objectUnderTest
55 def 'Create schema set.'() {
56 given: 'Valid yang resource as name-to-content map'
57 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
58 when: 'Create schema set method is invoked'
59 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
60 then: 'Parameters are validated and processing is delegated to persistence service'
61 1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
64 def 'Create schema set from new modules and existing modules.'() {
65 given: 'a list of existing modules module reference'
66 def moduleReferenceForExistingModule = new ExtendedModuleReference("test", "test.org", "2021-10-12")
67 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
68 when: 'create schema set from modules method is invoked'
69 objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
70 then: 'processing is delegated to persistence service'
71 1 * mockModuleStoreService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
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)
84 def 'Get schema set by name and dataspace.'() {
85 given: 'an already present schema set'
86 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
87 mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
88 when: 'get schema set method is invoked'
89 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
90 then: 'the correct schema set is returned'
91 result.getName().contains('someSchemaSet')
92 result.getDataspaceName().contains('someDataspace')
93 result.getExtendedModuleReferences().contains(new ExtendedModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
96 def 'Schema set caching.'() {
97 given: 'an schema set'
98 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
99 when: 'get schema set method is invoked twice'
101 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
103 then: 'the persistency service called only once'
104 1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
107 def 'Delete set by name and dataspace with #cascadeDeleteOption.'() {
108 when: 'schema set deletion is requested'
109 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
110 then: 'persistence service method is invoked with same parameters'
111 mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
112 where: 'following parameters are used'
113 dataspaceName | schemaSetname | cascadeDeleteOption
114 'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
115 'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
118 def 'Get all yang resources module references.'(){
119 given: 'an already present module reference'
120 def moduleReferences = [new ExtendedModuleReference()]
121 mockModuleStoreService.getAllYangResourceModuleReferences('someDataspaceName') >> moduleReferences
122 expect: 'the list provided by persistence service is returned as result'
123 objectUnderTest.getAllYangResourceModuleReferences('someDataspaceName') == moduleReferences