diff --git a/json-generator/src/main/java/io/apptik/json/generator/generators/ArrayGenerator.java b/json-generator/src/main/java/io/apptik/json/generator/generators/ArrayGenerator.java index 7adf693..5820630 100644 --- a/json-generator/src/main/java/io/apptik/json/generator/generators/ArrayGenerator.java +++ b/json-generator/src/main/java/io/apptik/json/generator/generators/ArrayGenerator.java @@ -61,7 +61,9 @@ public JsonArray generate() { if (maxItems == null) { maxItems = 500; } - maxItems = minItems + rnd.nextInt(maxItems-minItems); + if(maxItems > 1) { + maxItems = minItems + rnd.nextInt(maxItems - minItems); + } //meant for JSON generator after all, not OutOfMemory generator :) diff --git a/json-generator/src/main/java/io/apptik/json/generator/generators/BooleanGenerator.java b/json-generator/src/main/java/io/apptik/json/generator/generators/BooleanGenerator.java index b36c014..d18b9a0 100644 --- a/json-generator/src/main/java/io/apptik/json/generator/generators/BooleanGenerator.java +++ b/json-generator/src/main/java/io/apptik/json/generator/generators/BooleanGenerator.java @@ -18,10 +18,14 @@ import io.apptik.json.JsonBoolean; import io.apptik.json.JsonElement; +import io.apptik.json.JsonNumber; +import io.apptik.json.JsonString; import io.apptik.json.generator.JsonGeneratorConfig; import io.apptik.json.generator.JsonGenerator; import io.apptik.json.schema.Schema; +import java.util.Random; + public class BooleanGenerator extends JsonGenerator { public BooleanGenerator(Schema schema, JsonGeneratorConfig configuration) { @@ -34,6 +38,38 @@ public BooleanGenerator(Schema schema, JsonGeneratorConfig configuration, String @Override public JsonElement generate() { - return new JsonBoolean(rnd.nextBoolean()); + String defaultValue = schema.getDefault(); + + //Check for generic default types + if (schema.getDefaultBoolean() != null) { + return new JsonBoolean(schema.getDefaultBoolean()); + } + if (schema.getConstBoolean() != null) { + return new JsonBoolean(schema.getConstBoolean()); + } + if (schema.getExamples() != null && schema.getExamples().length() > 0) { + return checkIfDefaultIsABoolean(schema.getExamples().get(new Random().nextInt(schema.getExamples().length())).toString()); + } + if (schema.getEnum() != null && schema.getEnum().length() > 0) { + return checkIfDefaultIsABoolean(schema.getEnum().get(new Random().nextInt(schema.getEnum().length() -1)).toString()); + } + + if (defaultValue != null && !defaultValue.isEmpty()) { + if(!defaultValue.equalsIgnoreCase("true") && !defaultValue.equalsIgnoreCase("false")) { + throw new RuntimeException("Default value: " + defaultValue + " of key: " + propertyName + ", is not a boolean type"); + } else { + return (new JsonBoolean(Boolean.parseBoolean(defaultValue))); + } + } else { + return new JsonBoolean(rnd.nextBoolean()); + } + } + + private JsonBoolean checkIfDefaultIsABoolean(String defaultValue) { + if(!defaultValue.equalsIgnoreCase("true") && !defaultValue.equalsIgnoreCase("false")) { + throw new RuntimeException("Default value: " + defaultValue + " of key: " + propertyName + ", is not a boolean type"); + } else { + return (new JsonBoolean(Boolean.parseBoolean(defaultValue))); + } } } diff --git a/json-generator/src/main/java/io/apptik/json/generator/generators/IntegerGenerator.java b/json-generator/src/main/java/io/apptik/json/generator/generators/IntegerGenerator.java index 778d5a5..1d832b1 100644 --- a/json-generator/src/main/java/io/apptik/json/generator/generators/IntegerGenerator.java +++ b/json-generator/src/main/java/io/apptik/json/generator/generators/IntegerGenerator.java @@ -22,6 +22,8 @@ import io.apptik.json.generator.JsonGenerator; import io.apptik.json.schema.Schema; +import java.util.Random; + public class IntegerGenerator extends JsonGenerator { public IntegerGenerator(Schema schema, JsonGeneratorConfig configuration) { super(schema, configuration); @@ -35,6 +37,21 @@ public IntegerGenerator(Schema schema, JsonGeneratorConfig configuration, String public JsonElement generate() { int minValue = 0; int maxValue = Integer.MAX_VALUE; + + //Check for generic default types + if (schema.getDefaultInt() != null) { + return new JsonNumber(schema.getDefaultInt()); + } + if (schema.getConstInt() != null) { + return new JsonNumber(schema.getConstInt()); + } + if (schema.getExamples() != null && schema.getExamples().length() > 0) { + return checkIfDefaultIsAInteger(schema.getExamples().get(new Random().nextInt(schema.getExamples().length())).toString()); + } + if (schema.getEnum() != null && schema.getEnum().length() > 0) { + return checkIfDefaultIsAInteger(schema.getEnum().get(new Random().nextInt(schema.getEnum().length() -1)).toString()); + } + if(configuration!=null) { if (configuration.globalIntegerMin!=null) minValue = configuration.globalIntegerMin; if (configuration.globalIntegerMax!=null) maxValue = configuration.globalIntegerMax; @@ -47,4 +64,12 @@ public JsonElement generate() { return new JsonNumber(minValue + rnd.nextInt(maxValue-minValue)); } + + private JsonNumber checkIfDefaultIsAInteger(String defaultValue) { + try { + return new JsonNumber(Integer.parseInt(defaultValue)); + } catch (NumberFormatException ex) { + throw new RuntimeException("Default value: " + defaultValue + " of key: " + propertyName + ", is not a integer type"); + } + } } diff --git a/json-generator/src/main/java/io/apptik/json/generator/generators/NumberGenerator.java b/json-generator/src/main/java/io/apptik/json/generator/generators/NumberGenerator.java index 871dce5..c503c7f 100644 --- a/json-generator/src/main/java/io/apptik/json/generator/generators/NumberGenerator.java +++ b/json-generator/src/main/java/io/apptik/json/generator/generators/NumberGenerator.java @@ -18,10 +18,13 @@ import io.apptik.json.JsonElement; import io.apptik.json.JsonNumber; +import io.apptik.json.JsonString; import io.apptik.json.generator.JsonGenerator; import io.apptik.json.generator.JsonGeneratorConfig; import io.apptik.json.schema.Schema; +import java.util.Random; + public class NumberGenerator extends JsonGenerator { public NumberGenerator(Schema schema, JsonGeneratorConfig configuration) { super(schema, configuration); @@ -36,6 +39,22 @@ public JsonElement generate() { int minValue = 0; int maxValue = Integer.MAX_VALUE; + String defaultValue = schema.getDefault(); + + //Check for generic default types + if (schema.getDefaultNumber() != null) { + return new JsonNumber(schema.getDefaultNumber()); + } + if (schema.getConstNumber() != null) { + return new JsonNumber(schema.getConstNumber()); + } + if (schema.getExamples() != null && schema.getExamples().length() > 0) { + return checkIfDefaultIsANumber(schema.getExamples().get(new Random().nextInt(schema.getExamples().length())).toString()); + } + if (schema.getEnum() != null && schema.getEnum().length() > 0) { + return checkIfDefaultIsANumber(schema.getEnum().get(new Random().nextInt(schema.getEnum().length() -1)).toString()); + } + if(configuration!=null) { if (configuration.globalNumberMin!=null) minValue = configuration.globalNumberMin; if (configuration.globalNumberMax!=null) maxValue = configuration.globalNumberMax; @@ -48,4 +67,12 @@ public JsonElement generate() { return new JsonNumber(minValue + rnd.nextInt(maxValue-minValue) + Math.abs(rnd.nextDouble())); } + + private JsonNumber checkIfDefaultIsANumber(String defaultValue) { + try { + return new JsonNumber(Double.parseDouble(defaultValue)); + } catch (NumberFormatException ex) { + throw new RuntimeException("Default value: " + defaultValue + " of key: " + propertyName + ", is not a number type"); + } + } } diff --git a/json-generator/src/main/java/io/apptik/json/generator/generators/StringGenerator.java b/json-generator/src/main/java/io/apptik/json/generator/generators/StringGenerator.java index f3dc168..ec4d45b 100644 --- a/json-generator/src/main/java/io/apptik/json/generator/generators/StringGenerator.java +++ b/json-generator/src/main/java/io/apptik/json/generator/generators/StringGenerator.java @@ -27,6 +27,7 @@ import java.lang.reflect.InvocationTargetException; import java.util.Map; +import java.util.Random; import static io.apptik.json.generator.matcher.FormatMatchers.*; @@ -77,13 +78,27 @@ public JsonElement generate() { } } } + + //Check for generic default types + if (schema.getDefault() != null && !schema.getDefault().isEmpty()) { + return new JsonString(schema.getDefault()); + } + if (schema.getConst() != null && !schema.getConst().isEmpty()) { + return new JsonString(schema.getConst()); + } + if (schema.getExamples() != null && schema.getExamples().length() > 0) { + return new JsonString(schema.getExamples().get(new Random().nextInt(schema.getExamples().length())).toString()); + } + if (schema.getEnum() != null && schema.getEnum().length() > 0) { + return new JsonString(schema.getEnum().get(new Random().nextInt(schema.getEnum().length() -1)).toString()); + } + if(configuration!=null) { if (configuration.globalStringLengthMin!=null) minChars = configuration.globalStringLengthMin; if (configuration.globalStringLengthMax!=null) maxChars = configuration.globalStringLengthMax; if (propertyName != null ) { if (configuration.stringLengthMin.get(propertyName)!=null) minChars = configuration.stringLengthMin.get(propertyName); if (configuration.stringLengthMax.get(propertyName)!=null) maxChars = configuration.stringLengthMax.get(propertyName); - if (configuration.stringPredefinedValues.get(propertyName) != null) { return new JsonString(configuration.stringPredefinedValues.get(propertyName).get(rnd.nextInt(configuration.stringPredefinedValues.get(propertyName).size()))); } @@ -92,8 +107,9 @@ public JsonElement generate() { } String res = ""; - int cnt = minChars + rnd.nextInt(maxChars-minChars); - for(int i=0;i