2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
4 * Modifications Copyright (C) 2021-2023 Nordix Foundation
5 * Modifications Copyright (C) 2022 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.impl
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.hibernate.StaleStateException
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.entities.AnchorEntity
27 import org.onap.cps.spi.entities.DataspaceEntity
28 import org.onap.cps.spi.entities.FragmentEntity
29 import org.onap.cps.spi.exceptions.ConcurrencyException
30 import org.onap.cps.spi.exceptions.DataValidationException
31 import org.onap.cps.spi.model.DataNode
32 import org.onap.cps.spi.model.DataNodeBuilder
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.utils.SessionManager
37 import org.onap.cps.utils.JsonObjectMapper
38 import org.springframework.dao.DataIntegrityViolationException
39 import spock.lang.Specification
41 class CpsDataPersistenceServiceSpec extends Specification {
43 def mockDataspaceRepository = Mock(DataspaceRepository)
44 def mockAnchorRepository = Mock(AnchorRepository)
45 def mockFragmentRepository = Mock(FragmentRepository)
46 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
47 def mockSessionManager = Mock(SessionManager)
49 def objectUnderTest = Spy(new CpsDataPersistenceServiceImpl(mockDataspaceRepository, mockAnchorRepository,
50 mockFragmentRepository, jsonObjectMapper, mockSessionManager))
52 def anchorEntity = new AnchorEntity(id: 123, dataspace: new DataspaceEntity(id: 1))
55 mockAnchorRepository.getByDataspaceAndName(_, _) >> anchorEntity
58 def 'Storing data nodes individually when batch operation fails'(){
59 given: 'two data nodes and supporting repository mock behavior'
60 def dataNode1 = createDataNodeAndMockRepositoryMethodSupportingIt('xpath1','OK')
61 def dataNode2 = createDataNodeAndMockRepositoryMethodSupportingIt('xpath2','OK')
62 and: 'the batch store operation will fail'
63 mockFragmentRepository.saveAll(*_) >> { throw new DataIntegrityViolationException("Exception occurred") }
64 when: 'trying to store data nodes'
65 objectUnderTest.storeDataNodes('dataSpaceName', 'anchorName', [dataNode1, dataNode2])
66 then: 'the two data nodes are saved individually'
67 2 * mockFragmentRepository.save(_)
70 def 'Store single data node.'() {
72 def dataNode = new DataNode()
73 when: 'storing a single data node'
74 objectUnderTest.storeDataNode('dataspace1', 'anchor1', dataNode)
75 then: 'the call is redirected to storing a collection of data nodes with just the given data node'
76 1 * objectUnderTest.storeDataNodes('dataspace1', 'anchor1', [dataNode])
79 def 'Handling of StaleStateException (caused by concurrent updates) during update data node and descendants.'() {
80 given: 'the fragment repository returns a fragment entity'
81 mockFragmentRepository.getByAnchorAndXpath(*_) >> {
82 def fragmentEntity = new FragmentEntity()
83 fragmentEntity.setChildFragments([new FragmentEntity()] as Set<FragmentEntity>)
86 and: 'a data node is concurrently updated by another transaction'
87 mockFragmentRepository.save(_) >> { throw new StaleStateException("concurrent updates") }
88 when: 'attempt to update data node with submitted data nodes'
89 objectUnderTest.updateDataNodeAndDescendants('some-dataspace', 'some-anchor', new DataNodeBuilder().withXpath('/some/xpath').build())
90 then: 'concurrency exception is thrown'
91 def concurrencyException = thrown(ConcurrencyException)
92 assert concurrencyException.getDetails().contains('some-dataspace')
93 assert concurrencyException.getDetails().contains('some-anchor')
94 assert concurrencyException.getDetails().contains('/some/xpath')
97 def 'Handling of StaleStateException (caused by concurrent updates) during update data nodes and descendants.'() {
98 given: 'the system can update one datanode and has two more datanodes that throw an exception while updating'
99 def dataNodes = createDataNodesAndMockRepositoryMethodSupportingThem([
101 '/node2': 'EXCEPTION',
102 '/node3': 'EXCEPTION'])
103 and: 'the batch update will therefore also fail'
104 mockFragmentRepository.saveAll(*_) >> { throw new StaleStateException("concurrent updates") }
105 when: 'attempt batch update data nodes'
106 objectUnderTest.updateDataNodesAndDescendants('some-dataspace', 'some-anchor', dataNodes)
107 then: 'concurrency exception is thrown'
108 def thrown = thrown(ConcurrencyException)
109 assert thrown.message == 'Concurrent Transactions'
110 and: 'it does not contain the successfull datanode'
111 assert !thrown.details.contains('/node1')
112 and: 'it contains the failed datanodes'
113 assert thrown.details.contains('/node2')
114 assert thrown.details.contains('/node3')
117 def 'Retrieving a data node with a property JSON value of #scenario'() {
118 given: 'the db has a fragment with an attribute property JSON value of #scenario'
119 mockFragmentWithJson("{\"some attribute\": ${dataString}}")
120 when: 'getting the data node represented by this fragment'
121 def dataNode = objectUnderTest.getDataNodes('my-dataspace', 'my-anchor',
122 '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
123 then: 'the leaf is of the correct value and data type'
124 def attributeValue = dataNode[0].leaves.get('some attribute')
125 assert attributeValue == expectedValue
126 assert attributeValue.class == expectedDataClass
127 where: 'the following Data Type is passed'
128 scenario | dataString || expectedValue | expectedDataClass
129 'just numbers' | '15174' || 15174 | Integer
130 'number with dot' | '15174.32' || 15174.32 | Double
131 'number with 0 value after dot' | '15174.0' || 15174.0 | Double
132 'number with 0 value before dot' | '0.32' || 0.32 | Double
133 'number higher than max int' | '2147483648' || 2147483648 | Long
134 'just text' | '"Test"' || 'Test' | String
135 'number with exponent' | '1.2345e5' || 1.2345e5 | Double
136 'number higher than max int with dot' | '123456789101112.0' || 123456789101112.0 | Double
137 'text and numbers' | '"String = \'1234\'"' || "String = '1234'" | String
138 'number as String' | '"12345"' || '12345' | String
141 def 'Retrieving a data node with invalid JSON'() {
142 given: 'a fragment with invalid JSON'
143 mockFragmentWithJson('{invalid json')
144 when: 'getting the data node represented by this fragment'
145 objectUnderTest.getDataNodes('my-dataspace', 'my-anchor',
146 '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
147 then: 'a data validation exception is thrown'
148 thrown(DataValidationException)
151 def 'Retrieving multiple data nodes.'() {
152 given: 'fragment repository returns a collection of fragments'
153 def fragmentEntity1 = new FragmentEntity(xpath: '/xpath1', childFragments: [])
154 def fragmentEntity2 = new FragmentEntity(xpath: '/xpath2', childFragments: [])
155 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(123, ['/xpath1', '/xpath2'] as Set<String>) >> [fragmentEntity1, fragmentEntity2]
156 when: 'getting data nodes for 2 xpaths'
157 def result = objectUnderTest.getDataNodesForMultipleXpaths('some-dataspace', 'some-anchor', ['/xpath1', '/xpath2'], FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
158 then: '2 data nodes are returned'
159 assert result.size() == 2
162 def 'start session'() {
163 when: 'start session'
164 objectUnderTest.startSession()
165 then: 'the session manager method to start session is invoked'
166 1 * mockSessionManager.startSession()
169 def 'close session'() {
171 def someSessionId = 'someSessionId'
172 when: 'close session method is called with session ID as parameter'
173 objectUnderTest.closeSession(someSessionId)
174 then: 'the session manager method to close session is invoked with parameter'
175 1 * mockSessionManager.closeSession(someSessionId, mockSessionManager.WITH_COMMIT)
178 def 'Lock anchor.'(){
179 when: 'lock anchor method is called with anchor entity details'
180 objectUnderTest.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
181 then: 'the session manager method to lock anchor is invoked with same parameters'
182 1 * mockSessionManager.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
185 def 'update data node leaves: #scenario'(){
186 given: 'A node exists for the given xpath'
187 mockFragmentRepository.getByAnchorAndXpath(_, '/some/xpath') >> new FragmentEntity(xpath: '/some/xpath', attributes: existingAttributes)
188 when: 'the node leaves are updated'
189 objectUnderTest.updateDataLeaves('some-dataspace', 'some-anchor', '/some/xpath', newAttributes as Map<String, Serializable>)
190 then: 'the fragment entity saved has the original and new attributes'
191 1 * mockFragmentRepository.save({fragmentEntity -> {
192 assert fragmentEntity.getXpath() == '/some/xpath'
193 assert fragmentEntity.getAttributes() == mergedAttributes
195 where: 'the following attributes combinations are used'
196 scenario | existingAttributes | newAttributes | mergedAttributes
197 'add new leaf' | '{"existing":"value"}' | ["new":"value"] | '{"existing":"value","new":"value"}'
198 'update existing leaf' | '{"existing":"value"}' | ["existing":"value2"] | '{"existing":"value2"}'
199 'update nothing with nothing' | '' | [] | ''
200 'update with nothing' | '{"existing":"value"}' | [] | '{"existing":"value"}'
201 'update with same value' | '{"existing":"value"}' | ["existing":"value"] | '{"existing":"value"}'
204 def 'update data node and descendants: #scenario'(){
205 given: 'the fragment repository returns fragment entities related to the xpath inputs'
206 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(_, [] as Set) >> []
207 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(_, ['/test/xpath'] as Set) >> [new FragmentEntity(xpath: '/test/xpath', childFragments: [])]
208 when: 'replace data node tree'
209 objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', dataNodes)
210 then: 'call fragment repository save all method'
211 1 * mockFragmentRepository.saveAll({fragmentEntities -> assert fragmentEntities as List == expectedFragmentEntities})
212 where: 'the following Data Type is passed'
213 scenario | dataNodes || expectedFragmentEntities
214 'empty data node list' | [] || []
215 'one data node in list' | [new DataNode(xpath: '/test/xpath', leaves: ['id': 'testId'], childDataNodes: [])] || [new FragmentEntity(xpath: '/test/xpath', attributes: '{"id":"testId"}', childFragments: [])]
218 def 'update data nodes and descendants'() {
219 given: 'the fragment repository returns fragment entities related to the xpath inputs'
220 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(_, ['/test/xpath1', '/test/xpath2'] as Set) >> [
221 new FragmentEntity(xpath: '/test/xpath1', childFragments: [], anchor: anchorEntity),
222 new FragmentEntity(xpath: '/test/xpath2', childFragments: [], anchor: anchorEntity)]
223 and: 'some data nodes with descendants'
224 def dataNode1 = new DataNode(xpath: '/test/xpath1', leaves: ['id': 'testId1'], childDataNodes: [new DataNode(xpath: '/test/xpath1/child', leaves: ['id': 'childTestId1'])])
225 def dataNode2 = new DataNode(xpath: '/test/xpath2', leaves: ['id': 'testId2'], childDataNodes: [new DataNode(xpath: '/test/xpath2/child', leaves: ['id': 'childTestId2'])])
226 when: 'the fragment entities are update by the data nodes'
227 objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', [dataNode1, dataNode2])
228 then: 'call fragment repository save all method is called with the updated fragments'
229 1 * mockFragmentRepository.saveAll({fragmentEntities -> {
230 fragmentEntities.containsAll([
231 new FragmentEntity(xpath: '/test/xpath1', attributes: '{"id":"testId1"}', childFragments: [new FragmentEntity(xpath: '/test/xpath1/child', attributes: '{"id":"childTestId1"}', childFragments: [])]),
232 new FragmentEntity(xpath: '/test/xpath2', attributes: '{"id":"testId2"}', childFragments: [new FragmentEntity(xpath: '/test/xpath2/child', attributes: '{"id":"childTestId2"}', childFragments: [])])
234 assert fragmentEntities.size() == 2
238 def createDataNodeAndMockRepositoryMethodSupportingIt(xpath, scenario) {
239 def dataNode = new DataNodeBuilder().withXpath(xpath).build()
240 def fragmentEntity = new FragmentEntity(xpath: xpath, childFragments: [])
241 mockFragmentRepository.getByAnchorAndXpath(_, xpath) >> fragmentEntity
242 if ('EXCEPTION' == scenario) {
243 mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
248 def createDataNodesAndMockRepositoryMethodSupportingThem(Map<String, String> xpathToScenarioMap) {
250 def fragmentEntities = []
251 xpathToScenarioMap.each {
253 def scenario = it.value
254 def dataNode = new DataNodeBuilder().withXpath(xpath).build()
255 dataNodes.add(dataNode)
256 def fragmentEntity = new FragmentEntity(xpath: xpath, childFragments: [])
257 fragmentEntities.add(fragmentEntity)
258 mockFragmentRepository.getByAnchorAndXpath(_, xpath) >> fragmentEntity
259 if ('EXCEPTION' == scenario) {
260 mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
263 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(_, xpathToScenarioMap.keySet()) >> fragmentEntities
267 def mockFragmentWithJson(json) {
268 def fragmentEntity = new FragmentEntity(xpath: '/parent-01', childFragments: [], attributes: json)
269 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(123, ['/parent-01'] as Set<String>) >> [fragmentEntity]