diff --git a/src/org/labkey/test/util/TestDataGenerator.java b/src/org/labkey/test/util/TestDataGenerator.java index c4a031c157..1128cce0bc 100644 --- a/src/org/labkey/test/util/TestDataGenerator.java +++ b/src/org/labkey/test/util/TestDataGenerator.java @@ -411,7 +411,7 @@ private Map generateRow() { if (!_dataSuppliers.containsKey(columnName)) { - _dataSuppliers.put(columnName, getDefaultDataSupplier(_columns.get(columnName).getRangeURI())); + _dataSuppliers.put(columnName, getDefaultDataSupplier(_columns.get(columnName))); } if (_autoGeneratedFields.contains(columnName)) @@ -425,29 +425,47 @@ private Map generateRow() return newRow; } - private Supplier getDefaultDataSupplier(String columnType) + private Supplier getDefaultDataSupplier(PropertyDescriptor propertyDescriptor) { + Map 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 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"); } @@ -595,7 +613,7 @@ public static String randomFieldName(@NotNull String part, int numStartChars, in return randomFieldName; } - public static String randomChoice(List choices) + public static T randomChoice(List choices) { return choices.get(randomInt(0, choices.size() - 1)); }