Data fragment update by xpath #2 - persistence layer

Issue-ID: CPS-58
Change-Id: Ifc4580936d06c6907d6b5ab20657063b6707ccbe
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java
index 6776528..53ceaaa 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ *  Modifications Copyright (C) 2021 Pantheon.tech
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -83,7 +84,7 @@
     @JoinColumn(name = "anchor_id")
     private AnchorEntity anchor;
 
-    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
+    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
     @JoinColumn(name = "parent_id")
     private Set<FragmentEntity> childFragments;
 }
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
index d8f3df1..fd4e768 100644
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
@@ -29,6 +29,7 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 import org.onap.cps.spi.CpsDataPersistenceService;
 import org.onap.cps.spi.FetchDescendantsOption;
@@ -60,12 +61,10 @@
     @Override
     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
         final DataNode dataNode) {
-        final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
-        final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
-        final FragmentEntity parentFragment =
-            fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, parentXpath);
-        final FragmentEntity childFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNode);
-        parentFragment.getChildFragments().add(childFragment);
+        final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath);
+        final FragmentEntity fragmentEntity =
+            toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode);
+        parentFragment.getChildFragments().add(fragmentEntity);
         fragmentRepository.save(parentFragment);
     }
 
@@ -114,11 +113,15 @@
     @Override
     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
         final FetchDescendantsOption fetchDescendantsOption) {
+        final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
+        return toDataNode(fragmentEntity, fetchDescendantsOption);
+    }
+
+    private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
+        final String xpath) {
         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
-        final FragmentEntity fragmentEntity =
-            fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
-        return toDataNode(fragmentEntity, fetchDescendantsOption);
+        return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
     }
 
     private static DataNode toDataNode(final FragmentEntity fragmentEntity,
@@ -140,4 +143,32 @@
         }
         return Collections.emptyList();
     }
+
+    @Override
+    public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
+        final Map<String, Object> leaves) {
+        final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
+        fragmentEntity.setAttributes(GSON.toJson(leaves));
+        fragmentRepository.save(fragmentEntity);
+    }
+
+    @Override
+    public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) {
+        final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
+        removeExistingDescendants(fragmentEntity);
+
+        fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves()));
+        final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map(
+            childDataNode -> convertToFragmentWithAllDescendants(
+                fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode)
+        ).collect(Collectors.toUnmodifiableSet());
+        fragmentEntity.setChildFragments(childFragmentEntities);
+
+        fragmentRepository.save(fragmentEntity);
+    }
+
+    private void removeExistingDescendants(final FragmentEntity fragmentEntity) {
+        fragmentEntity.setChildFragments(Collections.emptySet());
+        fragmentRepository.save(fragmentEntity);
+    }
 }
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
index e3fa885..a6e4701 100644
--- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
+++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
@@ -19,8 +19,14 @@
  */
 package org.onap.cps.spi.impl
 
+import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
+import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
+
 import com.google.common.collect.ImmutableSet
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
 import org.onap.cps.spi.CpsDataPersistenceService
+import org.onap.cps.spi.entities.FragmentEntity
 import org.onap.cps.spi.exceptions.AnchorNotFoundException
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
@@ -31,28 +37,29 @@
 import org.springframework.test.context.jdbc.Sql
 import spock.lang.Unroll
 
-import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
-import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
-
 class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
 
     @Autowired
     CpsDataPersistenceService objectUnderTest
 
+    static final Gson GSON = new GsonBuilder().create()
+
     static final String SET_DATA = '/data/fragment.sql'
     static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
     static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
     static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
+    static final long UPDATE_DATA_NODE_FRAGMENT_ID = 4202L
+    static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L
 
     static final DataNode newDataNode = new DataNodeBuilder().build()
     static DataNode existingDataNode
     static DataNode existingChildDataNode
 
-    static Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
-            '/parent-100'                      : ["x": "y"],
-            '/parent-100/child-001'            : ["a": "b", "c": ["d", "e", "f"]],
-            '/parent-100/child-002'            : ["g": "h", "i": ["j", "k"]],
-            '/parent-100/child-002/grand-child': ["l": "m", "n": ["o", "p"]]
+    def expectedLeavesByXpathMap = [
+            '/parent-100'                      : ['parent-leaf': 'parent-leaf-value'],
+            '/parent-100/child-001'            : ['first-child-leaf': 'first-child-leaf-value'],
+            '/parent-100/child-002'            : ['second-child-leaf': 'second-child-leaf-value'],
+            '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf-value']
     ]
 
     static {
@@ -201,4 +208,98 @@
             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant' || AnchorNotFoundException
             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH'     || DataNodeNotFoundException
     }
