changeset 869:6cba3257c006

reverseTransform
author Devel 1
date Thu, 25 Jan 2018 12:53:30 +0100
parents c8844cd52ad4
children 84c93f726188
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterExtractorTransformer.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterInjectorTransformer.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSequenceFilter.java
diffstat 5 files changed, 92 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilter.java	Thu Jan 25 08:54:52 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilter.java	Thu Jan 25 12:53:30 2018 +0100
@@ -2,8 +2,11 @@
 
 import com.passus.commons.Assert;
 import com.passus.commons.annotations.Plugin;
+import com.passus.config.CTupleNode;
+import com.passus.config.CValueNode;
 import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
+import com.passus.config.NodeType;
 import com.passus.config.annotations.NodeDefinitionCreate;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
@@ -12,6 +15,7 @@
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.config.schema.ValueNodeDefinition;
+import com.passus.config.validation.Errors;
 import com.passus.data.ByteString;
 import com.passus.net.http.HttpCookie;
 import com.passus.net.http.HttpMessage;
@@ -23,6 +27,7 @@
 import com.passus.st.client.http.HttpFlowContext;
 import com.passus.st.plugin.PluginConstants;
 import com.passus.st.validation.HeaderNameValidator;
+import static com.passus.st.validation.NodeValidationUtils.validateType;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedList;
@@ -333,4 +338,20 @@
         }
 
     }
