2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
4 * Modifications Copyright (C) 2021-2022 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.FragmentEntity
28 import org.onap.cps.spi.entities.FragmentExtract
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, mockFragmentRepository, jsonObjectMapper, mockSessionManager))
51 def 'Storing data nodes individually when batch operation fails'(){
52 given: 'two data nodes and supporting repository mock behavior'
53 def dataNode1 = createDataNodeAndMockRepositoryMethodSupportingIt('xpath1','OK')
54 def dataNode2 = createDataNodeAndMockRepositoryMethodSupportingIt('xpath2','OK')
55 and: 'the batch store operation will fail'
56 mockFragmentRepository.saveAll(*_) >> { throw new DataIntegrityViolationException("Exception occurred") }
57 when: 'trying to store data nodes'
58 objectUnderTest.storeDataNodes('dataSpaceName', 'anchorName', [dataNode1, dataNode2])
59 then: 'the two data nodes are saved individually'
60 2 * mockFragmentRepository.save(_);
63 def 'Store single data node.'() {
65 def dataNode = new DataNode()
66 when: 'storing a single data node'
67 objectUnderTest.storeDataNode('dataspace1', 'anchor1', dataNode)
68 then: 'the call is redirected to storing a collection of data nodes with just the given data node'
69 1 * objectUnderTest.storeDataNodes('dataspace1', 'anchor1', [dataNode])
72 def 'Handling of StaleStateException (caused by concurrent updates) during update data node and descendants.'() {
73 given: 'the fragment repository returns a fragment entity'
74 mockFragmentRepository.getByDataspaceAndAnchorAndXpath(*_) >> {
75 def fragmentEntity = new FragmentEntity()
76 fragmentEntity.setChildFragments([new FragmentEntity()] as Set<FragmentEntity>)
79 and: 'a data node is concurrently updated by another transaction'
80 mockFragmentRepository.save(_) >> { throw new StaleStateException("concurrent updates") }
81 when: 'attempt to update data node with submitted data nodes'
82 objectUnderTest.updateDataNodeAndDescendants('some-dataspace', 'some-anchor', new DataNodeBuilder().withXpath('/some/xpath').build())
83 then: 'concurrency exception is thrown'
84 def concurrencyException = thrown(ConcurrencyException)
85 assert concurrencyException.getDetails().contains('some-dataspace')
86 assert concurrencyException.getDetails().contains('some-anchor')
87 assert concurrencyException.getDetails().contains('/some/xpath')
90 def 'Handling of StaleStateException (caused by concurrent updates) during update data nodes and descendants.'() {
91 given: 'the system contains and can update one datanode'
92 def dataNode1 = createDataNodeAndMockRepositoryMethodSupportingIt('/node1', 'OK')
93 and: 'the system contains two more datanodes that throw an exception while updating'
94 def dataNode2 = createDataNodeAndMockRepositoryMethodSupportingIt('/node2', 'EXCEPTION')
95 def dataNode3 = createDataNodeAndMockRepositoryMethodSupportingIt('/node3', 'EXCEPTION')
96 and: 'the batch update will therefore also fail'
97 mockFragmentRepository.saveAll(*_) >> { throw new StaleStateException("concurrent updates") }
98 when: 'attempt batch update data nodes'
99 objectUnderTest.updateDataNodesAndDescendants('some-dataspace', 'some-anchor', [dataNode1, dataNode2, dataNode3])
100 then: 'concurrency exception is thrown'
101 def thrown = thrown(ConcurrencyException)
102 assert thrown.message == 'Concurrent Transactions'
103 and: 'it does not contain the successfull datanode'
104 assert !thrown.details.contains('/node1')
105 and: 'it contains the failed datanodes'
106 assert thrown.details.contains('/node2')
107 assert thrown.details.contains('/node3')
110 def 'Retrieving a data node with a property JSON value of #scenario'() {
111 given: 'the db has a fragment with an attribute property JSON value of #scenario'
112 mockFragmentWithJson("{\"some attribute\": ${dataString}}")
113 when: 'getting the data node represented by this fragment'
114 def dataNode = objectUnderTest.getDataNode('my-dataspace', 'my-anchor',
115 '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
116 then: 'the leaf is of the correct value and data type'
117 def attributeValue = dataNode.leaves.get('some attribute')
118 assert attributeValue == expectedValue
119 assert attributeValue.class == expectedDataClass
120 where: 'the following Data Type is passed'
121 scenario | dataString || expectedValue | expectedDataClass
122 'just numbers' | '15174' || 15174 | Integer
123 'number with dot' | '15174.32' || 15174.32 | Double
124 'number with 0 value after dot' | '15174.0' || 15174.0 | Double
125 'number with 0 value before dot' | '0.32' || 0.32 | Double
126 'number higher than max int' | '2147483648' || 2147483648 | Long
127 'just text' | '"Test"' || 'Test' | String
128 'number with exponent' | '1.2345e5' || 1.2345e5 | Double
129 'number higher than max int with dot' | '123456789101112.0' || 123456789101112.0 | Double
130 'text and numbers' | '"String = \'1234\'"' || "String = '1234'" | String
131 'number as String' | '"12345"' || '12345' | String
134 def 'Retrieving a data node with invalid JSON'() {
135 given: 'a fragment with invalid JSON'
136 mockFragmentWithJson('{invalid json')
137 when: 'getting the data node represented by this fragment'
138 objectUnderTest.getDataNode('my-dataspace', 'my-anchor',
139 '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
140 then: 'a data validation exception is thrown'
141 thrown(DataValidationException)
144 def 'Retrieving multiple data nodes.'() {
145 given: 'db contains an anchor'
146 def anchorEntity = new AnchorEntity(id:123)
147 mockAnchorRepository.getByDataspaceAndName(*_) >> anchorEntity
148 and: 'fragment repository returns a collection of fragments'
149 def fragmentEntity1 = new FragmentEntity(xpath: 'xpath1', childFragments: [])
150 def fragmentEntity2 = new FragmentEntity(xpath: 'xpath2', childFragments: [])
151 mockFragmentRepository.findByAnchorAndMultipleCpsPaths(123, ['xpath1','xpath2']) >> [ fragmentEntity1, fragmentEntity2 ]
152 when: 'getting data nodes for 2 xpaths'
153 def result = objectUnderTest.getDataNodes('some-dataspace', 'some-anchor', ['xpath1','xpath2'],FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
154 then: '2 data nodes are returned'
155 assert result.size() == 2
158 def 'start session'() {
159 when: 'start session'
160 objectUnderTest.startSession()
161 then: 'the session manager method to start session is invoked'
162 1 * mockSessionManager.startSession()
165 def 'close session'() {
167 def someSessionId = 'someSessionId'
168 when: 'close session method is called with session ID as parameter'
169 objectUnderTest.closeSession(someSessionId)
170 then: 'the session manager method to close session is invoked with parameter'
171 1 * mockSessionManager.closeSession(someSessionId, mockSessionManager.WITH_COMMIT)
174 def 'Lock anchor.'(){
175 when: 'lock anchor method is called with anchor entity details'
176 objectUnderTest.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
177 then: 'the session manager method to lock anchor is invoked with same parameters'
178 1 * mockSessionManager.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
181 def 'update data node and descendants: #scenario'(){
182 given: 'mocked responses'
183 mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath') >> new FragmentEntity(xpath: '/test/xpath', childFragments: [])
184 when: 'replace data node tree'
185 objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', dataNodes)
186 then: 'call fragment repository save all method'
187 1 * mockFragmentRepository.saveAll({fragmentEntities -> assert fragmentEntities as List == expectedFragmentEntities})
188 where: 'the following Data Type is passed'
189 scenario | dataNodes || expectedFragmentEntities
190 'empty data node list' | [] || []
191 'one data node in list' | [new DataNode(xpath: '/test/xpath', leaves: ['id': 'testId'], childDataNodes: [])] || [new FragmentEntity(xpath: '/test/xpath', attributes: '{"id":"testId"}', childFragments: [])]
194 def 'update data nodes and descendants'() {
195 given: 'the fragment repository returns a fragment entity related to the xpath input'
196 mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath1') >> new FragmentEntity(xpath: '/test/xpath1', childFragments: [])
197 mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath2') >> new FragmentEntity(xpath: '/test/xpath2', childFragments: [])
198 and: 'some data nodes with descendants'
199 def dataNode1 = new DataNode(xpath: '/test/xpath1', leaves: ['id': 'testId1'], childDataNodes: [new DataNode(xpath: '/test/xpath1/child', leaves: ['id': 'childTestId1'])])
200 def dataNode2 = new DataNode(xpath: '/test/xpath2', leaves: ['id': 'testId2'], childDataNodes: [new DataNode(xpath: '/test/xpath2/child', leaves: ['id': 'childTestId2'])])
201 when: 'the fragment entities are update by the data nodes'
202 objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', [dataNode1, dataNode2])
203 then: 'call fragment repository save all method is called with the updated fragments'
204 1 * mockFragmentRepository.saveAll({fragmentEntities -> {
205 fragmentEntities.containsAll([
206 new FragmentEntity(xpath: '/test/xpath1', attributes: '{"id":"testId1"}', childFragments: [new FragmentEntity(xpath: '/test/xpath1/child', attributes: '{"id":"childTestId1"}', childFragments: [])]),
207 new FragmentEntity(xpath: '/test/xpath2', attributes: '{"id":"testId2"}', childFragments: [new FragmentEntity(xpath: '/test/xpath2/child', attributes: '{"id":"childTestId2"}', childFragments: [])])
209 assert fragmentEntities.size() == 2
213 def createDataNodeAndMockRepositoryMethodSupportingIt(xpath, scenario) {
214 def dataNode = new DataNodeBuilder().withXpath(xpath).build()
215 def fragmentEntity = new FragmentEntity(xpath: xpath, childFragments: [])
216 mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, xpath) >> fragmentEntity
217 if ('EXCEPTION' == scenario) {
218 mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
223 def mockFragmentWithJson(json) {
224 def anchorEntity = new AnchorEntity(id:123)
225 mockAnchorRepository.getByDataspaceAndName(*_) >> anchorEntity
226 def mockFragmentExtract = Mock(FragmentExtract)
227 mockFragmentExtract.getId() >> 456
228 mockFragmentExtract.getAttributes() >> json
229 mockFragmentRepository.findByAnchorIdAndParentXpath(*_) >> [mockFragmentExtract]