-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultValidatorDispatchTest.java
More file actions
238 lines (209 loc) · 8.1 KB
/
Copy pathDefaultValidatorDispatchTest.java
File metadata and controls
238 lines (209 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.retailsvc.http.validate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.retailsvc.http.ValidationException;
import com.retailsvc.http.spec.schema.AllOfSchema;
import com.retailsvc.http.spec.schema.AlwaysSchema;
import com.retailsvc.http.spec.schema.AnyOfSchema;
import com.retailsvc.http.spec.schema.BooleanSchema;
import com.retailsvc.http.spec.schema.NeverSchema;
import com.retailsvc.http.spec.schema.NotSchema;
import com.retailsvc.http.spec.schema.NullSchema;
import com.retailsvc.http.spec.schema.OneOfSchema;
import com.retailsvc.http.spec.schema.StringSchema;
import com.retailsvc.http.spec.schema.TypeName;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
class DefaultValidatorDispatchTest {
private final Validator v =
new DefaultValidator(
name -> {
throw new AssertionError("no refs");
});
@Test
void nullSchemaAcceptsNull() {
v.validate(null, new NullSchema(Map.of()), "");
}
@Test
void nullSchemaRejectsNonNull() {
var schema = new NullSchema(Map.of());
assertThatThrownBy(() -> v.validate("x", schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("type");
}
@Test
void booleanSchemaAcceptsBoolean() {
v.validate(true, new BooleanSchema(Set.of(TypeName.BOOLEAN), Map.of()), "/v");
}
@Test
void booleanSchemaRejectsString() {
var schema = new BooleanSchema(Set.of(TypeName.BOOLEAN), Map.of());
assertThatThrownBy(() -> v.validate("x", schema, "/v")).isInstanceOf(ValidationException.class);
}
private StringSchema stringSchema(Integer min, Integer max) {
return new StringSchema(Set.of(TypeName.STRING), null, min, max, null, null, Map.of());
}
@Test
void allOfPassesWhenAllBranchesPass() {
var schema = new AllOfSchema(List.of(stringSchema(1, null), stringSchema(null, 10)), Map.of());
v.validate("hello", schema, "/v");
}
@Test
void allOfPropagatesFirstFailingBranch() {
var schema = new AllOfSchema(List.of(stringSchema(1, null), stringSchema(null, 3)), Map.of());
assertThatThrownBy(() -> v.validate("hello", schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("maxLength");
}
@Test
void anyOfPassesWhenOneBranchPasses() {
var schema =
new AnyOfSchema(List.of(stringSchema(100, null), stringSchema(null, 10)), Map.of());
v.validate("hello", schema, "/v");
}
@Test
void anyOfFailsWhenNoBranchMatches() {
var schema = new AnyOfSchema(List.of(stringSchema(100, null), stringSchema(null, 2)), Map.of());
assertThatThrownBy(() -> v.validate("hello", schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("anyOf");
}
@Test
void oneOfPassesWhenExactlyOneBranchMatches() {
// value "hello" — len 5. branch[0] requires min 100 (fails), branch[1] max 10 (passes).
var schema =
new OneOfSchema(List.of(stringSchema(100, null), stringSchema(null, 10)), Map.of());
v.validate("hello", schema, "/v");
}
@Test
void oneOfFailsWhenZeroBranchesMatch() {
var schema = new OneOfSchema(List.of(stringSchema(100, null), stringSchema(null, 2)), Map.of());
assertThatThrownBy(() -> v.validate("hello", schema, "/v"))
.isInstanceOf(ValidationException.class)
.satisfies(
t -> {
var err = ((ValidationException) t).error();
assertThat(err.keyword()).isEqualTo("oneOf");
assertThat(err.message()).contains("matched 0 of 2");
});
}
@Test
void oneOfFailsWhenTwoBranchesMatch() {
// value "hello" — both branches accept.
var schema = new OneOfSchema(List.of(stringSchema(null, 10), stringSchema(1, null)), Map.of());
assertThatThrownBy(() -> v.validate("hello", schema, "/v"))
.isInstanceOf(ValidationException.class)
.satisfies(
t -> {
var err = ((ValidationException) t).error();
assertThat(err.keyword()).isEqualTo("oneOf");
assertThat(err.message()).contains("matched 2 of 2");
});
}
@Test
void notPassesWhenInnerFails() {
var schema = new NotSchema(stringSchema(100, null), Map.of());
v.validate("hello", schema, "/v");
}
@Test
void notFailsWhenInnerPasses() {
var schema = new NotSchema(stringSchema(null, 10), Map.of());
assertThatThrownBy(() -> v.validate("hello", schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("not");
}
@Test
void allOfWithEmptyPartsAlwaysPasses() {
// Empty allOf is vacuously true per JSON Schema 2020-12.
v.validate("anything", new AllOfSchema(List.of(), Map.of()), "/v");
}
@Test
void anyOfWithEmptyOptionsAlwaysFails() {
var schema = new AnyOfSchema(List.of(), Map.of());
assertThatThrownBy(() -> v.validate("anything", schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("anyOf");
}
@Test
void oneOfWithEmptyOptionsAlwaysFails() {
var schema = new OneOfSchema(List.of(), Map.of());
assertThatThrownBy(() -> v.validate("anything", schema, "/v"))
.isInstanceOf(ValidationException.class)
.satisfies(
t -> {
var err = ((ValidationException) t).error();
assertThat(err.keyword()).isEqualTo("oneOf");
assertThat(err.message()).contains("matched 0 of 0");
});
}
@Test
void notWithNullSchemaRejectsNull() {
// not(NullSchema) — inner accepts null, outer must reject.
var schema = new NotSchema(new NullSchema(Map.of()), Map.of());
assertThatThrownBy(() -> v.validate(null, schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("not");
}
@Test
void anyOfMatchesNullViaNullSchema() {
// anyOf containing a NullSchema branch must pass for a null value.
var schema =
new AnyOfSchema(List.of(stringSchema(1, null), new NullSchema(Map.of())), Map.of());
v.validate(null, schema, "/v");
}
@Test
void alwaysSchemaAcceptsString() {
v.validate("anything", new AlwaysSchema(Map.of()), "/v");
}
@Test
void alwaysSchemaAcceptsInteger() {
v.validate(42, new AlwaysSchema(Map.of()), "/v");
}
@Test
void alwaysSchemaAcceptsObject() {
v.validate(Map.of("a", 1), new AlwaysSchema(Map.of()), "/v");
}
@Test
void alwaysSchemaAcceptsNull() {
v.validate(null, new AlwaysSchema(Map.of()), "/v");
}
@Test
void neverSchemaRejectsString() {
var schema = new NeverSchema(Map.of());
assertThatThrownBy(() -> v.validate("anything", schema, "/v"))
.isInstanceOf(ValidationException.class)
.satisfies(
t -> {
var err = ((ValidationException) t).error();
assertThat(err.keyword()).isEqualTo("false");
assertThat(err.message()).isEqualTo("schema rejects all values");
assertThat(err.pointer()).isEqualTo("/v");
assertThat(err.rejectedValue()).isEqualTo("anything");
});
}
// Full ValidationError surface is verified by neverSchemaRejectsString; these cover keyword only.
@Test
void neverSchemaRejectsInteger() {
var schema = new NeverSchema(Map.of());
assertThatThrownBy(() -> v.validate(42, schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("false");
}
@Test
void neverSchemaRejectsNull() {
var schema = new NeverSchema(Map.of());
assertThatThrownBy(() -> v.validate(null, schema, "/v"))
.isInstanceOf(ValidationException.class)
.extracting(t -> ((ValidationException) t).error().keyword())
.isEqualTo("false");
}
}