Mercurial > stress-tester
changeset 817:c5f42c314230
removed transformer used by deleted HttpResponseContentExtractorFilter
author | Devel 1 |
---|---|
date | Wed, 17 Jan 2018 12:14:07 +0100 |
parents | 2fb899e95c4b |
children | 8abe3a914cc6 |
files | stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorTransformer.java stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorTransformerTest.java stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorUtilsTest.java |
diffstat | 3 files changed, 43 insertions(+), 107 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorTransformer.java Tue Jan 16 16:46:20 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package com.passus.st.client.http.extractor; - -import com.passus.commons.ConversionException; -import com.passus.commons.Transformer; -import com.passus.config.CMapNode; -import com.passus.config.CNode; -import com.passus.config.CTupleNode; -import com.passus.config.CValueNode; -import com.passus.config.NodeConversionException; -import com.passus.config.NodeType; - -/** - * - * @author mikolaj.podbielski - */ -public class ContentExtractorTransformer implements Transformer<CNode, ContentExtractor> { - - public static final ContentExtractorTransformer DEFAULT = new ContentExtractorTransformer(); - - @Override - public ContentExtractor transform(CNode node) throws ConversionException { - if (node.getType() == NodeType.MAP) { - CMapNode mapNode = (CMapNode) node; - if (mapNode.size() != 1) { - throw new NodeConversionException("Invalid expression.", node); - } - - CTupleNode tupleNode = mapNode.getFirstChild(); - try { - CValueNode valNode = (CValueNode) tupleNode.getNode(); - String expression = valNode.getValue().toString(); - switch (tupleNode.getName()) { - case "$regex": - return new RegexValueExtractor(expression); - case "$jsonpath": - return new JsonValueExtractor(expression); - case "$xpath": - return new XmlValueExtractor(expression); - } - } catch (Exception ex) { - throw new NodeConversionException("Invalid expression.", node); - } - - throw new NodeConversionException("Invalid extraction operator: " + tupleNode.getName(), node); - } else { - throw new NodeConversionException("Map node required.", node); - } - } - - @Override - public CNode reverseTransform(ContentExtractor obj) throws ConversionException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - -}
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorTransformerTest.java Tue Jan 16 16:46:20 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package com.passus.st.client.http.extractor; - -import com.passus.config.CMapNode; -import com.passus.config.CTupleNode; -import com.passus.config.YamlConfigurationReader; -import static org.testng.AssertJUnit.*; -import org.testng.annotations.Test; - -/** - * - * @author mikolaj.podbielski - */ -public class ContentExtractorTransformerTest { - - private final ContentExtractorTransformer transformer = new ContentExtractorTransformer(); - - private static CTupleNode read(String content) throws Exception { - CMapNode mapNode = (CMapNode) YamlConfigurationReader.readNodeFromString(content); - return mapNode.getFirstChild(); - } - - @Test - public void testTransform_RegexValueExtractor() throws Exception { - CTupleNode node = read("field: {$regex: 'key1:[ ]*\"(.*?)\"'}"); - - ContentExtractor extractor = transformer.transform(node.getNode()); - assertTrue(extractor instanceof RegexValueExtractor); - CharSequence value = extractor.extract("key1: \" value1 \", \"\""); - assertEquals(" value1 ", value); - } - - @Test - public void testTransform_JsonValueExtractor() throws Exception { - CTupleNode node = read("field: {$jsonpath: '$.node3.node4'}"); - - ContentExtractor extractor = transformer.transform(node.getNode()); - assertTrue(extractor instanceof JsonValueExtractor); - CharSequence value = extractor.extract("{\"node3\": {\"node4\": \" value1 \"}}"); - assertEquals(" value1 ", value); - } - - @Test - public void testTransform_XmlValueExtractor() throws Exception { - CTupleNode node = read("field: {$xpath: '/root/node1'}"); - - ContentExtractor extractor = transformer.transform(node.getNode()); - assertTrue(extractor instanceof XmlValueExtractor); - CharSequence value = extractor.extract("<root><node1> value1 </node1></root>"); - assertEquals(" value1 ", value); - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorUtilsTest.java Wed Jan 17 12:14:07 2018 +0100 @@ -0,0 +1,43 @@ +package com.passus.st.client.http.extractor; + +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 { + + @DataProvider(name = "validExtractors") + 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"} + }; + } + + @Test(dataProvider = "validExtractors") + public void testCreateReplacer(String rule, Class cls, String content, String expected) throws IOException { + ContentReplacer r = createReplacer(rule); + assertTrue(cls.isInstance(r)); + CharSequence replaced = r.replace(content, "VALUE"); + assertEquals(expected, replaced); + } + + @Test(dataProvider = "validExtractors") + public void testCreateExtractor(String rule, Class cls, String content, String ignored) throws IOException { + ContentExtractor e = createExtractor(rule); + assertTrue(cls.isInstance(e)); + CharSequence extracted = e.extract(content); + assertEquals("X", extracted.toString()); //PostValueExtractor extracts ByteStringImpl + } + +}