changeset 744:50e2a0229e7b

Vars
author Devel 1
date Thu, 07 Dec 2017 10:31:35 +0100
parents b405a798b5b1
children 6c06ad27997a
files stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java stress-tester/src/main/java/com/passus/st/vars/VarsConfigurator.java stress-tester/src/main/java/com/passus/st/vars/VarsValueExtractorResolver.java stress-tester/src/test/java/com/passus/st/vars/VarsConfiguratorTest.java
diffstat 4 files changed, 91 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java	Thu Dec 07 10:12:10 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java	Thu Dec 07 10:31:35 2017 +0100
@@ -4,9 +4,6 @@
 import com.jayway.jsonpath.DocumentContext;
 import com.jayway.jsonpath.JsonPath;
 import static com.jayway.jsonpath.Option.DEFAULT_PATH_LEAF_TO_NULL;
-import com.passus.net.http.HttpMessage;
-import com.passus.net.http.HttpMessageHelper;
-import java.io.IOException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsConfigurator.java	Thu Dec 07 10:31:35 2017 +0100
@@ -0,0 +1,50 @@
+package com.passus.st.vars;
+
+import com.passus.commons.ConversionException;
+import com.passus.config.CMapNode;
+import com.passus.config.CNode;
+import com.passus.config.CTupleNode;
+import com.passus.config.Configuration;
+import com.passus.config.NodeConversionException;
+import com.passus.filter.ValueExtractor;
+import com.passus.filter.ValueExtractorParser;
+import com.passus.filter.config.ExpressionNodeTransformer;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class VarsConfigurator {
+
+    private static final ValueExtractorParser PARSER = ValueExtractorParser.DEFAULT;
+    private static final ExpressionNodeTransformer EXPR_TRANS = new ExpressionNodeTransformer(PARSER);
+
+    public static Map<String, ValueExtractor> getGlobalVars(Configuration cfg) throws ConversionException {
+        CMapNode root = (CMapNode) cfg.getRootNode();
+        CNode globalVarsNode = root.get("globalVars").getNode();
+
+        Map<String, ValueExtractor> result = new HashMap<>();
+        switch (globalVarsNode.getType()) {
+            case MAP:
+                CMapNode globalVarsMapNode = (CMapNode) globalVarsNode;
+                readVars(result, globalVarsMapNode);
+                break;
+            default:
+                throw new NodeConversionException("Map or list node required.", globalVarsNode);
+        }
+        return result;
+    }
+
+    private static void readVars(Map<String, ValueExtractor> result, CMapNode varNode) throws ConversionException {
+        List<CTupleNode> children = varNode.getChildren();
+        for (CTupleNode child : children) {
+            String name = child.getName();
+            CNode node = child.getNode();
+            ValueExtractor extractor = EXPR_TRANS.transform(node);
+            result.put(name, extractor);
+        }
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/vars/VarsValueExtractorResolver.java	Thu Dec 07 10:12:10 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsValueExtractorResolver.java	Thu Dec 07 10:31:35 2017 +0100
@@ -1,6 +1,5 @@
 package com.passus.st.vars;
 
-import com.passus.filter.CompoundValueExtractor;
 import com.passus.filter.ValueExtractor;
 import com.passus.filter.config.DefaultValueExtractorResolver;
 import java.util.Map;
@@ -21,11 +20,10 @@
 
     @Override
     public ValueExtractor resolveValueExtractor(String fieldName) {
-        ValueExtractor defaultExtractor = extractorFactory.resolveValueExtractor(fieldName);
         if (globalVars.containsKey(fieldName)) {
-            return CompoundValueExtractor.create(globalVars.get(fieldName), defaultExtractor);
+            return globalVars.get(fieldName);
         } else {
-            return defaultExtractor;
+            return extractorFactory.resolveValueExtractor(fieldName);
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/vars/VarsConfiguratorTest.java	Thu Dec 07 10:31:35 2017 +0100
@@ -0,0 +1,39 @@
+package com.passus.st.vars;
+
+import com.passus.config.Configuration;
+import com.passus.config.YamlConfigurationReader;
+import com.passus.filter.MvelValueExtractor;
+import com.passus.filter.UnmutableValueExtractor;
+import com.passus.filter.ValueExtractor;
+import static com.passus.st.vars.VarsConfigurator.getGlobalVars;
+import java.util.Map;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class VarsConfiguratorTest {
+
+    @Test
+    public void testGetGlobalVars_map() throws Exception {
+        String varsFile = "globalVars:\n"
+                + "    var1: \"value1\"\n"
+                + "    var2: {$expr: \"return 'value2'\"\n}";
+
+        Configuration cfg = YamlConfigurationReader.readFromString(varsFile);
+        Map<String, ValueExtractor> vars = getGlobalVars(cfg);
+
+        assertEquals(2, vars.size());
+
+        ValueExtractor uve = vars.get("var1");
+        assertTrue(uve instanceof UnmutableValueExtractor);
+        assertEquals("value1", uve.extract(null));
+
+        ValueExtractor mve = vars.get("var2");
+        assertTrue(mve instanceof MvelValueExtractor);
+        assertEquals("value2", mve.extract(null));
+    }
+
+}