aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java
blob: bb50438ca66bfa5c4060ba4a8862811b99aa310c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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() {
        return dropFieldFromString("testApi", Features.FLAG_ADD_MSO_TESTAPI_FIELD,
                "simulatorRequest", "body", "requestDetails", "requestParameters", "testApi");
    }

    public static UnaryOperator<String> dropFieldCloudOwnerFromString() {
        return dropFieldFromString("cloudOwner", Features.FLAG_ADD_MSO_TESTAPI_FIELD,
                "simulatorRequest", "body", "requestDetails", "cloudConfiguration", "cloudOwner");
    }
    private static UnaryOperator<String> dropFieldFromString(String text, Features featureFlag, String basePath, String... nodes){
        if (featureFlag.isActive()) {
            // do nothing
            return in -> in;
        } else {
            final ObjectMapper objectMapper = new ObjectMapper();
            return in -> {
                if (!in.contains(text)) {
                    // short circuit
                    return in;
                }

                try {
                    final JsonNode tree = objectMapper.readTree(in);
                    final JsonNode node = tree.path(basePath);
                    if (removePath(node, nodes) != 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;
            }
        }
    }

}