2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.api.impl
23 import org.onap.cps.TestUtils
24 import org.onap.cps.api.CpsAdminService
25 import org.onap.cps.spi.CpsDataPersistenceService
26 import org.onap.cps.spi.CpsModulePersistenceService
27 import org.onap.cps.spi.exceptions.ModelValidationException
28 import org.onap.cps.spi.model.ModuleReference
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.caffeine.CaffeineCacheManager
34 import org.springframework.context.annotation.ComponentScan
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
42 @ComponentScan("org.onap.cps")
43 @ContextConfiguration(classes = CpsModuleServiceImplSpec.class)
44 class CpsModuleServiceImplSpec extends Specification {
46 CpsModulePersistenceService mockModuleStoreService = Mock()
48 CpsAdminService mockCpsAdminService = Mock()
50 CpsDataPersistenceService mockDataPersistenceService = Mock()
52 CpsModuleServiceImpl objectUnderTest = new CpsModuleServiceImpl()
54 CacheManager cacheManager = new CaffeineCacheManager("yangSchema");
56 def 'Create schema set'() {
57 given: 'Valid yang resource as name-to-content map'
58 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
59 when: 'Create schema set method is invoked'
60 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
61 then: 'Parameters are validated and processing is delegated to persistence service'
62 1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
65 def 'Create schema set from invalid resources'() {
66 given: 'Invalid yang resource as name-to-content map'
67 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
68 when: 'Create schema set method is invoked'
69 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
70 then: 'Model validation exception is thrown'
71 thrown(ModelValidationException.class)
74 def 'Get schema set by name and dataspace.'() {
75 given: 'an already present schema set'
76 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
77 mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
78 when: 'get schema set method is invoked'
79 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
80 then: 'the correct schema set is returned'
81 result.getName().contains('someSchemaSet')
82 result.getDataspaceName().contains('someDataspace')
83 result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
86 def 'Schema set caching.'() {
87 given: 'an schema set'
88 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
89 when: 'get schema set method is invoked twice'
91 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
93 then: 'the persistency service called only once'
94 1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
97 def 'Delete set by name and dataspace with #cascadeDeleteOption.'() {
98 when: 'schema set deletion is requested'
99 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
100 then: 'persistence service method is invoked with same parameters'
101 mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
102 where: 'following parameters are used'
103 dataspaceName | schemaSetname | cascadeDeleteOption
104 'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
105 'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED