2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 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.ModuleReference
32 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
33 import spock.lang.Specification
34 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
35 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
37 class CpsModuleServiceImplSpec extends Specification {
39 def mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
40 def mockCpsAdminService = Mock(CpsAdminService)
41 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
43 def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, mockYangTextSchemaSourceSetCache, mockCpsAdminService)
45 def 'Create schema set.'() {
46 given: 'Valid yang resource as name-to-content map'
47 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
48 when: 'Create schema set method is invoked'
49 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
50 then: 'Parameters are validated and processing is delegated to persistence service'
51 1 * mockCpsModulePersistenceService.storeSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
54 def 'Create schema set from new modules and existing modules.'() {
55 given: 'a list of existing modules module reference'
56 def moduleReferenceForExistingModule = new ModuleReference("test", "2021-10-12","test.org")
57 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
58 when: 'create schema set from modules method is invoked'
59 objectUnderTest.createSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
60 then: 'processing is delegated to persistence service'
61 1 * mockCpsModulePersistenceService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
64 def 'Create schema set from invalid resources'() {
65 given: 'Invalid yang resource as name-to-content map'
66 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
67 when: 'Create schema set method is invoked'
68 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
69 then: 'Model validation exception is thrown'
70 thrown(ModelValidationException.class)
73 def 'Get schema set by name and dataspace.'() {
74 given: 'an already present schema set'
75 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
76 and: 'yang resource cache returns the expected schema set'
77 mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
78 when: 'get schema set method is invoked'
79 def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
80 then: 'the correct schema set is returned'
81 result.getName().contains('someSchemaSet')
82 result.getDataspaceName().contains('someDataspace')
83 result.getModuleReferences().contains(new ModuleReference('stores', '2020-09-15', 'org:onap:ccsdk:sample'))
86 def 'Delete schema-set when cascade is allowed.'() {
87 given: '#numberOfAnchors anchors are associated with schemaset'
88 def associatedAnchors = createAnchors(numberOfAnchors)
89 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
90 when: 'schema set deletion is requested with cascade allowed'
91 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
92 then: 'anchor deletion is called #numberOfAnchors times'
93 numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
94 and: 'persistence service method is invoked with same parameters'
95 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
96 and: 'schema set will be removed from the cache'
97 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
98 and: 'orphan yang resources are deleted'
99 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
100 where: 'following parameters are used'
101 numberOfAnchors << [0, 3]
104 def 'Delete schema-set when cascade is prohibited.'() {
105 given: 'no anchors are associated with schemaset'
106 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
107 when: 'schema set deletion is requested with cascade allowed'
108 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
109 then: 'no anchors are deleted'
110 0 * mockCpsAdminService.deleteAnchor(_, _)
111 and: 'persistence service method is invoked with same parameters'
112 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
113 and: 'schema set will be removed from the cache'
114 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
115 and: 'orphan yang resources are deleted'
116 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
119 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
120 given: '2 anchors are associated with schemaset'
121 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
122 when: 'schema set deletion is requested with cascade allowed'
123 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
124 then: 'Schema-Set in Use exception is thrown'
125 thrown(SchemaSetInUseException)
128 def createAnchors(int anchorCount) {
130 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
134 def 'Get all yang resources module references.'() {
135 given: 'an already present module reference'
136 def moduleReferences = [new ModuleReference('some module name','some revision name')]
137 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
138 expect: 'the list provided by persistence service is returned as result'
139 objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
143 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
144 given: 'the module store service service returns a list module references'
145 def moduleReferences = [new ModuleReference()]
146 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
147 expect: 'the list provided by persistence service is returned as result'
148 objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences
151 def 'Identifying new module references'(){
152 given: 'module references from cm handle'
153 def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
154 when: 'identifyNewModuleReferences is called'
155 objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
156 then: 'cps module persistence service is called with module references to check'
157 1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);