DMI Data AVC to use kafka headers
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2021 Bell Canada.
4  * Modifications Copyright (C) 2021-2023 Nordix Foundation
5  * Modifications Copyright (C) 2022-2023 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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19 */
20
21 package org.onap.cps.spi.impl
22
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.entities.FragmentExtract
30 import org.onap.cps.spi.exceptions.ConcurrencyException
31 import org.onap.cps.spi.exceptions.DataValidationException
32 import org.onap.cps.spi.model.DataNode
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.onap.cps.spi.repository.AnchorRepository
35 import org.onap.cps.spi.repository.DataspaceRepository
36 import org.onap.cps.spi.repository.FragmentRepository
37 import org.onap.cps.spi.utils.SessionManager
38 import org.onap.cps.utils.JsonObjectMapper
39 import org.springframework.dao.DataIntegrityViolationException
40 import spock.lang.Specification
41
42 class CpsDataPersistenceServiceSpec extends Specification {
43
44     def mockDataspaceRepository = Mock(DataspaceRepository)
45     def mockAnchorRepository = Mock(AnchorRepository)
46     def mockFragmentRepository = Mock(FragmentRepository)
47     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
48     def mockSessionManager = Mock(SessionManager)
49
50     def objectUnderTest = Spy(new CpsDataPersistenceServiceImpl(mockDataspaceRepository, mockAnchorRepository,
51             mockFragmentRepository, jsonObjectMapper, mockSessionManager))
52
53     static def anchorEntity = new AnchorEntity(id: 123, dataspace: new DataspaceEntity(id: 1))
54
55     def setup() {
56         mockAnchorRepository.getByDataspaceAndName(_, _) >> anchorEntity
57     }
58
59     def 'Storing data nodes individually when batch operation fails'(){
60         given: 'two data nodes and supporting repository mock behavior'
61             def dataNode1 = createDataNodeAndMockRepositoryMethodSupportingIt('xpath1','OK')
62             def dataNode2 = createDataNodeAndMockRepositoryMethodSupportingIt('xpath2','OK')
63         and: 'the batch store operation will fail'
64             mockFragmentRepository.saveAll(*_) >> { throw new DataIntegrityViolationException("Exception occurred") }
65         when: 'trying to store data nodes'
66             objectUnderTest.storeDataNodes('dataSpaceName', 'anchorName', [dataNode1, dataNode2])
67         then: 'the two data nodes are saved individually'
68             2 * mockFragmentRepository.save(_)
69     }
70
71     def 'Handling of StaleStateException (caused by concurrent updates) during update data nodes and descendants.'() {
72         given: 'the system can update one datanode and has two more datanodes that throw an exception while updating'
73             def dataNodes = createDataNodesAndMockRepositoryMethodSupportingThem([
74                 '/node1': 'OK',
75                 '/node2': 'EXCEPTION',
76                 '/node3': 'EXCEPTION'])
77         and: 'the batch update will therefore also fail'
78             mockFragmentRepository.saveAll(*_) >> { throw new StaleStateException("concurrent updates") }
79         when: 'attempt batch update data nodes'
80             objectUnderTest.updateDataNodesAndDescendants('some-dataspace', 'some-anchor', dataNodes)
81         then: 'concurrency exception is thrown'
82             def thrown = thrown(ConcurrencyException)
83             assert thrown.message == 'Concurrent Transactions'
84         and: 'it does not contain the successfull datanode'
85             assert !thrown.details.contains('/node1')
86         and: 'it contains the failed datanodes'
87             assert thrown.details.contains('/node2')
88             assert thrown.details.contains('/node3')
89     }
90
91     def 'Retrieving a data node with a property JSON value of #scenario'() {
92         given: 'the db has a fragment with an attribute property JSON value of #scenario'
93             mockFragmentWithJson("{\"some attribute\": ${dataString}}")
94         when: 'getting the data node represented by this fragment'
95             def dataNode = objectUnderTest.getDataNodes('my-dataspace', 'my-anchor',
96                     '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
97         then: 'the leaf is of the correct value and data type'
98             def attributeValue = dataNode[0].leaves.get('some attribute')
99             assert attributeValue == expectedValue
100             assert attributeValue.class == expectedDataClass
101         where: 'the following Data Type is passed'
102             scenario                              | dataString            || expectedValue     | expectedDataClass
103             'just numbers'                        | '15174'               || 15174             | Integer
104             'number with dot'                     | '15174.32'            || 15174.32          | Double
105             'number with 0 value after dot'       | '15174.0'             || 15174.0           | Double
106             'number with 0 value before dot'      | '0.32'                || 0.32              | Double
107             'number higher than max int'          | '2147483648'          || 2147483648        | Long
108             'just text'                           | '"Test"'              || 'Test'            | String
109             'number with exponent'                | '1.2345e5'            || 1.2345e5          | Double
110             'number higher than max int with dot' | '123456789101112.0'   || 123456789101112.0 | Double
111             'text and numbers'                    | '"String = \'1234\'"' || "String = '1234'" | String
112             'number as String'                    | '"12345"'             || '12345'           | String
113     }
114
115     def 'Retrieving a data node with invalid JSON'() {
116         given: 'a fragment with invalid JSON'
117             mockFragmentWithJson('{invalid json')
118         when: 'getting the data node represented by this fragment'
119             objectUnderTest.getDataNodes('my-dataspace', 'my-anchor',
120                     '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
121         then: 'a data validation exception is thrown'
122             thrown(DataValidationException)
123     }
124
125     def 'Retrieving multiple data nodes.'() {
126         given: 'fragment repository returns a collection of fragments'
127             mockFragmentRepository.findExtractsWithDescendants(123, ['/xpath1', '/xpath2'] as Set, _) >> [
128                 mockFragmentExtract(1, null, 123, '/xpath1', null),
129                 mockFragmentExtract(2, null, 123, '/xpath2', null)
130             ]
131         when: 'getting data nodes for 2 xpaths'
132             def result = objectUnderTest.getDataNodesForMultipleXpaths('some-dataspace', 'some-anchor', ['/xpath1', '/xpath2'], FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
133         then: '2 data nodes are returned'
134             assert result.size() == 2
135     }
136
137     def 'start session'() {
138         when: 'start session'
139             objectUnderTest.startSession()
140         then: 'the session manager method to start session is invoked'
141             1 * mockSessionManager.startSession()
142     }
143
144     def 'close session'() {
145         given: 'session ID'
146             def someSessionId = 'someSessionId'
147         when: 'close session method is called with session ID as parameter'
148             objectUnderTest.closeSession(someSessionId)
149         then: 'the session manager method to close session is invoked with parameter'
150             1 * mockSessionManager.closeSession(someSessionId, mockSessionManager.WITH_COMMIT)
151     }
152
153     def 'Lock anchor.'(){
154         when: 'lock anchor method is called with anchor entity details'
155             objectUnderTest.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
156         then: 'the session manager method to lock anchor is invoked with same parameters'
157             1 * mockSessionManager.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
158     }
159
160     def 'update data node leaves: #scenario'(){
161         given: 'A node exists for the given xpath'
162             mockFragmentRepository.getByAnchorAndXpath(_, '/some/xpath') >> new FragmentEntity(xpath: '/some/xpath', attributes:  existingAttributes)
163         when: 'the node leaves are updated'
164             objectUnderTest.updateDataLeaves('some-dataspace', 'some-anchor', '/some/xpath', newAttributes as Map<String, Serializable>)
165         then: 'the fragment entity saved has the original and new attributes'
166             1 * mockFragmentRepository.save({fragmentEntity -> {
167                 assert fragmentEntity.getXpath() == '/some/xpath'
168                 assert fragmentEntity.getAttributes() == mergedAttributes
169             }})
170         where: 'the following attributes combinations are used'
171             scenario                      | existingAttributes     | newAttributes         | mergedAttributes
172             'add new leaf'                | '{"existing":"value"}' | ["new":"value"]       | '{"existing":"value","new":"value"}'
173             'update existing leaf'        | '{"existing":"value"}' | ["existing":"value2"] | '{"existing":"value2"}'
174             'update nothing with nothing' | ''                     | []                    | ''
175             'update with nothing'         | '{"existing":"value"}' | []                    | '{"existing":"value"}'
176             'update with same value'      | '{"existing":"value"}' | ["existing":"value"]  | '{"existing":"value"}'
177     }
178
179     def 'update data node and descendants: #scenario'(){
180         given: 'the fragment repository returns fragment entities related to the xpath inputs'
181             mockFragmentRepository.findExtractsWithDescendants(_, [] as Set, _) >> []
182             mockFragmentRepository.findExtractsWithDescendants(_, ['/test/xpath'] as Set, _) >> [
183                 mockFragmentExtract(1, null, 123, '/test/xpath', null)
184             ]
185         when: 'replace data node tree'
186             objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', dataNodes)
187         then: 'call fragment repository save all method'
188             1 * mockFragmentRepository.saveAll({fragmentEntities -> assert fragmentEntities as List == expectedFragmentEntities})
189         where: 'the following Data Type is passed'
190             scenario                         | dataNodes                                                                          || expectedFragmentEntities
191             'empty data node list'           | []                                                                                 || []
192             'one data node in list'          | [new DataNode(xpath: '/test/xpath', leaves: ['id': 'testId'], childDataNodes: [])] || [new FragmentEntity(xpath: '/test/xpath', attributes: '{"id":"testId"}', anchor: anchorEntity, childFragments: [])]
193     }
194
195     def 'update data nodes and descendants'() {
196         given: 'the fragment repository returns fragment entities related to the xpath inputs'
197             mockFragmentRepository.findExtractsWithDescendants(_, ['/test/xpath1', '/test/xpath2'] as Set, _) >> [
198                 mockFragmentExtract(1, null, 123, '/test/xpath1', null),
199                 mockFragmentExtract(2, null, 123, '/test/xpath2', null)
200             ]
201         and: 'some data nodes with descendants'
202             def dataNode1 = new DataNode(xpath: '/test/xpath1', leaves: ['id': 'testId1'], childDataNodes: [new DataNode(xpath: '/test/xpath1/child', leaves: ['id': 'childTestId1'])])
203             def dataNode2 = new DataNode(xpath: '/test/xpath2', leaves: ['id': 'testId2'], childDataNodes: [new DataNode(xpath: '/test/xpath2/child', leaves: ['id': 'childTestId2'])])
204         when: 'the fragment entities are update by the data nodes'
205             objectUnderTest.updateDataNodesAndDescendants('dataspace', 'anchor', [dataNode1, dataNode2])
206         then: 'call fragment repository save all method is called with the updated fragments'
207             1 * mockFragmentRepository.saveAll({fragmentEntities -> {
208                 assert fragmentEntities.size() == 2
209                 def fragmentEntityPerXpath = fragmentEntities.collectEntries { [it.xpath, it] }
210                 assert fragmentEntityPerXpath.get('/test/xpath1').childFragments.first().attributes == '{"id":"childTestId1"}'
211                 assert fragmentEntityPerXpath.get('/test/xpath2').childFragments.first().attributes == '{"id":"childTestId2"}'
212             }})
213     }
214
215     def createDataNodeAndMockRepositoryMethodSupportingIt(xpath, scenario) {
216         def dataNode = new DataNodeBuilder().withXpath(xpath).build()
217         def fragmentEntity = new FragmentEntity(xpath: xpath, childFragments: [])
218         mockFragmentRepository.getByAnchorAndXpath(_, xpath) >> fragmentEntity
219         if ('EXCEPTION' == scenario) {
220             mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
221         }
222         return dataNode
223     }
224
225     def createDataNodesAndMockRepositoryMethodSupportingThem(Map<String, String> xpathToScenarioMap) {
226         def dataNodes = []
227         def fragmentExtracts = []
228         def fragmentId = 1
229         xpathToScenarioMap.each {
230             def xpath = it.key
231             def scenario = it.value
232             def dataNode = new DataNodeBuilder().withXpath(xpath).build()
233             dataNodes.add(dataNode)
234             def fragmentExtract = mockFragmentExtract(fragmentId, null, 123, xpath, null)
235             fragmentExtracts.add(fragmentExtract)
236             def fragmentEntity = new FragmentEntity(id: fragmentId, anchor: anchorEntity, xpath: xpath, childFragments: [])
237             if ('EXCEPTION' == scenario) {
238                 mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
239             }
240             fragmentId++
241         }
242         mockFragmentRepository.findExtractsWithDescendants(_, xpathToScenarioMap.keySet(), _) >> fragmentExtracts
243         return dataNodes
244     }
245
246     def mockFragmentWithJson(json) {
247         def fragmentExtract = mockFragmentExtract(456, null, 123, '/parent-01', json)
248         mockFragmentRepository.findExtractsWithDescendants(123, ['/parent-01'] as Set, _) >> [fragmentExtract]
249     }
250
251     def mockFragmentExtract(id, parentId, anchorId, xpath, attributes) {
252         def fragmentExtract = Mock(FragmentExtract)
253         fragmentExtract.getId() >> id
254         fragmentExtract.getParentId() >> parentId
255         fragmentExtract.getAnchorId() >> anchorId
256         fragmentExtract.getXpath() >> xpath
257         fragmentExtract.getAttributes() >> attributes
258         return fragmentExtract
259     }
260
261 }