changeset 850:ecc2247b5254

TestJobConfigurator + ProjectConfigurator
author Devel 2
date Tue, 23 Jan 2018 13:53:16 +0100
parents 022eb9aac397
children ba2ddf2d553c
files stress-tester/src/main/java/com/passus/st/config/ProjectConfigurator.java stress-tester/src/main/java/com/passus/st/config/TestJobConfigurator.java stress-tester/src/main/java/com/passus/st/project/Project.java stress-tester/src/main/java/com/passus/st/project/ProjectConfigurator.java stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java
diffstat 6 files changed, 107 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/config/ProjectConfigurator.java	Tue Jan 23 13:52:29 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,104 +0,0 @@
-package com.passus.st.config;
-
-import com.passus.config.CMapNode;
-import com.passus.config.CNode;
-import com.passus.config.CTupleNode;
-import com.passus.config.Configuration;
-import com.passus.config.ConfigurationContext;
-import static com.passus.config.ConfigurationUtils.validateType;
-import com.passus.config.Configurator;
-import com.passus.config.DomainConfigurator;
-import com.passus.config.NodeType;
-import com.passus.config.validation.Errors;
-import java.util.List;
-import java.util.function.BiConsumer;
-import java.util.function.Consumer;
-
-/**
- *
- * @author Mirosław Hawrot
- */
-public class ProjectConfigurator implements Configurator {
-
-    private final DomainConfiguratorFactory configuratorFactory = DomainConfiguratorFactory.getInstance();
-
-    private void processRootNode(Configuration config, Errors errors, BiConsumer<CTupleNode, Errors> operation) {
-
-    }
-
-    @Override
-    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
-        CNode rootNode = config.getRootNode();
-        if (!validateType(rootNode, NodeType.MAP, errors)) {
-            return;
-        }
-
-        CMapNode mapNode = (CMapNode) rootNode;
-        List<CTupleNode> tuples = mapNode.getChildren();
-
-        String name = null;
-        boolean active = true;
-        for (CTupleNode tuple : tuples) {
-            String fieldName = tuple.getName();
-
-            switch (fieldName.toLowerCase()) {
-                case "name":
-                    break;
-                case "active":
-                    break;
-                default:
-                    if (!configuratorFactory.containsName(fieldName)) {
-                        errors.reject(tuple, "Invalid property '%s'.", fieldName);
-                        return;
-                    }
-
-                    DomainConfigurator configurator = configuratorFactory.getInstanceByName(name);
-
-                    try {
-                        errors.pushNestedPath(fieldName);
-                        configurator.validate(config, errors, context);
-                        if( errors.hasError() ) {
-                            break;
-                        }
-                        
-                    } finally {
-                        errors.popNestedPath();
-                    }
-
-                    break;
-            }
-        }
-    }
-
-    @Override
-    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
-
-    }
-
-    private class ValidationConsumer implements BiConsumer<CTupleNode, Errors> {
-
-        @Override
-        public void accept(CTupleNode tuple, Errors errors) {
-            String fieldName = tuple.getName();
-
-            switch (fieldName.toLowerCase()) {
-                case "name":
-                    break;
-                case "active":
-                    break;
-                default:
-
-            }
-        }
-
-    }
-
-    private class ConfiguratorConsumer implements BiConsumer<CTupleNode, Errors> {
-
-        @Override
-        public void accept(CTupleNode tuple, Errors errors) {
-
-        }
-
-    }
-}
--- a/stress-tester/src/main/java/com/passus/st/config/TestJobConfigurator.java	Tue Jan 23 13:52:29 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/config/TestJobConfigurator.java	Tue Jan 23 13:53:16 2018 +0100
@@ -2,26 +2,21 @@
 
 import com.passus.commons.Assert;
 import com.passus.commons.plugin.PluginFactory;
-import com.passus.config.CMapNode;
-import com.passus.config.CNode;
-import com.passus.config.CTupleNode;
-import com.passus.config.Configuration;
-import com.passus.config.ConfigurationContext;
-import static com.passus.config.ConfigurationUtils.validateType;
-import com.passus.config.Configurator;
-import com.passus.config.DomainConfigurator;
-import com.passus.config.NodeType;
+import com.passus.config.*;
 import com.passus.config.validation.Errors;
 import com.passus.st.job.TestJob;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
+import java.util.function.BiPredicate;
+
+import static com.passus.config.ConfigurationUtils.validateType;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class TestJobConfigurator implements Configurator {
@@ -59,11 +54,13 @@
         this.configuratorFactory = configuratorFactory;
     }
 
-    private Map<String, DomainConfigurator> getDomainsConfigurator(Configuration config, Errors errors) {
-        return getDomainsConfigurator(config.getRootNode(), errors);
+    protected Map<String, DomainConfigurator> getDomainsConfigurator(Configuration config, Errors errors,
+                                                                     BiPredicate<String, DomainConfigurator> filter) {
+        return getDomainsConfigurator(config.getRootNode(), errors, filter);
     }
 
-    private Map<String, DomainConfigurator> getDomainsConfigurator(CNode rootNode, Errors errors) {
+    protected Map<String, DomainConfigurator> getDomainsConfigurator(CNode rootNode, Errors errors,
+                                                                     BiPredicate<String, DomainConfigurator> filter) {
         if (!validateType(rootNode, NodeType.MAP, errors)) {
             return null;
         }
@@ -73,6 +70,10 @@
         List<CTupleNode> tuples = mapNode.getChildren();
         for (CTupleNode tuple : tuples) {
             String domain = tuple.getName();
+            if (filter != null && filter.test(domain, null)) {
+                continue;
+            }
+
             DomainConfigurator configurator = null;
 
             try {
@@ -81,7 +82,7 @@
             }
 
             if (configurator == null) {
-                errors.reject(tuple, "Unknwon property '%s'.", domain);
+                errors.reject(tuple, "Unknown property '%s'.", domain);
                 break;
             }
 
@@ -91,12 +92,17 @@
         return domainsConfigurator;
     }
 
-    @Override
-    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
-        Map<String, DomainConfigurator> configurators = getDomainsConfigurator(config, errors);
+    protected void doValidate(Configuration config, Errors errors, ConfigurationContext context,
+                              BiPredicate<String, DomainConfigurator> filter) {
+        Map<String, DomainConfigurator> configurators = getDomainsConfigurator(config, errors, filter);
         if (!errors.hasError()) {
             for (Entry<String, DomainConfigurator> entry : configurators.entrySet()) {
-                Configuration subConfig = config.subConfiguration(entry.getKey());
+                String keyName = entry.getKey();
+                if (filter != null && filter.test(keyName, entry.getValue())) {
+                    continue;
+                }
+
+                Configuration subConfig = config.subConfiguration(keyName);
                 DomainConfigurator configurator = entry.getValue();
                 configurator.validate(subConfig, errors, context);
                 if (errors.hasError()) {
@@ -107,11 +113,21 @@
     }
 
     @Override
-    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
-        Map<String, DomainConfigurator> configurators = getDomainsConfigurator(config, errors);
+    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
+        doValidate(config, errors, context, null);
+    }
+
+    protected void doConfigure(Configuration config, Errors errors, ConfigurationContext context,
+                               BiPredicate<String, DomainConfigurator> filter) {
+        Map<String, DomainConfigurator> configurators = getDomainsConfigurator(config, errors, filter);
         if (!errors.hasError()) {
             for (Entry<String, DomainConfigurator> entry : configurators.entrySet()) {
-                Configuration subConfig = config.subConfiguration(entry.getKey());
+                String keyName = entry.getKey();
+                if (filter != null && filter.test(keyName, entry.getValue())) {
+                    continue;
+                }
+
+                Configuration subConfig = config.subConfiguration(keyName);
                 DomainConfigurator configurator = entry.getValue();
                 configurator.configure(subConfig, errors, context);
                 if (errors.hasError()) {
@@ -121,4 +137,9 @@
         }
     }
 
+    @Override
+    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
+        doConfigure(config, errors, context, null);
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/project/Project.java	Tue Jan 23 13:52:29 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/project/Project.java	Tue Jan 23 13:53:16 2018 +0100
@@ -3,7 +3,6 @@
 import com.passus.config.Configuration;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class Project {
@@ -14,6 +13,8 @@
 
     private boolean active;
 
+    private String description;
+
     private Configuration config;
 
     public String getId() {
@@ -40,6 +41,14 @@
         this.active = active;
     }
 
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
     public Configuration getConfig() {
         return config;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/project/ProjectConfigurator.java	Tue Jan 23 13:53:16 2018 +0100
@@ -0,0 +1,49 @@
+package com.passus.st.project;
+
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.DomainConfigurator;
+import com.passus.config.validation.Errors;
+import com.passus.st.config.TestJobConfigurator;
+import com.passus.st.utils.ConfigurationContextConsts;
+
+import java.util.function.BiPredicate;
+
+import static com.passus.config.schema.validation.NodeValidationMessages.TUPLE_NOT_DEFINED_MSG;
+
+public class ProjectConfigurator extends TestJobConfigurator {
+
+    private final BiPredicate<String, DomainConfigurator> projectKeysPredicate = (keyName, configurator) -> {
+        return keyName != null &&
+                keyName.equals("name") ||
+                keyName.equals("active") ||
+                keyName.equals("description");
+    };
+
+    @Override
+    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
+        if (config.getString("name") == null) {
+            errors.reject(TUPLE_NOT_DEFINED_MSG, "name");
+            return;
+        }
+
+        super.doValidate(config, errors, context, projectKeysPredicate);
+    }
+
+    @Override
+    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
+        super.doConfigure(config, errors, context, projectKeysPredicate);
+        if (!errors.hasError()) {
+            String name = config.getString("name");
+            boolean active = config.getBoolean("active", true);
+            String description = config.getString("description", "");
+
+            Project project = new Project();
+            project.setName(name);
+            project.setActive(active);
+            project.setDescription(description);
+            project.setConfig(config);
+            context.add(ConfigurationContextConsts.PROJECT, project);
+        }
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Tue Jan 23 13:52:29 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Tue Jan 23 13:53:16 2018 +0100
@@ -37,6 +37,8 @@
 
     public static final String REPORTER_DEFAULT_DESTINATION =  "reporter.default_destination";
 
+    public static final String PROJECT = "project";
+
     private ConfigurationContextConsts() {
     }
 
--- a/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java	Tue Jan 23 13:52:29 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java	Tue Jan 23 13:53:16 2018 +0100
@@ -24,7 +24,8 @@
 
     @Test
     public void testConfigure_InvalidFilter() throws Exception {
-        String configStr = "clients:\n"
+        String configStr =
+                "clients:\n"
                 + "    - type: http\n"
                 + "      filters:\n"
                 + "        - type: unknownFilter\n";