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.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 mockCpsModulePersistenceService = Mock(CpsModulePersistenceService)
41 def mockCpsAdminService = Mock(CpsAdminService)
42 def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
44 def objectUnderTest = new CpsModuleServiceImpl(mockCpsModulePersistenceService, 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 * mockCpsModulePersistenceService.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 * mockCpsModulePersistenceService.storeSchemaSetFromModules("someDataspaceName", "someSchemaSetName", [newModule: "newContent"], listOfExistingModulesModuleReference)
65 def 'Create schema set from invalid resources'() {
66 given: 'Invalid yang resource as name-to-content map'
67 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('invalid.yang')
68 when: 'Create schema set method is invoked'
69 objectUnderTest.createSchemaSet('someDataspace', 'someSchemaSet', yangResourcesNameToContentMap)
70 then: 'Model validation exception is thrown'
71 thrown(ModelValidationException.class)
74 def 'Get schema set by name and dataspace.'() {
75 given: 'an already present schema set'
76 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
77 and: 'yang resource cache returns the expected schema set'
78 mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(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.getExtendedModuleReferences().contains(new ExtendedModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
87 def 'Delete schema-set when cascade is allowed.'() {
88 given: '#numberOfAnchors anchors are associated with schemaset'
89 def associatedAnchors = createAnchors(numberOfAnchors)
90 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> associatedAnchors
91 when: 'schema set deletion is requested with cascade allowed'
92 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_ALLOWED)
93 then: 'anchor deletion is called #numberOfAnchors times'
94 numberOfAnchors * mockCpsAdminService.deleteAnchor('my-dataspace', _)
95 and: 'persistence service method is invoked with same parameters'
96 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
97 and: 'schema set will be removed from the cache'
98 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
99 and: 'orphan yang resources are deleted'
100 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
101 where: 'following parameters are used'
102 numberOfAnchors << [0, 3]
105 def 'Delete schema-set when cascade is prohibited.'() {
106 given: 'no anchors are associated with schemaset'
107 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> Collections.emptyList()
108 when: 'schema set deletion is requested with cascade allowed'
109 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
110 then: 'no anchors are deleted'
111 0 * mockCpsAdminService.deleteAnchor(_, _)
112 and: 'persistence service method is invoked with same parameters'
113 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset')
114 and: 'schema set will be removed from the cache'
115 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset')
116 and: 'orphan yang resources are deleted'
117 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules()
120 def 'Delete schema-set when cascade is prohibited and schema-set has anchors.'() {
121 given: '2 anchors are associated with schemaset'
122 mockCpsAdminService.getAnchors('my-dataspace', 'my-schemaset') >> createAnchors(2)
123 when: 'schema set deletion is requested with cascade allowed'
124 objectUnderTest.deleteSchemaSet('my-dataspace', 'my-schemaset', CASCADE_DELETE_PROHIBITED)
125 then: 'Schema-Set in Use exception is thrown'
126 thrown(SchemaSetInUseException)
129 def createAnchors(int anchorCount) {
131 (0..<anchorCount).each { anchors.add(new Anchor("my-anchor-$it", 'my-dataspace', 'my-schemaset')) }
135 def 'Get all yang resources module references.'() {
136 given: 'an already present module reference'
137 def moduleReferences = [new ExtendedModuleReference()]
138 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName') >> moduleReferences
139 expect: 'the list provided by persistence service is returned as result'
140 objectUnderTest.getYangResourceModuleReferences('someDataspaceName') == moduleReferences
144 def 'Get all yang resources module references for the given dataspace name and anchor name.'() {
145 given: 'the module store service service returns a list module references'
146 def moduleReferences = [new ModuleReference()]
147 mockCpsModulePersistenceService.getYangResourceModuleReferences('someDataspaceName', 'someAnchorName') >> moduleReferences
148 expect: 'the list provided by persistence service is returned as result'
149 objectUnderTest.getYangResourcesModuleReferences('someDataspaceName', 'someAnchorName') == moduleReferences
152 def 'Identifying new module references'(){
153 given: 'module references from cm handle'
154 def moduleReferencesToCheck = [new ModuleReference('some-module', 'some-revision')]
155 when: 'identifyNewModuleReferences is called'
156 objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
157 then: 'cps module persistence service is called with module references to check'
158 1 * mockCpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);