Mercurial > stress-tester
changeset 732:f1fb6cf53868
JsonValueExtractor, PostValueExtractor, RegexpValueExtractor, XmlValueExtractor
replace method added.
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java Mon Dec 04 13:26:28 2017 +0100 @@ -1,8 +1,11 @@ package com.passus.st.client.http.extractor; import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; import static com.jayway.jsonpath.Option.DEFAULT_PATH_LEAF_TO_NULL; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * @@ -10,6 +13,8 @@ */ public class JsonValueExtractor implements ContentExtractor { + private static final Logger LOGGER = LogManager.getLogger(JsonValueExtractor.class); + private final Configuration configuration = Configuration.builder() .options(DEFAULT_PATH_LEAF_TO_NULL) .build(); @@ -20,16 +25,51 @@ this.path = JsonPath.compile(path); } - @Override - public String extract(String json) { + private DocumentContext parse(String json) { try { - Object result = JsonPath + return JsonPath .using(configuration) - .parse(json) - .read(path); - return result == null ? null : result.toString(); + .parse(json); } catch (Exception ex) { return null; } } + + public String replace(String json, String value) { + try { + DocumentContext context = parse(json); + if (context == null) { + return null; + } + + context.set(path, value); + return context.jsonString(); + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + + return null; + } + + } + + @Override + public String extract(String json) { + try { + DocumentContext context = parse(json); + if (context == null) { + return null; + } + + Object result = context.read(path); + return result == null ? null : result.toString(); + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + + return null; + } + } }
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/PostValueExtractor.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/PostValueExtractor.java Mon Dec 04 13:26:28 2017 +0100 @@ -22,6 +22,16 @@ this.fieldName = ByteString.create(fieldName); } + public String replace(String content, String value) throws IOException { + HttpParameters params = helper.decodeFormUrlencoded(content); + if (params != null) { + params.set(fieldName, value); + return params.toString(); + } + + return null; + } + @Override public String extract(HttpMessage message) throws IOException { HttpParameters params = helper.decodeFormUrlencoded(message);
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/RegexValueExtractor.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/RegexValueExtractor.java Mon Dec 04 13:26:28 2017 +0100 @@ -11,10 +11,34 @@ private final Pattern pattern; + private final StringBuilder sb = new StringBuilder(); + public RegexValueExtractor(String expression) { pattern = Pattern.compile(expression); } + public String replace(String object, String value) { + if (object == null || object.isEmpty()) { + return object; + } + + Matcher matcher = pattern.matcher(object); + if (matcher.find()) { + if (matcher.groupCount() >= 1) { + int start = matcher.start(1); + int end = matcher.end(1); + if (start != -1 && end != -1) { + sb.setLength(0); + sb.append(object); + sb.replace(start, end, value); + return sb.toString(); + } + + } + } + return object; + } + @Override public String extract(String object) { String string = (String) object;
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java Mon Dec 04 13:26:28 2017 +0100 @@ -2,8 +2,14 @@ import com.passus.commons.Assert; import java.io.StringReader; +import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; @@ -25,7 +31,9 @@ private final XPathExpression xPathExpr; - private DocumentBuilder builder; + private DocumentBuilder docBuilder; + + private Transformer xmlTransformer; private boolean returnNullIfPathNotFound; @@ -44,28 +52,14 @@ this.returnNullIfPathNotFound = returnNullIfPathNotFound; } - @Override - public String extract(String content) { + private DocumentBuilder getDocBuilder() { try { - if (builder == null) { + if (docBuilder == null) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - builder = builderFactory.newDocumentBuilder(); - } - } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(e.getMessage(), e); + docBuilder = builderFactory.newDocumentBuilder(); } - return null; - } - - try (StringReader sr = new StringReader(content)) { - Document doc = builder.parse(new InputSource(sr)); - if (returnNullIfPathNotFound) { - Node node = (Node) xPathExpr.evaluate(doc, XPathConstants.NODE); - node.getTextContent(); - } - return xPathExpr.evaluate(doc); + return docBuilder; } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); @@ -74,4 +68,66 @@ return null; } } + + private Transformer getXmlTransformer() { + try { + if (xmlTransformer == null) { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + xmlTransformer = transformerFactory.newTransformer(); + } + + return xmlTransformer; + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + + return null; + } + + public String replace(String content, String value) { + DocumentBuilder builder = getDocBuilder(); + if (builder != null) { + try (StringReader sr = new StringReader(content)) { + Document doc = builder.parse(new InputSource(sr)); + Node node = (Node) xPathExpr.evaluate(doc, XPathConstants.NODE); + Transformer transformer = getXmlTransformer(); + if (node != null && transformer != null) { + node.setTextContent(value); + try (StringWriter writer = new StringWriter()) { + transformer.transform(new DOMSource(doc), new StreamResult(writer)); + return writer.getBuffer().toString(); + } + } + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + + return content; + } + + @Override + public String extract(String content) { + DocumentBuilder builder = getDocBuilder(); + if (builder != null) { + try (StringReader sr = new StringReader(content)) { + Document doc = builder.parse(new InputSource(sr)); + if (returnNullIfPathNotFound) { + Node node = (Node) xPathExpr.evaluate(doc, XPathConstants.NODE); + node.getTextContent(); + } + return xPathExpr.evaluate(doc); + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + + return null; + } }
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/JsonValueExtractorTest.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/JsonValueExtractorTest.java Mon Dec 04 13:26:28 2017 +0100 @@ -9,6 +9,14 @@ */ public class JsonValueExtractorTest { + private static String extract(String path, String content) { + return new JsonValueExtractor(path).extract(content); + } + + private static String replace(String path, String content, String value) { + return new JsonValueExtractor(path).replace(content, value); + } + @Test public void testExtract() { String json = "{\n" @@ -32,7 +40,7 @@ } @Test - public void testIvnalidJsonPath() { + public void testInvalidJsonPath() { try { JsonValueExtractor e = new JsonValueExtractor("not a path"); fail("should throw exception"); @@ -40,7 +48,11 @@ } } - static String extract(String path, String content) { - return new JsonValueExtractor(path).extract(content); + @Test + public void testReplace() { + String json = "{\"node1\": \"value1\"}"; + assertEquals("{\"node1\":\"newValue1\"}", replace("$.node1", json, "newValue1")); + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/PostValueExtractorTest.java Mon Dec 04 13:26:28 2017 +0100 @@ -0,0 +1,23 @@ +package com.passus.st.client.http.extractor; + +import java.io.IOException; +import static org.testng.AssertJUnit.*; +import org.testng.annotations.Test; + +/** + * + * @author Mirosław Hawrot + */ +public class PostValueExtractorTest { + + private static String replace(String fieldName, String content, String value) throws IOException { + return new PostValueExtractor(fieldName).replace(content, value); + } + + @Test + public void testReplace() throws Exception { + String content = "a=value1&b=value2"; + assertEquals("a=value1&b=newValue2", replace("b", content, "newValue2")); + } + +}
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/RegexValueExtractorTest.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/RegexValueExtractorTest.java Mon Dec 04 13:26:28 2017 +0100 @@ -9,6 +9,14 @@ */ public class RegexValueExtractorTest { + private static String extract(String regex, String content) { + return new RegexValueExtractor(regex).extract(content); + } + + private static String replace(String regex, String content, String value) { + return new RegexValueExtractor(regex).replace(content, value); + } + @Test public void testExtract() { String content = "<!DOCTYPE html>\n" @@ -44,7 +52,11 @@ } } - static String extract(String regex, String content) { - return new RegexValueExtractor(regex).extract(content); + @Test + public void testReplace() { + String content = "abc 123 def"; + assertEquals("abc newValue def", replace("(\\d+)", content, "newValue")); + assertEquals("abc 123 def", replace("\\d+", content, "newValue")); } + }
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java Mon Dec 04 12:50:58 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java Mon Dec 04 13:26:28 2017 +0100 @@ -18,6 +18,20 @@ + " <emptyNode></emptyNode>" + "</root>"; + private static String extract(String path, String content) throws Exception { + return new XmlValueExtractor(path).extract(content); + } + + private static String extractWithNull(String path, String content) throws Exception { + XmlValueExtractor e = new XmlValueExtractor(path); + e.setReturnNullIfPathNotFound(true); + return e.extract(content); + } + + private static String replace(String path, String content, String value) throws Exception { + return new XmlValueExtractor(path).replace(content, value); + } + @Test public void testExtract() throws Exception { assertEquals("value1", extract("/root/node1", xml)); @@ -55,13 +69,10 @@ } } - static String extract(String path, String content) throws Exception { - return new XmlValueExtractor(path).extract(content); - } - - static String extractWithNull(String path, String content) throws Exception { - XmlValueExtractor e = new XmlValueExtractor(path); - e.setReturnNullIfPathNotFound(true); - return e.extract(content); + @Test + public void testReplace() throws Exception { + String xml = "<?xml version=\"1.0\"?><root><node1>value1</node1></root>"; + assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root><node1>newValue1</node1></root>", replace("/root/node1", xml, "newValue1")); + assertEquals(xml, replace("/root/unknwonNode", xml, "newValue1")); } }