Merge "Update Model to allow Persisting of alternateId"
[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.CpsModulePersistenceService
24 import org.onap.cps.spi.entities.SchemaSetEntity
25 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
26 import org.onap.cps.spi.model.ModuleReference
27 import org.onap.cps.spi.repository.DataspaceRepository
28 import org.onap.cps.spi.repository.ModuleReferenceRepository
29 import org.onap.cps.spi.repository.SchemaSetRepository
30 import org.onap.cps.spi.repository.YangResourceRepository
31 import org.springframework.dao.DataIntegrityViolationException
32 import spock.lang.Specification
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 mockDataspaceRepository = Mock(DataspaceRepository)
43     def mockYangResourceRepository = Mock(YangResourceRepository)
44     def mockSchemaSetRepository = Mock(SchemaSetRepository)
45     def mockModuleReferenceRepository = Mock(ModuleReferenceRepository)
46
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     static yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
61     static yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
62     static sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
63     static checksumIntegrityException =  new DataIntegrityViolationException('checksum integrity exception',
64                     new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
65     static checksumIntegrityExceptionWithoutChecksum =  new DataIntegrityViolationException('checksum integrity exception',
66                     new ConstraintViolationException('', new SQLException('no checksum'), yangResourceChecksumDbConstraint))
67     static otherIntegrityException = new DataIntegrityViolationException('another integrity exception')
68
69     def setup() {
70         objectUnderTest = new CpsModulePersistenceServiceImpl(mockYangResourceRepository, mockSchemaSetRepository,
71             mockDataspaceRepository, mockModuleReferenceRepository)
72     }
73
74     def 'Store schema set error scenario: #scenario.'() {
75         given: 'no yang resource are currently saved'
76             mockYangResourceRepository.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList()
77         and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
78             mockYangResourceRepository.saveAll(_) >> { throw dbException }
79         when: 'attempt to store schema set '
80             def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
81             objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
82         then: 'an #expectedThrownException is thrown'
83             def e = thrown(expectedThrownException)
84             assert e.getMessage().contains(expectedThrownExceptionMessage)
85         where: 'the following data is used'
86             scenario                            | dbException                               || expectedThrownException         | expectedThrownExceptionMessage
87             'checksum data failure'             | checksumIntegrityException                || DuplicatedYangResourceException | yangResourceChecksum
88             'checksum failure without checksum' | checksumIntegrityExceptionWithoutChecksum || DuplicatedYangResourceException | 'no checksum found'
89             'other data failure'                | otherIntegrityException                   || DataIntegrityViolationException | 'another integrity exception'
90     }
91
92     def 'Upgrade existing schema set'() {
93         given: 'old schema has empty yang resource'
94             mockYangResourceRepository.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList()
95         def schemaSetEntity = new SchemaSetEntity(id: 1)
96             mockSchemaSetRepository.getByDataspaceAndName(_, _) >> schemaSetEntity
97         when: 'schema set update is requested'
98             objectUnderTest.updateSchemaSetFromModules('my-dataspace', 'my-schemaset', [:], [new ModuleReference('some module name', 'some revision name')])
99         then: 'no exception is thrown '
100             noExceptionThrown()
101     }
102
103 }