8bd7f86ea294b0d32c4f9759b1a8e79c9431bf10
[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  * ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17 */
18
19 package org.onap.cps.spi.impl
20
21 import org.hibernate.exception.ConstraintViolationException
22 import org.onap.cps.spi.CpsModulePersistenceService
23 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
24 import org.onap.cps.spi.repository.DataspaceRepository
25 import org.onap.cps.spi.repository.SchemaSetRepository
26 import org.onap.cps.spi.repository.YangResourceRepository
27 import org.springframework.dao.DataIntegrityViolationException
28 import spock.lang.Shared
29 import spock.lang.Specification
30
31 import java.sql.SQLException
32
33 /**
34  * Specification unit test class for CPS module persistence service.
35  */
36 class CpsModulePersistenceServiceSpec extends Specification {
37
38     // Instance to test
39     CpsModulePersistenceService objectUnderTest
40
41     // Mocks
42     def dataspaceRepositoryMock = Mock(DataspaceRepository)
43     def yangResourceRepositoryMock = Mock(YangResourceRepository)
44     def schemaSetRepositoryMock = Mock(SchemaSetRepository)
45
46     // Constants
47     def yangResourceName = 'my-yang-resource-name'
48     def yangResourceContent = 'module stores {\n' +
49             '    yang-version 1.1;\n' +
50             '    namespace "org:onap:ccsdk:sample";\n' +
51             '\n' +
52             '    prefix book-store;\n' +
53             '\n' +
54             '    revision "2020-09-15" {\n' +
55             '        description\n' +
56             '        "Sample Model";\n' +
57             '    }' +
58             '}'
59
60     // Scenario data
61     @Shared
62     yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
63     @Shared
64     yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
65     @Shared
66     sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
67     @Shared
68     checksumIntegrityException =
69             new DataIntegrityViolationException(
70                     "checksum integrity exception",
71                     new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
72     @Shared
73     anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
74
75     def setup() {
76         objectUnderTest = new CpsModulePersistenceServiceImpl()
77         objectUnderTest.dataspaceRepository = dataspaceRepositoryMock
78         objectUnderTest.yangResourceRepository = yangResourceRepositoryMock
79         objectUnderTest.schemaSetRepository = schemaSetRepositoryMock
80     }
81
82     def 'Store schema set error scenario: #scenario.'() {
83         given: 'no yang resource are currently saved'
84             yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
85         and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
86             yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
87         when: 'attempt to store schema set '
88             def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
89             objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
90         then: 'an #expectedThrownException is thrown'
91             def e = thrown(expectedThrownException)
92             e.getMessage().contains(expectedThrownExceptionMessage)
93         where: 'the following data is used'
94             scenario                | dbException                || expectedThrownException | expectedThrownExceptionMessage
95             'checksum data failure' | checksumIntegrityException || DuplicatedYangResourceException | yangResourceChecksum
96             'other data failure'    | anotherIntegrityException  || DataIntegrityViolationException | 'another integrity exception'
97     }
98
99 }