+
+    static <T> T createExtractorInjector(CTupleNode tuple, Errors errors, Class<? extends T> clazz) {
+        if (validateType(tuple.getNode(), NodeType.VALUE, errors)) {
+            CValueNode valNode = (CValueNode) tuple.getNode();
+            try {
+                return clazz
+                        .getConstructor(CharSequence.class)
+                        .newInstance(valNode.getValue().toString());
+            } catch (Exception e) {
+                throw new RuntimeException(e.getMessage(), e);
+            }
+        }
+
+        return null;
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterExtractorTransformer.java	Thu Jan 25 08:54:52 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterExtractorTransformer.java	Thu Jan 25 12:53:30 2018 +0100
@@ -5,13 +5,13 @@
 import com.passus.config.CTupleNode;
 import com.passus.config.CValueNode;
 import com.passus.config.ConfigurationContext;
-import com.passus.config.NodeType;
 import com.passus.config.schema.NodeTransformer;
 import com.passus.config.validation.Errors;
+import com.passus.data.ByteString;
 import com.passus.st.client.http.filter.HttpCsrfFilter.CookieExtractor;
 import com.passus.st.client.http.filter.HttpCsrfFilter.Extractor;
 import com.passus.st.client.http.filter.HttpCsrfFilter.HeaderExtractor;
-import static com.passus.st.validation.NodeValidationUtils.validateType;
+import static com.passus.st.client.http.filter.HttpCsrfFilter.createExtractorInjector;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -22,21 +22,6 @@
  */
 class HttpCsrfFilterExtractorTransformer implements NodeTransformer<CValueNode> {
 
-    private Extractor createExtractor(CTupleNode tuple, Errors errors, Class<? extends Extractor> clazz) {
-        if (validateType(tuple.getNode(), NodeType.VALUE, errors)) {
-            CValueNode valNode = (CValueNode) tuple.getNode();
-            try {
-                return clazz
-                        .getConstructor(CharSequence.class)
-                        .newInstance(valNode.getValue().toString());
-            } catch (Exception e) {
-                throw new RuntimeException(e.getMessage(), e);
-            }
-        }
-
-        return null;
-    }
-
     @SuppressWarnings("unchecked")
     @Override
     public CValueNode transform(CNode node, Errors errors, ConfigurationContext context) {
@@ -57,10 +42,10 @@
                 Extractor extractor = null;
                 switch (opName.toLowerCase()) {
                     case "cookie":
-                        extractor = createExtractor(tuple, errors, CookieExtractor.class);
+                        extractor = createExtractorInjector(tuple, errors, CookieExtractor.class);
                         break;
                     case "header":
-                        extractor = createExtractor(tuple, errors, HeaderExtractor.class);
+                        extractor = createExtractorInjector(tuple, errors, HeaderExtractor.class);
                         break;
                     default:
                         throw new IllegalStateException("Not supported extractor '" + opName + "'.");
@@ -79,7 +64,36 @@
 
     @Override
     public CNode reverseTransform(CValueNode node, Errors errors, ConfigurationContext context) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        CValueNode root = (CValueNode) node;
+        List<Extractor> extractors = (List<Extractor>) root.getValue();
+        CMapNode mapNode = new CMapNode();
+
+        for (Extractor extractor : extractors) {
+            String opName;
+            ByteString value;
+            Class<? extends Extractor> extractorClass = extractor.getClass();
+
+            if (extractorClass == CookieExtractor.class) {
+                opName = "cookie";
+                value = ((CookieExtractor) extractor).getCookieName();
+            } else if (extractorClass == HeaderExtractor.class) {
+                opName = "header";
+                value = ((HeaderExtractor) extractor).getHeaderName();
+            } else {
+                throw new IllegalStateException("Not supported extractor type '" + extractorClass + "'.");
+            }
+
+            try {
+                errors.pushNestedPath(opName);
+                CValueNode valNode = new CValueNode(value.toString());
+                CTupleNode tuple = new CTupleNode(opName, valNode);
+                mapNode.add(tuple);
+            } finally {
+                errors.popNestedPath();
+            }
+        }
+
+        return mapNode;
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterInjectorTransformer.java	Thu Jan 25 08:54:52 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterInjectorTransformer.java	Thu Jan 25 12:53:30 2018 +0100
@@ -5,13 +5,13 @@
 import com.passus.config.CTupleNode;
 import com.passus.config.CValueNode;
 import com.passus.config.ConfigurationContext;
-import com.passus.config.NodeType;
 import com.passus.config.schema.NodeTransformer;
 import com.passus.config.validation.Errors;
+import com.passus.data.ByteString;
 import com.passus.st.client.http.filter.HttpCsrfFilter.CookieInjector;
 import com.passus.st.client.http.filter.HttpCsrfFilter.HeaderInjector;
 import com.passus.st.client.http.filter.HttpCsrfFilter.Injector;
-import static com.passus.st.validation.NodeValidationUtils.validateType;
+import static com.passus.st.client.http.filter.HttpCsrfFilter.createExtractorInjector;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -22,21 +22,7 @@
  */
 class HttpCsrfFilterInjectorTransformer implements NodeTransformer<CValueNode> {
 
-    private Injector createInjector(CTupleNode tuple, Errors errors, Class<? extends Injector> clazz) {
-        if (validateType(tuple.getNode(), NodeType.VALUE, errors)) {
-            CValueNode valNode = (CValueNode) tuple.getNode();
-            try {
-                return clazz
-                        .getConstructor(CharSequence.class)
-                        .newInstance(valNode.getValue().toString());
-            } catch (Exception e) {
-                throw new RuntimeException(e.getMessage(), e);
-            }
-        }
-
-        return null;
-    }
-
+    @SuppressWarnings("unchecked")
     @Override
     public CValueNode transform(CNode node, Errors errors, ConfigurationContext context) {
         CMapNode mapNode = (CMapNode) node;
@@ -56,10 +42,10 @@
                 Injector injector = null;
                 switch (opName.toLowerCase()) {
                     case "header":
-                        injector = createInjector(tuple, errors, HeaderInjector.class);
+                        injector = createExtractorInjector(tuple, errors, HeaderInjector.class);
                         break;
                     case "cookie":
-                        injector = createInjector(tuple, errors, CookieInjector.class);
+                        injector = createExtractorInjector(tuple, errors, CookieInjector.class);
                         break;
                     default:
                         throw new IllegalStateException("Not supported injector '" + opName + "'.");
@@ -78,7 +64,36 @@
 
     @Override
     public CNode reverseTransform(CValueNode node, Errors errors, ConfigurationContext context) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        CValueNode root = (CValueNode) node;
+        List<Injector> injectors = (List<Injector>) root.getValue();
+        CMapNode mapNode = new CMapNode();
+
+        for (Injector injector : injectors) {
+            String opName;
+            ByteString value;
+            Class<? extends Injector> extractorClass = injector.getClass();
+
+            if (extractorClass == CookieInjector.class) {
+                opName = "cookie";
+                value = ((CookieInjector) injector).getCookieName();
+            } else if (extractorClass == HeaderInjector.class) {
+                opName = "header";
+                value = ((HeaderInjector) injector).getHeaderName();
+            } else {
+                throw new IllegalStateException("Not supported extractor type '" + extractorClass + "'.");
+            }
+
+            try {
+                errors.pushNestedPath(opName);
+                CValueNode valNode = new CValueNode(value.toString());
+                CTupleNode tuple = new CTupleNode(opName, valNode);
+                mapNode.add(tuple);
+            } finally {
+                errors.popNestedPath();
+            }
+        }
+
+        return mapNode;
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Thu Jan 25 08:54:52 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Thu Jan 25 12:53:30 2018 +0100
@@ -263,7 +263,7 @@
 
     @Override
     public CNode reverseTransform(CNode node, Errors errors, ConfigurationContext context) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        throw new UnsupportedOperationException("Impossible."); // ValueExtractor
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSequenceFilter.java	Thu Jan 25 08:54:52 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSequenceFilter.java	Thu Jan 25 12:53:30 2018 +0100
@@ -486,7 +486,7 @@
 
         @Override
         public CNode reverseTransform(CNode node, Errors errors, ConfigurationContext context) {
-            throw new UnsupportedOperationException("Impossible.");
+            throw new UnsupportedOperationException("Impossible."); // ValueExtractor
         }
 
     }