changeset 819:53537abab89b

Global configuration in progress
author Devel 2
date Thu, 18 Jan 2018 10:07:11 +0100
parents 8abe3a914cc6
children 3b8b53f6ec89
files stress-tester/src/main/java/com/passus/st/ConverterHttpClient.java stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterAware.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterChain.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersNodeDefinitionCreator.java stress-tester/src/main/java/com/passus/st/config/ProjectConfigurator.java stress-tester/src/test/java/com/passus/st/client/http/HttpSynchClientWorkerTest.java stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java
diffstat 8 files changed, 119 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/ConverterHttpClient.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/ConverterHttpClient.java	Thu Jan 18 10:07:11 2018 +0100
@@ -15,6 +15,7 @@
 import com.passus.st.source.EventDestination;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -110,6 +111,11 @@
     }
 
     @Override
+    public List<HttpFilter> getFilters() {
+        return filterChain.getFilters();
+    }
+
+    @Override
     public void addFilter(HttpFilter filter) {
         Assert.notNull(filter, "filter");
         filterChain.addFilter(filter);
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java	Thu Jan 18 10:07:11 2018 +0100
@@ -23,7 +23,9 @@
 import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDefInteger;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDefBool;
+import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
 import com.passus.config.validation.LongValidator;
+import com.passus.st.client.http.filter.HttpFilterFactory;
 import com.passus.st.client.http.filter.HttpFiltersNodeDefinitionCreator;
 import java.util.Collection;
 import java.util.Collections;
@@ -93,6 +95,11 @@
     }
 
     @Override
+    public List<HttpFilter> getFilters() {
+        return filterChain.getFilters();
+    }
+
+    @Override
     public void addFilter(HttpFilter filter) {
         Assert.notNull(filter, "filter");
         filterChain.addFilter(filter);
@@ -321,7 +328,9 @@
                     tupleDef("workers", valueDefInteger()
                             .addValueValidator(LongValidator.GREATER_ZERO)
                     ).setRequired(false),
-                    tupleDef("filters", HttpFiltersNodeDefinitionCreator.createFiltersList()).setRequired(false)
+                    tupleDef("filters",
+                            HttpFiltersNodeDefinitionCreator.createFiltersList(true)
+                    ).setRequired(false)
             );
         }
 
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterAware.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterAware.java	Thu Jan 18 10:07:11 2018 +0100
@@ -1,6 +1,7 @@
 package com.passus.st.client.http.filter;
 
 import java.util.Collection;
