Mercurial > stress-tester
changeset 591:87ed37e4d18f
CredentialsProviderFactory - implementation
author | Devel 1 |
---|---|
date | Tue, 03 Oct 2017 13:19:58 +0200 |
parents | bddfc1384d32 |
children | e3471e627962 |
files | stress-tester/pom.xml stress-tester/src/main/java/com/passus/st/client/credentials/CredentialsProviderFactory.java 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/resources/csv/comma.csv stress-tester/src/test/resources/csv/comma1.csv stress-tester/src/test/resources/csv/semicolon.csv stress-tester/src/test/resources/csv/semicolon1.csv |
diffstat | 8 files changed, 157 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/pom.xml Tue Oct 03 10:59:48 2017 +0200 +++ b/stress-tester/pom.xml Tue Oct 03 13:19:58 2017 +0200 @@ -146,6 +146,12 @@ </dependency> <dependency> + <groupId>com.opencsv</groupId> + <artifactId>opencsv</artifactId> + <version>3.10</version> + </dependency> + + <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.10</version>
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/CredentialsProviderFactory.java Tue Oct 03 10:59:48 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/credentials/CredentialsProviderFactory.java Tue Oct 03 13:19:58 2017 +0200 @@ -1,7 +1,6 @@ package com.passus.st.client.credentials; import com.passus.commons.plugin.PluginFactory; -import com.passus.st.client.http.filter.HttpFilterFactory; import com.passus.st.plugin.PluginConstants; /**
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java Tue Oct 03 10:59:48 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java Tue Oct 03 13:19:58 2017 +0200 @@ -1,7 +1,16 @@ package com.passus.st.client.credentials; +import com.opencsv.CSVReader; import com.passus.commons.Assert; import com.passus.config.Configuration; +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.List; +import java.util.concurrent.ThreadLocalRandom; /** * @@ -10,13 +19,30 @@ public class CsvUsernamePasswordCredentialsProvider implements CredentialsProvider { public enum ReadMode { - RANDOM, STEP_BY_STEP + RANDOM { + @Override + public int index(int index) { + return ThreadLocalRandom.current().nextInt(); + } + }, + STEP_BY_STEP { + @Override + public int index(int index) { + return index; + } + }; + + public abstract int index(int index); } private String fileName; private ReadMode readMode = ReadMode.STEP_BY_STEP; + private List<Credentials> credentialses; + + private int index; + public String getFileName() { return fileName; } @@ -34,9 +60,19 @@ this.readMode = readMode; } + void setCredentialses(List<Credentials> credentialses) { + this.credentialses = credentialses; + } + @Override public Credentials getCredentials(ProviderContext context) { - throw new UnsupportedOperationException("Not supported yet."); + // TODO: remove + if (credentialses == null || credentialses.isEmpty()) { + return null; + } + + int current = (readMode.index(index++) & 0x7f_ff_ff_ff) % credentialses.size(); + return credentialses.get(current); } @Override @@ -44,4 +80,18 @@ throw new UnsupportedOperationException("Not supported yet."); } + static List<Credentials> load(File csvFile, char separator) throws IOException { + List<Credentials> result = new ArrayList<>(); + try (FileInputStream fis = new FileInputStream(csvFile); + InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); + CSVReader reader = new CSVReader(isr, separator);) { + String tokens[]; + while ((tokens = reader.readNext()) != null) { + if (tokens.length >= 2) { + result.add(new Credentials(tokens[0], tokens[1])); + } + } + } + return result; + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProviderTest.java Tue Oct 03 13:19:58 2017 +0200 @@ -0,0 +1,79 @@ +package com.passus.st.client.credentials; + +import com.passus.commons.utils.ResourceUtils; +import com.passus.st.client.credentials.CsvUsernamePasswordCredentialsProvider.ReadMode; +import static com.passus.st.client.credentials.CsvUsernamePasswordCredentialsProvider.load; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import static org.testng.AssertJUnit.*; +import org.testng.annotations.Test; + +/** + * + * @author mikolaj.podbielski + */ +public class CsvUsernamePasswordCredentialsProviderTest { + + @Test + public void testLoad() throws IOException { + String names[] = {"csv/comma.csv", "csv/comma1.csv", "csv/semicolon.csv", "csv/semicolon1.csv"}; + char[] separators = {',', ',', ';', ';'}; + for (int i = 0; i < names.length; ++i) { + File file = ResourceUtils.getFile(names[i]); + List<Credentials> result = load(file, separators[i]); + + String desc = "file:" + names[i]; + assertEquals(desc, 5, result.size()); + assertCred(desc, result.get(0), "user 0", "password 0"); + assertCred(desc, result.get(1), "user 1", "password 1"); + assertCred(desc, result.get(2), "us\"er", "pass\"word"); + assertCred(desc, result.get(3), "us;er", "pass;word"); + assertCred(desc, result.get(4), "us,er", "pass,word"); + } + } + + @Test + public void testModes() { + assertEquals(123, ReadMode.STEP_BY_STEP.index(123)); + assertEquals(125, ReadMode.STEP_BY_STEP.index(125)); +// for (int i = 0; i < 100; ++i) { +// assertTrue(ReadMode.RANDOM.index(i) ?????); +// } + } + + @Test + public void testGetCredentials() { + CsvUsernamePasswordCredentialsProvider provider = new CsvUsernamePasswordCredentialsProvider(); + List<Credentials> cred = Arrays.asList( + c("u0", "p0"), c("u1", "p1"), c("u2", "p2") + ); + provider.setCredentialses(cred); + + provider.setReadMode(ReadMode.STEP_BY_STEP); + assertCred("", provider.getCredentials(null), "u0", "p0"); + assertCred("", provider.getCredentials(null), "u1", "p1"); + assertCred("", provider.getCredentials(null), "u2", "p2"); + assertCred("", provider.getCredentials(null), "u0", "p0"); + + provider.setReadMode(ReadMode.RANDOM); + for (int i = 0; i < 16; i++) { + assertNotNull(provider.getCredentials(null)); + } + } + + private static void assertCred(String desc, Credentials c, String u, String p) { + assertEquals(desc, u, c.getUser()); + assertEquals(desc, p, c.getPassword()); + } + + private static Credentials c(String u, String p) { + return new Credentials(u, p); + } + + @Test + public void testConfigure() { + assertTrue(true); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/resources/csv/comma.csv Tue Oct 03 13:19:58 2017 +0200 @@ -0,0 +1,5 @@ +user 0,password 0 +"user 1","password 1" +"us""er","pass""word" +us;er,pass;word +"us,er","pass,word"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/resources/csv/comma1.csv Tue Oct 03 13:19:58 2017 +0200 @@ -0,0 +1,5 @@ +user 0,password 0 +"user 1","password 1" +"us""er","pass""word" +us;er,pass;word +"us,er","pass,word" \ No newline at end of file