Investigate and update Spock version
[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.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
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     @SpringBean
50     CpsDataPersistenceService mockDataPersistenceService = Mock()
51     @Autowired
52     CpsModuleServiceImpl objectUnderTest = new CpsModuleServiceImpl()
53     @SpringBean
54     CacheManager cacheManager = new CaffeineCacheManager("yangSchema");
55
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)
63     }
64
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)
72     }
73
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'))
84     }
85
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'
90             2.times {
91                 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
92             }
93         then: 'the persistency service called only once'
94             1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
95     }
96
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
106     }
107 }