diff options
author | Ittay Stern <ittay.stern@att.com> | 2019-03-11 09:34:34 +0200 |
---|---|---|
committer | Ittay Stern <ittay.stern@att.com> | 2019-03-17 17:02:43 +0200 |
commit | 66af8b9b391879be78660d6ccb0a1f1f9340b423 (patch) | |
tree | fc0d510f7ea28a437bcb1e3b950d1281ac88a4e4 /vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java | |
parent | 37ad0cc1d36ec6ff68ec39fcaaf2617eef7d08fe (diff) |
Merge automation from ECOMP's repository
Reference commit in ECOMP: 8e92a8c6
Issue-ID: VID-378
Change-Id: Ia32f4813378ef95097f788246aa5b1172e20ca48
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java')
-rw-r--r-- | vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java | 137 |
1 files changed, 73 insertions, 64 deletions
diff --git a/vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java b/vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java index e175b88b1..bb50438ca 100644 --- a/vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java +++ b/vid-automation/src/main/java/vid/automation/test/services/DropTestApiField.java @@ -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;
+
+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;
+ }
+ }
+ }
+
+}
|