Skip to content

Commit bc21a99

Browse files
author
Bastian Wilhelm
committed
#34 create unit tests
1 parent 25d4016 commit bc21a99

File tree

1 file changed

+266
-0
lines changed

1 file changed

+266
-0
lines changed
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/*
2+
* Copyright 2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.writer
18+
19+
import com.github.hauner.openapi.spring.model.datatypes.ArrayDataType
20+
import com.github.hauner.openapi.spring.model.datatypes.DataType
21+
import com.github.hauner.openapi.spring.model.datatypes.DataTypeConstraints
22+
import com.github.hauner.openapi.spring.model.datatypes.DoubleDataType
23+
import com.github.hauner.openapi.spring.model.datatypes.FloatDataType
24+
import com.github.hauner.openapi.spring.model.datatypes.IntegerDataType
25+
import com.github.hauner.openapi.spring.model.datatypes.ListDataType
26+
import com.github.hauner.openapi.spring.model.datatypes.LocalDateDataType
27+
import com.github.hauner.openapi.spring.model.datatypes.LongDataType
28+
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
29+
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
30+
import com.github.hauner.openapi.spring.model.datatypes.SetDataType
31+
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
32+
import spock.lang.Specification
33+
import spock.lang.Unroll
34+
35+
class BeanValidationFactorySpec extends Specification {
36+
37+
BeanValidationFactory beanValidationFactory = new BeanValidationFactory()
38+
39+
@Unroll
40+
void "check @Valid for Object (type: #type)"() {
41+
setup:
42+
DataType dataType = type.getDeclaredConstructor().newInstance()
43+
44+
when:
45+
def imports = beanValidationFactory.collectImports(dataType)
46+
def annotations = beanValidationFactory.createAnnotations(dataType)
47+
48+
then:
49+
imports == resultImports as Set<String>
50+
annotations == resultAnnnotation
51+
52+
where:
53+
type || resultImports | resultAnnnotation
54+
ObjectDataType || ["javax.validation.Valid"] | "@Valid"
55+
StringDataType || [] | ""
56+
IntegerDataType || [] | ""
57+
LongDataType || [] | ""
58+
ListDataType || [] | ""
59+
MappedDataType || [] | ""
60+
FloatDataType || [] | ""
61+
LocalDateDataType || [] | ""
62+
}
63+
64+
@Unroll
65+
void "check import @Size for String (minLength: #minLength, maxLength: #maxLength, type: #type)"() {
66+
setup:
67+
DataType dataType = type.getDeclaredConstructor().newInstance()
68+
dataType.constraints = new DataTypeConstraints()
69+
dataType.constraints.minLength = minLength
70+
dataType.constraints.maxLength = maxLength
71+
72+
when:
73+
def imports = beanValidationFactory.collectImports(dataType)
74+
def annotations = beanValidationFactory.createAnnotations(dataType)
75+
76+
then:
77+
imports == resultImports as Set<String>
78+
annotations == resultAnnotations
79+
80+
where:
81+
type | minLength | maxLength || resultImports | resultAnnotations
82+
StringDataType | null | null || [] | ""
83+
StringDataType | 1 | null || ["javax.validation.constraints.Size"] | "@Size(min = 1)"
84+
StringDataType | null | 2 || ["javax.validation.constraints.Size"] | "@Size(max = 2)"
85+
StringDataType | 1 | 2 || ["javax.validation.constraints.Size"] | "@Size(min = 1, max = 2)"
86+
IntegerDataType | 1 | null || [] | ""
87+
IntegerDataType | null | 2 || [] | ""
88+
IntegerDataType | 1 | 2 || [] | ""
89+
ListDataType | 1 | 2 || [] | ""
90+
}
91+
92+
@Unroll
93+
void "check import @Size for Collections (minItems: #minItems, maxItems: #maxItems, type: #type)"() {
94+
setup:
95+
DataType dataType = type.getDeclaredConstructor().newInstance()
96+
dataType.constraints = new DataTypeConstraints()
97+
dataType.constraints.minItems = minItems
98+
dataType.constraints.maxItems = maxItems
99+
100+
when:
101+
def imports = beanValidationFactory.collectImports(dataType)
102+
def annotations = beanValidationFactory.createAnnotations(dataType)
103+
104+
then:
105+
imports == resultImports as Set<String>
106+
annotations == resultAnnotations
107+
108+
where:
109+
type | minItems | maxItems || resultImports | resultAnnotations
110+
ArrayDataType | null | null || [] | ""
111+
ArrayDataType | 1 | null || ["javax.validation.constraints.Size"] | "@Size(min = 1)"
112+
ArrayDataType | null | 2 || ["javax.validation.constraints.Size"] | "@Size(max = 2)"
113+
ArrayDataType | 1 | 2 || ["javax.validation.constraints.Size"] | "@Size(min = 1, max = 2)"
114+
ListDataType | null | 2 || ["javax.validation.constraints.Size"] | "@Size(max = 2)"
115+
SetDataType | 1 | 2 || ["javax.validation.constraints.Size"] | "@Size(min = 1, max = 2)"
116+
StringDataType | 1 | null || [] | ""
117+
StringDataType | null | 2 || [] | ""
118+
LongDataType | 1 | 2 || [] | ""
119+
}
120+
121+
@Unroll
122+
void "check import @NotNull (nullable: #nullable, type: #type)"() {
123+
setup:
124+
DataType dataType = type.getDeclaredConstructor().newInstance()
125+
dataType.constraints = new DataTypeConstraints()
126+
dataType.constraints.nullable = nullable
127+
128+
when:
129+
def imports = beanValidationFactory.collectImports(dataType)
130+
def annotations = beanValidationFactory.createAnnotations(dataType)
131+
132+
then:
133+
imports == resultImports as Set<String>
134+
annotations == resultAnnotations
135+
136+
where:
137+
type | nullable || resultImports | resultAnnotations
138+
IntegerDataType | null || [] | ""
139+
IntegerDataType | true || [] | ""
140+
IntegerDataType | false || ["javax.validation.constraints.NotNull"] | "@NotNull"
141+
StringDataType | null || [] | ""
142+
StringDataType | true || [] | ""
143+
StringDataType | false || ["javax.validation.constraints.NotNull"] | "@NotNull"
144+
ListDataType | null || [] | ""
145+
ListDataType | true || [] | ""
146+
ListDataType | false || ["javax.validation.constraints.NotNull"] | "@NotNull"
147+
}
148+
149+
@Unroll
150+
void "check import @DecimalMin (minimum: #minimum, exclusiveMinimum: #exclusiveMinimum, type: #type)"() {
151+
setup:
152+
DataType dataType = type.getDeclaredConstructor().newInstance()
153+
dataType.constraints = new DataTypeConstraints()
154+
dataType.constraints.minimum = minimum
155+
dataType.constraints.exclusiveMinimum = exclusiveMinimum
156+
157+
when:
158+
def imports = beanValidationFactory.collectImports(dataType)
159+
def annotations = beanValidationFactory.createAnnotations(dataType)
160+
161+
then:
162+
imports == resultImports as Set<String>
163+
annotations == resultAnnotations
164+
165+
where:
166+
type | minimum | exclusiveMinimum || resultImports | resultAnnotations
167+
IntegerDataType | null | null || [] | ""
168+
IntegerDataType | null | true || [] | ""
169+
IntegerDataType | null | false || [] | ""
170+
IntegerDataType | 1 | null || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
171+
IntegerDataType | 1 | true || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\", inclusive = false)"
172+
IntegerDataType | 1 | false || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
173+
LongDataType | null | null || [] | ""
174+
LongDataType | null | true || [] | ""
175+
LongDataType | null | false || [] | ""
176+
LongDataType | 1 | null || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
177+
LongDataType | 1 | true || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\", inclusive = false)"
178+
LongDataType | 1 | false || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
179+
FloatDataType | null | null || [] | ""
180+
FloatDataType | null | true || [] | ""
181+
FloatDataType | null | false || [] | ""
182+
FloatDataType | 1 | null || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
183+
FloatDataType | 1 | true || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\", inclusive = false)"
184+
FloatDataType | 1 | false || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
185+
DoubleDataType | null | null || [] | ""
186+
DoubleDataType | null | true || [] | ""
187+
DoubleDataType | null | false || [] | ""
188+
DoubleDataType | 1 | null || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
189+
DoubleDataType | 1 | true || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\", inclusive = false)"
190+
DoubleDataType | 1 | false || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\")"
191+
StringDataType | 1 | null || [] | ""
192+
}
193+
194+
@Unroll
195+
void "check import @DecimalMax (maximum: #maximum, exclusiveMaximum: #exclusiveMaximum, type: #type)"() {
196+
setup:
197+
DataType dataType = type.getDeclaredConstructor().newInstance()
198+
dataType.constraints = new DataTypeConstraints()
199+
dataType.constraints.maximum = maximum
200+
dataType.constraints.exclusiveMaximum = exclusiveMaximum
201+
202+
when:
203+
def imports = beanValidationFactory.collectImports(dataType)
204+
def annotations = beanValidationFactory.createAnnotations(dataType)
205+
206+
then:
207+
imports == resultImports as Set<String>
208+
annotations == resultAnnotations
209+
210+
where:
211+
type | maximum | exclusiveMaximum || resultImports | resultAnnotations
212+
IntegerDataType | null | null || [] | ""
213+
IntegerDataType | null | true || [] | ""
214+
IntegerDataType | null | false || [] | ""
215+
IntegerDataType | 1 | null || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
216+
IntegerDataType | 1 | true || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\", inclusive = false)"
217+
IntegerDataType | 1 | false || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
218+
LongDataType | null | null || [] | ""
219+
LongDataType | null | true || [] | ""
220+
LongDataType | null | false || [] | ""
221+
LongDataType | 1 | null || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
222+
LongDataType | 1 | true || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\", inclusive = false)"
223+
LongDataType | 1 | false || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
224+
FloatDataType | null | null || [] | ""
225+
FloatDataType | null | true || [] | ""
226+
FloatDataType | null | false || [] | ""
227+
FloatDataType | 1 | null || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
228+
FloatDataType | 1 | true || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\", inclusive = false)"
229+
FloatDataType | 1 | false || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
230+
DoubleDataType | null | null || [] | ""
231+
DoubleDataType | null | true || [] | ""
232+
DoubleDataType | null | false || [] | ""
233+
DoubleDataType | 1 | null || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
234+
DoubleDataType | 1 | true || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\", inclusive = false)"
235+
DoubleDataType | 1 | false || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"1\")"
236+
StringDataType | 1 | null || [] | ""
237+
}
238+
239+
@Unroll
240+
void "check import @DecimalMin and @DecimalMax (minimum: #minimum, exclusiveMinimum: #exclusiveMinimum maximum: #maximum, exclusiveMaximum: #exclusiveMaximum)"() {
241+
setup:
242+
DataType dataType = new DoubleDataType()
243+
dataType.constraints = new DataTypeConstraints()
244+
dataType.constraints.minimum = minimum
245+
dataType.constraints.exclusiveMinimum = exclusiveMinimum
246+
dataType.constraints.maximum = maximum
247+
dataType.constraints.exclusiveMaximum = exclusiveMaximum
248+
249+
when:
250+
def imports = beanValidationFactory.collectImports(dataType)
251+
def annotations = beanValidationFactory.createAnnotations(dataType)
252+
253+
then:
254+
imports == resultImports as Set<String>
255+
annotations == resultAnnotations
256+
257+
where:
258+
minimum | exclusiveMinimum | maximum | exclusiveMaximum || resultImports | resultAnnotations
259+
1 | false | 2 | false || ["javax.validation.constraints.DecimalMin", "javax.validation.constraints.DecimalMax"] | "@DecimalMin(value = \"1\") @DecimalMax(value = \"2\")"
260+
1 | true | 2 | false || ["javax.validation.constraints.DecimalMin", "javax.validation.constraints.DecimalMax"] | "@DecimalMin(value = \"1\", inclusive = false) @DecimalMax(value = \"2\")"
261+
1 | false | 2 | true || ["javax.validation.constraints.DecimalMin", "javax.validation.constraints.DecimalMax"] | "@DecimalMin(value = \"1\") @DecimalMax(value = \"2\", inclusive = false)"
262+
1 | true | 2 | true || ["javax.validation.constraints.DecimalMin", "javax.validation.constraints.DecimalMax"] | "@DecimalMin(value = \"1\", inclusive = false) @DecimalMax(value = \"2\", inclusive = false)"
263+
1 | true | null | true || ["javax.validation.constraints.DecimalMin"] | "@DecimalMin(value = \"1\", inclusive = false)"
264+
null | true | 2 | true || ["javax.validation.constraints.DecimalMax"] | "@DecimalMax(value = \"2\", inclusive = false)"
265+
}
266+
}

0 commit comments

Comments
 (0)