Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/org/labkey/test/util/TestDataGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ private Map<String, Object> generateRow()
{
if (!_dataSuppliers.containsKey(columnName))
{
_dataSuppliers.put(columnName, getDefaultDataSupplier(_columns.get(columnName).getRangeURI()));
_dataSuppliers.put(columnName, getDefaultDataSupplier(_columns.get(columnName)));
}

if (_autoGeneratedFields.contains(columnName))
Expand All @@ -425,29 +425,47 @@ private Map<String, Object> generateRow()
return newRow;
}

private Supplier<Object> getDefaultDataSupplier(String columnType)
private Supplier<Object> getDefaultDataSupplier(PropertyDescriptor propertyDescriptor)
{
Map<String, Object> allProperties = Objects.requireNonNullElse(propertyDescriptor.getAllProperties(), Collections.emptyMap());
String columnType = (String) allProperties.get("conceptURI");
if (columnType == null) columnType = propertyDescriptor.getRangeURI();

switch (columnType.substring(columnType.indexOf('#') + 1).toLowerCase())
{
case "string":
return ()-> randomString(20, _excludedChars, _alphaNumericStr ? ALPHANUMERIC_STRING : CHARSET_STRING);
return () -> randomString(20, _excludedChars, _alphaNumericStr ? ALPHANUMERIC_STRING : CHARSET_STRING);
case "int":
return ()-> randomInt(0, 20);
return () -> randomInt(0, 20);
case "float":
return ()-> randomFloat(0, 20);
return () -> randomFloat(0, 20);
case "double":
return ()-> randomDouble(0, 20);
return () -> randomDouble(0, 20);
case "boolean":
return this::randomBoolean;
case "date":
case "datetime":
return ()-> randomDateString(DateUtils.addWeeks(new Date(), -39), new Date());
return () -> randomDateString(DateUtils.addWeeks(new Date(), -39), new Date());
case "time":
return ()->
return () ->
randomInt(0, 23) + ":" + // hour
StringUtils.leftPad(String.valueOf(randomInt(0, 59)), 2, "0") + ":" + // minute
StringUtils.leftPad(String.valueOf(randomInt(0, 59)), 2, "0") + "." + // second
StringUtils.leftPad(String.valueOf(randomInt(0, 999)), 3, "0"); // millisecond
case "textchoice":
List<String> textChoices = ((FieldDefinition) propertyDescriptor)
.getValidators().stream()
.map(v -> {
if (v instanceof FieldDefinition.TextChoiceValidator tcv && !tcv.getValues().isEmpty())
return tcv.getValues();
else
return null;
})
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new IllegalStateException("No choices defined for textChoice field : " + propertyDescriptor.getName()));

return () -> randomChoice(textChoices);
default:
throw new IllegalArgumentException("ColumnType " + columnType + " isn't implemented yet");
}
Expand Down Expand Up @@ -595,7 +613,7 @@ public static String randomFieldName(@NotNull String part, int numStartChars, in
return randomFieldName;
}

public static String randomChoice(List<String> choices)
public static <T> T randomChoice(List<T> choices)
{
return choices.get(randomInt(0, choices.size() - 1));
}
Expand Down