changeset 489:0f7e221f9099

ContentExtractorTransformer
author Devel 1
date Thu, 10 Aug 2017 11:58:39 +0200
parents 878dc1253eef
children df8db8c1a760
files stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java 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
diffstat 3 files changed, 104 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java	Thu Aug 10 10:16:49 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java	Thu Aug 10 11:58:39 2017 +0200
@@ -1,10 +1,19 @@
 package com.passus.st.client.http.extractor;
 
+import com.passus.net.http.HttpMessage;
+import com.passus.net.http.HttpMessageHelper;
+import java.io.IOException;
+
 /**
  *
  * @author mikolaj.podbielski
  */
 public interface ContentExtractor {
 
+    public default String extract(HttpMessage response) throws IOException {
+        String content = HttpMessageHelper.STRICT.contentToString(response, true);
+        return extract(content);
+    }
+
     public String extract(String content);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractorTransformer.java	Thu Aug 10 11:58:39 2017 +0200
@@ -0,0 +1,53 @@
+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 "$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.
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/ContentExtractorTransformerTest.java	Thu Aug 10 11:58:39 2017 +0200
@@ -0,0 +1,42 @@
+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);
+        String value = extractor.extract("key1: \" 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);
+        String value = extractor.extract("<root><node1> value1 </node1></root>");
+        assertEquals(" value1 ", value);
+    }
+
+}