changeset 877:145eb1d60983

ST-115 in progress
author Devel 1
date Fri, 02 Feb 2018 15:11:38 +0100
parents 40674a682e7f
children ad51a4cd465e
files stress-tester/src/main/java/com/passus/st/CliHelper.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpVarsFilter.java stress-tester/src/main/java/com/passus/st/vars/MapExtractorResolver.java stress-tester/src/main/java/com/passus/st/vars/VarsExecutor.java stress-tester/src/main/java/com/passus/st/vars/VarsExtractorResolver.java stress-tester/src/main/java/com/passus/st/vars/VarsValueExtractorResolver.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java
diffstat 7 files changed, 97 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/CliHelper.java	Fri Feb 02 12:27:02 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/CliHelper.java	Fri Feb 02 15:11:38 2018 +0100
@@ -24,7 +24,6 @@
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
 import com.passus.st.vars.VarsConfigurator;
-import com.passus.st.vars.VarsValueExtractorResolver;
 import java.io.File;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -195,9 +194,8 @@
             filters = new ArrayList<>();
         }
 
-        Map<String, ValueExtractor> globalVars = readGlobalVars(cl);
-        VarsValueExtractorResolver resolver = new VarsValueExtractorResolver(globalVars);
-        HttpVarsFilter httpVarsFilter = new HttpVarsFilter(resolver);
+        Map<String, ValueExtractor> appVars = readGlobalVars(cl);
+        HttpVarsFilter httpVarsFilter = new HttpVarsFilter(appVars);
 
         filters.add(httpVarsFilter);
         return filters;
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpVarsFilter.java	Fri Feb 02 12:27:02 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpVarsFilter.java	Fri Feb 02 15:11:38 2018 +0100
@@ -4,7 +4,7 @@
 import com.passus.data.ByteBuff;
 import com.passus.data.ByteBuffDataSource;
 import com.passus.data.HeapByteBuff;
-import com.passus.filter.config.ValueExtractorResolver;
+import com.passus.filter.ValueExtractor;
 import com.passus.net.http.HttpMessageHelper;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
@@ -12,8 +12,10 @@
 import com.passus.st.vars.Var;
 import com.passus.st.vars.VarsCompiler;
 import com.passus.st.vars.VarsExecutor;
+import com.passus.st.vars.VarsExtractorResolver;
 import java.io.IOException;
 import java.util.List;
+import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -34,9 +36,9 @@
         executor = new VarsExecutor();
     }
 
