|
3 | 3 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
4 | 4 |
|
5 | 5 | import com.retailsvc.http.ValidationException; |
| 6 | +import com.retailsvc.http.spec.schema.AllOfSchema; |
| 7 | +import com.retailsvc.http.spec.schema.AnyOfSchema; |
6 | 8 | import com.retailsvc.http.spec.schema.BooleanSchema; |
| 9 | +import com.retailsvc.http.spec.schema.NotSchema; |
7 | 10 | import com.retailsvc.http.spec.schema.NullSchema; |
8 | 11 | import com.retailsvc.http.spec.schema.OneOfSchema; |
| 12 | +import com.retailsvc.http.spec.schema.StringSchema; |
9 | 13 | import com.retailsvc.http.spec.schema.TypeName; |
10 | 14 | import java.util.List; |
11 | 15 | import java.util.Set; |
@@ -43,10 +47,86 @@ void booleanSchemaRejectsString() { |
43 | 47 | assertThatThrownBy(() -> v.validate("x", schema, "/v")).isInstanceOf(ValidationException.class); |
44 | 48 | } |
45 | 49 |
|
| 50 | + private StringSchema stringSchema(Integer min, Integer max) { |
| 51 | + return new StringSchema(Set.of(TypeName.STRING), null, min, max, null, null); |
| 52 | + } |
| 53 | + |
46 | 54 | @Test |
47 | | - void combinatorThrowsUnsupported() { |
48 | | - var schema = new OneOfSchema(List.of()); |
49 | | - assertThatThrownBy(() -> v.validate("x", schema, "/v")) |
50 | | - .isInstanceOf(UnsupportedOperationException.class); |
| 55 | + void allOfPassesWhenAllBranchesPass() { |
| 56 | + var schema = new AllOfSchema(List.of(stringSchema(1, null), stringSchema(null, 10))); |
| 57 | + v.validate("hello", schema, "/v"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void allOfPropagatesFirstFailingBranch() { |
| 62 | + var schema = new AllOfSchema(List.of(stringSchema(1, null), stringSchema(null, 3))); |
| 63 | + assertThatThrownBy(() -> v.validate("hello", schema, "/v")) |
| 64 | + .isInstanceOf(ValidationException.class) |
| 65 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 66 | + .isEqualTo("maxLength"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void anyOfPassesWhenOneBranchPasses() { |
| 71 | + var schema = new AnyOfSchema(List.of(stringSchema(100, null), stringSchema(null, 10))); |
| 72 | + v.validate("hello", schema, "/v"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void anyOfFailsWhenNoBranchMatches() { |
| 77 | + var schema = new AnyOfSchema(List.of(stringSchema(100, null), stringSchema(null, 2))); |
| 78 | + assertThatThrownBy(() -> v.validate("hello", schema, "/v")) |
| 79 | + .isInstanceOf(ValidationException.class) |
| 80 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 81 | + .isEqualTo("anyOf"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void oneOfPassesWhenExactlyOneBranchMatches() { |
| 86 | + // value "hello" — len 5. branch[0] requires min 100 (fails), branch[1] max 10 (passes). |
| 87 | + var schema = new OneOfSchema(List.of(stringSchema(100, null), stringSchema(null, 10))); |
| 88 | + v.validate("hello", schema, "/v"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void oneOfFailsWhenZeroBranchesMatch() { |
| 93 | + var schema = new OneOfSchema(List.of(stringSchema(100, null), stringSchema(null, 2))); |
| 94 | + assertThatThrownBy(() -> v.validate("hello", schema, "/v")) |
| 95 | + .isInstanceOf(ValidationException.class) |
| 96 | + .satisfies( |
| 97 | + t -> { |
| 98 | + var err = ((ValidationException) t).error(); |
| 99 | + org.assertj.core.api.Assertions.assertThat(err.keyword()).isEqualTo("oneOf"); |
| 100 | + org.assertj.core.api.Assertions.assertThat(err.message()).contains("matched 0 of 2"); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void oneOfFailsWhenTwoBranchesMatch() { |
| 106 | + // value "hello" — both branches accept. |
| 107 | + var schema = new OneOfSchema(List.of(stringSchema(null, 10), stringSchema(1, null))); |
| 108 | + assertThatThrownBy(() -> v.validate("hello", schema, "/v")) |
| 109 | + .isInstanceOf(ValidationException.class) |
| 110 | + .satisfies( |
| 111 | + t -> { |
| 112 | + var err = ((ValidationException) t).error(); |
| 113 | + org.assertj.core.api.Assertions.assertThat(err.keyword()).isEqualTo("oneOf"); |
| 114 | + org.assertj.core.api.Assertions.assertThat(err.message()).contains("matched 2 of 2"); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void notPassesWhenInnerFails() { |
| 120 | + var schema = new NotSchema(stringSchema(100, null)); |
| 121 | + v.validate("hello", schema, "/v"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void notFailsWhenInnerPasses() { |
| 126 | + var schema = new NotSchema(stringSchema(null, 10)); |
| 127 | + assertThatThrownBy(() -> v.validate("hello", schema, "/v")) |
| 128 | + .isInstanceOf(ValidationException.class) |
| 129 | + .extracting(t -> ((ValidationException) t).error().keyword()) |
| 130 | + .isEqualTo("not"); |
51 | 131 | } |
52 | 132 | } |
0 commit comments