Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / vid / automation / test / services / DropTestApiField.java
index e175b88..bb50438 100644 (file)
@@ -1,64 +1,73 @@
-package vid.automation.test.services;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import vid.automation.test.infra.Features;
-
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.function.UnaryOperator;
-
-public class DropTestApiField {
-
-    public static UnaryOperator<String> dropTestApiFieldFromString() {
-        if (Features.FLAG_ADD_MSO_TESTAPI_FIELD.isActive()) {
-            // do nothing
-            return in -> in;
-        } else {
-            final ObjectMapper objectMapper = new ObjectMapper();
-            return in -> {
-                if (!in.contains("testApi")) {
-                    // short circuit
-                    return in;
-                }
-
-                try {
-                    final JsonNode tree = objectMapper.readTree(in);
-                    final JsonNode node = tree.path("simulatorRequest");
-                    if (removePath(node, "body", "requestDetails", "requestParameters", "testApi") != null) {
-                        // tree modified, write back to string
-                        return objectMapper.writeValueAsString(tree);
-                    } else {
-                        // else...
-                        return in;
-                    }
-                } catch (IOException e) {
-                    return in;
-                }
-            };
-        }
-    }
-
-    private static JsonNode removePath(JsonNode tree, String... nodes) {
-        // remove the nodes; remove also the parent, if an empty object was left
-        // returns the removed node
-        // returns null if no modification to tree
-        if (nodes.length > 1) {
-            final JsonNode node = tree.path(nodes[0]);
-            final JsonNode removed = removePath(node, Arrays.copyOfRange(nodes, 1, nodes.length));
-            if (removed != null && node.size() == 0) {
-                return removePath(tree, nodes[0]);
-            } else {
-                return removed; // non-null if node.size() != 0
-            }
-        } else {
-            if (tree instanceof ObjectNode) {
-                return ((ObjectNode) tree).remove(nodes[0]);
-            } else {
-                return null;
-            }
-        }
-    }
-
-}
+package vid.automation.test.services;\r
+\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.node.ObjectNode;\r
+import vid.automation.test.infra.Features;\r
+\r
+import java.io.IOException;\r
+import java.util.Arrays;\r
+import java.util.function.UnaryOperator;\r
+\r
+public class DropTestApiField {\r
+\r
+    public static UnaryOperator<String> dropTestApiFieldFromString() {\r
+        return dropFieldFromString("testApi", Features.FLAG_ADD_MSO_TESTAPI_FIELD,\r
+                "simulatorRequest", "body", "requestDetails", "requestParameters", "testApi");\r
+    }\r
+\r
+    public static UnaryOperator<String> dropFieldCloudOwnerFromString() {\r
+        return dropFieldFromString("cloudOwner", Features.FLAG_ADD_MSO_TESTAPI_FIELD,\r
+                "simulatorRequest", "body", "requestDetails", "cloudConfiguration", "cloudOwner");\r
+    }\r
+    private static UnaryOperator<String> dropFieldFromString(String text, Features featureFlag, String basePath, String... nodes){\r
+        if (featureFlag.isActive()) {\r
+            // do nothing\r
+            return in -> in;\r
+        } else {\r
+            final ObjectMapper objectMapper = new ObjectMapper();\r
+            return in -> {\r
+                if (!in.contains(text)) {\r
+                    // short circuit\r
+                    return in;\r
+                }\r
+\r
+                try {\r
+                    final JsonNode tree = objectMapper.readTree(in);\r
+                    final JsonNode node = tree.path(basePath);\r
+                    if (removePath(node, nodes) != null) {\r
+                        // tree modified, write back to string\r
+                        return objectMapper.writeValueAsString(tree);\r
+                    } else {\r
+                        // else...\r
+                        return in;\r
+                    }\r
+                } catch (IOException e) {\r
+                    return in;\r
+                }\r
+            };\r
+        }\r
+    }\r
+\r
+    private static JsonNode removePath(JsonNode tree, String... nodes) {\r
+        // remove the nodes; remove also the parent, if an empty object was left\r
+        // returns the removed node\r
+        // returns null if no modification to tree\r
+        if (nodes.length > 1) {\r
+            final JsonNode node = tree.path(nodes[0]);\r
+            final JsonNode removed = removePath(node, Arrays.copyOfRange(nodes, 1, nodes.length));\r
+            if (removed != null && node.size() == 0) {\r
+                return removePath(tree, nodes[0]);\r
+            } else {\r
+                return removed; // non-null if node.size() != 0\r
+            }\r
+        } else {\r
+            if (tree instanceof ObjectNode) {\r
+                return ((ObjectNode) tree).remove(nodes[0]);\r
+            } else {\r
+                return null;\r
+            }\r
+        }\r
+    }\r
+\r
+}\r