Attach a (JSON) data instance for a container with children to a given Anchor
[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 import spock.lang.Unroll
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 @ComponentScan("org.onap.cps")
44 @ContextConfiguration(classes = CpsModuleServiceImplSpec.class)
45 class CpsModuleServiceImplSpec extends Specification {
46     @SpringBean
47     CpsModulePersistenceService mockModuleStoreService = Mock()
48     @SpringBean
49     CpsAdminService mockCpsAdminService = Mock()
50     @SpringBean
51     CpsDataPersistenceService mockDataPersistenceService = Mock()
52     @Autowired
53     CpsModuleServiceImpl objectUnderTest = new CpsModuleServiceImpl()
54     @SpringBean
55     CacheManager cacheManager = new CaffeineCacheManager("yangSchema");
56
57     def 'Create schema set'() {
58         given: 'Valid yang resource as name-to-content map'
59             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
60         when: 'Create schema set method is invoked'
61             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
62         then: 'Parameters are validated and processing is delegated to persistence service'
63             1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
64     }
65
66     def 'Create schema set from invalid resources'() {
67         given: 'Invalid yang resource as name-to-content map'
68             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
69         when: 'Create schema set method is invoked'
70             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
71         then: 'Model validation exception is thrown'
72             thrown(ModelValidationException.class)
73     }
74
75     def 'Get schema set by name and dataspace.'() {
76         given: 'an already present schema set'
77             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
78             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
79         when: 'get schema set method is invoked'
80             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
81         then: 'the correct schema set is returned'
82             result.getName().contains('someSchemaSet')
83             result.getDataspaceName().contains('someDataspace')
84             result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
85     }
86
87     def 'Schema set caching.'() {
88         given: 'an  schema set'
89             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
90         when: 'get schema set method is invoked twice'
91             2.times {
92                 objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
93             }
94         then: 'the persistency service called only once'
95             1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
96     }
97
98     @Unroll
99     def 'Delete set by name and dataspace with #cascadeDeleteOption.'() {
100         when: 'schema set deletion is requested'
101             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
102         then: 'persistence service method is invoked with same parameters'
103             mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
104         where: 'following parameters are used'
105             dataspaceName | schemaSetname   | cascadeDeleteOption
106             'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
107             'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
108     }
109 }