+import java.util.List;
 
 /**
  *
@@ -8,6 +9,8 @@
  */
 public interface HttpFilterAware {
 
+    public List<HttpFilter> getFilters();
+
     public void setFilters(Collection<HttpFilter> filters);
 
     public void addFilter(HttpFilter filter);
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterChain.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterChain.java	Thu Jan 18 10:07:11 2018 +0100
@@ -4,9 +4,11 @@
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
 import com.passus.st.client.http.HttpFlowContext;
+import java.util.Collections;
 import java.util.Deque;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.List;
 
 /**
  *
@@ -14,7 +16,11 @@
  */
 public final class HttpFilterChain {
 
-    private final Deque<HttpFilter> filters = new LinkedList<>();
+    private final LinkedList<HttpFilter> filters = new LinkedList<>();
+
+    public List<HttpFilter> getFilters() {
+        return Collections.unmodifiableList(filters);
+    }
 
     @SuppressWarnings("unchecked")
     public void addFilter(HttpFilter filter) {
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersNodeDefinitionCreator.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersNodeDefinitionCreator.java	Thu Jan 18 10:07:11 2018 +0100
@@ -1,13 +1,9 @@
 package com.passus.st.client.http.filter;
 
-import com.passus.commons.plugin.PluginInfo;
-import com.passus.config.annotations.NodeDefinitionCreate;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
-import com.passus.config.schema.KeyValueVaryListNodeDefinition;
-import com.passus.config.schema.MapNodeDefinition;
+import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
-import java.util.Collection;
 
 /**
  *
@@ -16,32 +12,12 @@
 public class HttpFiltersNodeDefinitionCreator implements NodeDefinitionCreator {
 
     public static NodeDefinition createFiltersList() {
-        KeyValueVaryListNodeDefinition listDef = keyValueVaryListDef("type");
-        HttpFilterFactory factory = HttpFilterFactory.getInstance();
-        Collection<PluginInfo> plugins = factory.getAll();
-        for (PluginInfo plugin : plugins) {
-            final Class<?> pluginClass = plugin.getPluginClass();
-            NodeDefinitionCreate nodeDefCreate = pluginClass.getAnnotation(NodeDefinitionCreate.class);
-            NodeDefinition nodeDef;
-            if (nodeDefCreate == null) {
-                nodeDef = mapDef();
-            } else {
-                try {
-                    NodeDefinitionCreator creator = nodeDefCreate.value().newInstance();
-                    nodeDef = creator.create();
-                    if (!(nodeDef instanceof MapNodeDefinition)) {
-                        throw new RuntimeException("NodeDefinitionCreator '" + creator.getClass() + "' "
-                                + "should return '" + MapNodeDefinition.class + "'.");
-                    }
-                } catch (Exception ex) {
-                    throw new RuntimeException(ex.getMessage(), ex);
-                }
-            }
+        return createFiltersList(false);
+    }
 
-            listDef.add(plugin.getElementName(), (MapNodeDefinition) nodeDef);
-        }
-
-        return listDef;
+    public static NodeDefinition createFiltersList(boolean transformToObject) {
+        return new DynaKeyValueVaryListNodeDefinition("type", HttpFilterFactory.getInstance())
+                .setTransformToPluginObject(transformToObject);
     }
 
     @Override
--- a/stress-tester/src/main/java/com/passus/st/config/ProjectConfigurator.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/config/ProjectConfigurator.java	Thu Jan 18 10:07:11 2018 +0100
@@ -7,9 +7,12 @@
 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;
 
 /**
  *
@@ -19,24 +22,83 @@
 
     private final DomainConfiguratorFactory configuratorFactory = DomainConfiguratorFactory.getInstance();
 
-    private void processRootNode(CNode rootNode, Errors errors) {
+    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();
-        
-    }
 
-    @Override
-    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
-        
+        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/test/java/com/passus/st/client/http/HttpSynchClientWorkerTest.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/HttpSynchClientWorkerTest.java	Thu Jan 18 10:07:11 2018 +0100
@@ -7,7 +7,6 @@
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
 import com.passus.net.http.HttpResponseEncoder;
-import com.passus.st.Log4jConfigurationFactory;
 import com.passus.st.client.Event;
 import com.passus.st.client.SessionEvent;
 import com.passus.st.client.SessionStatusEvent;
@@ -234,7 +233,6 @@
 
     @Test(enabled = true)
     public void testHandle_SimpleRequestResponse_ConnectPartialSession() throws Exception {
-        Log4jConfigurationFactory.enableFactory("debug");
         List<Event> events = readEvents("pcap/http/http_req_resp.pcap");
         assertEquals(4, events.size());
 
--- a/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java	Wed Jan 17 14:47:57 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java	Thu Jan 18 10:07:11 2018 +0100
@@ -1,21 +1,17 @@
 package com.passus.st.config;
 
-import com.passus.commons.plugin.PluginFactory;
 import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
 import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
-import com.passus.st.Log4jConfigurationFactory;
 import com.passus.st.client.Client;
+import com.passus.st.client.credentials.MultiCredentialsProvider;
+import com.passus.st.client.credentials.UsernamePasswordCredentialsProvider;
 import com.passus.st.client.http.HttpClient;
-import java.io.IOException;
+import com.passus.st.client.http.filter.HttpFormLoginFilter;
 import java.util.List;
 import static org.testng.Assert.*;
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 /**
@@ -26,14 +22,13 @@
 
     @Test
     public void testConfigure() throws Exception {
-        Log4jConfigurationFactory.enableFactory("debug");
         String configStr = "client:\n"
                 + "    - type: http\n"
                 + "      connectPartialSession: true\n"
                 + "      collectMetrics: true\n"
-                + "      workers: 4\n";
-                /*+ "      filters:\n"
-                + "        - type: formAuth\n"
+                + "      workers: 4\n"
+                + "      filters:\n"
+                + "        - type: formLogin\n"
                 + "          active: true\n"
                 + "          loginCheckUrl: /login\n"
                 + "          userField: username\n"
@@ -43,7 +38,7 @@
                 + "          provider:\n"
                 + "            UsernamePassword:\n"
                 + "                username: user2\n"
-                + "                password: 'Qwe!23'";*/
+                + "                password: 'Qwe!23'";
 
         Configuration config = YamlConfigurationReader.readFromString(configStr);
         ClientConfigurator configurator = new ClientConfigurator();
@@ -59,6 +54,17 @@
         assertTrue(httpClient.isCollectMetrics());
         assertTrue(httpClient.isConnectPartialSession());
         assertEquals(4, httpClient.getWorkersNum());
+        assertEquals(1, httpClient.getFilters().size());
+        assertTrue(httpClient.getFilters().get(0) instanceof HttpFormLoginFilter);
+
+        HttpFormLoginFilter formLoginFilter = (HttpFormLoginFilter) httpClient.getFilters().get(0);
+        assertTrue(formLoginFilter.getCredentialsProvider() instanceof MultiCredentialsProvider);
+
+        MultiCredentialsProvider multiCredentialsProvider = (MultiCredentialsProvider) formLoginFilter.getCredentialsProvider();
+        assertTrue(multiCredentialsProvider.getProviders().get(0) instanceof UsernamePasswordCredentialsProvider);
+
+        UsernamePasswordCredentialsProvider credentialsProvider = (UsernamePasswordCredentialsProvider) multiCredentialsProvider.getProviders().get(0);
+        assertEquals("user2", credentialsProvider.getUsername());
     }
 
 }