2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Nordix Foundation
4 * Modifications Copyright (C) 2020-2021 Pantheon.tech
5 * Modifications Copyright (C) 2020-2022 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl
25 import org.onap.cps.TestUtils
26 import org.onap.cps.api.CpsAdminService
27 import org.onap.cps.spi.CpsModulePersistenceService
28 import org.onap.cps.spi.exceptions.ModelValidationException
29 import org.onap.cps.spi.exceptions.SchemaSetInUseException
30 import org.onap.cps.spi.model.Anchor
31 import org.onap.cps.spi.model.ExtendedModuleReference
32 import org.onap.cps.spi.model.ModuleReference
33 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
34 import spock.lang.Specification
35 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
36 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
38 class CpsModuleServiceImplSpec extends Specification {
40 def mockModuleStoreService = Mock(CpsModulePersistenceService)
41 def mockCpsAdminService = Mock(CpsAdminService)
42 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
44 def objectUnderTest = new CpsModuleServiceImpl(mockModuleStoreService, mockYangTextSchemaSourceSetCache, mockCpsAdminService)
46 def 'Create schema set.'() {
47 given: 'Valid yang resource as name-to-content map'
48 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
49 when: 'Create schema set method is invoked'
50 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
51 then: 'Parameters are validated and processing is delegated to persistence service'
52 1 * mockModuleStoreService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
55 def 'Create schema set from new modules and existing modules.'() {
56 given: 'a list of existing modules module reference'
57 def moduleReferenceForExistingModule = new ExtendedModuleReference("test", "test.org", "2021-10-12")
58 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
59 when: 'create schema set from modules method is invoked'
60 objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
61 then: 'processing is delegated to persistence service'
62 1 * mockModuleStoreService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
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)
75 def 'Get schema set by name and dataspace.'() {
76 given: 'an already present schema set'
77 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
78 and: 'yang resource cache returns the expected schema set'
79 mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
80 when: 'get schema set method is invoked'
81 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
82 then: 'the correct schema set is returned'
83 result.getName().contains('someSchemaSet')
84 result.getDataspaceName().contains('someDataspace')
85 result.getExtendedModuleReferences().contains(new ExtendedModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
88 def 'Delete schema-set when cascade is allowed.'() {
89 given: '#numberOfAnchors anchors are associated with schemaset'
90 def associatedAnchors = createAnchors(numberOfAnchors)
91 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
92 when: 'schema set deletion is requested with cascade allowed'
93 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
94 then: 'anchor deletion is called #numberOfAnchors times'
95 numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
96 and: 'persistence service method is invoked with same parameters'
97 1 * mockModuleStoreService.deleteSchemaSet('my-dataspace', 'my-schemaset')
98 and: 'schema set will be removed from the cache'
99 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
100 and: 'orphan yang resources are deleted'
101 1 * mockModuleStoreService.deleteUnusedYangResourceModules()
102 where: 'following parameters are used'
103 numberOfAnchors << [0, 3]
106 def 'Delete schema-set when cascade is prohibited.'() {
107 given: 'no anchors are associated with schemaset'
108 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
109 when: 'schema set deletion is requested with cascade allowed'
110 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
111 then: 'no anchors are deleted'
112 0 * mockCpsAdminService.deleteAnchor(_, _)
113 and: 'persistence service method is invoked with same parameters'
114 1 * mockModuleStoreService.deleteSchemaSet('my-dataspace', 'my-schemaset')
115 and: 'schema set will be removed from the cache'
116 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
117 and: 'orphan yang resources are deleted'
118 1 * mockModuleStoreService.deleteUnusedYangResourceModules()
121 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
122 given: '2 anchors are associated with schemaset'
123 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
124 when: 'schema set deletion is requested with cascade allowed'
125 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
126 then: 'Schema-Set in Use exception is thrown'
127 thrown(SchemaSetInUseException)
130 def createAnchors(int anchorCount) {
132 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
136 def 'Get all yang resources module references.'() {
137 given: 'an already present module reference'
138 def moduleReferences = [new ExtendedModuleReference()]
139 mockModuleStoreService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
140 expect: 'the list provided by persistence service is returned as result'
141 objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
145 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
146 given: 'the module store service service returns a list module references'
147 def moduleReferences = [new ModuleReference()]
148 mockModuleStoreService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
149 expect: 'the list provided by persistence service is returned as result'
150 objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences