Introduce caffeine cache
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsModuleServiceImplSpec.groovy
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.api.impl
22
23 import org.onap.cps.TestUtils
24 import org.onap.cps.api.CpsAdminService
25 import org.onap.cps.spi.CpsModulePersistenceService
26 import org.onap.cps.spi.exceptions.ModelValidationException
27 import org.onap.cps.spi.model.ModuleReference
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.context.SpringBootTest
31 import org.springframework.cache.CacheManager
32 import org.springframework.cache.caffeine.CaffeineCacheManager
33 import org.springframework.context.annotation.ComponentScan
34 import org.springframework.test.context.ContextConfiguration
35 import spock.lang.Specification
36 import spock.lang.Unroll
37
38 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
40
41 @SpringBootTest
42 @ComponentScan("org.onap.cps")
43 @ContextConfiguration(classes = CpsModuleServiceImplSpec.class)
44 class CpsModuleServiceImplSpec extends Specification {
45     @SpringBean
46     CpsModulePersistenceService mockModuleStoreService = Mock()
47     @SpringBean
48     CpsAdminService mockCpsAdminService = Mock()
49     @Autowired
50     CpsModuleServiceImpl objectUnderTest = new CpsModuleServiceImpl()
51     @SpringBean
52     CacheManager cacheManager = new CaffeineCacheManager("yangSchema");
53
54     def 'Create schema set'() {
55         given: 'Valid yang resource as name-to-content map'
56             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
57         when: 'Create schema set method is invoked'
58             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
59         then: 'Parameters are validated and processing is delegated to persistence service'
60             1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
61     }
62
63     def 'Create schema set from invalid resources'() {
64         given: 'Invalid yang resource as name-to-content map'
65             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
66         when: 'Create schema set method is invoked'
67             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
68         then: 'Model validation exception is thrown'
69             thrown(ModelValidationException.class)
70     }
71
72     def 'Get schema set by name and dataspace.'() {
73         given: 'an already present schema set'
74             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
75             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
76         when: 'get schema set method is invoked'
77             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
78         then: 'the correct schema set is returned'
79             result.getName().contains('someSchemaSet')
80             result.getDataspaceName().contains('someDataspace')
81             result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
82     }
83
84     def 'Schema set caching.'() {
85         given: 'an  schema set'
86             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
87         when: 'get schema set method is invoked twice'
88             2.times {
89                 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
90             }
91         then: 'the persistency service called only once'
92             1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
93     }
94
95     @Unroll
96     def 'Delete set by name and dataspace with #cascadeDeleteOption.'(){
97         when: 'schema set deletion is requested'
98             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
99         then: 'persistence service method is invoked with same parameters'
100             mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
101         where: 'following parameters are used'
102             dataspaceName | schemaSetname | cascadeDeleteOption
103             'dataspace-1'  | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
104             'dataspace-2'  | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
105     }
106 }