|
| 1 | +package com.retailsvc.http.validate; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | + |
| 6 | +import com.retailsvc.http.ValidationException; |
| 7 | +import com.retailsvc.http.spec.schema.AdditionalProperties; |
| 8 | +import com.retailsvc.http.spec.schema.ObjectSchema; |
| 9 | +import com.retailsvc.http.spec.schema.Schema; |
| 10 | +import com.retailsvc.http.spec.schema.StringSchema; |
| 11 | +import com.retailsvc.http.spec.schema.TypeName; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class ObjectValidationTest { |
| 18 | + private final Validator v = |
| 19 | + new DefaultValidator( |
| 20 | + name -> { |
| 21 | + throw new AssertionError(); |
| 22 | + }); |
| 23 | + |
| 24 | + private ObjectSchema obj( |
| 25 | + Map<String, Schema> props, List<String> required, AdditionalProperties ap) { |
| 26 | + return new ObjectSchema(Set.of(TypeName.OBJECT), props, required, ap, null, null); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void requiredFieldMissing() { |
| 31 | + var s = |
| 32 | + obj( |
| 33 | + Map.of("name", new StringSchema(Set.of(TypeName.STRING), null, null, null, null, null)), |
| 34 | + List.of("name"), |
| 35 | + new AdditionalProperties.Allowed()); |
| 36 | + assertThatThrownBy(() -> v.validate(Map.of(), s, "")) |
| 37 | + .isInstanceOf(ValidationException.class) |
| 38 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 39 | + .isEqualTo("required"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void propertyValidatedAtPointer() { |
| 44 | + var s = |
| 45 | + obj( |
| 46 | + Map.of("name", new StringSchema(Set.of(TypeName.STRING), null, 3, null, null, null)), |
| 47 | + List.of(), |
| 48 | + new AdditionalProperties.Allowed()); |
| 49 | + assertThatThrownBy(() -> v.validate(Map.of("name", "ab"), s, "")) |
| 50 | + .extracting(t -> ((ValidationException) t).error().pointer()) |
| 51 | + .isEqualTo("/name"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void additionalPropertiesAllowedByDefault() { |
| 56 | + var s = obj(Map.of(), List.of(), new AdditionalProperties.Allowed()); |
| 57 | + assertThatCode(() -> v.validate(Map.of("extra", "x"), s, "")).doesNotThrowAnyException(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void additionalPropertiesForbidden() { |
| 62 | + var s = obj(Map.of(), List.of(), new AdditionalProperties.Forbidden()); |
| 63 | + assertThatThrownBy(() -> v.validate(Map.of("extra", "x"), s, "")) |
| 64 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 65 | + .isEqualTo("additionalProperties"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void rejectsNonObject() { |
| 70 | + var s = obj(Map.of(), List.of(), new AdditionalProperties.Allowed()); |
| 71 | + assertThatThrownBy(() -> v.validate("nope", s, "/v")) |
| 72 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 73 | + .isEqualTo("type"); |
| 74 | + } |
| 75 | +} |
0 commit comments