Mercurial > stress-tester
changeset 939:bcda60787f9e
ArrayValueGenerator finished
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/generator/ArrayValueGenerator.java Tue Jun 05 15:19:58 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/generator/ArrayValueGenerator.java Wed Jun 06 10:02:06 2018 +0200 @@ -21,10 +21,18 @@ public static final String TYPE = "arrayValue"; + public enum SelectorType { + RANDOM, SEQUENCE + } + + private static final SelectorType DEFAULT_SELECTOR_TYPE = SelectorType.RANDOM; + private static final ValueSelector DEFAULT_SELECTOR = new RandomValueSelector(); private Object[] values = new Object[0]; + private SelectorType selectorType = DEFAULT_SELECTOR_TYPE; + private ValueSelector selector = DEFAULT_SELECTOR; @Override @@ -40,19 +48,30 @@ this.values = values; } - public ValueSelector getSelector() { - return selector; + public SelectorType getSelector() { + return selectorType; } - public void setSelector(ValueSelector selector) { - Assert.notNull(selector, "selector"); - this.selector = selector; - selector.reset(); + public void setSelector(SelectorType selectorType) { + Assert.notNull(selectorType, "selector"); + + if (this.selectorType != selectorType) { + switch (this.selectorType) { + case RANDOM: + this.selector = new RandomValueSelector(); + case SEQUENCE: + this.selector = new SequenceSelector(); + } + } else { + this.selector.reset(); + } + + this.selectorType = selectorType; } @Override public void configure(Configuration config, ConfigurationContext context) { - setSelector(config.get("selector", DEFAULT_SELECTOR)); + setSelector(config.get("selector", DEFAULT_SELECTOR_TYPE)); List valuesList = config.getList("values", Collections.EMPTY_LIST); setValues(valuesList.toArray(new Object[]{})); } @@ -66,28 +85,20 @@ return selector.select(values); } - - public static final class RandomArrayValueGeneratorDefCreator implements NodeDefinitionCreator { + public interface ValueSelector { - @Override - public NodeDefinition create() { - return mapDef( - tupleDef("values", listDef()).setRequired(false) - ).setCanBeEmpty(true); - } - } + void reset(); - - public static interface ValueSelector { - - public void reset(); - - public Object select(Object[] values); + Object select(Object[] values); } public static final class RandomValueSelector implements ValueSelector { + private RandomValueSelector() { + + } + @Override public void reset() { @@ -100,7 +111,7 @@ } } - public static final class NextValueSelector implements ValueSelector { + public static final class SequenceSelector implements ValueSelector { private int lastIndex = 0; @@ -119,4 +130,16 @@ return out; } } + + public static final class RandomArrayValueGeneratorDefCreator implements NodeDefinitionCreator { + + @Override + public NodeDefinition create() { + return mapDef( + tupleDef("values", listDef()).setRequired(false), + tupleDef("selector", enumDef(ArrayValueGenerator.SelectorType.class)).setRequired(false) + ).setCanBeEmpty(true); + } + } + }
--- a/stress-tester/src/main/java/com/passus/st/generator/GeneratorValueExtractorTransformer.java Tue Jun 05 15:19:58 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/generator/GeneratorValueExtractorTransformer.java Wed Jun 06 10:02:06 2018 +0200 @@ -47,17 +47,17 @@ String attrName = tuple.getName(); switch (attrName.toLowerCase()) { - case "name": - String genName = ConfigurationUtils.extractString(valueNode); - if (genName == null) { - errors.reject(valueNode, "Value of attribute 'name' cannot be null."); + case "type": + String genType = ConfigurationUtils.extractString(valueNode); + if (genType == null) { + errors.reject(valueNode, "Value of attribute 'type' cannot be null."); return null; - } else if (!pluginFactory.containsName(genName)) { - errors.reject(valueNode, "Unknown generator '" + genName + "'."); + } else if (!pluginFactory.containsName(genType)) { + errors.reject(valueNode, "Unknown generator '" + genType + "'."); return null; } - plugin = pluginFactory.get(genName); + plugin = pluginFactory.get(genType); break; default: configNodes.add(tuple); @@ -65,7 +65,7 @@ } if (plugin == null) { - errors.reject(node, "Attribute 'name' required."); + errors.reject(node, "Attribute 'type' required."); } else { final Class<?> pluginClass = plugin.getPluginClass(); NodeDefinitionCreate nodeDefCreate = pluginClass.getAnnotation(NodeDefinitionCreate.class);
--- a/stress-tester/src/main/java/com/passus/st/generator/RandomStringGenerator.java Tue Jun 05 15:19:58 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/generator/RandomStringGenerator.java Wed Jun 06 10:02:06 2018 +0200 @@ -82,7 +82,7 @@ @Override public void configure(Configuration config, ConfigurationContext context) { - this.setCharactersSet((CharactersSet) config.get("charactersSet", DEFAULT_CHARACTERS_SET)); + this.setCharactersSet(config.get("charactersSet", DEFAULT_CHARACTERS_SET)); this.setMinLength(config.getInteger("minLength", DEFAULT_MIN_LENGTH)); this.setMaxLength(config.getInteger("maxLength", DEFAULT_MAX_LENGTH)); @@ -106,23 +106,21 @@ @Override public Object generate() { - int count = randCount(); - switch (charactersSet) { case ASCII: - return RandomStringUtils.randomAscii(count); + return RandomStringUtils.randomAscii(randCount()); case LETTERS: - return RandomStringUtils.randomAlphabetic(count); + return RandomStringUtils.randomAlphabetic(randCount()); case NUMBERS: - return RandomStringUtils.randomNumeric(count); + return RandomStringUtils.randomNumeric(randCount()); case LETTERS_NUMBERS: - return RandomStringUtils.randomAlphanumeric(count); + return RandomStringUtils.randomAlphanumeric(randCount()); case CUSTOM: if (chars == null) { return StringUtils.EMPTY; } - return RandomStringUtils.random(count, 0, 0, true, true, chars); + return RandomStringUtils.random(randCount(), 0, 0, true, true, chars); } return StringUtils.EMPTY;
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java Tue Jun 05 15:19:58 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java Wed Jun 06 10:02:06 2018 +0200 @@ -317,7 +317,7 @@ public void testTransform_generators() throws Exception { String config = "operations:\n" + " setHeader:\n" - + " H2: {$gen: {name: 'randomString', minLength: 10, maxLength: 10}}\n"; + + " H2: {$gen: {type: 'randomString', minLength: 10, maxLength: 10}}\n"; CTupleNode node = read(config);
--- a/stress-tester/src/test/java/com/passus/st/generator/ArrayValueGeneratorTest.java Tue Jun 05 15:19:58 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/generator/ArrayValueGeneratorTest.java Wed Jun 06 10:02:06 2018 +0200 @@ -32,10 +32,10 @@ } @Test - public void testGenerate_NextValueSelector() { + public void testGenerate_SequenceSelector() { List<String> values = Arrays.asList(new String[]{"1", "2", "3", "4", "5", "6"}); ArrayValueGenerator generator = new ArrayValueGenerator(); - generator.setSelector(new ArrayValueGenerator.NextValueSelector()); + generator.setSelector(ArrayValueGenerator.SelectorType.SEQUENCE); generator.setValues(values.toArray()); for (int i = 0; i < 5; i++) { @@ -53,7 +53,7 @@ @Test public void testConfigure() throws Exception { - String configStr = "{name: 'arrayValue', values: [1,2,3,'4',5,6]}"; + String configStr = "{type: 'arrayValue', values: [1,2,3,'4',5,6]}"; ArrayValueGenerator generator = transformToGenerator(configStr); Object[] expected = new Object[]{"1", "2", "3", "4", "5", "6"}; Object[] values = generator.getValues();
--- a/stress-tester/src/test/java/com/passus/st/generator/RandomNumberGeneratorTest.java Tue Jun 05 15:19:58 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/generator/RandomNumberGeneratorTest.java Wed Jun 06 10:02:06 2018 +0200 @@ -22,7 +22,7 @@ @Test public void testConfigure() throws Exception { - String configStr = "{name: 'randomNumber', minValue: 5.2, maxValue: 5, returnType: LONG}"; + String configStr = "{type: 'randomNumber', minValue: 5.2, maxValue: 5, returnType: long}"; RandomNumberGenerator generator = transformToGenerator(configStr); assertEquals(RandomNumberGenerator.ReturnType.LONG, generator.getReturnType());
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/generator/RandomStringGeneratorTest.java Wed Jun 06 10:02:06 2018 +0200 @@ -0,0 +1,43 @@ +package com.passus.st.generator; + +import org.testng.annotations.Test; + +import static org.testng.AssertJUnit.*; + +public class RandomStringGeneratorTest { + + @Test + public void testGenerate_NumbersCharactersSet() { + RandomStringGenerator gen = new RandomStringGenerator(); + gen.setCharactersSet(RandomStringGenerator.CharactersSet.NUMBERS); + gen.setMinLength(1); + gen.setMaxLength(10); + + for (int i = 0; i < 100; i++) { + String res = (String) gen.generate(); + assertTrue(res.length() >= 1 && res.length() <= 10); + + try { + Integer.parseInt(res); + } catch (NumberFormatException e) { + fail("Invalid number '" + res + "'."); + } + } + } + + @Test + public void testGenerate_CustomCharactersSet() { + String chars = "abc"; + RandomStringGenerator gen = new RandomStringGenerator(); + gen.setCharactersSet(RandomStringGenerator.CharactersSet.CUSTOM); + gen.setChars(chars.toCharArray()); + gen.setMinLength(1); + gen.setMaxLength(1); + + for (int i = 0; i < 100; i++) { + String res = (String) gen.generate(); + assertEquals(1, res.length()); + assertTrue(chars.indexOf(res) >= 0); + } + } +} \ No newline at end of file