|
| 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.ArraySchema; |
| 8 | +import com.retailsvc.http.spec.schema.BooleanSchema; |
| 9 | +import com.retailsvc.http.spec.schema.IntegerSchema; |
| 10 | +import com.retailsvc.http.spec.schema.Schema; |
| 11 | +import com.retailsvc.http.spec.schema.TypeName; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +class ArrayValidationTest { |
| 17 | + private final Validator v = |
| 18 | + new DefaultValidator( |
| 19 | + name -> { |
| 20 | + throw new AssertionError(); |
| 21 | + }); |
| 22 | + |
| 23 | + private ArraySchema arr(Schema item, Integer minI, Integer maxI, boolean unique) { |
| 24 | + return new ArraySchema(Set.of(TypeName.ARRAY), item, minI, maxI, unique); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void itemsValidated() { |
| 29 | + var s = |
| 30 | + arr( |
| 31 | + new IntegerSchema(Set.of(TypeName.INTEGER), 0L, 100L, null, null, null, "int32"), |
| 32 | + null, |
| 33 | + null, |
| 34 | + false); |
| 35 | + assertThatCode(() -> v.validate(List.of(1, 2, 3), s, "")).doesNotThrowAnyException(); |
| 36 | + assertThatThrownBy(() -> v.validate(List.of(1, -1), s, "")) |
| 37 | + .extracting(t -> ((ValidationException) t).error().pointer()) |
| 38 | + .isEqualTo("/1"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void minItemsEnforced() { |
| 43 | + var s = arr(new BooleanSchema(Set.of(TypeName.BOOLEAN)), 2, null, false); |
| 44 | + assertThatThrownBy(() -> v.validate(List.of(true), s, "")) |
| 45 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 46 | + .isEqualTo("minItems"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void maxItemsEnforced() { |
| 51 | + var s = arr(new BooleanSchema(Set.of(TypeName.BOOLEAN)), null, 1, false); |
| 52 | + assertThatThrownBy(() -> v.validate(List.of(true, false), s, "")) |
| 53 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 54 | + .isEqualTo("maxItems"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void uniqueItemsEnforced() { |
| 59 | + var s = |
| 60 | + arr( |
| 61 | + new IntegerSchema(Set.of(TypeName.INTEGER), null, null, null, null, null, "int32"), |
| 62 | + null, |
| 63 | + null, |
| 64 | + true); |
| 65 | + assertThatThrownBy(() -> v.validate(List.of(1, 2, 1), s, "")) |
| 66 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 67 | + .isEqualTo("uniqueItems"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void rejectsNonIterable() { |
| 72 | + var s = arr(new BooleanSchema(Set.of(TypeName.BOOLEAN)), null, null, false); |
| 73 | + assertThatThrownBy(() -> v.validate("nope", s, "/v")) |
| 74 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 75 | + .isEqualTo("type"); |
| 76 | + } |
| 77 | +} |
0 commit comments