Renamed UnitSpec to Spec
[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  * ================================================================================
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  * ============LICENSE_END=========================================================
17 */
18
19 package org.onap.cps.spi.impl
20
21 import org.hibernate.StaleStateException
22 import org.onap.cps.spi.entities.FragmentEntity
23 import org.onap.cps.spi.exceptions.ConcurrencyException
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import org.onap.cps.spi.repository.AnchorRepository
26 import org.onap.cps.spi.repository.DataspaceRepository
27 import org.onap.cps.spi.repository.FragmentRepository
28 import spock.lang.Specification
29
30
31 class CpsDataPersistenceServiceSpec extends Specification {
32
33     def mockDataspaceRepository = Mock(DataspaceRepository)
34     def mockAnchorRepository = Mock(AnchorRepository)
35     def mockFragmentRepository = Mock(FragmentRepository)
36
37     def objectUnderTest = new CpsDataPersistenceServiceImpl(
38             mockDataspaceRepository, mockAnchorRepository, mockFragmentRepository)
39
40     def 'Handling of StaleStateException (caused by concurrent updates) during data node tree update.'() {
41
42         def parentXpath = 'parent-01'
43         def myDataspaceName = 'my-dataspace'
44         def myAnchorName = 'my-anchor'
45
46         given: 'data node object'
47             def submittedDataNode = new DataNodeBuilder()
48                     .withXpath(parentXpath)
49                     .withLeaves(['leaf-name': 'leaf-value'])
50                     .build()
51         and: 'fragment to be updated'
52             mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, _) >> {
53                 def fragmentEntity = new FragmentEntity()
54                 fragmentEntity.setXpath(parentXpath)
55                 fragmentEntity.setChildFragments(Collections.emptySet())
56                 return fragmentEntity
57             }
58         and: 'data node is concurrently updated by another transaction'
59             mockFragmentRepository.save(_) >> { throw new StaleStateException("concurrent updates") }
60
61         when: 'attempt to update data node'
62             objectUnderTest.replaceDataNodeTree(myDataspaceName, myAnchorName, submittedDataNode)
63
64         then: 'concurrency exception is thrown'
65             def concurrencyException = thrown(ConcurrencyException)
66             assert concurrencyException.getDetails().contains(myDataspaceName)
67             assert concurrencyException.getDetails().contains(myAnchorName)
68             assert concurrencyException.getDetails().contains(parentXpath)
69     }
70
71
72 }