changeset 925:7dfc850d0715

Transformer refactorization
author Devel 2
date Fri, 27 Apr 2018 13:00:56 +0200
parents 3a6dc239361d
children bb5eeffce305
files stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterStoreTransformer.java stress-tester/src/main/java/com/passus/st/config/StringToExecutableStatementValueTransformer.java stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapperValueTransformer.java stress-tester/src/main/java/com/passus/st/emitter/RuleSessionMapperValueTransformer.java stress-tester/src/main/java/com/passus/st/utils/PeriodValueTransformer.java stress-tester/src/main/java/com/passus/st/utils/SizeValueTransformer.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java
diffstat 9 files changed, 88 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,18 +1,17 @@
 package com.passus.st.client.http;
 
 import com.passus.commons.Assert;
-import com.passus.commons.ConversionException;
 import com.passus.commons.annotations.Plugin;
 import com.passus.commons.service.ServiceException;
 import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
 import com.passus.config.annotations.NodeDefinitionCreate;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
 import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.config.validation.EnumValidator;
+import com.passus.config.validation.Errors;
 import com.passus.config.validation.LongValidator;
 import com.passus.st.client.Client;
 import com.passus.st.client.Event;
@@ -26,13 +25,9 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
 
 
 /**
@@ -353,7 +348,7 @@
         private static final DispatcherTransformer INSTANCE = new DispatcherTransformer();
 
         @Override
-        public HttpClientWorkerDispatcher transform(Object obj) throws ConversionException {
+        public HttpClientWorkerDispatcher transform(Object obj, Errors errors, ConfigurationContext context) {
             if (obj == null) {
                 return null;
             }
@@ -365,14 +360,17 @@
                     case "parallel":
                         return null;
                     default:
-                        throw new ConversionException("Unknown dispatcher type: " + s);
+                        errors.reject("Unknown dispatcher type: " + s);
+                        return null;
                 }
             }
-            throw new ConversionException("Invalid type.");
+
+            errors.reject("Invalid type.");
+            return null;
         }
 
         @Override
-        public Object reverseTransform(Object obj) throws ConversionException {
+        public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
             throw new UnsupportedOperationException("Not supported yet.");
         }
 
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java	Fri Apr 27 13:00:56 2018 +0200
@@ -52,7 +52,6 @@
 
             return null;
         }
-
     }
 
     @Override
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterStoreTransformer.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpCsrfFilterStoreTransformer.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,16 +1,16 @@
 package com.passus.st.client.http.filter;
 
-import com.passus.commons.ConversionException;
+import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
 
 /**
- *
  * @author mikolaj.podbielski
  */
 class HttpCsrfFilterStoreTransformer implements ValueTransformer {
 
     @Override
-    public Object transform(Object obj) throws ConversionException {
+    public Object transform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj instanceof String) {
             String s = (String) obj;
             switch (s) {
@@ -19,20 +19,23 @@
                 case "single":
                     return new HttpCsrfFilter.SingleTokenStore();
                 default:
-                    throw new ConversionException("Unknown token store type: " + s);
+                    errors.reject("Unknown token store type: " + s);
+                    return null;
             }
         }
-        throw new ConversionException("Invalid token store type.");
+
+        errors.reject("Invalid token store type.");
+        return null;
     }
 
     @Override
-    public Object reverseTransform(Object obj) throws ConversionException {
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj instanceof HttpCsrfFilter.QueueStore) {
             return "queue";
         } else if (obj instanceof HttpCsrfFilter.SingleTokenStore) {
             return "single";
         } else {
-            throw new ConversionException("Unknown token store type: " + obj.getClass().getSimpleName());
+            throw new IllegalArgumentException("Unknown token store type: " + obj.getClass().getSimpleName());
         }
     }
 
--- a/stress-tester/src/main/java/com/passus/st/config/StringToExecutableStatementValueTransformer.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/config/StringToExecutableStatementValueTransformer.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,18 +1,18 @@
 package com.passus.st.config;
 
-import com.passus.commons.ConversionException;
+import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
 import org.mvel2.MVEL;
 import org.mvel2.compiler.ExecutableStatement;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class StringToExecutableStatementValueTransformer implements ValueTransformer {
 
     @Override
-    public Object transform(Object obj) throws ConversionException {
+    public Object transform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj == null) {
             return null;
         }
@@ -21,12 +21,14 @@
         try {
             return (ExecutableStatement) MVEL.compileExpression(str);
         } catch (Exception ex) {
-            throw new ConversionException("Compilation error:\n" + ex.getMessage());
+            errors.reject("Compilation error:\n" + ex.getMessage());
         }
+
+        return null;
     }
 
     @Override
-    public Object reverseTransform(Object obj) throws ConversionException {
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
         throw new UnsupportedOperationException("Impossible.");
     }
 
--- a/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapperValueTransformer.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapperValueTransformer.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,12 +1,13 @@
 package com.passus.st.emitter;
 
