Data fragment update by xpath - parsing and validation
[cps.git] / cps-service / src / test / java / org / onap / cps / TestUtils.java
index 4ec4e4a..bf59e17 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation
+ *  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.
@@ -24,6 +25,7 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.util.Map;
+import org.onap.cps.spi.model.DataNode;
 
 /**
  * Common convenience methods for testing.
@@ -69,4 +71,24 @@ public class TestUtils {
         }
         return yangResourceNameToContentBuilder.build();
     }
+
+    /**
+     * Represents given data node object as flatten map by xpath.
+     * For easy finding child node within hierarchy.
+     *
+     * @param dataNode data node representing a root of tree structure
+     * @return the map containing all the data nodes from given structure where key is xpath, value is datanode object
+     */
+    public static Map<String, DataNode> getFlattenMapByXpath(final DataNode dataNode) {
+        final ImmutableMap.Builder<String, DataNode> dataNodeMapBuilder = ImmutableMap.builder();
+        buildFlattenMapByXpath(dataNode, dataNodeMapBuilder);
+        return dataNodeMapBuilder.build();
+    }
+
+    private static void buildFlattenMapByXpath(final DataNode dataNode,
+        final ImmutableMap.Builder<String, DataNode> dataNodeMapBuilder) {
+        dataNodeMapBuilder.put(dataNode.getXpath(), dataNode);
+        dataNode.getChildDataNodes()
+            .forEach(childDataNode -> buildFlattenMapByXpath(childDataNode, dataNodeMapBuilder));
+    }
 }