Merge "Fix getManualTasks double-addition of baseUrl"
[vid.git] / vid-automation / src / main / java / vid / automation / test / services / DropTestApiField.java
1 package vid.automation.test.services;\r
2 \r
3 import com.fasterxml.jackson.databind.JsonNode;\r
4 import com.fasterxml.jackson.databind.ObjectMapper;\r
5 import com.fasterxml.jackson.databind.node.ObjectNode;\r
6 import vid.automation.test.infra.Features;\r
7 \r
8 import java.io.IOException;\r
9 import java.util.Arrays;\r
10 import java.util.function.UnaryOperator;\r
11 \r
12 public class DropTestApiField {\r
13 \r
14     public static UnaryOperator<String> dropTestApiFieldFromString() {\r
15         return dropFieldFromString("testApi", Features.FLAG_ADD_MSO_TESTAPI_FIELD,\r
16                 "simulatorRequest", "body", "requestDetails", "requestParameters", "testApi");\r
17     }\r
18 \r
19     public static UnaryOperator<String> dropFieldCloudOwnerFromString() {\r
20         return dropFieldFromString("cloudOwner", Features.FLAG_ADD_MSO_TESTAPI_FIELD,\r
21                 "simulatorRequest", "body", "requestDetails", "cloudConfiguration", "cloudOwner");\r
22     }\r
23     private static UnaryOperator<String> dropFieldFromString(String text, Features featureFlag, String basePath, String... nodes){\r
24         if (featureFlag.isActive()) {\r
25             // do nothing\r
26             return in -> in;\r
27         } else {\r
28             final ObjectMapper objectMapper = new ObjectMapper();\r
29             return in -> {\r
30                 if (!in.contains(text)) {\r
31                     // short circuit\r
32                     return in;\r
33                 }\r
34 \r
35                 try {\r
36                     final JsonNode tree = objectMapper.readTree(in);\r
37                     final JsonNode node = tree.path(basePath);\r
38                     if (removePath(node, nodes) != null) {\r
39                         // tree modified, write back to string\r
40                         return objectMapper.writeValueAsString(tree);\r
41                     } else {\r
42                         // else...\r
43                         return in;\r
44                     }\r
45                 } catch (IOException e) {\r
46                     return in;\r
47                 }\r
48             };\r
49         }\r
50     }\r
51 \r
52     private static JsonNode removePath(JsonNode tree, String... nodes) {\r
53         // remove the nodes; remove also the parent, if an empty object was left\r
54         // returns the removed node\r
55         // returns null if no modification to tree\r
56         if (nodes.length > 1) {\r
57             final JsonNode node = tree.path(nodes[0]);\r
58             final JsonNode removed = removePath(node, Arrays.copyOfRange(nodes, 1, nodes.length));\r
59             if (removed != null && node.size() == 0) {\r
60                 return removePath(tree, nodes[0]);\r
61             } else {\r
62                 return removed; // non-null if node.size() != 0\r
63             }\r
64         } else {\r
65             if (tree instanceof ObjectNode) {\r
66                 return ((ObjectNode) tree).remove(nodes[0]);\r
67             } else {\r
68                 return null;\r
69             }\r
70         }\r
71     }\r
72 \r
73 }\r