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.AlreadyDefinedException
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
36 class CpsModulePersistenceServiceSpec extends CpsPersistenceSpecBase {
39 CpsModulePersistenceService objectUnderTest
42 AnchorRepository anchorRepository
45 SchemaSetRepository schemaSetRepository
48 CpsAdminPersistenceService cpsAdminPersistenceService
50 static final String SET_DATA = '/data/schemaset.sql'
51 static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
52 static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
53 static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = 'SCHEMA-SET-101'
54 static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
56 static final Long NEW_RESOURCE_ABSTRACT_ID = 0L
57 static final String NEW_RESOURCE_NAME = 'some new resource'
58 static final String NEW_RESOURCE_CONTENT = 'some resource content'
59 static final String NEW_RESOURCE_CHECKSUM = '09002da02ee2683898d2c81c67f9e22cdbf8577d8c2de16c84d724e4ae44a0a6'
61 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
65 dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
68 @Sql([CLEAR_DATA, SET_DATA])
69 def 'Store schema set error scenario: #scenario.'() {
70 when: 'attempt to store schema set #schemaSetName in dataspace #dataspaceName'
71 objectUnderTest.storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesNameToContentMap)
72 then: 'an #expectedException is thrown'
73 thrown(expectedException)
74 where: 'the following data is used'
75 scenario | dataspaceName | schemaSetName || expectedException
76 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
77 'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException
80 @Sql([CLEAR_DATA, SET_DATA])
81 def 'Store new schema set.'() {
82 when: 'a new schemaset is stored'
83 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
84 then: 'the schema set is persisted correctly'
85 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME,
86 NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM)
89 @Sql([CLEAR_DATA, SET_DATA])
90 def 'Retrieving schema set (resources) by anchor.'() {
91 given: 'a new schema set is stored'
92 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
93 and: 'an anchor is created with that schema set'
94 cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1)
95 when: 'the schema set resources for that anchor is retrieved'
96 def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1)
97 then: 'the correct resources are returned'
98 result == newYangResourcesNameToContentMap
101 @Sql([CLEAR_DATA, SET_DATA])
102 def 'Storing duplicate schema content.'() {
103 given: 'a new schema set with a resource with the same content as an existing resource'
104 def existingResourceContent = 'CONTENT-001'
105 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent]
106 when: 'the schema set with duplicate resource is stored'
107 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
108 then: 'the schema persisted (re)uses the existing id, name and has the same checksum'
109 def existingResourceId = 3001L
110 def existingResourceName = 'module1@2020-02-02.yang'
111 def existingResourceChecksum = 'e8bdda931099310de66532e08c3fafec391db29f55c81927b168f6aa8f81b73b'
112 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
113 existingResourceId, existingResourceName, existingResourceContent, existingResourceChecksum)
116 @Sql([CLEAR_DATA, SET_DATA])
117 def 'Delete schema set with cascade delete prohibited but no anchors using it'() {
118 when: 'a schema set is deleted with cascade-prohibited option'
119 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
120 CASCADE_DELETE_PROHIBITED)
121 then: 'the schema set has been deleted'
122 schemaSetRepository.findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent() == false
123 and: 'any orphaned (not used by any schema set anymore) yang resources are deleted'
124 def orphanedResourceId = 3100L
125 yangResourceRepository.findById(orphanedResourceId).isPresent() == false
126 and: 'any shared (still in use by other schema set) yang resources still persists'
127 def sharedResourceId = 3003L
128 yangResourceRepository.findById(sharedResourceId).isPresent()
131 @Sql([CLEAR_DATA, SET_DATA])
132 def 'Delete schema set with cascade allowed.'() {
133 when: 'a schema set is deleted with cascade-allowed option'
134 objectUnderTest.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA,
135 CASCADE_DELETE_ALLOWED)
136 then: 'the schema set has been deleted'
138 .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent() == false
139 and: 'the associated anchors are removed'
140 def associatedAnchorsIds = [ 6001, 6002 ]
141 associatedAnchorsIds.each {anchorRepository.findById(it).isPresent() == false }
142 and: 'the fragment(s) under those anchors are removed'
143 def fragmentUnderAnchor1Id = 7001L
144 fragmentRepository.findById(fragmentUnderAnchor1Id).isPresent() == false
145 and: 'the shared resources still persist'
146 def sharedResourceIds = [ 3003L, 3004L ]
147 sharedResourceIds.each {yangResourceRepository.findById(it).isPresent() }
150 @Sql([CLEAR_DATA, SET_DATA])
151 def 'Delete schema set error scenario: #scenario.'() {
152 when: 'attempt to delete a schema set where #scenario'
153 objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
154 then: 'an #expectedException is thrown'
155 thrown(expectedException)
156 where: 'the following data is used'
157 scenario | dataspaceName | schemaSetName || expectedException
158 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException
159 'schema set does not exists' | DATASPACE_NAME | 'unknown' || SchemaSetNotFoundException
160 'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
163 def assertSchemaSetPersisted(expectedDataspaceName,
164 expectedSchemaSetName,
165 expectedYangResourceId,
166 expectedYangResourceName,
167 expectedYangResourceContent,
168 expectedYangResourceChecksum) {
169 // assert the schema set is persisted
170 def schemaSetEntity = schemaSetRepository
171 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
172 assert schemaSetEntity.name == expectedSchemaSetName
173 assert schemaSetEntity.dataspace.name == expectedDataspaceName
175 // assert the attached yang resource is persisted
176 def yangResourceEntities = schemaSetEntity.getYangResources()
177 yangResourceEntities.size() == 1
179 // assert the attached yang resource content
180 YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
181 assert yangResourceEntity.id != null
182 if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
183 // existing resource with known id
184 assert yangResourceEntity.id == expectedYangResourceId
186 yangResourceEntity.name == expectedYangResourceName
187 yangResourceEntity.content == expectedYangResourceContent
188 yangResourceEntity.checksum == expectedYangResourceChecksum