1 package vid.automation.test.services;
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.node.ObjectNode;
6 import vid.automation.test.infra.Features;
8 import java.io.IOException;
9 import java.util.Arrays;
10 import java.util.function.UnaryOperator;
12 public class DropTestApiField {
14 public static UnaryOperator<String> dropTestApiFieldFromString() {
15 if (Features.FLAG_ADD_MSO_TESTAPI_FIELD.isActive()) {
19 final ObjectMapper objectMapper = new ObjectMapper();
21 if (!in.contains("testApi")) {
27 final JsonNode tree = objectMapper.readTree(in);
28 final JsonNode node = tree.path("simulatorRequest");
29 if (removePath(node, "body", "requestDetails", "requestParameters", "testApi") != null) {
30 // tree modified, write back to string
31 return objectMapper.writeValueAsString(tree);
36 } catch (IOException e) {
43 private static JsonNode removePath(JsonNode tree, String... nodes) {
44 // remove the nodes; remove also the parent, if an empty object was left
45 // returns the removed node
46 // returns null if no modification to tree
47 if (nodes.length > 1) {
48 final JsonNode node = tree.path(nodes[0]);
49 final JsonNode removed = removePath(node, Arrays.copyOfRange(nodes, 1, nodes.length));
50 if (removed != null && node.size() == 0) {
51 return removePath(tree, nodes[0]);
53 return removed; // non-null if node.size() != 0
56 if (tree instanceof ObjectNode) {
57 return ((ObjectNode) tree).remove(nodes[0]);