2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.cps.spi.impl
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.hibernate.exception.ConstraintViolationException
24 import org.mockito.InjectMocks
25 import org.mockito.Mock
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.CpsModulePersistenceService
28 import org.onap.cps.spi.entities.DataspaceEntity
29 import org.onap.cps.spi.entities.YangResourceEntity
30 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
31 import org.onap.cps.spi.model.ExtendedModuleReference
32 import org.onap.cps.spi.model.ModuleReference
33 import org.onap.cps.spi.repository.AnchorRepository
34 import org.onap.cps.spi.repository.DataspaceRepository
35 import org.onap.cps.spi.repository.FragmentRepository
36 import org.onap.cps.spi.repository.SchemaSetRepository
37 import org.onap.cps.spi.repository.YangResourceRepository
38 import org.onap.cps.utils.JsonObjectMapper
39 import org.spockframework.spring.SpringBean
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.test.context.SpringBootTest
42 import org.springframework.boot.test.mock.mockito.MockBean
43 import org.springframework.dao.DataIntegrityViolationException
44 import org.springframework.test.context.jdbc.Sql
45 import spock.lang.Shared
46 import spock.lang.Specification
48 import java.sql.SQLException
50 class CpsModulePersistenceServiceConcurrencySpec extends CpsPersistenceSpecBase {
53 CpsModulePersistenceService objectUnderTest
56 AnchorRepository anchorRepository
59 SchemaSetRepository schemaSetRepository
62 CpsAdminPersistenceService cpsAdminPersistenceService
65 YangResourceRepository yangResourceRepositoryMock = Mock()
68 DataspaceRepository dataspaceRepositoryMock = Mock()
71 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
73 static final String DATASPACE_NAME = 'DATASPACE-001'
74 static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
75 static final String NEW_RESOURCE_NAME = 'some new resource'
76 static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
77 ' yang-version 1.1;\n' +
78 ' namespace "org:onap:ccsdk:sample";\n' +
81 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
84 yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
87 yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
90 sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
93 checksumIntegrityException =
94 new DataIntegrityViolationException("checksum integrity exception",
95 new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
97 def 'Store new schema set, retry mechanism'() {
98 given: 'no pre-existing schemaset in database'
99 dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity()
100 yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
101 when: 'a new schemaset is stored'
102 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
103 then: ' duplicated yang resource exception is thrown '
104 def e = thrown(DuplicatedYangResourceException)
105 and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)'
106 5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException }
109 def 'Store schema set using modules, retry mechanism'() {
110 given: 'map of new modules, a list of existing modules, module reference'
111 def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }']
112 def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12")
113 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
114 and: 'no pre-existing schemaset in database'
115 dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity()
116 yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
117 when: 'a new schemaset is stored from a module'
118 objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference)
119 then: ' duplicated yang resource exception is thrown '
120 def e = thrown(DuplicatedYangResourceException)
121 and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)'
122 5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException }