changeset 871:56e55f320734

VarsGlobalDomainConfigurator
author Devel 1
date Thu, 25 Jan 2018 15:36:53 +0100
parents 84c93f726188
children f67a557c1afb
files stress-tester/src/main/java/com/passus/st/vars/VarsConfigurator.java stress-tester/src/main/java/com/passus/st/vars/VarsGlobalDomainConfigurator.java stress-tester/src/test/java/com/passus/st/config/TestJobConfiguratorTest.java
diffstat 3 files changed, 42 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/vars/VarsConfigurator.java	Thu Jan 25 12:58:42 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsConfigurator.java	Thu Jan 25 15:36:53 2018 +0100
@@ -5,7 +5,10 @@
 import com.passus.config.CNode;
 import com.passus.config.CTupleNode;
 import com.passus.config.Configuration;
-import com.passus.config.NodeConversionException;
+import com.passus.config.NodeType;
+import static com.passus.config.schema.validation.NodeValidationMessages.INVALID_NODE_TYPE_MSG;
+import com.passus.config.validation.Errors;
+import com.passus.config.validation.ObjectError;
 import com.passus.filter.ValueExtractor;
 import com.passus.filter.ValueExtractorParser;
 import com.passus.filter.config.ExpressionNodeTransformer;
@@ -26,25 +29,42 @@
         CMapNode root = (CMapNode) cfg.getRootNode();
         CNode globalVarsNode = root.get("globalVars").getNode();
 
+        Errors errors = new Errors();
+        Map<String, ValueExtractor> globalVars = getGlobalVars(globalVarsNode, errors);
+        if (errors.getErrorCount() != 0) {
+            ObjectError error = errors.getAllErrors().get(0);
+            throw new ConversionException(error.getMessage());
+        }
+        return globalVars;
+    }
+
+    static Map<String, ValueExtractor> getGlobalVars(CNode globalVarsNode, Errors errors) {
         Map<String, ValueExtractor> result = new HashMap<>();
         switch (globalVarsNode.getType()) {
             case MAP:
                 CMapNode globalVarsMapNode = (CMapNode) globalVarsNode;
-                readVars(result, globalVarsMapNode);
+                readVars(result, globalVarsMapNode, errors);
                 break;
             default:
-                throw new NodeConversionException("Map or list node required.", globalVarsNode);
+                errors.reject(globalVarsNode, INVALID_NODE_TYPE_MSG, NodeType.MAP, globalVarsNode.getType());
         }
         return result;
     }
 
-    private static void readVars(Map<String, ValueExtractor> result, CMapNode varNode) throws ConversionException {
+    private static void readVars(Map<String, ValueExtractor> result, CMapNode varNode, Errors errors) {
         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);
+            try {
+                errors.pushNestedPath(name);
+                CNode node = child.getNode();
+                ValueExtractor extractor = EXPR_TRANS.transform(node);
+                result.put(name, extractor);
+            } catch (ConversionException ex) {
+                errors.reject(child, "Invalid expression.");
+            } finally {
+                errors.popNestedPath();
+            }
         }
     }
 }
--- a/stress-tester/src/main/java/com/passus/st/vars/VarsGlobalDomainConfigurator.java	Thu Jan 25 12:58:42 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsGlobalDomainConfigurator.java	Thu Jan 25 15:36:53 2018 +0100
@@ -5,7 +5,9 @@
 import com.passus.config.ConfigurationContext;
 import com.passus.config.DomainConfigurator;
 import com.passus.config.validation.Errors;
+import com.passus.filter.ValueExtractor;
 import com.passus.st.plugin.PluginConstants;
+import java.util.Map;
 
 @Plugin(name = VarsGlobalDomainConfigurator.DOMAIN, category = PluginConstants.CATEGORY_DOMAIN_CONFIGURATOR)
 public class VarsGlobalDomainConfigurator implements DomainConfigurator {
@@ -19,11 +21,12 @@
 
     @Override
     public void validate(Configuration config, Errors errors, ConfigurationContext context) {
-
+        VarsConfigurator.getGlobalVars(config.getRootNode(), errors);
     }
 
     @Override
     public void configure(Configuration config, Errors errors, ConfigurationContext context) {
-
+        Map<String, ValueExtractor> vars = VarsConfigurator.getGlobalVars(config.getRootNode(), errors);
+        context.add(DOMAIN, vars);
     }
 }
--- a/stress-tester/src/test/java/com/passus/st/config/TestJobConfiguratorTest.java	Thu Jan 25 12:58:42 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/config/TestJobConfiguratorTest.java	Thu Jan 25 15:36:53 2018 +0100
@@ -6,16 +6,20 @@
 import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
+import com.passus.filter.UnmutableValueExtractor;
+import com.passus.filter.ValueExtractor;
 import com.passus.st.client.Client;
 import com.passus.st.client.http.HttpClient;
 import com.passus.st.source.EventSource;
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
 import com.passus.st.utils.ConfigurationContextConsts;
+import com.passus.st.vars.VarsGlobalDomainConfigurator;
 import org.testng.annotations.Test;
 
 import java.io.File;
 import java.util.List;
+import java.util.Map;
 
 import static org.testng.AssertJUnit.*;
 
@@ -41,5 +45,10 @@
         assertEquals(2, sources.size());
         assertTrue(sources.get(0) instanceof PcapSessionEventSource);
         assertTrue(sources.get(1) instanceof NcEventSource);
+
+        Map<String, ValueExtractor> vars = (Map<String, ValueExtractor>) context.get(VarsGlobalDomainConfigurator.DOMAIN);
+        assertEquals(1, vars.size());
+        UnmutableValueExtractor value = (UnmutableValueExtractor) vars.get("varName");
+        assertEquals("varValue", value.getValue());
     }
-}
\ No newline at end of file
+}