Editing of Nordix Licenses to ONAP guidelines
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Bell Canada.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the 'License');
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an 'AS IS' BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.onap.cps.spi.impl
22
23 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
24 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
25
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.CpsModulePersistenceService
28 import org.onap.cps.spi.entities.YangResourceEntity
29 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
30 import org.onap.cps.spi.exceptions.AlreadyDefinedException
31 import org.onap.cps.spi.exceptions.SchemaSetInUseException
32 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
33 import org.onap.cps.spi.repository.AnchorRepository
34 import org.onap.cps.spi.repository.SchemaSetRepository
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.jdbc.Sql
37
38 class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
39
40     @Autowired
41     CpsModulePersistenceService objectUnderTest
42
43     @Autowired
44     AnchorRepository anchorRepository
45
46     @Autowired
47     SchemaSetRepository schemaSetRepository
48
49     @Autowired
50     CpsAdminPersistenceService cpsAdminPersistenceService
51
52     static final String SET_DATA = '/data/schemaset.sql'
53     static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1
54     static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100'
55     static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = 'SCHEMA-SET-101'
56     static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
57
58     static final Long NEW_RESOURCE_ABSTRACT_ID = 0L
59     static final String NEW_RESOURCE_NAME = 'some new resource'
60     static final String NEW_RESOURCE_CONTENT = 'some resource content'
61     static final String NEW_RESOURCE_CHECKSUM = '09002da02ee2683898d2c81c67f9e22cdbf8577d8c2de16c84d724e4ae44a0a6'
62
63     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
64     def dataspaceEntity
65
66     def setup() {
67         dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
68     }
69
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 || AlreadyDefinedException
80     }
81
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)
89     }
90
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
101     }
102
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)
116     }
117
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()
131     }
132
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'
139             schemaSetRepository
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() }
150     }
151
152     @Sql([CLEAR_DATA, SET_DATA])
153     def 'Delete schema set error scenario: #scenario.'() {
154         when: 'attempt to delete a schema set where #scenario'
155             objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
156         then: 'an #expectedException is thrown'
157             thrown(expectedException)
158         where: 'the following data is used'
159             scenario                                   | dataspaceName  | schemaSetName                         || expectedException
160             'dataspace does not exist'                 | 'unknown'      | 'not-relevant'                        || DataspaceNotFoundException
161             'schema set does not exists'               | DATASPACE_NAME | 'unknown'                             || SchemaSetNotFoundException
162             'cascade prohibited but schema set in use' | DATASPACE_NAME | SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA || SchemaSetInUseException
163     }
164
165     def assertSchemaSetPersisted(expectedDataspaceName,
166                              expectedSchemaSetName,
167                              expectedYangResourceId,
168                              expectedYangResourceName,
169                              expectedYangResourceContent,
170                              expectedYangResourceChecksum) {
171         // assert the schema set is persisted
172         def schemaSetEntity = schemaSetRepository
173                 .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow()
174         assert schemaSetEntity.name == expectedSchemaSetName
175         assert schemaSetEntity.dataspace.name == expectedDataspaceName
176
177         // assert the attached yang resource is persisted
178         def yangResourceEntities = schemaSetEntity.getYangResources()
179         yangResourceEntities.size() == 1
180
181         // assert the attached yang resource content
182         YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next()
183         assert yangResourceEntity.id != null
184         if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
185             // existing resource with known id
186             assert yangResourceEntity.id == expectedYangResourceId
187         }
188         yangResourceEntity.name == expectedYangResourceName
189         yangResourceEntity.content == expectedYangResourceContent
190         yangResourceEntity.checksum == expectedYangResourceChecksum
191     }
192
193 }