/
BeanValidationTest.java
214 lines (187 loc) · 8.19 KB
/
BeanValidationTest.java
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
/**
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.microprofile.openapi.tck.beanvalidation;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import org.eclipse.microprofile.openapi.apps.beanvalidation.BeanValidationApp;
import org.eclipse.microprofile.openapi.tck.AppTestBase;
import org.hamcrest.Matcher;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.annotations.Test;
import io.restassured.response.ValidatableResponse;
public class BeanValidationTest extends AppTestBase {
@Deployment
public static WebArchive buildApp() {
return ShrinkWrap.create(WebArchive.class, "beanValidation.war")
.addPackage(BeanValidationApp.class.getPackage());
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void notEmptyStringTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "notEmptyString", hasEntry("minLength", 1));
assertProperty(vr, "notEmptyString", hasEntry("type", "string"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void notEmptyListTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "notEmptyList", hasEntry("minItems", 1));
assertProperty(vr, "notEmptyList", hasEntry("type", "array"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void notEmptyMapTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "notEmptyMap", hasEntry("minProperties", 1));
assertProperty(vr, "notEmptyMap", hasEntry("type", "object"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void notBlankStringTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "notBlankString", hasEntry("pattern", "\\S"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void sizedStringTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "sizedString", hasEntry("minLength", 2));
assertProperty(vr, "sizedString", hasEntry("maxLength", 7));
assertProperty(vr, "sizedString", hasEntry("type", "string"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void sizedListTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "sizedList", hasEntry("minItems", 1));
assertProperty(vr, "sizedList", hasEntry("maxItems", 10));
assertProperty(vr, "sizedList", hasEntry("type", "array"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void sizedMapTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "sizedMap", hasEntry("minProperties", 3));
assertProperty(vr, "sizedMap", hasEntry("maxProperties", 5));
assertProperty(vr, "sizedMap", hasEntry("type", "object"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void maxDecimalInclusiveTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "maxDecimalInclusive", hasEntry("maximum", 1.5f));
assertProperty(vr, "maxDecimalInclusive", hasEntry("type", "number"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void maxDecimalExclusiveTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "maxDecimalExclusive", hasEntry("maximum", 1.5f));
assertProperty(vr, "maxDecimalExclusive", hasEntry("exclusiveMaximum", true));
assertProperty(vr, "maxDecimalExclusive", hasEntry("type", "number"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void minDecimalInclusiveTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "minDecimalInclusive", hasEntry("minimum", 3.25f));
assertProperty(vr, "minDecimalInclusive", hasEntry("type", "number"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void minDecimalExclusiveTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "minDecimalExclusive", hasEntry("minimum", 3.25f));
assertProperty(vr, "minDecimalExclusive", hasEntry("exclusiveMinimum", true));
assertProperty(vr, "minDecimalExclusive", hasEntry("type", "number"));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void maxIntTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "maxInt", hasEntry("maximum", 5));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void minIntTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "minInt", hasEntry("minimum", 7));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void negativeIntTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "negativeInt", hasEntry("maximum", 0));
assertProperty(vr, "negativeInt", hasEntry("exclusiveMaximum", true));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void negativeOrZeroIntTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "negativeOrZeroInt", hasEntry("maximum", 0));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void positiveIntTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "positiveInt", hasEntry("minimum", 0));
assertProperty(vr, "positiveInt", hasEntry("exclusiveMinimum", true));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void positiveOrZeroIntTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "positiveOrZeroInt", hasEntry("minimum", 0));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void overridenBySchemaAnnotationTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "overridenBySchemaAnnotation", hasEntry("minLength", 6));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void nonDefaultGroupTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "nonDefaultGroup", not(hasKey("minLength")));
}
@Test(dataProvider = "formatProvider")
@RunAsClient
public void defaultAndOtherGroupsTest(String format) {
ValidatableResponse vr = callEndpoint(format);
assertProperty(vr, "defaultAndOtherGroups", hasEntry("minLength", 1));
}
/**
* Asserts that a property from the test schema matches the given matcher
*
* @param vr
* the response
* @param propertyName
* the property to test
* @param matcher
* the matcher to assert
*/
public static void assertProperty(ValidatableResponse vr, String propertyName, Matcher<?> matcher) {
String schemaPath = dereference(vr, "paths.'/'.post.requestBody", "content.'application/json'.schema");
String propertyPath = schemaPath + ".properties." + propertyName;
vr.body(propertyPath, matcher);
}
}