changeset 838:6122fb555251

ClientConfiguration improvements
author Devel 2
date Mon, 22 Jan 2018 11:41:12 +0100
parents 0910a8d32fd6
children 6b4a56251b57
files stress-tester/src/main/java/com/passus/st/config/ClientConfigurator.java stress-tester/src/main/java/com/passus/st/config/ClientNodeDefinitionCreator.java stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java
diffstat 4 files changed, 76 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/config/ClientConfigurator.java	Mon Jan 22 11:40:54 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/config/ClientConfigurator.java	Mon Jan 22 11:41:12 2018 +0100
@@ -2,26 +2,22 @@
 
 import com.passus.commons.Assert;
 import com.passus.commons.annotations.Plugin;
-import com.passus.commons.plugin.PluginFactory;
-import com.passus.config.CListNode;
 import com.passus.config.CNode;
 import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
-import com.passus.config.ConfigurationUtils;
-import static com.passus.config.ConfigurationUtils.validateType;
 import com.passus.config.DomainConfigurator;
-import com.passus.config.NodeType;
-import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
 import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.config.validation.Errors;
-import com.passus.data.type.Type;
 import com.passus.st.client.Client;
-import com.passus.st.client.ClientFactory;
+import com.passus.st.client.http.HttpClient;
 import com.passus.st.plugin.PluginConstants;
+import com.passus.st.utils.ConfigurationContextConsts;
+
+import java.util.ArrayList;
 import java.util.List;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 @Plugin(name = ClientConfigurator.DOMAIN, category = PluginConstants.CATEGORY_DOMAIN_CONFIGURATOR)
@@ -29,47 +25,50 @@
 
     public static final String DOMAIN = "client";
 
-    private PluginFactory<Client> clientFactory = ClientFactory.getInstance();
+    private ClientNodeDefinitionCreator creator = new ClientNodeDefinitionCreator();
 
-    private NodeDefinition nodeDef = createNodeDef(clientFactory);
+    private NodeDefinition nodeDef = creator.create();
 
     @Override
     public String getDomain() {
         return DOMAIN;
     }
 
