+
+ def 'update data node and descendants: #scenario'(){
+ given: 'mocked responses'
+ mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath') >> new FragmentEntity(xpath: '/test/xpath', childFragments: [])
+ when: 'replace data node tree'
+ objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', dataNodes)
+ then: 'call fragment repository save all method'
+ 1 * mockFragmentRepository.saveAll({fragmentEntities -> assert fragmentEntities as List == expectedFragmentEntities})
+ where: 'the following Data Type is passed'
+ scenario | dataNodes || expectedFragmentEntities
+ 'empty data node list' | [] || []
+ 'one data node in list' | [new DataNode(xpath: '/test/xpath', leaves: ['id': 'testId'], childDataNodes: [])] || [new FragmentEntity(xpath: '/test/xpath', attributes: '{"id":"testId"}', childFragments: [])]
+ }
+
+ def 'update data nodes and descendants'() {
+ given: 'the fragment repository returns a fragment entity related to the xpath input'
+ mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath1') >> new FragmentEntity(xpath: '/test/xpath1', childFragments: [])
+ mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, '/test/xpath2') >> new FragmentEntity(xpath: '/test/xpath2', childFragments: [])
+ and: 'some data nodes with descendants'
+ def dataNode1 = new DataNode(xpath: '/test/xpath1', leaves: ['id': 'testId1'], childDataNodes: [new DataNode(xpath: '/test/xpath1/child', leaves: ['id': 'childTestId1'])])
+ def dataNode2 = new DataNode(xpath: '/test/xpath2', leaves: ['id': 'testId2'], childDataNodes: [new DataNode(xpath: '/test/xpath2/child', leaves: ['id': 'childTestId2'])])
+ when: 'the fragment entities are update by the data nodes'
+ objectUnderTest.updateDataNodesAndDescendants('dataspaceName', 'anchorName', [dataNode1, dataNode2])
+ then: 'call fragment repository save all method is called with the updated fragments'
+ 1 * mockFragmentRepository.saveAll({fragmentEntities -> {
+ fragmentEntities.containsAll([
+ new FragmentEntity(xpath: '/test/xpath1', attributes: '{"id":"testId1"}', childFragments: [new FragmentEntity(xpath: '/test/xpath1/child', attributes: '{"id":"childTestId1"}', childFragments: [])]),
+ new FragmentEntity(xpath: '/test/xpath2', attributes: '{"id":"testId2"}', childFragments: [new FragmentEntity(xpath: '/test/xpath2/child', attributes: '{"id":"childTestId2"}', childFragments: [])])
+ ])
+ assert fragmentEntities.size() == 2
+ }})
+ }
+
+ def mockDataNodeAndFragmentEntity(xpath, scenario) {
+ def dataNode = new DataNodeBuilder().withXpath(xpath).build()
+ def fragmentEntity = new FragmentEntity(xpath: xpath, childFragments: [])
+ mockFragmentRepository.getByDataspaceAndAnchorAndXpath(_, _, xpath) >> fragmentEntity
+ if ('EXCEPTION' == scenario) {
+ mockFragmentRepository.save(fragmentEntity) >> { throw new StaleStateException("concurrent updates") }
+ }
+ return dataNode
+ }
+
+ def mockFragmentWithJson(json) {
+ def anchorName = 'some anchor'
+ def mockAnchor = Mock(AnchorEntity)
+ mockAnchor.getId() >> 123
+ mockAnchor.getName() >> anchorName
+ mockAnchorRepository.getByDataspaceAndName(*_) >> mockAnchor
+ def mockFragmentExtract = Mock(FragmentExtract)
+ mockFragmentExtract.getId() >> 456
+ mockFragmentExtract.getAttributes() >> json
+ mockFragmentRepository.findByAnchorIdAndParentXpath(*_) >> [mockFragmentExtract]
+ }
+
+}