Skip to content

Commit

Permalink
annotation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elifKurtay committed Nov 21, 2024
1 parent 35f820f commit 3a4ae77
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ public static List<AnnotationDef> getAnnotations(Schema schema, TypeDef property
((int) value) - EXCLUSIVE_DELTA_INT)
.build());
}
if (schema.getMaxLength() != null || schema.getMaxItems() != null) {
if (schema.getMaxLength() != null || schema.getMaxItems() != null || schema.getMaxContains() != null) {
var value = schema.getMaxLength() != null ? schema.getMaxLength() : schema.getMaxItems();
value = value == null ? schema.getMaxContains() : value;
annotations.add(AnnotationDef
.builder(ClassTypeDef.of(SIZE_ANN))
.addMember("max", value).build());
}
if (schema.getMinLength() != null || schema.getMinItems() != null) {
if (schema.getMinLength() != null || schema.getMinItems() != null || schema.getMinContains() != null) {
var value = schema.getMinLength() != null ? schema.getMinLength() : schema.getMinItems();
value = value == null ? schema.getMinContains() : value;
annotations.add(AnnotationDef
.builder(ClassTypeDef.of(SIZE_ANN))
.addMember("min", value).build());
Expand All @@ -126,7 +128,7 @@ public static List<AnnotationDef> getAnnotations(Schema schema, TypeDef property
.builder(ClassTypeDef.of(MIN_ANN))
.addMember("value", 1)
.build());
case "^\\d*\\.?\\d+$" -> // positive decimal
case "^\\d*.?\\d+$", "^[1-9][0-9]*.?[0-9]+$" -> // positive decimal
annotations.add(AnnotationDef
.builder(ClassTypeDef.of(DECIMAL_MIN_ANN))
.addMember("value", "" + EXCLUSIVE_DELTA_DOUBLE)
Expand All @@ -136,17 +138,17 @@ public static List<AnnotationDef> getAnnotations(Schema schema, TypeDef property
.builder(ClassTypeDef.of(MIN_ANN))
.addMember("value", 0)
.build());
case "^-\\d+$", "^-[1-9][0-9]*$" -> // negative int
case "^-d+$", "^-[1-9][0-9]*$" -> // negative int
annotations.add(AnnotationDef
.builder(ClassTypeDef.of(MAX_ANN))
.addMember("value", 0)
.build());
case "^-\\d*\\.?\\d+$", "^-[1-9][0-9]*\\.?[0-9]+$" -> // negative decimal
case "^-d*.?d+$", "^-[1-9][0-9]*.?[0-9]+$" -> // negative decimal
annotations.add(AnnotationDef
.builder(ClassTypeDef.of(DECIMAL_MAX_ANN))
.addMember("value", "" + (0.0 - EXCLUSIVE_DELTA_DOUBLE))
.build());
case "^(-\\d+(\\.\\d+)?|0(\\.0+)?)$", "^-?(0|[1-9][0-9]{0,17})(\\.[0-9]{1,17})?([eE][+-]?[0-9]{1,9}})?$" -> // negative or zero
case "^(-d+(.d+)?|0(.0+)?)$", "^-?(0|[1-9][0-9]{0,17})(.[0-9]{1,17})?([eE][+-]?[0-9]{1,9}})?$" -> // negative or zero
annotations.add(AnnotationDef
.builder(ClassTypeDef.of(MAX_ANN))
.addMember("value", 0)
Expand All @@ -160,10 +162,10 @@ public static List<AnnotationDef> getAnnotations(Schema schema, TypeDef property
if (schema.getFormat() != null && schema.getFormat().equals("email")) {
annotations.add(AnnotationDef.builder(ClassTypeDef.of(EMAIL_ANN)).build());
}
if (schema.getConstValue() != null) {
if (schema.getConstValue().equals("true")) {
if (schema.getConstValue() != null && propertyType.equals(TypeDef.Primitive.BOOLEAN)) {
if (schema.getConstValue().toString().equals("true")) {
annotations.add(AnnotationDef.builder(ClassTypeDef.of(ASSERT_TRUE_ANN)).build());
} else if (schema.getConstValue().equals("false")) {
} else if (schema.getConstValue().toString().equals("false")) {
annotations.add(AnnotationDef.builder(ClassTypeDef.of(ASSERT_FALSE_ANN)).build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class SimpleGeneratorSpec extends AbstractGeneratorSpec {
// https://json-schema.org/understanding-json-schema/reference/numeric
'integer' | '{"type": "integer"}' | 'int integer'
'test' | '{"type": "number"}' | "float test"
'test' | '{"type": "number", "pattern": "^[0]|[-+]?[1-9][0-9]*$"}' | "Integer test"
'test' | '{"type": "number", "pattern": "^[0]|[-+]?[1-9][0-9]*.?[0-9]+$"}' | "Float test"
// https://json-schema.org/understanding-json-schema/reference/array
'array' | '{"type": "array", "items": {"type": "string"}}' | "List<String> array"
'array' | '{"type": "array", "uniqueItems": true, "items": {"type": "string"}}' | "Set<String> array"
Expand Down Expand Up @@ -222,12 +224,29 @@ class SimpleGeneratorSpec extends AbstractGeneratorSpec {

where:
propertyName | propertySchema | expectedJava
// TODO fill in more test cases
'test' | '{"type": "number", "minimum": 10}' | "@DecimalMin(\"10\") float test"
'test' | '{"type": "number", "maximum": 10}' | "@DecimalMax(\"10\") float test"
'test' | '{"type": "number", "exclusiveMaximum": 10.0}' | "@DecimalMax(\"9.999\") float test"
'test' | '{"type": "number", "exclusiveMinimum": 10.0}' | "@DecimalMin(\"10.001\") float test"
'test' | '{"type": "number", "pattern": "^[1-9][0-9]*$"}' | "@Min(1) Integer test"
'test' | '{"type": "number", "pattern": "^[1-9][0-9]*.?[0-9]+$"}' | "@DecimalMin(\"0.001\") Float test"
'test' | '{"type": "number", "pattern": "^[0]|([1-9][0-9]*)$"}' | "@Min(0) Integer test"
'test' | '{"type": "number", "pattern": "^-d+$"}' | "@Max(0) Integer test"
'test' | '{"type": "number", "pattern": "^[0]|[-+]?[1-9][0-9]*$"}' | "Integer test"
'test' | '{"type": "string", "pattern": "[A-Z]+"}' | "@Pattern(regexp = \"[A-Z]+\") String test"
'test' | '{"type": "boolean", "const": true}' | "@AssertTrue boolean test"
'test' | '{"type": "boolean", "const": false}' | "@AssertFalse boolean test"
// array annotations
'array' |'{"type": "array", "items": {"type": "number", "minimum": 10.0}}'| "List<@DecimalMin(\"10.0\") Float> array"
'arrayMulti' |'{"type": "array", "items": {"type": "array", ' +
'"items": {"type": "number", "minimum": 10.0}, ' +
'"uniqueItems": true, "minItems": 2}, "minItems": 1}' | "@Size(min = 1) List<@Size(min = 2) Set<@DecimalMin(\"10.0\") Float>> arrayMulti"
'array' |'{"type": "array", "contains": {"type": "number"}, ' +
'"minContains": 2, "maxContains": 3}' | "@Size(max = 3) @Size(min = 2) List<Float> array"
'array' |'{"type": "array", "items": {"type": "number"}, ' +
'"minLength": 2, "maxLength": 3}' | "@Size(max = 3) @Size(min = 2) List<Float> array"
'array' |'{"type": "array", "items": {"type":"number"},"nullable": true}' | "@Nullable List<Float> array"
'array' |'{"type": "array", "items": {"type":"number"},"nullable": false}'| "@NotNull List<Float> array"
}

}
3 changes: 2 additions & 1 deletion test-suite-generator/src/test/resources/animal.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
},
"hasMate": {
"description": "True when the dog has found their mate.",
"$ref": "#/definitions/boolean"
"$ref": "#/definitions/boolean",
"const": true
},
"nickname" : {
"description": "A nickname of a good doggo.",
Expand Down

0 comments on commit 3a4ae77

Please sign in to comment.