Rename CpsModulePersistenceServiceSpec test class
[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 = 'my-yang-resource-content'
49
50     // Scenario data
51     @Shared
52     yangResourceChecksum = 'ac2352cc20c10467528b2390bbf2d72d48b0319152ebaabcda207786b4a641c2'
53     @Shared
54     yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
55     @Shared
56     sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
57     @Shared
58     checksumIntegrityException =
59             new DataIntegrityViolationException(
60                     "checksum integrity exception",
61                     new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
62     @Shared
63     anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
64
65     def setup() {
66         objectUnderTest = new CpsModulePersistenceServiceImpl()
67         objectUnderTest.dataspaceRepository = dataspaceRepositoryMock
68         objectUnderTest.yangResourceRepository = yangResourceRepositoryMock
69         objectUnderTest.schemaSetRepository = schemaSetRepositoryMock
70     }
71
72     def 'Store schema set error scenario: #scenario.'() {
73         given: 'no yang resource are currently saved'
74             yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
75         and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
76             yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
77         when: 'attempt to store schema set '
78             def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
79             objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
80         then: 'an #expectedThrownException is thrown'
81             def e = thrown(expectedThrownException)
82             e.getMessage().contains(expectedThrownExceptionMessage)
83         where: 'the following data is used'
84             scenario                | dbException                || expectedThrownException | expectedThrownExceptionMessage
85             'checksum data failure' | checksumIntegrityException || DuplicatedYangResourceException | yangResourceChecksum
86             'other data failure'    | anotherIntegrityException  || DataIntegrityViolationException | 'another integrity exception'
87     }
88
89 }