CPS PATCH operation does not merge existing data
[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-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
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.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
40
41 class CpsDataPersistenceServiceSpec extends Specification {
42
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)
48
49     def objectUnderTest = Spy(new CpsDataPersistenceServiceImpl(mockDataspaceRepository, mockAnchorRepository, mockFragmentRepository, jsonObjectMapper, mockSessionManager))
50
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(_);
61     }
62
63     def 'Store single data node.'() {
64         given: 'a 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])
70     }
71
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>)
77                 return fragmentEntity
78             }
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')
88     }
89
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')
108     }
109
110
111     def 'Retrieving a data node with a property JSON value of #scenario'() {
112         given: 'the db has a fragment with an attribute property JSON value of #scenario'
113             mockFragmentWithJson("{\"some attribute\": ${dataString}}")
114         when: 'getting the data node represented by this fragment'
115             def dataNode = objectUnderTest.getDataNode('my-dataspace', 'my-anchor',
116                     '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
117         then: 'the leaf is of the correct value and data type'
118             def attributeValue = dataNode.leaves.get('some attribute')
119             assert attributeValue == expectedValue
120             assert attributeValue.class == expectedDataClass
121         where: 'the following Data Type is passed'
122             scenario                              | dataString            || expectedValue     | expectedDataClass
123             'just numbers'                        | '15174'               || 15174             | Integer
124             'number with dot'                     | '15174.32'            || 15174.32          | Double
125             'number with 0 value after dot'       | '15174.0'             || 15174.0           | Double
126             'number with 0 value before dot'      | '0.32'                || 0.32              | Double
127             'number higher than max int'          | '2147483648'          || 2147483648        | Long
128             'just text'                           | '"Test"'              || 'Test'            | String
129             'number with exponent'                | '1.2345e5'            || 1.2345e5          | Double
130             'number higher than max int with dot' | '123456789101112.0'   || 123456789101112.0 | Double
131             'text and numbers'                    | '"String = \'1234\'"' || "String = '1234'" | String
132             'number as String'                    | '"12345"'             || '12345'           | String
133     }
134
135     def 'Retrieving a data node with invalid JSON'() {
136         given: 'a fragment with invalid JSON'
137             mockFragmentWithJson('{invalid json')
138         when: 'getting the data node represented by this fragment'
139             objectUnderTest.getDataNode('my-dataspace', 'my-anchor',
140                     '/parent-01', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
141         then: 'a data validation exception is thrown'
142             thrown(DataValidationException)
143     }
144
145     def 'start session'() {
146         when: 'start session'
147             objectUnderTest.startSession()
148         then: 'the session manager method to start session is invoked'
149             1 * mockSessionManager.startSession()
150     }
151
152     def 'close session'() {
153         given: 'session ID'
154             def someSessionId = 'someSessionId'
155         when: 'close session method is called with session ID as parameter'
156             objectUnderTest.closeSession(someSessionId)
157         then: 'the session manager method to close session is invoked with parameter'
158             1 * mockSessionManager.closeSession(someSessionId, mockSessionManager.WITH_COMMIT)
159     }
160
161     def 'Lock anchor.'(){
162         when: 'lock anchor method is called with anchor entity details'
163             objectUnderTest.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
164         then: 'the session manager method to lock anchor is invoked with same parameters'
165             1 * mockSessionManager.lockAnchor('mySessionId', 'myDataspaceName', 'myAnchorName', 123L)
166     }
167
168     def 'update data node leaves: #scenario'(){
169         given: 'A node exists for the given xpath'
170             mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/some/xpath') >> new FragmentEntity(xpath: '/some/xpath', attributes:  existingAttributes)
171         when: 'the node leaves are updated'
172             objectUnderTest.updateDataLeaves('some-dataspace', 'some-anchor', '/some/xpath', newAttributes as Map<String, Serializable>)
173         then: 'the fragment entity saved has the original and new attributes'
174             1 * mockFragmentRepository.save({fragmentEntity -> {
175                 assert fragmentEntity.getXpath() == '/some/xpath'
176                 assert fragmentEntity.getAttributes() == mergedAttributes
177             }})
178         where: 'the following attributes combinations are used'
179             scenario                      | existingAttributes     | newAttributes         | mergedAttributes
180             'add new leaf'                | '{"existing":"value"}' | ["new":"value"]       | '{"existing":"value","new":"value"}'
181             'update existing leaf'        | '{"existing":"value"}' | ["existing":"value2"] | '{"existing":"value2"}'
182             'update nothing with nothing' | ''                     | []                    | ''
183             'update with nothing'         | '{"existing":"value"}' | []                    | '{"existing":"value"}'
184             'update with same value'      | '{"existing":"value"}' | ["existing":"value"]  | '{"existing":"value"}'
185     }
186
187     def 'update data node and descendants: #scenario'(){
188         given: 'mocked responses'
189             mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath') >> new FragmentEntity(xpath: '/test/xpath', childFragments: [])
190         when: 'replace data node tree'
191             objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', dataNodes)
192         then: 'call fragment repository save all method'
193             1 * mockFragmentRepository.saveAll({fragmentEntities -> assert fragmentEntities as List == expectedFragmentEntities})
194         where: 'the following Data Type is passed'
195             scenario                         | dataNodes                                                                          || expectedFragmentEntities
196             'empty data node list'           | []                                                                                 || []
197             'one data node in list'          | [new DataNode(xpath: '/test/xpath', leaves: ['id': 'testId'], childDataNodes: [])] || [new FragmentEntity(xpath: '/test/xpath', attributes: '{"id":"testId"}', childFragments: [])]
198     }
199
200     def 'update data nodes and descendants'() {
201         given: 'the fragment repository returns a fragment entity related to the xpath input'
202             mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath1') >> new FragmentEntity(xpath: '/test/xpath1', childFragments: [])
203             mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath2') >> new FragmentEntity(xpath: '/test/xpath2', childFragments: [])
204         and: 'some data nodes with descendants'
205             def dataNode1 = new DataNode(xpath: '/test/xpath1', leaves: ['id': 'testId1'], childDataNodes: [new DataNode(xpath: '/test/xpath1/child', leaves: ['id': 'childTestId1'])])
206             def dataNode2 = new DataNode(xpath: '/test/xpath2', leaves: ['id': 'testId2'], childDataNodes: [new DataNode(xpath: '/test/xpath2/child', leaves: ['id': 'childTestId2'])])
207         when: 'the fragment entities are update by the data nodes'
208             objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', [dataNode1, dataNode2])
209         then: 'call fragment repository save all method is called with the updated fragments'
210             1 * mockFragmentRepository.saveAll({fragmentEntities -> {
211                 fragmentEntities.containsAll([
212                     new FragmentEntity(xpath: '/test/xpath1', attributes: '{"id":"testId1"}', childFragments: [new FragmentEntity(xpath: '/test/xpath1/child', attributes: '{"id":"childTestId1"}', childFragments: [])]),
213                     new FragmentEntity(xpath: '/test/xpath2', attributes: '{"id":"testId2"}', childFragments: [new FragmentEntity(xpath: '/test/xpath2/child', attributes: '{"id":"childTestId2"}', childFragments: [])])
214                 ])
215                 assert fragmentEntities.size() == 2
216             }})
217     }
218
219     def createDataNodeAndMockRepositoryMethodSupportingIt(xpath, scenario) {
220         def dataNode = new DataNodeBuilder().withXpath(xpath).build()
221         def fragmentEntity = new FragmentEntity(xpath: xpath, childFragments: [])
222         mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, xpath) >> fragmentEntity
223         if ('EXCEPTION' == scenario) {
224             mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
225         }
226         return dataNode
227     }
228
229     def mockFragmentWithJson(json) {
230         def anchorName = 'some anchor'
231         def mockAnchor = Mock(AnchorEntity)
232         mockAnchor.getId() >> 123
233         mockAnchor.getName() >> anchorName
234         mockAnchorRepository.getByDataspaceAndName(*_) >> mockAnchor
235         def mockFragmentExtract = Mock(FragmentExtract)
236         mockFragmentExtract.getId() >> 456
237         mockFragmentExtract.getAttributes() >> json
238         mockFragmentRepository.findByAnchorIdAndParentXpath(*_) >> [mockFragmentExtract]
239     }
240
241 }