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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.cps.spi.impl
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
31 import java.sql.SQLException
34 * Specification unit test class for CPS module persistence service.
36 class CpsModulePersistenceServiceSpec extends Specification {
39 CpsModulePersistenceService objectUnderTest
42 def dataspaceRepositoryMock = Mock(DataspaceRepository)
43 def yangResourceRepositoryMock = Mock(YangResourceRepository)
44 def schemaSetRepositoryMock = Mock(SchemaSetRepository)
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' +
52 ' prefix book-store;\n' +
54 ' revision "2020-09-15" {\n' +
56 ' "Sample Model";\n' +
62 yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
64 yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
66 sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
68 checksumIntegrityException =
69 new DataIntegrityViolationException(
70 "checksum integrity exception",
71 new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
73 anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
76 objectUnderTest = new CpsModulePersistenceServiceImpl()
77 objectUnderTest.dataspaceRepository = dataspaceRepositoryMock
78 objectUnderTest.yangResourceRepository = yangResourceRepositoryMock
79 objectUnderTest.schemaSetRepository = schemaSetRepositoryMock
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'