-    public PluginFactory<Client> getClientFactory() {
-        return clientFactory;
+    public void setClientNodeDefinitionCreator(ClientNodeDefinitionCreator creator) {
+        Assert.notNull(creator, "creator");
+        this.creator = creator;
     }
 
-    public void setClientFactory(PluginFactory<Client> clientFactory) {
-        Assert.notNull(clientFactory, "clientFactory");
-        this.clientFactory = clientFactory;
-        nodeDef = createNodeDef(clientFactory);
+    public NodeDefinitionCreator getClientNodeDefinitionCreator() {
+        return creator;
     }
 
     @Override
     public void validate(Configuration config, Errors errors, ConfigurationContext context) {
         CNode rootNode = config.getRootNode();
-        if (!validateType(rootNode, NodeType.LIST, errors)) {
-            return;
-        }
+        nodeDef.validate(rootNode, errors, context);
+    }
 
-        nodeDef.validate(rootNode, errors, context);
+    private Client createDefaultClient() {
+        return new HttpClient();
     }
 
     @Override
     public void configure(Configuration config, Errors errors, ConfigurationContext context) {
         CNode rootNode = config.getRootNode();
-        if (!validateType(rootNode, NodeType.LIST, errors)) {
-            return;
-        }
+        nodeDef.transform(rootNode, errors, context);
+        if (!errors.hasError()) {
+            try {
+                List<Client> clients = (List<Client>) config.get("clients");
+                if (clients == null) {
+                    clients = new ArrayList<>(4);
+                }
 
-        CListNode listNode = (CListNode) nodeDef.transform(rootNode, errors, context);
-        if (listNode != null && !errors.hasError()) {
-            try {
-                List clients = ConfigurationUtils.convertToList(listNode, Type.OBJECT);
-                context.add("clients", clients);
+                if (!clients.isEmpty()) {
+                    clients.add(createDefaultClient());
+                }
+
+                context.add(ConfigurationContextConsts.CLIENT_CLIENTS, config.get("clients"));
             } catch (Exception e) {
                 throw new RuntimeException(e.getMessage(), e);
             }
@@ -77,9 +76,4 @@
 
     }
 
-    private static DynaKeyValueVaryListNodeDefinition createNodeDef(PluginFactory<Client> clientFactory) {
-        DynaKeyValueVaryListNodeDefinition nodeDef = new DynaKeyValueVaryListNodeDefinition("type", clientFactory);
-        nodeDef.setTransformToPluginObject(true);
-        return nodeDef;
-    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/config/ClientNodeDefinitionCreator.java	Mon Jan 22 11:41:12 2018 +0100
@@ -0,0 +1,39 @@
+package com.passus.st.config;
+
+import com.passus.commons.Assert;
+import com.passus.commons.plugin.PluginFactory;
+import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.st.client.Client;
+import com.passus.st.client.ClientFactory;
+import com.passus.st.utils.ConfigurationContextConsts;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+
+public class ClientNodeDefinitionCreator implements NodeDefinitionCreator {
+
+    private PluginFactory<Client> clientFactory = ClientFactory.getInstance();
+
+    public PluginFactory<Client> getClientFactory() {
+        return clientFactory;
+    }
+
+    public void setClientFactory(PluginFactory<Client> clientFactory) {
+        Assert.notNull(clientFactory, "clientFactory");
+        this.clientFactory = clientFactory;
+    }
+
+    public NodeDefinition createClientsListDef() {
+        return new DynaKeyValueVaryListNodeDefinition("type", clientFactory)
+                .setTransformToPluginObject(true);
+    }
+
+    @Override
+    public NodeDefinition create() {
+        return mapDef(
+                tupleDef("clients", createClientsListDef())
+        );
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Mon Jan 22 11:40:54 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Mon Jan 22 11:41:12 2018 +0100
@@ -10,6 +10,8 @@
      */
     public static final String EVENT_SOURCE_SOURCES = "eventSource.sources";
 
+    public static final String CLIENT_CLIENTS = "client.clients";
+
     /**
      * Domyślny mapper ustawiany jeżeli emitter nie ma zdefiniowanego własnego.
      */
--- a/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java	Mon Jan 22 11:40:54 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java	Mon Jan 22 11:41:12 2018 +0100
@@ -12,6 +12,8 @@
 import com.passus.st.client.http.filter.HttpFormLoginFilter;
 import java.util.List;
 import static org.testng.Assert.*;
+
+import com.passus.st.utils.ConfigurationContextConsts;
 import org.testng.annotations.Test;
 
 /**
@@ -22,7 +24,7 @@
 
     @Test
     public void testConfigure_InvalidFilter() throws Exception {
-        String configStr = "client:\n"
+        String configStr = "clients:\n"
                 + "    - type: http\n"
                 + "      filters:\n"
                 + "        - type: unknownFilter\n";
@@ -32,14 +34,14 @@
 
         Errors errors = new Errors();
         ConfigurationContext context = new ConfigurationContextImpl();
-        configurator.configure(config.subConfiguration("client"), errors, context);
+        configurator.configure(config, errors, context);
         assertTrue(errors.hasError());
     }
 
     @SuppressWarnings("unchecked")
     @Test
     public void testConfigure() throws Exception {
-        String configStr = "client:\n"
+        String configStr = "clients:\n"
                 + "    - type: http\n"
                 + "      connectPartialSession: true\n"
                 + "      collectMetrics: true\n"
@@ -62,11 +64,11 @@
 
         Errors errors = new Errors();
         ConfigurationContext context = new ConfigurationContextImpl();
-        configurator.configure(config.subConfiguration("client"), errors, context);
+        configurator.configure(config, errors, context);
 
         assertFalse(errors.hasError());
 
-        List<Client> clients = (List<Client>) context.get("clients");
+        List<Client> clients = (List<Client>) context.get(ConfigurationContextConsts.CLIENT_CLIENTS);
         assertTrue(clients.get(0) instanceof HttpClient);
         HttpClient httpClient = (HttpClient) clients.get(0);
         assertTrue(httpClient.isCollectMetrics());