2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the 'License');
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.onap.cps.spi.impl
21 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
22 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
24 import org.onap.cps.spi.CpsAdminPersistenceService
25 import org.onap.cps.spi.CpsModulePersistenceService
26 import org.onap.cps.spi.entities.YangResourceEntity
27 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
28 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException
29 import org.onap.cps.spi.exceptions.SchemaSetInUseException
30 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
31 import org.onap.cps.spi.repository.AnchorRepository
32 import org.onap.cps.spi.repository.SchemaSetRepository
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.test.context.jdbc.Sql
35 import spock.lang.Unroll
37 class CpsModulePersistenceServiceSpec extends CpsPersistenceSpecBase {
40 CpsModulePersistenceService objectUnderTest
43 AnchorRepository anchorRepository
46 SchemaSetRepository schemaSetRepository
49 CpsAdminPersistenceService cpsAdminPersistenceService
51 static final String SET_DATA = '/data/schemaset.sql'
52 static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
53 static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
54 static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = 'SCHEMA-SET-101'
55 static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
57 static final Long NEW_RESOURCE_ABSTRACT_ID = 0L
58 static final String NEW_RESOURCE_NAME = 'some new resource'
59 static final String NEW_RESOURCE_CONTENT = 'some resource content'
60 static final String NEW_RESOURCE_CHECKSUM = '09002da02ee2683898d2c81c67f9e22cdbf8577d8c2de16c84d724e4ae44a0a6'
62 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
66 dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
70 @Sql([CLEAR_DATA, SET_DATA])
71 def 'Store schema set error scenario: #scenario.'() {
72 when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
73 objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
74 then: 'an #expectedException is thrown'
75 thrown(expectedException)
76 where: 'the following data is used'
77 scenario | dataspaceName | schemaSetName || expectedException
78 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
79 'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || SchemaSetAlreadyDefinedException
82 @Sql([CLEAR_DATA, SET_DATA])
83 def 'Store new schema set.'() {
84 when: 'a new schemaset is stored'
85 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
86 then: 'the schema set is persisted correctly'
87 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME,
88 NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM)
91 @Sql([CLEAR_DATA, SET_DATA])
92 def 'Retrieving schema set (resources) by anchor.'() {
93 given: 'a new schema set is stored'
94 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
95 and: 'an anchor is created with that schema set'
96 cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
97 when: 'the schema set resources for that anchor is retrieved'
98 def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
99 then: 'the correct resources are returned'
100 result == newYangResourcesNameToContentMap
103 @Sql([CLEAR_DATA, SET_DATA])
104 def 'Storing duplicate schema content.'() {
105 given: 'a new schema set with a resource with the same content as an existing resource'
106 def existingResourceContent = 'CONTENT-001'
107 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
108 when: 'the schema set with duplicate resource is stored'
109 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
110 then: 'the schema persisted (re)uses the existing id, name and has the same checksum'
111 def existingResourceId = 3001L
112 def existingResourceName = 'module1@2020-02-02.yang'
113 def existingResourceChecksum = 'e8bdda931099310de66532e08c3fafec391db29f55c81927b168f6aa8f81b73b'
114 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
115 existingResourceId, existingResourceName, existingResourceContent, existingResourceChecksum)
118 @Sql([CLEAR_DATA, SET_DATA])
119 def 'Delete schema set with cascade delete prohibited but no anchors using it'() {
120 when: 'a schema set is deleted with cascade-prohibited option'
121 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
122 CASCADE_DELETE_PROHIBITED)
123 then: 'the schema set has been deleted'
124 schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
125 and: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
126 def orphanedResourceId = 3100L
127 yangResourceRepository.findById(orphanedResourceId).isPresent() == false
128 and: 'any shared (still in use by other schema set) yang resources still persists'
129 def sharedResourceId = 3003L
130 yangResourceRepository.findById(sharedResourceId).isPresent()
133 @Sql([CLEAR_DATA, SET_DATA])
134 def 'Delete schema set with cascade allowed.'() {
135 when: 'a schema set is deleted with cascade-allowed option'
136 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA,
137 CASCADE_DELETE_ALLOWED)
138 then: 'the schema set has been deleted'
140 .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent() == false
141 and: 'the associated anchors are removed'
142 def associatedAnchorsIds = [ 6001, 6002 ]
143 associatedAnchorsIds.each {anchorRepository.findById(it).isPresent() == false }
144 and: 'the fragment(s) under those anchors are removed'
145 def fragmentUnderAnchor1Id = 7001L
146 fragmentRepository.findById(fragmentUnderAnchor1Id).isPresent() == false
147 and: 'the shared resources still persist'
148 def sharedResourceIds = [ 3003L, 3004L ]
149 sharedResourceIds.each {yangResourceRepository.findById(it).isPresent() }
153 @Sql([CLEAR_DATA, SET_DATA])
154 def 'Delete schema set error scenario: #scenario.'() {
155 when: 'attempt to delete a schema set where #scenario'
156 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
157 then: 'an #expectedException is thrown'
158 thrown(expectedException)
159 where: 'the following data is used'
160 scenario | dataspaceName | schemaSetName || expectedException
161 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
162 'schema set does not exists' | DATASPACE_NAME | 'unknown' || SchemaSetNotFoundException
163 'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
166 def assertSchemaSetPersisted(expectedDataspaceName,
167 expectedSchemaSetName,
168 expectedYangResourceId,
169 expectedYangResourceName,
170 expectedYangResourceContent,
171 expectedYangResourceChecksum) {
172 // assert the schema set is persisted
173 def schemaSetEntity = schemaSetRepository
174 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
175 assert schemaSetEntity.name == expectedSchemaSetName
176 assert schemaSetEntity.dataspace.name == expectedDataspaceName
178 // assert the attached yang resource is persisted
179 def yangResourceEntities = schemaSetEntity.getYangResources()
180 yangResourceEntities.size() == 1
182 // assert the attached yang resource content
183 YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
184 assert yangResourceEntity.id != null
185 if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
186 // existing resource with known id
187 assert yangResourceEntity.id == expectedYangResourceId
189 yangResourceEntity.name == expectedYangResourceName
190 yangResourceEntity.content == expectedYangResourceContent
191 yangResourceEntity.checksum == expectedYangResourceChecksum