Mercurial > stress-tester
changeset 594:744c817abd6d
CredentialsProviderFactory - configuration
author | Devel 1 |
---|---|
date | Wed, 04 Oct 2017 08:53:54 +0200 |
parents | 2f8dfda084fe |
children | 6df4fe4ddd7e |
files | stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersConfigurator.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpBasicAuthLoginFilterTest.java |
diffstat | 3 files changed, 87 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java Tue Oct 03 15:17:58 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/credentials/CsvUsernamePasswordCredentialsProvider.java Wed Oct 04 08:53:54 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; @@ -21,6 +28,7 @@ * @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 { @@ -101,17 +109,33 @@ @Override public void configure(Configuration config) { - // TODO: move conversion to NodeDefCreator fileName = config.getString("fileName"); - readMode = (ReadMode) config.get("readMode", ReadMode.STEP_BY_STEP); + readMode = convertReadMode(config.get("readMode")); separator = config.getString("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); @@ -126,4 +150,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(); + } + } }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersConfigurator.java Tue Oct 03 15:17:58 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersConfigurator.java Wed Oct 04 08:53:54 2017 +0200 @@ -46,6 +46,7 @@ public static List<HttpFilter> getFilters(Configuration config, Errors errors) { SCHEMA.validate(config, errors); + // TODO: wywala się, bo robi transform mimo błędów Configuration tConfig = SCHEMA.transform(config, errors); if (errors.getErrorCount() == 0) { List<HttpFilter> filters = new ArrayList<>();
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpBasicAuthLoginFilterTest.java Tue Oct 03 15:17:58 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpBasicAuthLoginFilterTest.java Wed Oct 04 08:53:54 2017 +0200 @@ -1,5 +1,6 @@ package com.passus.st.client.http.filter; +import com.passus.commons.utils.ResourceUtils; import com.passus.config.ConfigurationImpl; import com.passus.config.validation.Errors; import com.passus.net.http.HttpHeaders; @@ -10,6 +11,8 @@ import com.passus.st.AppUtils; import com.passus.st.client.credentials.Credentials; import com.passus.st.client.credentials.CredentialsProvider; +import com.passus.st.client.credentials.CsvUsernamePasswordCredentialsProvider; +import com.passus.st.client.credentials.CsvUsernamePasswordCredentialsProvider.ReadMode; import com.passus.st.client.credentials.MultiCredentialsProvider; import com.passus.st.client.credentials.UsernamePasswordCredentialsProvider; import static com.passus.st.client.http.HttpConsts.PARAM_USERNAME; @@ -18,6 +21,7 @@ import static com.passus.st.client.http.filter.HttpBasicAuthLoginFilter.decodeUsername; import static com.passus.st.client.http.filter.HttpBasicAuthLoginFilter.encode; import com.passus.st.utils.TestHttpUtils; +import java.io.File; import java.util.List; import static org.testng.Assert.*; import org.testng.annotations.AfterClass; @@ -122,22 +126,37 @@ + " provider:\n" + " UsernamePassword:\n" + " username: login\n" - + " password: pass"; + + " password: pass\n" + + " CsvUsernamePassword:\n" + + " fileName: %s\n" + + " separator: ';'\n" + + " readMode: random\n"; + File file = ResourceUtils.getFile("csv/comma.csv"); + filterConfig = String.format(filterConfig, file.getAbsolutePath()); Errors errors = new Errors(); List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors); + HttpFilterTestUtils.printErrors(errors); - assertEquals(0, errors.getErrorCount()); - assertEquals(1, filters.size()); + assertEquals(errors.getErrorCount(), 0); + assertEquals(filters.size(), 1); assertTrue(filters.get(0) instanceof HttpBasicAuthLoginFilter); HttpBasicAuthLoginFilter filter = (HttpBasicAuthLoginFilter) filters.get(0); MultiCredentialsProvider multiCredentialsProvider = (MultiCredentialsProvider) filter.getCredentialsProvider(); - assertTrue(multiCredentialsProvider.getProviders().get(0) instanceof UsernamePasswordCredentialsProvider); + List<CredentialsProvider> providers = multiCredentialsProvider.getProviders(); + assertEquals(providers.size(), 2); - UsernamePasswordCredentialsProvider provider = (UsernamePasswordCredentialsProvider) multiCredentialsProvider.getProviders().get(0); - assertEquals("login", provider.getUsername()); - assertEquals("pass", provider.getPassword()); + assertTrue(providers.get(0) instanceof UsernamePasswordCredentialsProvider); + UsernamePasswordCredentialsProvider upcp = (UsernamePasswordCredentialsProvider) providers.get(0); + assertEquals(upcp.getUsername(), "login"); + assertEquals(upcp.getPassword(), "pass"); + + assertTrue(providers.get(1) instanceof CsvUsernamePasswordCredentialsProvider); + CsvUsernamePasswordCredentialsProvider cupcp = (CsvUsernamePasswordCredentialsProvider) providers.get(1); + assertEquals(cupcp.getFileName(), file.getAbsolutePath()); + assertEquals(cupcp.getReadMode(), ReadMode.RANDOM); + assertEquals(cupcp.getSeparator(), ';'); HttpRequest req = HttpRequestBuilder.get("http://test.com/html/basic/index.html") .header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==")