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 = 'my-yang-resource-content'
52 yangResourceChecksum = 'ac2352cc20c10467528b2390bbf2d72d48b0319152ebaabcda207786b4a641c2'
54 yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
56 sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
58 checksumIntegrityException =
59 new DataIntegrityViolationException(
60 "checksum integrity exception",
61 new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
63 anotherIntegrityException = new DataIntegrityViolationException("another integrity exception")
66 objectUnderTest = new CpsModulePersistenceServiceImpl()
67 objectUnderTest.dataspaceRepository = dataspaceRepositoryMock
68 objectUnderTest.yangResourceRepository = yangResourceRepositoryMock
69 objectUnderTest.schemaSetRepository = schemaSetRepositoryMock
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'