1 package vid.automation.test.services;
\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
8 import java.io.IOException;
\r
9 import java.util.Arrays;
\r
10 import java.util.function.UnaryOperator;
\r
12 public class DropTestApiField {
\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
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
23 private static UnaryOperator<String> dropFieldFromString(String text, Features featureFlag, String basePath, String... nodes){
\r
24 if (featureFlag.isActive()) {
\r
28 final ObjectMapper objectMapper = new ObjectMapper();
\r
30 if (!in.contains(text)) {
\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
45 } catch (IOException e) {
\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
62 return removed; // non-null if node.size() != 0
\r
65 if (tree instanceof ObjectNode) {
\r
66 return ((ObjectNode) tree).remove(nodes[0]);
\r