2c23aa1bc8d821c3da279245627ec482d1cc862e
[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.ExtendedModuleReference
29 import org.onap.cps.spi.model.ModuleReference
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.test.context.SpringBootTest
33 import org.springframework.cache.CacheManager
34 import org.springframework.cache.annotation.EnableCaching
35 import org.springframework.cache.caffeine.CaffeineCacheManager
36 import org.springframework.test.context.ContextConfiguration
37 import spock.lang.Specification
38
39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
40 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
41
42 @SpringBootTest
43 @EnableCaching
44 @ContextConfiguration(classes = [YangTextSchemaSourceSetCache.class, CpsModuleServiceImpl.class])
45 class CpsModuleServiceImplSpec extends Specification {
46
47     @SpringBean
48     CpsModulePersistenceService mockModuleStoreService = Mock()
49
50     @SpringBean
51     CacheManager cacheManager = new CaffeineCacheManager("yangSchema")
52
53     @Autowired
54     CpsModuleServiceImpl objectUnderTest
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 new modules and existing modules.'() {
66         given: 'a list of existing modules module reference'
67             def moduleReferenceForExistingModule = new ExtendedModuleReference("test", "test.org", "2021-10-12")
68             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
69         when: 'create schema set from modules method is invoked'
70             objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
71         then: 'processing is delegated to persistence service'
72             1 * mockModuleStoreService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
73
74     }
75
76     def 'Create schema set from invalid resources'() {
77         given: 'Invalid yang resource as name-to-content map'
78             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
79         when: 'Create schema set method is invoked'
80             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
81         then: 'Model validation exception is thrown'
82             thrown(ModelValidationException.class)
83     }
84
85     def 'Get schema set by name and dataspace.'() {
86         given: 'an already present schema set'
87             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
88             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
89         when: 'get schema set method is invoked'
90             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
91         then: 'the correct schema set is returned'
92             result.getName().contains('someSchemaSet')
93             result.getDataspaceName().contains('someDataspace')
94             result.getExtendedModuleReferences().contains(new ExtendedModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
95     }
96
97     def 'Schema set caching.'() {
98         given: 'an  schema set'
99             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
100         when: 'get schema set method is invoked twice'
101             2.times {
102                 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
103             }
104         then: 'the persistency service called only once'
105             1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
106     }
107
108     def 'Delete set by name and dataspace with #cascadeDeleteOption.'() {
109         when: 'schema set deletion is requested'
110             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
111         then: 'persistence service method is invoked with same parameters'
112             mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
113         where: 'following parameters are used'
114             dataspaceName | schemaSetname   | cascadeDeleteOption
115             'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
116             'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
117     }
118
119     def 'Get all yang resources module references.'(){
120         given: 'an already present module reference'
121             def moduleReferences = [new ExtendedModuleReference()]
122             mockModuleStoreService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
123         expect: 'the list provided by persistence service is returned as result'
124             objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
125     }
126
127
128     def 'Get all yang resources module references for the given dataspace name and anchor name.'(){
129         given: 'the module store service service returns a list module references'
130             def moduleReferences = [new ModuleReference()]
131             mockModuleStoreService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
132         expect: 'the list provided by persistence service is returned as result'
133             objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences
134     }
135 }