CPS-887 Decreasing performance of cmHandle registration
[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 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.Shared
32 import spock.lang.Specification
33
34 import java.sql.SQLException
35
36 /**
37  * Specification unit test class for CPS module persistence service.
38  */
39 class CpsModulePersistenceServiceSpec extends Specification {
40
41     // Instance to test
42     CpsModulePersistenceService objectUnderTest
43
44     // Mocks
45     def dataspaceRepositoryMock = Mock(DataspaceRepository)
46     def yangResourceRepositoryMock = Mock(YangResourceRepository)
47     def schemaSetRepositoryMock = Mock(SchemaSetRepository)
48     def cpsAdminPersistenceServiceMock = Mock(CpsAdminPersistenceService)
49     def moduleReferenceRepositoryMock = Mock(ModuleReferenceRepository)
50
51     // Constants
52     def yangResourceName = 'my-yang-resource-name'
53     def yangResourceContent = 'module stores {\n' +
54             '    yang-version 1.1;\n' +
55             '    namespace "org:onap:ccsdk:sample";\n' +
56             '\n' +
57             '    prefix book-store;\n' +
58             '\n' +
59             '    revision "2020-09-15" {\n' +
60             '        description\n' +
61             '        "Sample Model";\n' +
62             '    }' +
63             '}'
64
65     // Scenario data
66     @Shared
67     yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
68     @Shared
69     yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
70     @Shared
71     sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
72     @Shared
73     checksumIntegrityException =
74             new DataIntegrityViolationException(
75                     "checksum integrity exception",
76                     new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
77     @Shared
78     anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
79
80     def setup() {
81         objectUnderTest = new CpsModulePersistenceServiceImpl(yangResourceRepositoryMock, schemaSetRepositoryMock,
82             dataspaceRepositoryMock, cpsAdminPersistenceServiceMock, moduleReferenceRepositoryMock)
83     }
84
85     def 'Store schema set error scenario: #scenario.'() {
86         given: 'no yang resource are currently saved'
87             yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
88         and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
89             yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
90         when: 'attempt to store schema set '
91             def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
92             objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
93         then: 'an #expectedThrownException is thrown'
94             def e = thrown(expectedThrownException)
95             e.getMessage().contains(expectedThrownExceptionMessage)
96         where: 'the following data is used'
97             scenario                | dbException                || expectedThrownException | expectedThrownExceptionMessage
98             'checksum data failure' | checksumIntegrityException || DuplicatedYangResourceException | yangResourceChecksum
99             'other data failure'    | anotherIntegrityException  || DataIntegrityViolationException | 'another integrity exception'
100     }
101
102 }