Skip to content

Commit

Permalink
#340 Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Sep 20, 2023
1 parent ccfe550 commit 50ae8d3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pom-maven-central.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<artifactId>kloadgen</artifactId>

<version>5.6.6</version>
<version>5.6.7</version>

<name>KLoadGen</name>
<description>Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<artifactId>kloadgen</artifactId>

<version>5.6.6</version>
<version>5.6.7</version>

<name>KLoadGen</name>
<description>Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> fieldValueList, final Integer arraySize,
final Map<ConstraintTypeEnum, String> constraints) {
Expand Down
95 changes: 52 additions & 43 deletions src/main/java/com/sngular/kloadgen/randomtool/random/RandomMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> fieldValueList) {
return fieldValueList.get(RandomUtils.nextInt(0, fieldValueList.size())).trim().split(":");
}

private static String[] getMapEntryValueAndRemove(final List<String> 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<String> fieldValueList, final Integer arraySize,
final Map<ConstraintTypeEnum, String> constraints) {
Expand Down Expand Up @@ -135,40 +127,41 @@ private Map<String, Object> generate(
final Map<ConstraintTypeEnum, String> constraints) {
final int size = mapSize > 0 ? mapSize : RandomUtils.nextInt(1, 5);
final Map<String, Object> map = new HashMap<>(size);
final List<String> 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],
Expand All @@ -179,17 +172,33 @@ private Map<String, Object> 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<ConstraintTypeEnum, String> 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<String, Object> generateMapOfMap(
final String type, final Integer mapSize, final Integer innerMapSize, final List<String> fieldValueList, final int valueLength,
final Map<ConstraintTypeEnum, String> constraints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,22 +28,16 @@ private static Stream<Arguments> 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<Arguments> parametersForGenerateMapArrayRandomValueFromList() {
final HashMap<String, List<String>> resultMap = new HashMap<>();
final ArrayList<String> 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)
);
}
Expand All @@ -64,7 +57,7 @@ private static Stream<Arguments> parametersForGenerateMapFixedKeyRandomValue() {
@MethodSource("parametersForGenerateMapRandomValueFromList")
void generateMapRandomValueFromList(
final String fieldType, final Integer valueLength, final List<String> fieldValuesList, final Map<String, Object> expected, final Integer size) {
final Map.Entry<String, Object>[] expectedMap = expected.entrySet().toArray(new Map.Entry[1]);
final Entry<String, Object>[] expectedMap = expected.entrySet().toArray(new Entry[1]);
final Map<String, Object> result =
(Map<String, Object>) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap());
Assertions.assertThat(result).containsExactly(expectedMap);
Expand All @@ -76,19 +69,16 @@ void generateMapArrayRandomValueFromList(
final String fieldType, final Integer valueLength, final List<String> fieldValuesList, final Map<String, List<String>> expected,
final Integer size) {

final Map<String, List<String>> result =
(Map<String, List<String>>) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap());
final Map<String, List<String>> result = (Map<String, List<String>>) 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<String> fieldValuesList, final Integer size) {
final String[] expectedKeys = fieldValuesList.toArray(new String[1]);
final Map<String, Object> result =
(Map<String, Object>) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap());
final Map<String, Object> result = (Map<String, Object>) new RandomMap().generateMap(fieldType, valueLength, fieldValuesList, size, Collections.emptyMap());
Assertions.assertThat(result).containsKeys(expectedKeys).doesNotContainValue(null);
}
}

0 comments on commit 50ae8d3

Please sign in to comment.