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