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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.cps.spi.impl
22 import org.hibernate.exception.ConstraintViolationException
23 import org.onap.cps.spi.CpsAdminPersistenceService
24 import org.onap.cps.spi.CpsModulePersistenceService
25 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
26 import org.onap.cps.spi.repository.DataspaceRepository
27 import org.onap.cps.spi.repository.ModuleReferenceRepository
28 import org.onap.cps.spi.repository.SchemaSetRepository
29 import org.onap.cps.spi.repository.YangResourceRepository
30 import org.springframework.dao.DataIntegrityViolationException
31 import spock.lang.Specification
33 import java.sql.SQLException
36 * Specification unit test class for CPS module persistence service.
38 class CpsModulePersistenceServiceSpec extends Specification {
40 CpsModulePersistenceService objectUnderTest
42 def dataspaceRepositoryMock = Mock(DataspaceRepository)
43 def yangResourceRepositoryMock = Mock(YangResourceRepository)
44 def schemaSetRepositoryMock = Mock(SchemaSetRepository)
45 def cpsAdminPersistenceServiceMock = Mock(CpsAdminPersistenceService)
46 def moduleReferenceRepositoryMock = Mock(ModuleReferenceRepository)
48 def yangResourceName = 'my-yang-resource-name'
49 def yangResourceContent = 'module stores {\n' +
50 ' yang-version 1.1;\n' +
51 ' namespace "org:onap:ccsdk:sample";\n' +
53 ' prefix book-store;\n' +
55 ' revision "2020-09-15" {\n' +
57 ' "Sample Model";\n' +
61 static yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
62 static yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
63 static sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
64 static checksumIntegrityException = new DataIntegrityViolationException('checksum integrity exception',
65 new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
66 static checksumIntegrityExceptionWithoutChecksum = new DataIntegrityViolationException('checksum integrity exception',
67 new ConstraintViolationException('', new SQLException('no checksum'), yangResourceChecksumDbConstraint))
68 static otherIntegrityException = new DataIntegrityViolationException('another integrity exception')
71 objectUnderTest = new CpsModulePersistenceServiceImpl(yangResourceRepositoryMock, schemaSetRepositoryMock,
72 dataspaceRepositoryMock, moduleReferenceRepositoryMock)
75 def 'Store schema set error scenario: #scenario.'() {
76 given: 'no yang resource are currently saved'
77 yangResourceRepositoryMock.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList()
78 and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)'
79 yangResourceRepositoryMock.saveAll(_) >> { throw dbException }
80 when: 'attempt to store schema set '
81 def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent]
82 objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap)
83 then: 'an #expectedThrownException is thrown'
84 def e = thrown(expectedThrownException)
85 assert e.getMessage().contains(expectedThrownExceptionMessage)
86 where: 'the following data is used'
87 scenario | dbException || expectedThrownException | expectedThrownExceptionMessage
88 'checksum data failure' | checksumIntegrityException || DuplicatedYangResourceException | yangResourceChecksum
89 'checksum failure without checksum' | checksumIntegrityExceptionWithoutChecksum || DuplicatedYangResourceException | 'no checksum found'
90 'other data failure' | otherIntegrityException || DataIntegrityViolationException | 'another integrity exception'