changeset 950:a2be4422ceb5

first simple test works
author Devel 4
date Tue, 14 May 2019 16:16:09 +0200
parents 48f0ecbbf6d8
children 1d4771f798cc
files stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java
diffstat 2 files changed, 123 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java	Tue May 14 16:16:09 2019 +0200
@@ -0,0 +1,96 @@
+package com.passus.st.client.http.extractor;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.LinkedHashMap;
+
+import org.yaml.snakeyaml.Yaml;
+
+/**
+ * @author mikolaj.podbielski
+ */
+public class YamlExtractor implements ContentExtractor {
+
+//    private final List<Step> steps;
+    private final String path;
+    private final List<String> stringSteps;
+    private int finalIndex;
+
+    public YamlExtractor(String path) {
+//        steps = new ArrayList<>();
+        this.path = path;
+        stringSteps = convertPath(path);
+//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+    }
+
+    private List<String> convertPath(String path) {
+        finalIndex = Integer.parseInt(path.substring(path.lastIndexOf("[") + 1, path.lastIndexOf("]")));
+        long count = path.chars().filter(ch -> ch == '.').count();
+        List<String> converted = new ArrayList<>();
+        String subPath = "";
+        for (int i = 0; i <= count; i++) {
+            if (i != count) {
+                subPath = path.substring(0, path.indexOf('.'));
+            } else {
+                subPath = path.substring(0, path.indexOf("["));
+            }
+            converted.add(subPath);
+            path = path.substring(path.indexOf('.') + 1);
+        }
+        return converted;
+    }
+
+    @Override
+    public CharSequence extract(CharSequence content) throws IOException {
+        Yaml yaml = new Yaml();
+        Map<String, Object> root = (Map<String, Object>) yaml.load(content.toString());
+        CharSequence result = null;
+        Object o = root;
+        for (int i = 0; i < stringSteps.size(); i++) {
+            if (i < stringSteps.size() - 1) {
+                o = ((Map) o).get(stringSteps.get(i));
+            } else {
+                ArrayList<String> finalList = (ArrayList<String>) ((Map) o).get(stringSteps.get(i));
+                result = (CharSequence) finalList.get(finalIndex);;
+            }
+        }
+        return result;
+    }
+
+//    private static class ArrayStep implements Step {
+//
+//        private final int index;
+//
+//        public ArrayStep(int index) {
+//            this.index = index;
+//        }
+//
+//        @Override
+//        public Object step(Object in) {
+//            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+//        }
+//
+//    }
+//
+//    private static class MapStep implements Step {
+//
+//        private final String key;
+//
+//        public MapStep(String key) {
+//            this.key = key;
+//        }
+//
+//        @Override
+//        public Object step(Object in) {
+//            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+//        }
+//
+//    }
+//
+//    private static interface Step {
+//
+//        Object step(Object in);
+//    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java	Tue May 14 16:16:09 2019 +0200
@@ -0,0 +1,27 @@
+package com.passus.st.client.http.extractor;
+
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class YamlExtractorTest {
+
+    @Test
+    public void testExtract() throws Exception {
+        String path = "node1.node2[1]";
+        String yaml = ""
+                + "node1:\n"
+                + "    node2:\n"
+                + "       - abc\n"
+                + "       - def\n";
+        YamlExtractor extractor = new YamlExtractor(path);
+        assertEquals("def", extractor.extract(yaml));
+    }
+
+    // dopisz testy na:
+    // nieistniejÄ…cy indeks
+    // nieistniejÄ…ce pole
+}