Replace deprecated WebSecurityConfigurerAdapter
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsModulePersistenceServiceConcurrencySpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada.
4  *  Modifications Copyright (C) 2021-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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.onap.cps.spi.impl
22
23
24 import org.hibernate.exception.ConstraintViolationException
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.DataspaceRepository
30 import org.onap.cps.spi.repository.YangResourceRepository
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.dao.DataIntegrityViolationException
34 import spock.lang.Shared
35
36 import java.sql.SQLException
37
38 class CpsModulePersistenceServiceConcurrencySpec extends CpsPersistenceSpecBase {
39
40     @Autowired
41     CpsModulePersistenceService objectUnderTest
42
43     @SpringBean
44     YangResourceRepository yangResourceRepositoryMock = Mock()
45
46     @SpringBean
47     DataspaceRepository dataspaceRepositoryMock = Mock()
48
49     static final String DATASPACE_NAME = 'DATASPACE-001'
50     static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW'
51     static final String NEW_RESOURCE_NAME = 'some new resource'
52     static final String NEW_RESOURCE_CONTENT = 'module stores {\n' +
53             '    yang-version 1.1;\n' +
54             '    namespace "org:onap:ccsdk:sample";\n' +
55             '}'
56
57     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
58
59     @Shared
60     yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
61
62     @Shared
63     yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
64
65     @Shared
66     sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
67
68     @Shared
69     checksumIntegrityException =
70            new DataIntegrityViolationException("checksum integrity exception",
71                  new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
72
73     def 'Store new schema set, retry mechanism'() {
74         given: 'no pre-existing schemaset in database'
75             dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity()
76             yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
77         when: 'a new schemaset is stored'
78             objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap)
79         then: ' duplicated yang resource exception is thrown '
80             def e = thrown(DuplicatedYangResourceException)
81         and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)'
82             5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException }
83     }
84
85     def 'Store schema set using modules, retry mechanism'() {
86         given: 'map of new modules, a list of existing modules, module reference'
87             def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }']
88             def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12")
89             def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule]
90         and: 'no pre-existing schemaset in database'
91             dataspaceRepositoryMock.getByName(_) >> new DataspaceEntity()
92             yangResourceRepositoryMock.findAllByChecksumIn(_) >> Collections.emptyList()
93         when: 'a new schemaset is stored from a module'
94             objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference)
95         then: ' duplicated yang resource exception is thrown '
96             def e = thrown(DuplicatedYangResourceException)
97         and: 'the system will attempt to save the data 5 times (because checksum integrity exception is thrown each time)'
98             5 * yangResourceRepositoryMock.saveAll(_) >> { throw checksumIntegrityException }
99     }
100 }