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.onap.cps.spi.CpsAdminPersistenceService
25 import org.onap.cps.spi.CpsModulePersistenceService
26 import org.onap.cps.spi.entities.DataspaceEntity
27 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException
28 import org.onap.cps.spi.model.ModuleReference
29 import org.onap.cps.spi.repository.AnchorRepository
30 import org.onap.cps.spi.repository.DataspaceRepository
31 import org.onap.cps.spi.repository.SchemaSetRepository
32 import org.onap.cps.spi.repository.YangResourceRepository
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.spockframework.spring.SpringBean
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.dao.DataIntegrityViolationException
37 import spock.lang.Shared
39 import java.sql.SQLException
41 class CpsModulePersistenceServiceConcurrencySpec extends CpsPersistenceSpecBase {
44 CpsModulePersistenceService objectUnderTest
47 AnchorRepository anchorRepository
50 SchemaSetRepository schemaSetRepository
53 CpsAdminPersistenceService cpsAdminPersistenceService
56 YangResourceRepository yangResourceRepositoryMock = Mock()
59 DataspaceRepository dataspaceRepositoryMock = Mock()
62 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
64 static final String DATASPACE_NAME = 'DATASPACE-001'
65 static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
66 static final String NEW_RESOURCE_NAME = 'some new resource'
67 static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
68 ' yang-version 1.1;\n' +
69 ' namespace "org:onap:ccsdk:sample";\n' +
72 def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
75 yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
78 yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
81 sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
84 checksumIntegrityException =
85 new DataIntegrityViolationException("checksum integrity exception",
86 new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
88 def 'Store new schema set, retry mechanism'() {
89 given: 'no pre-existing schemaset in database'
90 dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity()
91 yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
92 when: 'a new schemaset is stored'
93 objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
94 then: ' duplicated yang resource exception is thrown '
95 def e = thrown(DuplicatedYangResourceException)
96 and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)'
97 5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException }
100 def 'Store schema set using modules, retry mechanism'() {
101 given: 'map of new modules, a list of existing modules, module reference'
102 def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }']
103 def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12")
104 def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
105 and: 'no pre-existing schemaset in database'
106 dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity()
107 yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
108 when: 'a new schemaset is stored from a module'
109 objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference)
110 then: ' duplicated yang resource exception is thrown '
111 def e = thrown(DuplicatedYangResourceException)
112 and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)'
113 5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException }