changeset 953:529b7a77f42e

YamlExtractor - replacer + tests
author Devel 4
date Wed, 15 May 2019 16:07:04 +0200
parents f20cac0f2290
children ebef2f426c73
files stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorUtils.java stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorUtilsTest.java stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java
diffstat 4 files changed, 122 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorUtils.java	Wed May 15 11:47:41 2019 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorUtils.java	Wed May 15 16:07:04 2019 +0200
@@ -28,6 +28,9 @@
             String expr = rule.substring(pos + 1);
 
             switch (extractorType.toLowerCase()) {
+                case "yaml":
+                    contentReplacer = new YamlExtractor(expr);
+                    break;
                 case "json":
                     contentReplacer = new JsonValueExtractor(expr);
                     break;
@@ -71,6 +74,9 @@
             String expr = rule.substring(pos + 1);
 
             switch (extractorType.toLowerCase()) {
+                case "yaml":
+                    contentExtractor = new YamlExtractor(expr);
+                    break;
                 case "json":
                     contentExtractor = new JsonValueExtractor(expr);
                     break;
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java	Wed May 15 11:47:41 2019 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java	Wed May 15 16:07:04 2019 +0200
@@ -2,6 +2,7 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
@@ -12,11 +13,12 @@
 /**
  * @author norbert.rostkowski
  */
-public class YamlExtractor implements ContentExtractor {
+public class YamlExtractor implements ContentExtractor, ContentReplacer {
+
+    private static final Pattern PATH_PATTERN = Pattern.compile("\\.([a-zA-Z0-9_]+)|\\[([0-9]+)\\]");
 
     private final List<Step> steps = new ArrayList<>();
     private final String path;
-    private static final Pattern PATH_PATTERN = Pattern.compile("\\.([a-zA-Z0-9_]+)|\\[([0-9]+)\\]");
 
     public YamlExtractor(String path) {
         this.path = path;
@@ -53,6 +55,21 @@
         return o.toString();
     }
 
+    @Override
+    public CharSequence replace(CharSequence content, CharSequence value) throws IOException {
+        Yaml yaml = new Yaml();
+        Object o = yaml.load(content.toString());
+        Object root = o;
+        if (value != null) {
+            int stepSize = steps.size();
+            for (int i = 0; i < stepSize - 1; i++) {
+                o = steps.get(i).step(o);
+            }
+            steps.get(stepSize - 1).replace(o, (String) value);
+        }
+        return yaml.dump(root);
+    }
+
 
     private static class ArrayStep implements Step {
 
@@ -72,6 +89,18 @@
             }
             return null;
         }
+
+        @Override
+        public void replace(Object in, String newValue) {
+            if (in instanceof List) {
+                List l = (List) in;
+                if (index < l.size() && index >= 0) {
+                    l.set(index, newValue);
+                }
+
+            }
+
+        }
     }
 
     private static class MapStep implements Step {
@@ -92,10 +121,24 @@
             }
         }
 
+        @Override
+        public void replace(Object in, String newValue) {
+            if (in instanceof Map) {
+                Map m = (Map) in;
+                if (m.containsKey(key)) {
+                    m.put(key, newValue);
+                }
+            }
+
+        }
+
     }
 
+
     private static interface Step {
 
         Object step(Object in);
+
+        void replace(Object in, String newValue);
     }
 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorUtilsTest.java	Wed May 15 11:47:41 2019 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorUtilsTest.java	Wed May 15 16:07:04 2019 +0200
@@ -2,13 +2,15 @@
 
 import static com.passus.st.client.http.extractor.ContentExtractorUtils.createExtractor;
 import static com.passus.st.client.http.extractor.ContentExtractorUtils.createReplacer;
+
 import java.io.IOException;
+
 import static org.testng.AssertJUnit.*;
