changeset 883:9cc298ac7305

ST-115 in progress
author Devel 1
date Mon, 05 Feb 2018 17:04:15 +0100
parents d993a93b541b
children f78b47850aa8
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTransformer.java stress-tester/src/main/java/com/passus/st/filter/Transformers.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java
diffstat 7 files changed, 155 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Mon Feb 05 17:04:15 2018 +0100
@@ -16,21 +16,22 @@
 import com.passus.st.client.http.extractor.ContentReplacer;
 import com.passus.st.client.http.filter.HttpMessageModificationFilter.*;
 import com.passus.st.filter.Transformers;
+import com.passus.st.utils.ConfigurationContextConsts;
 
 import static com.passus.st.validation.NodeValidationUtils.validateType;
 
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Mirosław Hawrot
  */
 public class HttpMessageModificationFilterTransformer implements NodeTransformer {
 
-    private static final ExpressionNodeTransformer TRANSFORMER = Transformers.EXPRESSION;
-
-    private Operation createNameOperation(CTupleNode tuple, Errors errors, Class<? extends Operation> clazz) {
+    private static Operation createNameOperation(CTupleNode tuple,
+            Errors errors, Class<? extends Operation> clazz) {
         if (validateType(tuple.getNode(), NodeType.VALUE, errors)) {
             CValueNode valNode = (CValueNode) tuple.getNode();
             try {
@@ -45,7 +46,10 @@
         return null;
     }
 
-    private AbstractNameValueOperation createNameValueOperation(CTupleNode nodeTuple, Errors errors, Class<? extends AbstractNameValueOperation> clazz) {
+    private static AbstractNameValueOperation createNameValueOperation(CTupleNode nodeTuple,
+            Errors errors, Class<? extends AbstractNameValueOperation> clazz,
+            ExpressionNodeTransformer expressionNodeTransformer) {
+
         AbstractNameValueOperation op = null;
         if (validateType(nodeTuple.getNode(), NodeType.MAP, errors)) {
             List<CTupleNode> tuples = ((CMapNode) nodeTuple.getNode()).getChildren();
@@ -63,7 +67,7 @@
                         if (valNode.getType() == NodeType.VALUE
                                 || valNode.getType() == NodeType.MAP) {
                             try {
-                                valueTransformer = TRANSFORMER.transform(valNode);
+                                valueTransformer = expressionNodeTransformer.transform(valNode);
                                 field = name;
                             } catch (Exception e) {
                                 errors.reject("Invalid expression.", valNode);
@@ -95,7 +99,9 @@
         return op;
     }
 
-    private AddParamOperation createAddParamOperation(CTupleNode nodeTuple, Errors errors, Class<? extends AddParamOperation> clazz) {
+    private static AddParamOperation createAddParamOperation(CTupleNode nodeTuple,
+            Errors errors, Class<? extends AddParamOperation> clazz) {
+
         AddParamOperation op = null;
         if (validateType(nodeTuple.getNode(), NodeType.MAP, errors)) {
             List<CTupleNode> tuples = ((CMapNode) nodeTuple.getNode()).getChildren();
@@ -158,6 +164,9 @@
             operations = new ArrayList<>(tuples.size());
         }
 
+        Map<String, ValueExtractor> vars = (Map<String, ValueExtractor>) context.get(ConfigurationContextConsts.APP_VARS);
+        final ExpressionNodeTransformer transformer = Transformers.expressionNodeTransformer(vars);
+
         for (CTupleNode tuple : tuples) {
             String opName = tuple.getName();
             try {
@@ -168,19 +177,19 @@
                         op = createNameOperation(tuple, errors, RemoveHeaderOperation.class);
                         break;
                     case "addheader":
-                        op = createNameValueOperation(tuple, errors, AddHeaderOperation.class);
+                        op = createNameValueOperation(tuple, errors, AddHeaderOperation.class, transformer);
                         break;
                     case "setheader":
-                        op = createNameValueOperation(tuple, errors, SetHeaderOperation.class);
+                        op = createNameValueOperation(tuple, errors, SetHeaderOperation.class, transformer);
                         break;
                     case "removecookie":
                         op = createNameOperation(tuple, errors, RemoveCookieOperation.class);
                         break;
                     case "addcookie":
-                        op = createNameValueOperation(tuple, errors, AddCookieOperation.class);
+                        op = createNameValueOperation(tuple, errors, AddCookieOperation.class, transformer);
                         break;
                     case "setcookie":
-                        op = createNameValueOperation(tuple, errors, SetCookieOperation.class);
+                        op = createNameValueOperation(tuple, errors, SetCookieOperation.class, transformer);
                         break;
                     case "removepostparam":
                         op = createNameOperation(tuple, errors, PostDataRemoveParamOperation.class);
@@ -195,10 +204,10 @@
                         op = createNameOperation(tuple, errors, RemoveQueryParameterOperation.class);
                         break;
                     case "addqueryparam":
-                        op = createNameValueOperation(tuple, errors, AddQueryParameterOperation.class);
+                        op = createNameValueOperation(tuple, errors, AddQueryParameterOperation.class, transformer);
                         break;
                     case "setqueryparam":
-                        op = createNameValueOperation(tuple, errors, SetQueryParameterOperation.class);
+                        op = createNameValueOperation(tuple, errors, SetQueryParameterOperation.class, transformer);
                         break;
                     case "setcontent":
                         if (validateType(tuple.getNode(), NodeType.MAP, errors)) {
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTransformer.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTransformer.java	Mon Feb 05 17:04:15 2018 +0100
@@ -19,10 +19,12 @@
 import com.passus.st.client.http.filter.HttpScopeModificationFilter.SetGlobalParamOperation;
 import com.passus.st.client.http.filter.HttpScopeModificationFilter.SetSessionParamOperation;
 import com.passus.st.filter.Transformers;
+import com.passus.st.utils.ConfigurationContextConsts;
 import static com.passus.st.validation.NodeValidationUtils.validateType;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 /**
  *
@@ -30,9 +32,10 @@
  */
 public class HttpScopeModificationFilterTransformer implements NodeTransformer<CValueNode> {
 
-    private static final ExpressionNodeTransformer TRANSFORMER = Transformers.EXPRESSION;
+    private static SetSessionParamOperation createSetSessionParamOperation(
+            CTupleNode tuple, Errors errors, boolean checkValueExists,
+            ExpressionNodeTransformer expressionNodeTransformer) {
 
-    private SetSessionParamOperation createSetSessionParamOperation(CTupleNode tuple, Errors errors, boolean checkValueExists) {
         if (validateType(tuple.getNode(), NodeType.MAP, errors)) {
             CMapNode mapNode = (CMapNode) tuple.getNode();
 
@@ -49,7 +52,7 @@
                         default:
                             try {
                                 paramName = mapTupleNode.getName();
-                                valueExtractor = TRANSFORMER.transform(mapTupleNode.getNode());
+                                valueExtractor = expressionNodeTransformer.transform(mapTupleNode.getNode());
                             } catch (Exception e) {
                                 errors.reject(mapTupleNode.getNode(), "Invalid expression.");
                             }
@@ -66,7 +69,10 @@
         return null;
     }
 
-    private SetGlobalParamOperation createSetGlobalParamOperation(CTupleNode tuple, Errors errors, boolean checkValueExists) {
+    private static SetGlobalParamOperation createSetGlobalParamOperation(
+            CTupleNode tuple, Errors errors, boolean checkValueExists,
+            ExpressionNodeTransformer expressionNodeTransformer) {
+
         if (validateType(tuple.getNode(), NodeType.MAP, errors)) {
             CMapNode mapNode = (CMapNode) tuple.getNode();
 
@@ -79,7 +85,7 @@
                         default:
                             try {
                                 paramName = mapTupleNode.getName();
-                                valueExtractor = TRANSFORMER.transform(mapTupleNode.getNode());
+                                valueExtractor = expressionNodeTransformer.transform(mapTupleNode.getNode());
                             } catch (Exception e) {
                                 errors.reject(mapTupleNode.getNode(), "Invalid expression.");
                             }
@@ -108,6 +114,9 @@
             operations = new ArrayList<>();
         }
 
+        Map<String, ValueExtractor> vars = (Map<String, ValueExtractor>) context.get(ConfigurationContextConsts.APP_VARS);
+        final ExpressionNodeTransformer transformer = Transformers.expressionNodeTransformer(vars);
+
         for (CTupleNode tuple : tuples) {
             String opName = tuple.getName();
             try {
@@ -125,10 +134,10 @@
                         break;
                     }
                     case "addsessionparam":
-                        op = createSetSessionParamOperation(tuple, errors, true);
+                        op = createSetSessionParamOperation(tuple, errors, true, transformer);
                         break;
                     case "setsessionparam":
-                        op = createSetSessionParamOperation(tuple, errors, false);
+                        op = createSetSessionParamOperation(tuple, errors, false, transformer);
                         break;
                     case "removeglobalparam": {
                         String paramName = ConfigurationUtils.extractString(tuple, errors);
@@ -138,10 +147,10 @@
                         break;
                     }
                     case "addglobalparam":
-                        op = createSetGlobalParamOperation(tuple, errors, true);
+                        op = createSetGlobalParamOperation(tuple, errors, true, transformer);
                         break;
                     case "setglobalparam":
-                        op = createSetGlobalParamOperation(tuple, errors, false);
+                        op = createSetGlobalParamOperation(tuple, errors, false, transformer);
                         break;
 
                     default:
--- a/stress-tester/src/main/java/com/passus/st/filter/Transformers.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/filter/Transformers.java	Mon Feb 05 17:04:15 2018 +0100
@@ -1,9 +1,12 @@
 package com.passus.st.filter;
 
+import com.passus.filter.ValueExtractor;
 import com.passus.filter.ValueExtractorParser;
 import com.passus.filter.config.ExpressionNodeTransformer;
 import com.passus.filter.config.PredicateNodeTransformer;
 import com.passus.filter.config.ValueExtractorResolver;
+import com.passus.st.vars.VarsExtractorResolver;
+import java.util.Map;
 
 /**
  *
@@ -16,4 +19,14 @@
     public static final PredicateNodeTransformer PREDICATE = new PredicateNodeTransformer(VALUE_EXTRACTOR_RESOLVER);
 
     public static final ExpressionNodeTransformer EXPRESSION = new ExpressionNodeTransformer(new ValueExtractorParser(VALUE_EXTRACTOR_RESOLVER));
+
+    public static ExpressionNodeTransformer expressionNodeTransformer(Map<String, ValueExtractor> vars) {
+        if (vars != null && !vars.isEmpty()) {
+            VarsExtractorResolver varsResolver = new VarsExtractorResolver(vars);
+            ValueExtractorParser parser = new ValueExtractorParser(varsResolver);
+            return new ExpressionNodeTransformer(parser);
+        } else {
+            return Transformers.EXPRESSION;
+        }
+    }
 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Mon Feb 05 17:04:15 2018 +0100
@@ -1,6 +1,10 @@
 package com.passus.st.client.http.filter;
 
+import static com.passus.commons.collection.FluentBuilder.e;
+import static com.passus.commons.collection.FluentBuilder.map;
+import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.validation.Errors;
+import com.passus.filter.ValueExtractor;
 import com.passus.net.MimeTypes;
 import com.passus.net.http.HttpHeaders;
 import com.passus.net.http.HttpMessageHelper;
@@ -10,21 +14,13 @@
 import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
 import com.passus.st.client.http.HttpFlowContext;
 import com.passus.st.client.http.extractor.ContentExtractorUtils;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.AddCookieOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.AddHeaderOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.AddQueryParameterOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.ContentOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataAddParamOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataRemoveParamOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataSetParamOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.RemoveCookieOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.RemoveHeaderOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.RemoveQueryParameterOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetCookieOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetHeaderOperation;
-import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetQueryParameterOperation;
+import com.passus.st.client.http.filter.HttpMessageModificationFilter.*;
+import static com.passus.st.client.http.filter.HttpVarsFilterTest.expr;
+import static com.passus.st.client.http.filter.HttpVarsFilterTest.val;
+import com.passus.st.utils.ConfigurationContextConsts;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
@@ -178,7 +174,8 @@
                 + "            Header7: \"@httpSession.get('testParam')\"\n";
 
         Errors errors = new Errors();
-        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, null);
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, emptyContext);
         assertEquals(0, errors.getErrorCount());
         assertEquals(1, filters.size());
         assertTrue(filters.get(0) instanceof HttpMessageModificationFilter);
@@ -207,6 +204,42 @@
 
     }
 
+    @Test
+    public void testConfigureAndFilterOutbound_appVars() throws Exception {
+        Map<String, ValueExtractor> appVars = map(
+                e("var0", expr("req.url.host")),
+                e("var1", val("value1"))
+        );
+
+        String filterConfig = "httpFilters:\n"
+                + "    - type: modifyMessage\n"
+                + "      operations:\n"
+                + "        setHeader: \n"
+                + "            Header1: '@req.url.path'\n"
+                + "        setHeader: \n"
+                + "            Header2: '@appVars.var0'\n"
+                + "        setHeader: \n"
+                + "            Header3: '@appVars.var1'\n";
+
+        Errors errors = new Errors();
+        ConfigurationContextImpl context = new ConfigurationContextImpl();
+        context.add(ConfigurationContextConsts.APP_VARS, appVars);
+        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, context);
+        assertEquals(0, errors.getErrorCount());
+        assertEquals(1, filters.size());
+        assertTrue(filters.get(0) instanceof HttpMessageModificationFilter);
+
+        HttpRequest req = HttpRequestBuilder.get("http://example.com/path?q=1").build();
+
+        HttpMessageModificationFilter filter = (HttpMessageModificationFilter) filters.get(0);
+        filter.filterOutbound(req, null, null);
+
+        HttpHeaders headers = req.getHeaders();
+        assertEquals("/path", headers.get("Header1").toString());
+        assertEquals("example.com", headers.get("Header2").toString());
+        assertEquals("value1", headers.get("Header3").toString());
+    }
+
     @Test(enabled = true)
     public void testComplexExpression() throws Exception {
         String filterConfig = "httpFilters:\n"
@@ -222,7 +255,8 @@
                 .build();
 
         Errors errors = new Errors();
-        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, null);
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, emptyContext);
 
         HttpMessageModificationFilter filter = (HttpMessageModificationFilter) filters.get(0);
         HttpFlowContext mockContext = HttpFilterTestUtils.createMockContext();
@@ -247,7 +281,8 @@
                 + "          regexp:aaa(.+)bbb: aaaa\n";
 
         Errors errors = new Errors();
-        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, null);
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, emptyContext);
         HttpFilterTestUtils.printErrors(errors);
 
         assertEquals(0, errors.getErrorCount());
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Mon Feb 05 17:04:15 2018 +0100
@@ -1,13 +1,22 @@
 package com.passus.st.client.http.filter;
 
+import static com.passus.commons.collection.FluentBuilder.e;
+import static com.passus.commons.collection.FluentBuilder.map;
 import com.passus.commons.utils.ReflectionUtils;
 import com.passus.config.CMapNode;
 import com.passus.config.CTupleNode;
 import com.passus.config.CValueNode;
+import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
+import com.passus.filter.ValueExtractor;
 import com.passus.st.client.http.filter.HttpMessageModificationFilter.*;
+import static com.passus.st.client.http.filter.HttpVarsFilterTest.expr;
+import static com.passus.st.client.http.filter.HttpVarsFilterTest.val;
+import com.passus.st.filter.HttpMessageWrapperDynamicExtractor;
+import com.passus.st.utils.ConfigurationContextConsts;
 import java.util.List;
+import java.util.Map;
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.Test;
 
@@ -58,7 +67,8 @@
         CTupleNode node = read(config);
 
         Errors errors = new Errors();
-        CValueNode value = transformer.transform(node.getNode(), errors, null);
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        CValueNode value = transformer.transform(node.getNode(), errors, emptyContext);
         HttpFilterTestUtils.printErrors(errors);
         List<Operation> operations = (List) value.getValue();
 
@@ -111,7 +121,8 @@
         CTupleNode node = read(config);
 
         Errors errors = new Errors();
-        CValueNode value = transformer.transform(node.getNode(), errors, null);
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        CValueNode value = transformer.transform(node.getNode(), errors, emptyContext);
         HttpFilterTestUtils.printErrors(errors);
         List<Operation> operations = (List) value.getValue();
 
@@ -149,6 +160,35 @@
         assertFalse(getReplacerEscape(operations.get(7)));
     }
 
+    @Test
+    public void testTransform_appVars() throws Exception {
+        Map<String, ValueExtractor> appVars = map(
+                e("var0", expr("req.url.host")),
+                e("var1", val("value1"))
+        );
+
+        String config = "operations:\n"
+                + "    setHeader: \n"
+                + "        Header1: '@req.url.path'\n"
+                + "    setHeader: \n"
+                + "        Header2: '@appVars.var0'\n"
+                + "    setHeader: \n"
+                + "        Header3: '@appVars.var1'\n";
+
+        CTupleNode node = read(config);
+
+        Errors errors = new Errors();
+        ConfigurationContextImpl context = new ConfigurationContextImpl();
+        context.add(ConfigurationContextConsts.APP_VARS, appVars);
+        CValueNode value = transformer.transform(node.getNode(), errors, context);
+        HttpFilterTestUtils.printErrors(errors);
+
+        List<Operation> operations = (List) value.getValue();
+        assertEquals("req.url.path", getFieldName(operations.get(0)));
+        assertEquals("req.url.host", getFieldName(operations.get(1)));
+        assertEquals("value1", getExtractorValue2(operations.get(2)));
+    }
+
     private static boolean getReplacerEscape(Operation o) {
         return ReflectionUtils.getFieldByPath(o, "replacer.escape");
     }
@@ -164,4 +204,9 @@
     private static Object getExtractorValue2(Operation o) {
         return ReflectionUtils.getInheritedFieldByPath(o, "valueExtractor.value");
     }
+
+    private static String getFieldName(Operation o) {
+        SetHeaderOperation shop = (SetHeaderOperation) o;
+        return ReflectionUtils.getInheritedField(shop.getValueExtractor(), "fieldName");
+    }
 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTest.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTest.java	Mon Feb 05 17:04:15 2018 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.client.http.filter;
 
+import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.validation.Errors;
 import com.passus.filter.UnmutableValueExtractor;
 import com.passus.net.MimeTypes;
@@ -194,7 +195,8 @@
 
     private HttpScopeModificationFilter processConfig(String cfg) throws Exception {
         Errors errors = new Errors();
-        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(cfg, errors, null);
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(cfg, errors, emptyContext);
         errors.getAllErrors().forEach(System.out::println);
 
         assertEquals(0, errors.getErrorCount());
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Mon Feb 05 12:57:12 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Mon Feb 05 17:04:15 2018 +0100
@@ -37,11 +37,11 @@
             e("type", val("x"))
     );
 
-    private static UnmutableValueExtractor val(Object o) {
+    public static UnmutableValueExtractor val(Object o) {
         return new UnmutableValueExtractor(o);
     }
 
-    private static ValueExtractor expr(String expr) {
+    public static ValueExtractor expr(String expr) {
         return Transformers.VALUE_EXTRACTOR_RESOLVER.resolveValueExtractor(expr);
     }