changeset 595:6df4fe4ddd7e

minor
author Devel 2
date Wed, 04 Oct 2017 13:21:18 +0200
parents 744c817abd6d
children db129043835c
files stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java
diffstat 1 files changed, 10 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java	Wed Oct 04 08:53:54 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java	Wed Oct 04 13:21:18 2017 +0200
@@ -2,16 +2,9 @@
 
 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 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 static com.passus.st.client.credentials.UsernamePasswordCredentialsProvider.TYPE;
 import com.passus.st.plugin.PluginConstants;
 import java.io.File;
 import java.io.FileInputStream;
@@ -28,7 +21,6 @@
  * @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 {
@@ -50,9 +42,13 @@
 
     public static final String TYPE = "CsvUsernamePassword";
 
-    private ReadMode readMode = ReadMode.STEP_BY_STEP;
+    public static final char DEFAULT_SEPARATOR = ',';
+
+    public static final ReadMode DEFAULT_READ_MODE = ReadMode.STEP_BY_STEP;
+
+    private ReadMode readMode = DEFAULT_READ_MODE;
     private String fileName;
-    private char separator;
+    private char separator = DEFAULT_SEPARATOR;
 
     private List<Credentials> credentialsList;
 
@@ -109,33 +105,17 @@
 
     @Override
     public void configure(Configuration config) {
+        // TODO: move conversion to NodeDefCreator
         fileName = config.getString("fileName");
-        readMode = convertReadMode(config.get("readMode"));
-        separator = config.getString("separator", ",").charAt(0);
+        readMode = (ReadMode) config.get("readMode", DEFAULT_READ_MODE);
+        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);
@@ -150,37 +130,4 @@
         }
         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();
-        }
-    }
 }