Removed ExtendedModuleReference Object
[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  *  ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.onap.cps.spi.impl
21
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
38
39 import java.sql.SQLException
40
41 class CpsModulePersistenceServiceConcurrencySpec extends CpsPersistenceSpecBase {
42
43     @Autowired
44     CpsModulePersistenceService objectUnderTest
45
46     @Autowired
47     AnchorRepository anchorRepository
48
49     @Autowired
50     SchemaSetRepository schemaSetRepository
51
52     @Autowired
53     CpsAdminPersistenceService cpsAdminPersistenceService
54
55     @SpringBean
56     YangResourceRepository yangResourceRepositoryMock = Mock()
57
58     @SpringBean
59     DataspaceRepository dataspaceRepositoryMock = Mock()
60
61     @SpringBean
62     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
63
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' +
70             '}'
71
72     def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT]
73
74     @Shared
75     yangResourceChecksum = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539'
76
77     @Shared
78     yangResourceChecksumDbConstraint = 'yang_resource_checksum_key'
79
80     @Shared
81     sqlExceptionMessage = String.format('(checksum)=(%s)', yangResourceChecksum)
82
83     @Shared
84     checksumIntegrityException =
85            new DataIntegrityViolationException("checksum integrity exception",
86                  new ConstraintViolationException('', new SQLException(sqlExceptionMessage), yangResourceChecksumDbConstraint))
87
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 }
98     }
99
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 }
114     }
115 }