Mercurial > stress-tester
changeset 938:fe4efd63b757
ArrayValueGenerator in progress
author | Devel 2 |
---|---|
date | Tue, 05 Jun 2018 15:19:58 +0200 |
parents | 6d35ff7ba9c0 |
children | bcda60787f9e |
files | stress-tester/src/main/java/com/passus/st/filter/Transformers.java stress-tester/src/main/java/com/passus/st/generator/ArrayValueGenerator.java stress-tester/src/test/java/com/passus/st/generator/ArrayValueGeneratorTest.java stress-tester/src/test/java/com/passus/st/generator/GeneratorUtils.java stress-tester/src/test/java/com/passus/st/generator/RandomNumberGeneratorTest.java |
diffstat | 5 files changed, 214 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/filter/Transformers.java Tue Jun 05 14:11:32 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/filter/Transformers.java Tue Jun 05 15:19:58 2018 +0200 @@ -79,13 +79,6 @@ } public static GeneratorValueExtractorTransformer generatorTransformer(ConfigurationContext context) { - List<Lookup> lookups; - if (context == null) { - lookups = Collections.EMPTY_LIST; - } else { - lookups = context.get(ConfigurationContextConsts.GENERATORS, Collections.EMPTY_LIST); - } - return new GeneratorValueExtractorTransformer(); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/generator/ArrayValueGenerator.java Tue Jun 05 15:19:58 2018 +0200 @@ -0,0 +1,122 @@ +package com.passus.st.generator; + +import com.passus.commons.Assert; +import com.passus.commons.annotations.Plugin; +import com.passus.config.Configuration; +import com.passus.config.ConfigurationContext; +import com.passus.config.annotations.NodeDefinitionCreate; +import com.passus.config.schema.NodeDefinition; +import com.passus.config.schema.NodeDefinitionCreator; +import com.passus.st.plugin.PluginConstants; +import org.apache.commons.lang3.RandomUtils; + +import java.util.Collections; +import java.util.List; + +import static com.passus.config.schema.ConfigurationSchemaBuilder.*; + +@NodeDefinitionCreate(ArrayValueGenerator.RandomArrayValueGeneratorDefCreator.class) +@Plugin(name = ArrayValueGenerator.TYPE, category = PluginConstants.CATEGORY_GENERATOR) +public class ArrayValueGenerator implements Generator { + + public static final String TYPE = "arrayValue"; + + private static final ValueSelector DEFAULT_SELECTOR = new RandomValueSelector(); + + private Object[] values = new Object[0]; + + private ValueSelector selector = DEFAULT_SELECTOR; + + @Override + public String getType() { + return TYPE; + } + + public Object[] getValues() { + return values; + } + + public void setValues(Object[] values) { + this.values = values; + } + + public ValueSelector getSelector() { + return selector; + } + + public void setSelector(ValueSelector selector) { + Assert.notNull(selector, "selector"); + this.selector = selector; + selector.reset(); + } + + @Override + public void configure(Configuration config, ConfigurationContext context) { + setSelector(config.get("selector", DEFAULT_SELECTOR)); + List valuesList = config.getList("values", Collections.EMPTY_LIST); + setValues(valuesList.toArray(new Object[]{})); + } + + @Override + public Object generate() { + if (values == null || values.length == 0) { + return null; + } + + return selector.select(values); + } + + + public static final class RandomArrayValueGeneratorDefCreator implements NodeDefinitionCreator { + + @Override + public NodeDefinition create() { + return mapDef( + tupleDef("values", listDef()).setRequired(false) + ).setCanBeEmpty(true); + } + } + + + public static interface ValueSelector { + + public void reset(); + + public Object select(Object[] values); + + } + + public static final class RandomValueSelector implements ValueSelector { + + @Override + public void reset() { + + } + + @Override + public Object select(Object[] values) { + int index = RandomUtils.nextInt(0, values.length); + return values[index]; + } + } + + public static final class NextValueSelector implements ValueSelector { + + private int lastIndex = 0; + + @Override + public void reset() { + lastIndex = 0; + } + + @Override + public Object select(Object[] values) { + Object out = values[lastIndex++]; + if (values.length == lastIndex) { + lastIndex = 0; + } + + return out; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/generator/ArrayValueGeneratorTest.java Tue Jun 05 15:19:58 2018 +0200 @@ -0,0 +1,63 @@ +package com.passus.st.generator; + +import org.apache.commons.lang3.ArrayUtils; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static com.passus.st.generator.GeneratorUtils.transformToGenerator; +import static org.testng.AssertJUnit.*; + +public class ArrayValueGeneratorTest { + + @Test + public void testGenerate_RandomValueSelector() { + List<String> values = Arrays.asList(new String[]{"1", "2", "3", "4", "5", "6"}); + ArrayValueGenerator generator = new ArrayValueGenerator(); + generator.setValues(values.toArray()); + + for (int i = 0; i < 5; i++) { + List<String> result = new ArrayList<>(); + for (int j = 0; j < values.size(); j++) { + String res = (String) generator.generate(); + assertTrue(values.contains(res)); + result.add(res); + } + + assertFalse(values.equals(result)); + result.clear(); + } + } + + @Test + public void testGenerate_NextValueSelector() { + List<String> values = Arrays.asList(new String[]{"1", "2", "3", "4", "5", "6"}); + ArrayValueGenerator generator = new ArrayValueGenerator(); + generator.setSelector(new ArrayValueGenerator.NextValueSelector()); + generator.setValues(values.toArray()); + + for (int i = 0; i < 5; i++) { + List<String> result = new ArrayList<>(); + for (int j = 0; j < values.size(); j++) { + String res = (String) generator.generate(); + result.add(res); + assertEquals(values.get(j), res); + } + + assertTrue(values.equals(result)); + result.clear(); + } + } + + @Test + public void testConfigure() throws Exception { + String configStr = "{name: '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(); + assertEquals(6, values.length); + assertTrue(ArrayUtils.contains(expected, generator.generate())); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/generator/GeneratorUtils.java Tue Jun 05 15:19:58 2018 +0200 @@ -0,0 +1,28 @@ +package com.passus.st.generator; + +import com.passus.config.Configuration; +import com.passus.config.ConfigurationContext; +import com.passus.config.ConfigurationContextImpl; +import com.passus.config.YamlConfigurationReader; +import com.passus.config.validation.Errors; + +public class GeneratorUtils { + + private GeneratorUtils() { + } + + public static <T extends Generator> T transformToGenerator(String configStr) throws Exception { + GeneratorValueExtractorTransformer transformer = new GeneratorValueExtractorTransformer(); + Configuration config = YamlConfigurationReader.readFromString(configStr); + + Errors errors = new Errors(); + ConfigurationContext context = new ConfigurationContextImpl(); + T generator = (T) transformer.transformToGenerator(config.getRootNode(), errors, context); + if (errors.hasError()) { + throw new Exception(errors.getAllErrors().get(0).toString()); + } + + return generator; + } + +}
--- a/stress-tester/src/test/java/com/passus/st/generator/RandomNumberGeneratorTest.java Tue Jun 05 14:11:32 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/generator/RandomNumberGeneratorTest.java Tue Jun 05 15:19:58 2018 +0200 @@ -1,31 +1,13 @@ package com.passus.st.generator; -import com.passus.config.Configuration; -import com.passus.config.ConfigurationContext; -import com.passus.config.ConfigurationContextImpl; -import com.passus.config.YamlConfigurationReader; -import com.passus.config.validation.Errors; import org.testng.annotations.Test; +import static com.passus.st.generator.GeneratorUtils.transformToGenerator; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertTrue; public class RandomNumberGeneratorTest { - private RandomNumberGenerator transformToGenerator(String configStr) throws Exception { - GeneratorValueExtractorTransformer transformer = new GeneratorValueExtractorTransformer(); - Configuration config = YamlConfigurationReader.readFromString(configStr); - - Errors errors = new Errors(); - ConfigurationContext context = new ConfigurationContextImpl(); - RandomNumberGenerator generator = (RandomNumberGenerator) transformer.transformToGenerator(config.getRootNode(), errors, context); - if (errors.hasError()) { - throw new Exception(errors.getAllErrors().get(0).toString()); - } - - return generator; - } - @Test public void testGenerate_Integer() { RandomNumberGenerator gen = new RandomNumberGenerator(); @@ -38,7 +20,6 @@ assertEquals(5, res); } - @Test public void testConfigure() throws Exception { String configStr = "{name: 'randomNumber', minValue: 5.2, maxValue: 5, returnType: LONG}";