+
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 /**
- *
  * @author mikolaj.podbielski
  */
 public class ContentExtractorUtilsTest {
@@ -17,10 +19,11 @@
     public Object[][] validExtractors() {
         String xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
         return new Object[][]{
-            {"regexp:aaa(.+)bbb", RegexValueExtractor.class, "aaaXbbbY", "aaaVALUEbbbY"},
-            {"json:$.xx.yy", JsonValueExtractor.class, "{\"xx\": {\"yy\": \"X\"}}", "{\"xx\":{\"yy\":\"VALUE\"}}"},
-            {"xml:/root/node1", XmlValueExtractor.class, "<root><node1>X</node1></root>", xmlPrefix + "<root><node1>VALUE</node1></root>"},
-            {"post:fld1", PostValueExtractor.class, "fld1=X&fld2=Y", "fld1=VALUE&fld2=Y"}
+                {"regexp:aaa(.+)bbb", RegexValueExtractor.class, "aaaXbbbY", "aaaVALUEbbbY"},
+                {"json:$.xx.yy", JsonValueExtractor.class, "{\"xx\": {\"yy\": \"X\"}}", "{\"xx\":{\"yy\":\"VALUE\"}}"},
+                {"xml:/root/node1", XmlValueExtractor.class, "<root><node1>X</node1></root>", xmlPrefix + "<root><node1>VALUE</node1></root>"},
+                {"post:fld1", PostValueExtractor.class, "fld1=X&fld2=Y", "fld1=VALUE&fld2=Y"},
+                {"yaml:$.scalar1", YamlExtractor.class, "scalar1: X", "{scalar1: VALUE}\n"}
         };
     }
 
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java	Wed May 15 11:47:41 2019 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java	Wed May 15 16:07:04 2019 +0200
@@ -2,16 +2,15 @@
 
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.Test;
+import org.yaml.snakeyaml.Yaml;
+
+import java.util.Map;
 
 /**
  *
- * @author mikolaj.podbielski
+ * @author norbert.rostkowski
  */
-public class YamlExtractorTest {
-
-    @Test
-    public void testExtract() throws Exception {
-        /* RAW YAML:
+public class YamlExtractorTest {   /* RAW YAML:
 scalar1: value1
 object1:
   nested_list:
@@ -36,29 +35,64 @@
     - cc
 
          */
-        String yaml = ""
-                + "scalar1: value1\n"
-                + "object1:\n"
-                + "  nested_list:\n"
-                + "    - a\n"
-                + "    - b\n"
-                + "  description: 'this is nested list'\n"
-                + "list_of_scalars:\n"
-                + "  - x\n"
-                + "  - y\n"
-                + "  - z\n"
-                + "list_of_objects:\n"
-                + "  - name: nested1\n"
-                + "    a: 1\n"
-                + "    b: 2\n"
-                + "  - name: nested2\n"
-                + "    a: 3\n"
-                + "    b: 4\n"
-                + "  - some_scalar\n"
-                + "  - \n"
-                + "    - aa\n"
-                + "    - bb\n"
-                + "    - cc\n";
+    String yaml = ""
+            + "scalar1: value1\n"
+            + "object1:\n"
+            + "  nested_list:\n"
+            + "    - a\n"
+            + "    - b\n"
+            + "  description: 'this is nested list'\n"
+            + "list_of_scalars:\n"
+            + "  - x\n"
+            + "  - y\n"
+            + "  - z\n"
+            + "list_of_objects:\n"
+            + "  - name: nested1\n"
+            + "    a: 1\n"
+            + "    b: 2\n"
+            + "  - name: nested2\n"
+            + "    a: 3\n"
+            + "    b: 4\n"
+            + "  - some_scalar\n"
+            + "  - \n"
+            + "    - aa\n"
+            + "    - bb\n"
+            + "    - cc\n";
+
+    @Test
+    public void replaceAndLoad() throws Exception {
+
+        testReplace("x", "$.object1.nested_list[1]");
+        testReplace("xx", "$.list_of_scalars[1]");
+        testReplace("val22", "$.scalar1");
+        testReplace("desc", "$.object1.description");
+        testReplace("name33", "$.list_of_objects[1].name");
+        testReplace("val44", "$.list_of_objects[2]");
+        testReplace("val44", "$.list_of_objects[3][1]");
+        testReplaceForNull("val44", "$.no_such_node");
+        testReplaceForNull("val44", "$.object1.no_such_node");
+        testReplaceForNull("val44", "$.no_such_node");
+        testReplaceForNull("val44", "$.list_of_scalars[777]");
+        testReplaceForNull("val44", "$.list_of_objects[3].kaczka");
+        testReplaceForNull("val44", "$.list_of_objects[1][0]");
+    }
+
+    private void testReplaceForNull(String newValue, String path) throws Exception {
+
+        YamlExtractor extractor = new YamlExtractor(path);
+        String replaced = extractor.replace(yaml, newValue).toString();
+        assertNull(new YamlExtractor(path).extract(yaml));
+    }
+
+    private void testReplace(String newValue, String path) throws Exception {
+        YamlExtractor extractor = new YamlExtractor(path);
+        String replaced = extractor.replace(yaml, newValue).toString();
+        assertEquals(newValue, extractor.extract(replaced).toString());
+    }
+
+    @Test
+    public void testExtract() throws Exception {
+
 
         assertEquals("value1", new YamlExtractor("$.scalar1").extract(yaml));
         assertEquals("this is nested list", new YamlExtractor("$.object1.description").extract(yaml));