From 50ae8d3408f29e8464ce0a5430b505619dedc4d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Enrique=20Garc=C3=ADa=20Maci=C3=B1eiras?= Date: Wed, 20 Sep 2023 19:16:08 +0200 Subject: [PATCH] #340 Fix test --- pom-maven-central.xml | 2 +- pom.xml | 2 +- .../randomtool/random/RandomArray.java | 4 + .../kloadgen/randomtool/random/RandomMap.java | 95 ++++++++++--------- .../randomtool/random/RandomMapTest.java | 24 ++--- 5 files changed, 65 insertions(+), 62 deletions(-) diff --git a/pom-maven-central.xml b/pom-maven-central.xml index 17769b03..2137a7e0 100644 --- a/pom-maven-central.xml +++ b/pom-maven-central.xml @@ -7,7 +7,7 @@ kloadgen - 5.6.6 + 5.6.7 KLoadGen Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial diff --git a/pom.xml b/pom.xml index 371ea816..b9fad739 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ kloadgen - 5.6.6 + 5.6.7 KLoadGen Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial diff --git a/src/main/java/com/sngular/kloadgen/randomtool/random/RandomArray.java b/src/main/java/com/sngular/kloadgen/randomtool/random/RandomArray.java index f62b7081..a20c0062 100644 --- a/src/main/java/com/sngular/kloadgen/randomtool/random/RandomArray.java +++ b/src/main/java/com/sngular/kloadgen/randomtool/random/RandomArray.java @@ -23,6 +23,10 @@ public RandomArray() { randomObject = new RandomObject(); } + protected static boolean isArray(final String type) { + return type.toLowerCase().endsWith("array"); + } + public final Object generateArray( final String fieldType, final Integer valueLength, final List fieldValueList, final Integer arraySize, final Map constraints) { diff --git a/src/main/java/com/sngular/kloadgen/randomtool/random/RandomMap.java b/src/main/java/com/sngular/kloadgen/randomtool/random/RandomMap.java index fec3fa80..0507aa27 100644 --- a/src/main/java/com/sngular/kloadgen/randomtool/random/RandomMap.java +++ b/src/main/java/com/sngular/kloadgen/randomtool/random/RandomMap.java @@ -6,7 +6,6 @@ package com.sngular.kloadgen.randomtool.random; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -21,24 +20,17 @@ public class RandomMap { private final RandomObject randomObject; + private final RandomArray randomArray; + public RandomMap() { randomObject = new RandomObject(); randomArray = new RandomArray(); } - private final RandomArray randomArray; - private static String[] getMapEntryValue(final List fieldValueList) { return fieldValueList.get(RandomUtils.nextInt(0, fieldValueList.size())).trim().split(":"); } - private static String[] getMapEntryValueAndRemove(final List fieldValueList) { - final int randomAux = RandomUtils.nextInt(0, fieldValueList.size()); - final String[] resultAux = fieldValueList.get(randomAux).trim().split(":"); - fieldValueList.remove(randomAux); - return resultAux; - } - public final Object generateMap( final String fieldType, final Integer mapSize, final List fieldValueList, final Integer arraySize, final Map constraints) { @@ -135,40 +127,41 @@ private Map generate( final Map constraints) { final int size = mapSize > 0 ? mapSize : RandomUtils.nextInt(1, 5); final Map map = new HashMap<>(size); - final List fieldValueListAux = new ArrayList<>(fieldValueList); if (!fieldValueList.isEmpty()) { while (map.size() < Math.min(size, fieldValueList.size())) { - final String[] tempValue = getMapEntryValueAndRemove(fieldValueListAux); + final String[] tempValue = getMapEntryValue(fieldValueList); if (tempValue.length > 1) { - switch (type) { - case ValidTypeConstants.INT: - map.put(tempValue[0], Integer.parseInt(tempValue[1])); - break; - case ValidTypeConstants.LONG: - map.put(tempValue[0], Long.parseLong(tempValue[1])); - break; - case ValidTypeConstants.FLOAT: - map.put(tempValue[0], Float.parseFloat(tempValue[1])); - break; - case ValidTypeConstants.DOUBLE: - map.put(tempValue[0], Double.parseDouble(tempValue[1])); - break; - case ValidTypeConstants.SHORT: - map.put(tempValue[0], Short.parseShort(tempValue[1])); - break; - case ValidTypeConstants.UUID: - map.put(tempValue[0], UUID.fromString(tempValue[1])); - break; - case ValidTypeConstants.STRING_ARRAY: - /* array.addAll() */ - String[] array = tempValue[1].substring(tempValue[1].indexOf("[")).replaceAll("[^a-zA-Z\\s*,\\s*^0-9]", "").split("\\s*,\\s*", -1); - map.put(tempValue[0], randomArray.generateArray(type, array.length, List.of(array), array.length, constraints)); - break; - default: - map.put(tempValue[0], tempValue[1]); - break; + if (RandomArray.isArray(type)) { + final String[] array = tempValue[1].substring(tempValue[1].indexOf("[")).replaceAll("[^a-zA-Z\\s*,\\s*^0-9]", "").split("\\s*,\\s*", -1); + map.put(tempValue[0], List.of(array)); + } else if (isMap(type)) { + final String[] fixMap = tempValue[1].substring(tempValue[1].indexOf("[")).replaceAll("[^a-zA-Z\\s*,\\s*^0-9]", "").split("\\s*,\\s*", -1); + map.put(tempValue[0], generateMap(type, fixMap.length, List.of(fixMap), fixMap.length, constraints)); + } else { + switch (type) { + case ValidTypeConstants.INT: + map.put(tempValue[0], Integer.parseInt(tempValue[1])); + break; + case ValidTypeConstants.LONG: + map.put(tempValue[0], Long.parseLong(tempValue[1])); + break; + case ValidTypeConstants.FLOAT: + map.put(tempValue[0], Float.parseFloat(tempValue[1])); + break; + case ValidTypeConstants.DOUBLE: + map.put(tempValue[0], Double.parseDouble(tempValue[1])); + break; + case ValidTypeConstants.SHORT: + map.put(tempValue[0], Short.parseShort(tempValue[1])); + break; + case ValidTypeConstants.UUID: + map.put(tempValue[0], UUID.fromString(tempValue[1])); + break; + default: + map.put(tempValue[0], tempValue[1]); + break; + } } - } else { map.put( tempValue[0], @@ -179,17 +172,33 @@ private Map generate( } if (map.size() != mapSize) { - for (int i = 0; i <= Math.abs(map.size() - mapSize); i++) { + final int limit = Math.abs(map.size() - mapSize); + for (int i = 0; i < limit; i++) { map.put( (String) randomObject.generateRandom(ValidTypeConstants.STRING, valueLength, Collections.emptyList(), constraints), - randomObject.generateRandom(type, valueLength, Collections.emptyList(), constraints) - ); + generateMapValue(type, valueLength, constraints)); } } return map; } + private Object generateMapValue(final String type, final int valueLength, final Map constraints) { + final Object value; + if (isMap(type)) { + value = generateMap(type, valueLength, Collections.emptyList(), valueLength, constraints); + } else if (RandomArray.isArray(type)) { + value = randomArray.generateArray(type, valueLength, Collections.emptyList(), valueLength, constraints); + } else { + value = randomObject.generateRandom(type, valueLength, Collections.emptyList(), constraints); + } + return value; + } + + private static boolean isMap(final String type) { + return type.toLowerCase().endsWith("map"); + } + private Map generateMapOfMap( final String type, final Integer mapSize, final Integer innerMapSize, final List fieldValueList, final int valueLength, final Map constraints) { diff --git a/src/test/java/com/sngular/kloadgen/randomtool/random/RandomMapTest.java b/src/test/java/com/sngular/kloadgen/randomtool/random/RandomMapTest.java index 2652fd39..686bb88c 100644 --- a/src/test/java/com/sngular/kloadgen/randomtool/random/RandomMapTest.java +++ b/src/test/java/com/sngular/kloadgen/randomtool/random/RandomMapTest.java @@ -6,11 +6,10 @@ package com.sngular.kloadgen.randomtool.random; -import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import java.util.stream.Stream; @@ -29,22 +28,16 @@ private static Stream parametersForGenerateMapRandomValueFromList() { Arguments.of("long-map", 1, Collections.singletonList("testString:1"), Maps.of("testString", 1L), 1), Arguments.of("short-map", 1, Collections.singletonList("testString:1"), Maps.of("testString", (short) 1), 1), Arguments.of("double-map", 1, Collections.singletonList("testString:1.0"), Maps.of("testString", 1.0), 1), - Arguments.of( - "uuid-map", 1, Collections.singletonList("testString:0177f035-e51c-4a46-8b82-5b157371c2a5"), + Arguments.of("uuid-map", 1, Collections.singletonList("testString:0177f035-e51c-4a46-8b82-5b157371c2a5"), Maps.of("testString", UUID.fromString("0177f035-e51c-4a46-8b82-5b157371c2a5")), 1 ) ); } private static Stream parametersForGenerateMapArrayRandomValueFromList() { - final HashMap> resultMap = new HashMap<>(); - final ArrayList resultList = new ArrayList<>(); - resultList.add("testString1"); - resultList.add("testString2"); - resultMap.put("testString", resultList); //The name of this type is due to: SchemaProcessorUtils.getOneDimensionValueType (this remove the last -map) - return Stream.of(Arguments.of("string-array",5, List.of("key1:[value1,value2,value3]", "key2:[valueB1,valueB2]"), + return Stream.of(Arguments.of("string-array", 5, List.of("key1:[value1,value2,value3]", "key2:[valueB1,valueB2]"), Map.of("key1", List.of("value1", "value2", "value3"), "key2", List.of("valueB1", "valueB2")), 1) ); } @@ -64,7 +57,7 @@ private static Stream parametersForGenerateMapFixedKeyRandomValue() { @MethodSource("parametersForGenerateMapRandomValueFromList") void generateMapRandomValueFromList( final String fieldType, final Integer valueLength, final List fieldValuesList, final Map expected, final Integer size) { - final Map.Entry[] expectedMap = expected.entrySet().toArray(new Map.Entry[1]); + final Entry[] expectedMap = expected.entrySet().toArray(new Entry[1]); final Map result = (Map) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap()); Assertions.assertThat(result).containsExactly(expectedMap); @@ -76,19 +69,16 @@ void generateMapArrayRandomValueFromList( final String fieldType, final Integer valueLength, final List fieldValuesList, final Map> expected, final Integer size) { - final Map> result = - (Map>) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap()); + final Map> result = (Map>) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap()); - Assertions.assertThat(result).containsKeys(expected.keySet().toArray(String[]::new)); - Assertions.assertThat(result.values().iterator().next()).isSubsetOf(expected.values().iterator().next()); + Assertions.assertThat(result).hasSize(valueLength).containsAllEntriesOf(expected); } @ParameterizedTest @MethodSource("parametersForGenerateMapFixedKeyRandomValue") void generateMapFixedKeyRandomValue(final String fieldType, final Integer valueLength, final List fieldValuesList, final Integer size) { final String[] expectedKeys = fieldValuesList.toArray(new String[1]); - final Map result = - (Map) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap()); + final Map result = (Map) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap()); Assertions.assertThat(result).containsKeys(expectedKeys).doesNotContainValue(null); } } \ No newline at end of file