-    public HttpVarsFilter(ValueExtractorResolver resolver) {
-        Assert.notNull(resolver, "resolver");
-        compiler = new VarsCompiler(resolver);
+    public HttpVarsFilter(Map<String, ValueExtractor> appVars) {
+        Assert.notNull(appVars, "appVars");
+        compiler = new VarsCompiler(new VarsExtractorResolver(appVars));
         executor = new VarsExecutor();
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/vars/MapExtractorResolver.java	Fri Feb 02 15:11:38 2018 +0100
@@ -0,0 +1,30 @@
+package com.passus.st.vars;
+
+import com.passus.filter.ValueExtractor;
+import com.passus.filter.config.DefaultValueExtractorResolver;
+import java.util.Map;
+import com.passus.filter.config.ValueExtractorResolver;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class MapExtractorResolver implements ValueExtractorResolver {
+
+    private final ValueExtractorResolver fallbackResolver = new DefaultValueExtractorResolver();
+    private final Map<String, ValueExtractor> extractors;
+
+    public MapExtractorResolver(Map<String, ValueExtractor> extractors) {
+        this.extractors = extractors;
+    }
+
+    @Override
+    public ValueExtractor resolveValueExtractor(String fieldName) {
+        if (extractors.containsKey(fieldName)) {
+            return extractors.get(fieldName);
+        } else {
+            return fallbackResolver.resolveValueExtractor(fieldName);
+        }
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/vars/VarsExecutor.java	Fri Feb 02 12:27:02 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsExecutor.java	Fri Feb 02 15:11:38 2018 +0100
@@ -3,6 +3,7 @@
 import com.passus.data.ByteBuff;
 import static com.passus.st.AppUtils.DEFAULT_CHARSET;
 import com.passus.data.ByteString;
+import com.passus.filter.ValueExtractor;
 import java.util.List;
 
 /**
@@ -45,7 +46,8 @@
 
             int length = varStartIndex - offset;
             result.append(data, offset, length);
-            Object value = var.getValueExtractor().extract(context);
+            ValueExtractor valueExtractor = var.getValueExtractor();
+            Object value = valueExtractor.extract(context);
             if (value != null) {
                 //TODO Malo efektywne - do poprawienia
                 if (value instanceof ByteString) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsExtractorResolver.java	Fri Feb 02 15:11:38 2018 +0100
@@ -0,0 +1,40 @@
+package com.passus.st.vars;
+
+import com.passus.filter.UnmutableValueExtractor;
+import com.passus.filter.ValueExtractor;
+import com.passus.filter.config.ValueExtractorResolver;
+import com.passus.st.filter.Transformers;
+import java.util.Map;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class VarsExtractorResolver implements ValueExtractorResolver {
+
+    private static final String UNRESOLVED = "unresolved_appVars_";
+    private static final String APP_VARS_PREFIX = "appVars.";
+    private static final int APP_VARS_PREFIX_LEN = APP_VARS_PREFIX.length();
+
+    private final Map<String, ValueExtractor> extractors;
+
+    public VarsExtractorResolver(Map<String, ValueExtractor> extractors) {
+        this.extractors = extractors;
+    }
+
+    @Override
+    public ValueExtractor resolveValueExtractor(String fieldName) {
+        fieldName = fieldName.trim();
+        if (fieldName.startsWith(APP_VARS_PREFIX)) {
+            fieldName = fieldName.substring(APP_VARS_PREFIX_LEN).trim();
+            if (extractors.containsKey(fieldName)) {
+                return extractors.get(fieldName);
+            } else {
+                return new UnmutableValueExtractor(UNRESOLVED + fieldName);
+            }
+        } else {
+            return Transformers.VALUE_EXTRACTOR_RESOLVER.resolveValueExtractor(fieldName);
+        }
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/vars/VarsValueExtractorResolver.java	Fri Feb 02 12:27:02 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-package com.passus.st.vars;
-
-import com.passus.filter.ValueExtractor;
-import com.passus.filter.config.DefaultValueExtractorResolver;
-import java.util.Map;
-import com.passus.filter.config.ValueExtractorResolver;
-
-/**
- *
- * @author mikolaj.podbielski
- */
-public class VarsValueExtractorResolver implements ValueExtractorResolver {
-
-    private final ValueExtractorResolver fallbackResolver = new DefaultValueExtractorResolver();
-    private final Map<String, ValueExtractor> globalVars;
-
-    public VarsValueExtractorResolver(Map<String, ValueExtractor> globalVars) {
-        this.globalVars = globalVars;
-    }
-
-    @Override
-    public ValueExtractor resolveValueExtractor(String fieldName) {
-        if (globalVars.containsKey(fieldName)) {
-            return globalVars.get(fieldName);
-        } else {
-            return fallbackResolver.resolveValueExtractor(fieldName);
-        }
-    }
-
-}
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Fri Feb 02 12:27:02 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Fri Feb 02 15:11:38 2018 +0100
@@ -14,7 +14,6 @@
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpRequestBuilder;
 import com.passus.st.utils.TestHttpUtils;
-import com.passus.st.vars.VarsValueExtractorResolver;
 import java.io.IOException;
 import java.nio.charset.Charset;
 import java.util.List;
@@ -30,14 +29,12 @@
 
     private static final Charset UTF_8 = Charset.forName("UTF-8");
 
-    private final Map<String, ValueExtractor> vars = map(
+    private final Map<String, ValueExtractor> appVars = map(
             e("var1", val("value1")),
             e("name", val("X")),
             e("type", val("x"))
     );
 
-    private final VarsValueExtractorResolver resolver = new VarsValueExtractorResolver(vars);
-
     private static UnmutableValueExtractor val(Object o) {
         return new UnmutableValueExtractor(o);
     }
@@ -56,7 +53,7 @@
                 + "user=admin&pass={@var1}";
         HttpRequest request = TestHttpUtils.request(reqs);
 
-        HttpVarsFilter filter = new HttpVarsFilter(resolver);
+        HttpVarsFilter filter = new HttpVarsFilter(appVars);
 
         filter.filterOutbound(request, null, null);
 
@@ -72,29 +69,29 @@
                 = "POST /event_processing/edit/53ba69fcc2364828037a6bd7 HTTP/1.1\r\n"
                 + "Host: 172.16.60.159\r\n"
                 + "X-Requested-With: XMLHttpRequest\r\n"
-                + "Content-Length: 359\r\n"
+                + "Content-Length: 375\r\n"
                 + "Content-Type: multipart/form-data; boundary=------------------1456847137\r\n\r\n";
         String c1
                 = "--------------------1456847137\r\n"
                 + "Content-Disposition: form-data; name=\"name\"\r\n\r\n"
-                + "Filter {@name}\r\n"
+                + "Filter {@appVars.name}\r\n"
                 + "--------------------1456847137\r\n"
                 + "Content-Disposition: form-data; name=\"description\"\r\n\r\n"
                 + "Komputery korzystajÄ…ce z netu bez logowania do domeny\r\n"
                 + "--------------------1456847137\r\n"
                 + "Content-Disposition: form-data; name=\"type\"\r\n\r\n"
-                + "{@type}\r\n";
+                + "{@appVars.type}\r\n";
         String cn = "--------------------1456847137--\r\n";
 
         byte[] bytes = (hdrs + c1 + cn).getBytes(UTF_8);
-        assertEquals(359, (c1 + cn).getBytes(UTF_8).length);
+        assertEquals(375, (c1 + cn).getBytes(UTF_8).length);
 
         HttpRequest request = TestHttpUtils.request(bytes);
         List<HttpDataPart> parts = HttpMessageHelper.get().decodeMultipart(request);
         assertEquals(3, parts.size());
         assertTrue(content(parts.get(1), UTF_8).contains("korzystajÄ…ce"));
 
-        HttpVarsFilter filter = new HttpVarsFilter(resolver);
+        HttpVarsFilter filter = new HttpVarsFilter(appVars);
 
         filter.filterOutbound(request, null, null);
 
@@ -107,22 +104,24 @@
 
     @Test
     public void testPut() throws IOException {
+        String content = "{\"abc\": \"{@appVars.var1}\", \"def\": \"{@req.url}\"}";
+        String expected = "{\"abc\": \"value1\", \"def\": \"http://test.com\"}";
+
         HttpRequestBuilder builder = new HttpRequestBuilder("http://test.com", HttpMethod.PUT);
-        builder.content("{\"abc\": \"{@var1}\"}", "application/json");
+        builder.content(content, "application/json");
         HttpRequest request = builder.build();
 
-        HttpVarsFilter filter = new HttpVarsFilter(resolver);
-
+        HttpVarsFilter filter = new HttpVarsFilter(appVars);
         filter.filterOutbound(request, null, null);
 
         int contentLength = (int) HttpMessageHelper.get().getContentLength(request);
-        assertEquals("{\"abc\": \"value1\"}", content(request, UTF_8));
-        assertEquals(17, contentLength);
+        assertEquals(expected, content(request, UTF_8));
+        assertEquals(expected.length(), contentLength);
     }
 
     @Test
     public void testPutDeflate() throws IOException {
-        String json = "{\"abc\": \"{@var1}\"}";
+        String json = "{\"abc\": \"{@appVars.var1}\"}";
         byte[] plain = json.getBytes(); //18
         byte[] deflate = CompressionUtils.deflate(plain); //26
 
@@ -130,8 +129,7 @@
         builder.content(deflate).header(HttpHeaders.CONTENT_ENCODING, HttpConsts.DEFLATE);
         HttpRequest request = builder.build();
 
-        HttpVarsFilter filter = new HttpVarsFilter(resolver);
-
+        HttpVarsFilter filter = new HttpVarsFilter(appVars);
         filter.filterOutbound(request, null, null);
 
         int contentLength = (int) HttpMessageHelper.get().getContentLength(request);