Merge "Add withTrustLevel condition to CmHandle Query API"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2021 Bell Canada.
4  * Modifications Copyright (C) 2022-2023 Nordix Foundation
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  * ============LICENSE_END=========================================================
18 */
19
20 package org.onap.cps.spi.impl
21
22 import org.hibernate.exception.ConstraintViolationException
23 import org.onap.cps.spi.CpsAdminPersistenceService
24 import org.onap.cps.spi.CpsModulePersistenceService
25 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
26 import org.onap.cps.spi.repository.DataspaceRepository
27 import org.onap.cps.spi.repository.ModuleReferenceRepository
28 import org.onap.cps.spi.repository.SchemaSetRepository
29 import org.onap.cps.spi.repository.YangResourceRepository
30 import org.springframework.dao.DataIntegrityViolationException
31 import spock.lang.Specification
32
33 import java.sql.SQLException
34
35 /**
36  * Specification unit test class for CPS module persistence service.
37  */
38 class CpsModulePersistenceServiceSpec extends Specification {
39
40     CpsModulePersistenceService objectUnderTest
41
42     def dataspaceRepositoryMock = Mock(DataspaceRepository)
43     def yangResourceRepositoryMock = Mock(YangResourceRepository)
44     def schemaSetRepositoryMock = Mock(SchemaSetRepository)
45     def cpsAdminPersistenceServiceMock = Mock(CpsAdminPersistenceService)
46     def moduleReferenceRepositoryMock = Mock(ModuleReferenceRepository)
47
48     def yangResourceName = 'my-yang-resource-name'
49     def yangResourceContent = 'module stores {\n' +
50             '    yang-version 1.1;\n' +
51             '    namespace "org:onap:ccsdk:sample";\n' +
52             '\n' +
53             '    prefix book-store;\n' +
54             '\n' +
55             '    revision "2020-09-15" {\n' +
56             '        description\n' +
57             '        "Sample Model";\n' +
58             '    }' +
59             '}'
60
61     static yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
62     static yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
63     static sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
64     static checksumIntegrityException =  new DataIntegrityViolationException('checksum integrity exception',
65                     new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
66     static checksumIntegrityExceptionWithoutChecksum =  new DataIntegrityViolationException('checksum integrity exception',
67                     new ConstraintViolationException('', new SQLException('no checksum'), yangResourceChecksumDbConstraint))
68     static otherIntegrityException = new DataIntegrityViolationException('another integrity exception')
69
70     def setup() {
71         objectUnderTest = new CpsModulePersistenceServiceImpl(yangResourceRepositoryMock, schemaSetRepositoryMock,
72             dataspaceRepositoryMock, moduleReferenceRepositoryMock)
73     }
74
75     def 'Store schema set error scenario: #scenario.'() {
76         given: 'no yang resource are currently saved'
77             yangResourceRepositoryMock.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList()
78         and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
79             yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
80         when: 'attempt to store schema set '
81             def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
82             objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
83         then: 'an #expectedThrownException is thrown'
84             def e = thrown(expectedThrownException)
85             assert e.getMessage().contains(expectedThrownExceptionMessage)
86         where: 'the following data is used'
87             scenario                            | dbException                               || expectedThrownException         | expectedThrownExceptionMessage
88             'checksum data failure'             | checksumIntegrityException                || DuplicatedYangResourceException | yangResourceChecksum
89             'checksum failure without checksum' | checksumIntegrityExceptionWithoutChecksum || DuplicatedYangResourceException | 'no checksum found'
90             'other data failure'                | otherIntegrityException                   || DataIntegrityViolationException | 'another integrity exception'
91     }
92
93 }