Merge "CPS-508: Create anchor/schemaset from new modules and existing modules"
[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 new modules and existing modules.'() {
65         given: 'a list of existing modules module reference'
66             def moduleReferenceForExistingModule = new ModuleReference("test", "test.org", "2021-10-12")
67             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
68         when: 'create schema set from modules method is invoked'
69             objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
70         then: 'processing is delegated to persistence service'
71             1 * mockModuleStoreService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
72
73     }
74
75     def 'Create schema set from invalid resources'() {
76         given: 'Invalid yang resource as name-to-content map'
77             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
78         when: 'Create schema set method is invoked'
79             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
80         then: 'Model validation exception is thrown'
81             thrown(ModelValidationException.class)
82     }
83
84     def 'Get schema set by name and dataspace.'() {
85         given: 'an already present schema set'
86             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
87             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
88         when: 'get schema set method is invoked'
89             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
90         then: 'the correct schema set is returned'
91             result.getName().contains('someSchemaSet')
92             result.getDataspaceName().contains('someDataspace')
93             result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
94     }
95
96     def 'Schema set caching.'() {
97         given: 'an  schema set'
98             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
99         when: 'get schema set method is invoked twice'
100             2.times {
101                 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
102             }
103         then: 'the persistency service called only once'
104             1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
105     }
106
107     def 'Delete set by name and dataspace with #cascadeDeleteOption.'() {
108         when: 'schema set deletion is requested'
109             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
110         then: 'persistence service method is invoked with same parameters'
111             mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
112         where: 'following parameters are used'
113             dataspaceName | schemaSetname   | cascadeDeleteOption
114             'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
115             'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
116     }
117
118     def 'Get all yang resources module references.'(){
119         given: 'an already present module reference'
120             def moduleReferences = [new ModuleReference()]
121             mockModuleStoreService.getAllYangResourcesModuleReferences() >> moduleReferences
122         expect: 'the list provided by persistence service is returned as result'
123             objectUnderTest.getAllYangResourcesModuleReferences() == moduleReferences
124     }
125 }