+
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Update data node leaves.'() {
+        when: 'update is performed for leaves'
+            objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
+                    "/parent-200/child-201", ['leaf-value': 'new'])
+        then: 'leaves are updated for selected data node'
+            def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
+            def updatedLeaves = getLeavesMap(updatedFragment)
+            assert updatedLeaves.size() == 1
+            assert updatedLeaves.'leaf-value' == 'new'
+        and: 'existing child entry remains as is'
+            def childFragment = updatedFragment.getChildFragments().iterator().next()
+            def childLeaves = getLeavesMap(childFragment)
+            assert childFragment.getId() == UPDATE_DATA_NODE_SUB_FRAGMENT_ID
+            assert childLeaves.'leaf-value' == 'original'
+    }
+
+    @Unroll
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Update data leaves error scenario: #scenario.'() {
+        when: 'attempt to update data node for #scenario'
+            objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
+        then: 'a #expectedException is thrown'
+            thrown(expectedException)
+        where: 'the following data is used'
+            scenario                 | dataspaceName  | anchorName                        | xpath                || expectedException
+            'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant'       || DataspaceNotFoundException
+            'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant'       || AnchorNotFoundException
+            'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
+    }
+
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Replace data node tree with descendants removal.'() {
+        given: 'data node object with leaves updated, no children'
+            def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [])
+        when: 'replace data node tree is performed'
+            objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
+        then: 'leaves have been updated for selected data node'
+            def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
+            def updatedLeaves = getLeavesMap(updatedFragment)
+            assert updatedLeaves.size() == 1
+            assert updatedLeaves.'leaf-value' == 'new'
+        and: 'updated entry has no children'
+            updatedFragment.getChildFragments().isEmpty()
+        and: 'previously attached child entry is removed from database'
+            fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
+    }
+
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Replace data node tree with descendants.'() {
+        given: 'data node object with leaves updated, having child with old content'
+            def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
+                    buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'original'], [])
+            ])
+        when: 'update is performed including descendants'
+            objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
+        then: 'leaves have been updated for selected data node'
+            def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
+            def updatedLeaves = getLeavesMap(updatedFragment)
+            assert updatedLeaves.size() == 1
+            assert updatedLeaves.'leaf-value' == 'new'
+        and: 'previously attached child entry is removed from database'
+            fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
+        and: 'new child entry with same content is created'
+            def childFragment = updatedFragment.getChildFragments().iterator().next()
+            def childLeaves = getLeavesMap(childFragment)
+            assert childFragment.getId() != UPDATE_DATA_NODE_SUB_FRAGMENT_ID
+            assert childLeaves.'leaf-value' == 'original'
+    }
+
+    @Unroll
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Replace data node tree error scenario: #scenario.'() {
+        given: 'data node object'
+            def submittedDataNode = buildDataNode(xpath, ['leaf-name':'leaf-value'], [])
+        when: 'attempt to update data node for #scenario'
+            objectUnderTest.replaceDataNodeTree(dataspaceName, anchorName, submittedDataNode)
+        then: 'a #expectedException is thrown'
+            thrown(expectedException)
+        where: 'the following data is used'
+            scenario                 | dataspaceName  | anchorName                        | xpath                || expectedException
+            'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant'       || DataspaceNotFoundException
+            'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant'       || AnchorNotFoundException
+            'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
+    }
+
+    static DataNode buildDataNode(xpath, leaves, childDataNodes) {
+        return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
+    }
+
+    static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
+        return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class)
+    }
 }
diff --git a/cps-ri/src/test/resources/data/fragment.sql b/cps-ri/src/test/resources/data/fragment.sql
index e652703..9599146 100644
--- a/cps-ri/src/test/resources/data/fragment.sql
+++ b/cps-ri/src/test/resources/data/fragment.sql
@@ -17,7 +17,12 @@
     (4006, '/parent-1/child-1/grandchild-1', 3001, 4004, 1001);
 
 INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES
-    (4101, 1001, 3003, null, '/parent-100', '{"x": "y"}'),
-    (4102, 1001, 3003, 4101, '/parent-100/child-001', '{"a": "b", "c": ["d", "e", "f"]}'),
-    (4103, 1001, 3003, 4101, '/parent-100/child-002', '{"g": "h", "i": ["j", "k"]}'),
-    (4104, 1001, 3003, 4103, '/parent-100/child-002/grand-child', '{"l": "m", "n": ["o", "p"]}');
\ No newline at end of file
+    (4101, 1001, 3003, null, '/parent-100', '{"parent-leaf": "parent-leaf-value"}'),
+    (4102, 1001, 3003, 4101, '/parent-100/child-001', '{"first-child-leaf": "first-child-leaf-value"}'),
+    (4103, 1001, 3003, 4101, '/parent-100/child-002', '{"second-child-leaf": "second-child-leaf-value"}'),
+    (4104, 1001, 3003, 4103, '/parent-100/child-002/grand-child', '{"grand-child-leaf": "grand-child-leaf-value"}');
+
+INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES
+    (4201, 1001, 3003, null, '/parent-200', '{"leaf-value": "original"}'),
+    (4202, 1001, 3003, 4201, '/parent-200/child-201', '{"leaf-value": "original"}'),
+    (4203, 1001, 3003, 4202, '/parent-200/child-201/grand-child', '{"leaf-value": "original"}');
\ No newline at end of file