Mercurial > stress-tester
changeset 951:1d4771f798cc
YamlExtractor
author | Devel 4 |
---|---|
date | Wed, 15 May 2019 11:20:53 +0200 |
parents | a2be4422ceb5 |
children | f20cac0f2290 |
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, 132 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java Tue May 14 16:16:09 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/YamlExtractor.java Wed May 15 11:20:53 2019 +0200 @@ -4,93 +4,98 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.LinkedHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.yaml.snakeyaml.Yaml; /** - * @author mikolaj.podbielski + * @author norbert.rostkowski */ public class YamlExtractor implements ContentExtractor { -// private final List<Step> steps; + private final List<Step> steps = new ArrayList<>(); private final String path; - private final List<String> stringSteps; - private int finalIndex; + private static final Pattern PATH_PATTERN = Pattern.compile("\\.([a-zA-Z0-9_]+)|\\[([0-9]+)\\]"); 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. + convertPath(path); } - 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("[")); + private void convertPath(String path) { + Matcher m = PATH_PATTERN.matcher(path); + while (m.find()) { + String g1 = m.group(1); + String g2 = m.group(2); + if (g1 != null) { + steps.add(new YamlExtractor.MapStep(g1)); } - converted.add(subPath); - path = path.substring(path.indexOf('.') + 1); + if (g2 != null) { + int index = Integer.parseInt(g2); + steps.add(new YamlExtractor.ArrayStep(index)); + } } - 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);; + Object o = yaml.load(content.toString()); + + for (Step step : steps) { + o = step.step(o); + if (o == null) { + return null; } } - return result; + return o.toString(); } -// 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); -// } + + private static class ArrayStep implements Step { + + private final int index; + + public ArrayStep(int index) { + this.index = index; + } + + @Override + public Object step(Object in) { + if (in instanceof List) { + List l = (List) in; + if (index < l.size() && index >= 0) { + return l.get(index); + } + } + return null; + } + } + + private static class MapStep implements Step { + + private final String key; + + public MapStep(String key) { + this.key = key; + } + + @Override + public Object step(Object in) { + if (in instanceof Map) { + Map m = (Map) in; + return m.get(key); + } else { + return null; + } + } + + } + + private static interface Step { + + Object step(Object in); + } }
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java Tue May 14 16:16:09 2019 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/YamlExtractorTest.java Wed May 15 11:20:53 2019 +0200 @@ -11,17 +11,69 @@ @Test public void testExtract() throws Exception { - String path = "node1.node2[1]"; + /* RAW YAML: +scalar1: value1 +object1: + nested_list: + - a + - b + description: 'this is nested list' +list_of_scalars: + - x + - y + - z +list_of_objects: + - name: nested1 + a: 1 + b: 2 + - name: nested2 + a: 3 + b: 4 + - some_scalar + - + - aa + - bb + - cc + + */ String yaml = "" - + "node1:\n" - + " node2:\n" - + " - abc\n" - + " - def\n"; - YamlExtractor extractor = new YamlExtractor(path); - assertEquals("def", extractor.extract(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"; + + assertEquals("value1", new YamlExtractor("$.scalar1").extract(yaml)); + assertEquals("this is nested list", new YamlExtractor("$.object1.description").extract(yaml)); + assertEquals("b", new YamlExtractor("$.object1.nested_list[1]").extract(yaml)); + assertEquals("y", new YamlExtractor("$.list_of_scalars[1]").extract(yaml)); + assertEquals("nested2", new YamlExtractor("$.list_of_objects[1].name").extract(yaml)); + assertEquals("some_scalar", new YamlExtractor("$.list_of_objects[2]").extract(yaml)); + assertEquals("bb", new YamlExtractor("$.list_of_objects[3][1]").extract(yaml)); +// + assertNull(new YamlExtractor("$.no_such_node").extract(yaml)); + assertNull(new YamlExtractor("$.object1.no_such_node").extract(yaml)); + assertNull(new YamlExtractor("$.list_of_scalars[777]").extract(yaml)); + + assertNull(new YamlExtractor("$.list_of_objects[3].kaczka").extract(yaml)); + assertNull(new YamlExtractor("$.list_of_objects[1][0]").extract(yaml)); } - // dopisz testy na: - // nieistniejÄ…cy indeks - // nieistniejÄ…ce pole }