-import com.passus.commons.ConversionException;
+import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
+
 import java.util.List;
 import java.util.stream.Collectors;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class RuleBasedSessionMapperValueTransformer implements ValueTransformer {
@@ -14,7 +15,7 @@
     public static final RuleBasedSessionMapperValueTransformer INSTANCE = new RuleBasedSessionMapperValueTransformer();
 
     @Override
-    public Object transform(Object value) throws ConversionException {
+    public Object transform(Object value, Errors errors, ConfigurationContext context) {
         if (value == null) {
             return null;
         }
@@ -27,7 +28,8 @@
                 try {
                     mapper.addRule(str);
                 } catch (Exception e) {
-                    throw new ConversionException("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage(), e);
+                    errors.reject("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage());
+                    return null;
                 }
             }
         } else {
@@ -35,7 +37,8 @@
             try {
                 mapper.addRule(str);
             } catch (Exception e) {
-                throw new ConversionException("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage(), e);
+                errors.reject("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage());
+                return null;
             }
         }
 
@@ -43,12 +46,12 @@
     }
 
     @Override
-    public Object reverseTransform(Object obj) throws ConversionException {
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj == null) {
             return null;
         }
         if (!(obj instanceof RuleBasedSessionMapper)) {
-            throw new ConversionException();
+            throw new IllegalArgumentException("RuleBasedSessionMapper required.");
         }
 
         RuleBasedSessionMapper mapper = (RuleBasedSessionMapper) obj;
--- a/stress-tester/src/main/java/com/passus/st/emitter/RuleSessionMapperValueTransformer.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/RuleSessionMapperValueTransformer.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,10 +1,10 @@
 package com.passus.st.emitter;
 
-import com.passus.commons.ConversionException;
+import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class RuleSessionMapperValueTransformer implements ValueTransformer {
@@ -12,7 +12,7 @@
     public static final RuleSessionMapperValueTransformer INSTANCE = new RuleSessionMapperValueTransformer();
 
     @Override
-    public Object transform(Object value) throws ConversionException {
+    public Object transform(Object value, Errors errors, ConfigurationContext context) {
         if (value == null) {
             return null;
         }
@@ -21,17 +21,19 @@
         try {
             return RuleBasedSessionMapper.parseRule(str);
         } catch (Exception e) {
-            throw new ConversionException("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage(), e);
+            errors.reject("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage());
+            return null;
         }
     }
 
+
     @Override
-    public Object reverseTransform(Object obj) throws ConversionException {
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj == null) {
             return null;
         }
         if (!(obj instanceof RuleBasedSessionMapper.Rule)) {
-            throw new ConversionException();
+            throw new IllegalArgumentException("RuleBasedSessionMapper.Rule required.");
         }
 
         return obj.toString();
--- a/stress-tester/src/main/java/com/passus/st/utils/PeriodValueTransformer.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/PeriodValueTransformer.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,10 +1,11 @@
 package com.passus.st.utils;
 
 import com.passus.commons.ConversionException;
+import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class PeriodValueTransformer implements ValueTransformer {
@@ -21,15 +22,35 @@
     }
 
     @Override
-    public Object reverseTransform(Object obj) throws ConversionException {
+    public Object transform(Object value, Errors errors, ConfigurationContext context) {
+        if (value == null) {
+            return null;
+        }
+
+        try {
+            return PeriodFormatter.INSTANCE.transform(value.toString());
+        } catch (ConversionException e) {
+            errors.reject("Invalid period.");
+            return null;
+        }
+    }
+
+    @Override
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj == null) {
             return null;
         } else if (!(obj instanceof Long)) {
-            throw new ConversionException();
+            throw new IllegalArgumentException();
         }
 
         Long l = (Long) obj;
-        return PeriodFormatter.INSTANCE.reverseTransform(l);
+
+        try {
+            return PeriodFormatter.INSTANCE.reverseTransform(l);
+        } catch (ConversionException e) {
+            errors.reject("Invalid period.");
+            return null;
+        }
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/utils/SizeValueTransformer.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/SizeValueTransformer.java	Fri Apr 27 13:00:56 2018 +0200
@@ -1,10 +1,11 @@
 package com.passus.st.utils;
 
 import com.passus.commons.ConversionException;
+import com.passus.config.ConfigurationContext;
 import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class SizeValueTransformer implements ValueTransformer {
@@ -12,26 +13,36 @@
     public static final SizeValueTransformer INSTANCE = new SizeValueTransformer();
 
     @Override
-    public Object transform(Object value) throws ConversionException {
+    public Object transform(Object value, Errors errors, ConfigurationContext context) {
         if (value == null) {
             return null;
         } else if (value instanceof Long) {
             return value;
         }
 
-        return SizeFormatter.INSTANCE.transform(value.toString());
+        try {
+            return SizeFormatter.INSTANCE.transform(value.toString());
+        } catch (ConversionException e) {
+            errors.reject("Invalid size.");
+            return null;
+        }
     }
 
     @Override
-    public Object reverseTransform(Object obj) throws ConversionException {
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
         if (obj == null) {
             return null;
         } else if (!(obj instanceof Long)) {
-            throw new ConversionException();
+            throw new IllegalArgumentException();
         }
 
         Long l = (Long) obj;
-        return SizeFormatter.INSTANCE.reverseTransform(l);
+        try {
+            return SizeFormatter.INSTANCE.reverseTransform(l);
+        } catch (ConversionException e) {
+            errors.reject("Invalid size.");
+            return null;
+        }
     }
 
 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Fri Apr 27 10:30:37 2018 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Fri Apr 27 13:00:56 2018 +0200
@@ -126,7 +126,6 @@
         filter.filterOutbound(req, null, null);
 
         HttpParameters params = helper.decodeMultipartParameters(req);
-
         assertEquals("param1=value11&param2=value2&param5=value5&param6=value6&param1=value12", params.toString());
     }