changeset 596:db129043835c

minor
author Devel 2
date Wed, 04 Oct 2017 13:26:22 +0200
parents 6df4fe4ddd7e
children e5e096814c43
files stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java
diffstat 1 files changed, 61 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java	Wed Oct 04 13:21:18 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java	Wed Oct 04 13:26:22 2017 +0200
@@ -2,9 +2,16 @@
 
 import com.opencsv.CSVReader;
 import com.passus.commons.Assert;
+import com.passus.commons.ConversionException;
 import com.passus.commons.annotations.Plugin;
 import com.passus.config.Configuration;
-import static com.passus.st.client.credentials.UsernamePasswordCredentialsProvider.TYPE;
+import com.passus.config.ValueTransformer;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDef;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.st.plugin.PluginConstants;
 import java.io.File;
 import java.io.FileInputStream;
@@ -18,9 +25,10 @@
 
 /**
  *
- * @author Mirosław Hawrot
+ * @author Mirosław Hawrot
  */
 @Plugin(name = CsvUsernamePasswordCredentialsProvider.TYPE, category = PluginConstants.CATEGORY_CREDENTIALS_PROVIDER)
+@NodeDefinitionCreate(CsvUsernamePasswordCredentialsProvider.NodeDefCreator.class)
 public class CsvUsernamePasswordCredentialsProvider implements CredentialsProvider {
 
     public enum ReadMode {
@@ -105,17 +113,33 @@
 
     @Override
     public void configure(Configuration config) {
-        // TODO: move conversion to NodeDefCreator
         fileName = config.getString("fileName");
-        readMode = (ReadMode) config.get("readMode", DEFAULT_READ_MODE);
+        readMode = convertReadMode(config.get("readMode"));
         separator = config.getString("separator", "" + DEFAULT_SEPARATOR).charAt(0);
         try {
             credentialsList = load(new File(fileName), separator);
         } catch (IOException ex) {
             credentialsList = Collections.EMPTY_LIST;
+        } catch (NullPointerException npe) {
+            // TODO: workaround, pomimo negatywnej walidacji wywołuje się configure (z pustym fileName)
+            credentialsList = Collections.EMPTY_LIST;
         }
     }
 
+    // TODO: workaround, transformer nie uruchamiany przy konfiguracji filtra
+    private static ReadMode convertReadMode(Object value) {
+        if (value instanceof ReadMode) {
+            return (ReadMode) value;
+        } else if (value instanceof String) {
+            String str = (String) value;
+            try {
+                return ReadMode.valueOf(str.toUpperCase());
+            } catch (IllegalArgumentException ignore) {
+            }
+        }
+        return ReadMode.STEP_BY_STEP;
+    }
+
     static List<Credentials> load(File csvFile, char separator) throws IOException {
         List<Credentials> result = new ArrayList<>();
         try (FileInputStream fis = new FileInputStream(csvFile);
@@ -130,4 +154,37 @@
         }
         return result;
     }
+
+    public static class NodeDefCreator implements NodeDefinitionCreator {
+
+        private static ReadModeTransformer TRANSFORMER = new ReadModeTransformer();
+
+        @Override
+        public NodeDefinition create() {
+            return mapDef(
+                    tupleDef("fileName", valueDef()),
+                    tupleDef("readMode", valueDef().setTransformer(TRANSFORMER)).setRequired(false),
+                    tupleDef("separator", valueDef()).setRequired(false)
+            );
+        }
+    }
+
+    private static class ReadModeTransformer implements ValueTransformer {
+
+        @Override
+        public Object transform(Object obj) throws ConversionException {
+            String readMode = (String) obj;
+            readMode = readMode.toUpperCase();
+            try {
+                return ReadMode.valueOf(readMode);
+            } catch (IllegalArgumentException ex) {
+                throw new ConversionException("Illegal value", ex);
+            }
+        }
+
+        @Override
+        public Object reverseTransform(Object obj) throws ConversionException {
+            return obj.toString();
+        }
+    }
 }