Delete schema set - REST and service layers
[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.spi.CascadeDeleteAllowed
25 import org.onap.cps.spi.CpsModulePersistenceService;
26 import org.onap.cps.spi.exceptions.ModelValidationException
27 import org.onap.cps.spi.model.ModuleReference
28 import spock.lang.Specification
29 import spock.lang.Unroll
30
31 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
32 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
33
34 class CpsModuleServiceImplSpec extends Specification {
35     def mockModuleStoreService = Mock(CpsModulePersistenceService)
36     def objectUnderTest = new CpsModuleServiceImpl()
37
38     def setup() {
39         objectUnderTest.cpsModulePersistenceService = mockModuleStoreService
40     }
41
42     def 'Create schema set'() {
43         given: 'Valid yang resource as name-to-content map'
44             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
45         when: 'Create schema set method is invoked'
46             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
47         then: 'Parameters are validated and processing is delegated to persistence service'
48             1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
49     }
50
51     def 'Create schema set from invalid resources'() {
52         given: 'Invalid yang resource as name-to-content map'
53             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
54         when: 'Create schema set method is invoked'
55             objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
56         then: 'Model validation exception is thrown'
57             thrown(ModelValidationException.class)
58     }
59
60     def 'Get schema set by name and dataspace.'() {
61         given: 'an already present schema set'
62             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
63             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
64         when: 'get schema set method is invoked'
65             def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
66         then: 'the correct schema set is returned'
67             result.getName().contains('someSchemaSet')
68             result.getDataspaceName().contains('someDataspace')
69             result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
70     }
71
72     @Unroll
73     def 'Delete set by name and dataspace with #cascadeDeleteOption.'(){
74         when: 'schema set deletion is requested'
75             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
76         then: 'persistence service method is invoked with same parameters'
77             mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption)
78         where: 'following parameters are used'
79             dataspaceName | schemaSetname | cascadeDeleteOption
80             'dataspace-1'  | 'schemas-set-1' | CASCADE_DELETE_ALLOWED
81             'dataspace-2'  | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED
82     }
83 }