Mercurial > stress-tester
changeset 508:91b6bab38277
JsonValueExtractor
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorTransformer.java Thu Aug 17 14:56:18 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorTransformer.java Thu Aug 17 15:00:42 2017 +0200 @@ -32,6 +32,8 @@ switch (tupleNode.getName()) { case "$regex": return new RegexValueExtractor(expression); + case "$jsonpath": + return new JsonValueExtractor(expression); case "$xpath": return new XmlValueExtractor(expression); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java Thu Aug 17 15:00:42 2017 +0200 @@ -0,0 +1,35 @@ +package com.passus.st.client.http.extractor; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; +import static com.jayway.jsonpath.Option.DEFAULT_PATH_LEAF_TO_NULL; + +/** + * + * @author mikolaj.podbielski + */ +public class JsonValueExtractor implements ContentExtractor { + + private final Configuration configuration = Configuration.builder().options(DEFAULT_PATH_LEAF_TO_NULL).build(); + + private final JsonPath path; + + public JsonValueExtractor(String path) { + this.path = JsonPath.compile(path); + } + + @Override + public String extract(String json) { + try { + Object result = JsonPath + .using(configuration) + .parse(json) + .read(path); +// System.out.println("path='" + path + "' result='" + result + "' class=" + (result == null ? null : result.getClass())); + return result == null ? null : result.toString(); + } catch (Exception ex) { +// System.out.println("path='" + path + "' exception=" + ex); + return null; + } + } +}
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpResponseContentExtractorFilter.java Thu Aug 17 14:56:18 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpResponseContentExtractorFilter.java Thu Aug 17 15:00:42 2017 +0200 @@ -177,6 +177,7 @@ // TODO: jak pozwolić na dokładnie jedno z poniższych MapNodeDefinition mapDef = mapDef( tupleDef("$regex", valueDef()).setRequired(false), + tupleDef("$jsonpath", valueDef()).setRequired(false), tupleDef("$xpath", valueDef()).setRequired(false) );
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorTransformerTest.java Thu Aug 17 14:56:18 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorTransformerTest.java Thu Aug 17 15:00:42 2017 +0200 @@ -30,6 +30,16 @@ } @Test + public void testTransform_JsonValueExtractor() throws Exception { + CTupleNode node = read("field: {$jsonpath: '$.node3.node4'}"); + + ContentExtractor extractor = transformer.transform(node.getNode()); + assertTrue(extractor instanceof JsonValueExtractor); + String value = extractor.extract("{\"node3\": {\"node4\": \" value1 \"}}"); + assertEquals(" value1 ", value); + } + + @Test public void testTransform_XmlValueExtractor() throws Exception { CTupleNode node = read("field: {$xpath: '/root/node1'}");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/JsonValueExtractorTest.java Thu Aug 17 15:00:42 2017 +0200 @@ -0,0 +1,46 @@ +package com.passus.st.client.http.extractor; + +import static org.testng.AssertJUnit.*; +import org.testng.annotations.Test; + +/** + * + * @author mikolaj.podbielski + */ +public class JsonValueExtractorTest { + + @Test + public void testExtract() { + String json = "{\n" + + " \"node1\": \"value1\",\n" + + " \"node2\": \"value2\",\n" + + " \"node2\": \"value3\",\n" // this is invalid JSON containing duplicate key + + " \"node3\": {\"node4\": \"value4\"},\n" + + " \"emptyString\": \"\",\n" + + " \"emptyObject\": {},\n" + + " \"emptyArray\": []\n" + + "}"; + + assertEquals("value1", extract("$.node1", json)); + assertEquals("value3", extract("$.node2", json)); // duplicate key + assertEquals("value4", extract("$.node3.node4", json)); + assertEquals("", extract("$.emptyString", json)); + assertEquals("{}", extract("$.emptyObject", json)); + assertEquals("[]", extract("$.emptyArray", json)); + assertEquals(null, extract("$.noSuchPath", json)); + assertEquals(null, extract("$.node", "NOT A JSON")); + } + + @Test + public void testIvnalidJsonPath() { + try { + JsonValueExtractor e = new JsonValueExtractor("not a path"); + fail("should throw exception"); + } catch (Exception ignore) { + } + } + + static String extract(String path, String content) { + return new JsonValueExtractor(path).extract(content); + } +}
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpResponseContentExtractorFilterTest.java Thu Aug 17 14:56:18 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpResponseContentExtractorFilterTest.java Thu Aug 17 15:00:42 2017 +0200 @@ -7,6 +7,7 @@ import com.passus.st.client.http.HttpFlowContext; import com.passus.st.client.http.HttpScopes; import com.passus.st.client.http.extractor.ContentExtractor; +import com.passus.st.client.http.extractor.JsonValueExtractor; import com.passus.st.client.http.extractor.RegexValueExtractor; import com.passus.st.client.http.extractor.XmlValueExtractor; import static com.passus.st.client.http.filter.HttpFilterTestUtils.createMockContext; @@ -79,7 +80,9 @@ + " \"@autocreate\": true\n" + " SecureToken: {$regex: '\"SECURE_TOKEN\"[ ]*:[ ]*\"(.*?)\"'}\n" + " $addGlobalParam:\n" - + " SomeVariable: {$xpath: 'root.abc'}"; + + " SomeVariable: {$xpath: 'root.abc'}\n" + + " $addGlobalParam:\n" + + " SomeVariable2: {$jsonpath: '$.root.abc'}\n"; Errors errors = new Errors(); List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors); @@ -90,24 +93,30 @@ assertTrue(filters.get(0) instanceof HttpResponseContentExtractorFilter); HttpResponseContentExtractorFilter filter = (HttpResponseContentExtractorFilter) filters.get(0); - + assertNotNull(filter.getPredicate()); - + List<Operation> operations = filter.getOperations(); - assertEquals(2, operations.size()); + assertEquals(3, operations.size()); assertTrue(operations.get(0) instanceof SetSessionParamOperation); assertTrue(operations.get(1) instanceof SetGlobalParamOperation); + assertTrue(operations.get(2) instanceof SetGlobalParamOperation); SetSessionParamOperation op0 = (SetSessionParamOperation) operations.get(0); assertEquals(true, op0.autocreate); assertEquals(false, op0.checkValueExists); assertEquals("SecureToken", op0.paramName); assertTrue(op0.contentExtractor instanceof RegexValueExtractor); - + SetGlobalParamOperation op1 = (SetGlobalParamOperation) operations.get(1); assertEquals(true, op1.checkValueExists); assertEquals("SomeVariable", op1.paramName); assertTrue(op1.contentExtractor instanceof XmlValueExtractor); + + SetGlobalParamOperation op2 = (SetGlobalParamOperation) operations.get(2); + assertEquals(true, op2.checkValueExists); + assertEquals("SomeVariable2", op2.paramName); + assertTrue(op2.contentExtractor instanceof JsonValueExtractor); } private static class ConstContentExtractor implements ContentExtractor {