CPS-506: List all known modules and revision
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsModuleServiceImplSpec.groovy
1 /*
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl
24
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.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.annotation.EnableCaching
34 import org.springframework.cache.caffeine.CaffeineCacheManager
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 @EnableCaching
43 @ContextConfiguration(classes = [YangTextSchemaSourceSetCache.class, CpsModuleServiceImpl.class])
44 class CpsModuleServiceImplSpec extends Specification {
45
46     @SpringBean
47     CpsModulePersistenceService mockModuleStoreService = Mock()
48
49     @SpringBean
50     CacheManager cacheManager = new CaffeineCacheManager("yangSchema")
51
52     @Autowired
53     CpsModuleServiceImpl objectUnderTest
54
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)
62     }
63
64     def 'Create schema set from invalid resources'() {
65         given: 'Invalid yang resource as name-to-content map'
66             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
67         when: 'Create schema set method is invoked'
68             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
69         then: 'Model validation exception is thrown'
70             thrown(ModelValidationException.class)
71     }
72
73     def 'Get schema set by name and dataspace.'() {
74         given: 'an already present schema set'
75             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
76             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
77         when: 'get schema set method is invoked'
78             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
79         then: 'the correct schema set is returned'
80             result.getName().contains('someSchemaSet')
81             result.getDataspaceName().contains('someDataspace')
82             result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
83     }
84
85     def 'Schema set caching.'() {
86         given: 'an  schema set'
87             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
88         when: 'get schema set method is invoked twice'
89             2.times {
90                 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
91             }
92         then: 'the persistency service called only once'
93             1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
94     }
95
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
107     def 'Get all yang resources module references.'(){
108         given: 'an already present module reference'
109             def moduleReferences = [new ModuleReference()]
110             mockModuleStoreService.getAllYangResourcesModuleReferences() >> moduleReferences
111         expect: 'the list provided by persistence service is returned as result'
112             objectUnderTest.getAllYangResourcesModuleReferences() == moduleReferences
113     }
114 }