changeset 592:e3471e627962

MultiCredentialsProviderTransformerTest
author Devel 1
date Tue, 03 Oct 2017 15:10:31 +0200
parents 87ed37e4d18f
children 2f8dfda084fe
files stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java stress-tester/src/test/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProviderTest.java stress-tester/src/test/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformerTest.java
diffstat 3 files changed, 130 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java	Tue Oct 03 13:19:58 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java	Tue Oct 03 15:10:31 2017 +0200
@@ -2,13 +2,16 @@
 
 import com.opencsv.CSVReader;
 import com.passus.commons.Assert;
+import com.passus.commons.annotations.Plugin;
 import com.passus.config.Configuration;
+import com.passus.st.plugin.PluginConstants;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ThreadLocalRandom;
 
@@ -16,6 +19,7 @@
  *
  * @author Mirosław Hawrot
  */
+@Plugin(name = CsvUsernamePasswordCredentialsProvider.TYPE, category = PluginConstants.CATEGORY_CREDENTIALS_PROVIDER)
 public class CsvUsernamePasswordCredentialsProvider implements CredentialsProvider {
 
     public enum ReadMode {
@@ -35,22 +39,16 @@
         public abstract int index(int index);
     }
 
-    private String fileName;
+    public static final String TYPE = "CsvUsernamePassword";
 
     private ReadMode readMode = ReadMode.STEP_BY_STEP;
+    private String fileName;
+    private char separator;
 
-    private List<Credentials> credentialses;
+    private List<Credentials> credentialsList;
 
     private int index;
 
-    public String getFileName() {
-        return fileName;
-    }
-
-    public void setFileName(String fileName) {
-        this.fileName = fileName;
-    }
-
     public ReadMode getReadMode() {
         return readMode;
     }
@@ -60,24 +58,52 @@
         this.readMode = readMode;
     }
 
-    void setCredentialses(List<Credentials> credentialses) {
-        this.credentialses = credentialses;
+    public String getFileName() {
+        return fileName;
+    }
+
+    public void setFileName(String fileName) {
+        this.fileName = fileName;
+    }
+
+    public char getSeparator() {
+        return separator;
+    }
+
+    public void setSeparator(char separator) {
+        this.separator = separator;
+    }
+
+    List<Credentials> getCredentialsList() {
+        return credentialsList;
+    }
+
+    void setCredentialsList(List<Credentials> credentialsList) {
+        this.credentialsList = credentialsList;
     }
 
     @Override
     public Credentials getCredentials(ProviderContext context) {
         // TODO: remove
-        if (credentialses == null || credentialses.isEmpty()) {
+        if (credentialsList == null || credentialsList.isEmpty()) {
             return null;
         }
 
-        int current = (readMode.index(index++) & 0x7f_ff_ff_ff) % credentialses.size();
-        return credentialses.get(current);
+        int current = (readMode.index(index++) & 0x7f_ff_ff_ff) % credentialsList.size();
+        return credentialsList.get(current);
     }
 
     @Override
     public void configure(Configuration config) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        // TODO: move conversion to NodeDefCreator
+        fileName = config.getString("fileName");
+        readMode = (ReadMode) config.get("readMode", ReadMode.STEP_BY_STEP);
+        separator = config.getString("separator", ",").charAt(0);
+        try {
+            credentialsList = load(new File(fileName), separator);
+        } catch (IOException ex) {
+            credentialsList = Collections.EMPTY_LIST;
+        }
     }
 
     static List<Credentials> load(File csvFile, char separator) throws IOException {
--- a/stress-tester/src/test/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProviderTest.java	Tue Oct 03 13:19:58 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProviderTest.java	Tue Oct 03 15:10:31 2017 +0200
@@ -49,7 +49,7 @@
         List<Credentials> cred = Arrays.asList(
                 c("u0", "p0"), c("u1", "p1"), c("u2", "p2")
         );
-        provider.setCredentialses(cred);
+        provider.setCredentialsList(cred);
 
         provider.setReadMode(ReadMode.STEP_BY_STEP);
         assertCred("", provider.getCredentials(null), "u0", "p0");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformerTest.java	Tue Oct 03 15:10:31 2017 +0200
@@ -0,0 +1,87 @@
+package com.passus.st.client.credentials;
+
+import com.passus.commons.ConversionException;
+import com.passus.commons.utils.ResourceUtils;
+import com.passus.config.CCompositeNode;
+import com.passus.config.CNode;
+import com.passus.config.CTupleNode;
+import com.passus.config.CValueNode;
+import com.passus.config.Configuration;
+import com.passus.config.NodeException;
+import com.passus.config.YamlConfigurationReader;
+import com.passus.st.client.credentials.CsvUsernamePasswordCredentialsProvider.ReadMode;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class MultiCredentialsProviderTransformerTest {
+
+    @Test
+    public void testTransformUsernamePassword() throws Exception {
+        String content
+                = "provider:\n"
+                + "    UsernamePassword:\n"
+                + "        username: login\n"
+                + "        password: pass\n"
+                + "    UsernamePassword:\n"
+                + "        username: login2\n"
+                + "        password: pass2\n";
+
+        List<CredentialsProvider> providers = readAndTransform(content).getProviders();
+        UsernamePasswordCredentialsProvider upcp;
+
+        assertEquals(2, providers.size());
+
+        assertTrue(providers.get(0) instanceof UsernamePasswordCredentialsProvider);
+        upcp = (UsernamePasswordCredentialsProvider) providers.get(0);
+        assertEquals("login", upcp.getUsername());
+        assertEquals("pass", upcp.getPassword());
+
+        assertTrue(providers.get(1) instanceof UsernamePasswordCredentialsProvider);
+        upcp = (UsernamePasswordCredentialsProvider) providers.get(1);
+        assertEquals("login2", upcp.getUsername());
+        assertEquals("pass2", upcp.getPassword());
+    }
+
+    @Test
+    public void testTransformCsvUsernamePassword() throws Exception {
+        File file = ResourceUtils.getFile("csv/comma.csv");
+        String content
+                = "provider:\n"
+                + "    CsvUsernamePassword:\n"
+                + "        fileName: '%s'\n"
+                + "        separator: ','\n";
+        content = String.format(content, file.getAbsolutePath());
+
+        List<CredentialsProvider> providers = readAndTransform(content).getProviders();
+
+        assertEquals(1, providers.size());
+
+        assertTrue(providers.get(0) instanceof CsvUsernamePasswordCredentialsProvider);
+        CsvUsernamePasswordCredentialsProvider cupcp = (CsvUsernamePasswordCredentialsProvider) providers.get(0);
+        assertEquals(ReadMode.STEP_BY_STEP, cupcp.getReadMode());
+        assertEquals(file.getAbsolutePath(), cupcp.getFileName());
+        assertEquals(',', cupcp.getSeparator());
+        List<Credentials> actualList = cupcp.getCredentialsList();
+        assertEquals(5, actualList.size());
+        assertEquals("pass,word", actualList.get(4).getPassword());
+    }
+
+    private static MultiCredentialsProvider readAndTransform(String content) throws IOException, NodeException, ConversionException {
+        Configuration cfg = YamlConfigurationReader.readFromString(content);
+        CCompositeNode rootNode = cfg.getRootNode();
+        CTupleNode providerNode = (CTupleNode) rootNode.getFirstChild();
+        CNode node = providerNode.getNode();
+
+        MultiCredentialsProviderTransformer transformer = new MultiCredentialsProviderTransformer();
+        CValueNode valueNode = transformer.transform(node);
+        return (MultiCredentialsProvider) valueNode.getValue();
+    }
+
+}