Skip to content
This repository was archived by the owner on Mar 30, 2020. It is now read-only.

Commit e4c436a

Browse files
add unit tests
1 parent 6f58eef commit e4c436a

File tree

1 file changed

+303
-0
lines changed

1 file changed

+303
-0
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
/*
2+
* Copyright 2019 VicTools.
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.victools.jsonschema.module.javax.validation;
18+
19+
import com.github.victools.jsonschema.generator.JavaType;
20+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
21+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart;
22+
import com.github.victools.jsonschema.generator.TypeVariableContext;
23+
import java.lang.reflect.Field;
24+
import java.lang.reflect.Method;
25+
import java.math.BigDecimal;
26+
import java.math.BigInteger;
27+
import java.util.List;
28+
import java.util.Set;
29+
import java.util.function.BiFunction;
30+
import javax.validation.constraints.DecimalMax;
31+
import javax.validation.constraints.DecimalMin;
32+
import javax.validation.constraints.Max;
33+
import javax.validation.constraints.Min;
34+
import javax.validation.constraints.Negative;
35+
import javax.validation.constraints.NegativeOrZero;
36+
import javax.validation.constraints.NotBlank;
37+
import javax.validation.constraints.NotEmpty;
38+
import javax.validation.constraints.NotNull;
39+
import javax.validation.constraints.Null;
40+
import javax.validation.constraints.Positive;
41+
import javax.validation.constraints.PositiveOrZero;
42+
import javax.validation.constraints.Size;
43+
import junitparams.JUnitParamsRunner;
44+
import junitparams.Parameters;
45+
import org.junit.Assert;
46+
import org.junit.Before;
47+
import org.junit.Test;
48+
import org.junit.runner.RunWith;
49+
import org.mockito.ArgumentCaptor;
50+
import org.mockito.Mockito;
51+
52+
/**
53+
* Test for the {@link JavaxValidationModule}.
54+
*/
55+
@RunWith(JUnitParamsRunner.class)
56+
public class JavaxValidationModuleTest {
57+
58+
private SchemaGeneratorConfigBuilder configBuilder;
59+
private SchemaGeneratorConfigPart<Field> fieldConfigPart;
60+
private SchemaGeneratorConfigPart<Method> methodConfigPart;
61+
62+
@Before
63+
public void setUp() {
64+
this.configBuilder = Mockito.mock(SchemaGeneratorConfigBuilder.class);
65+
this.fieldConfigPart = Mockito.mock(SchemaGeneratorConfigPart.class);
66+
this.methodConfigPart = Mockito.mock(SchemaGeneratorConfigPart.class);
67+
Mockito.when(this.configBuilder.forFields()).thenReturn(this.fieldConfigPart);
68+
Mockito.when(this.configBuilder.forMethods()).thenReturn(this.methodConfigPart);
69+
}
70+
71+
@Test
72+
public void testApplyToConfigBuilder() {
73+
new JavaxValidationModule().applyToConfigBuilder(this.configBuilder);
74+
75+
Mockito.verify(this.configBuilder).forFields();
76+
Mockito.verify(this.configBuilder).forMethods();
77+
78+
Mockito.verify(this.fieldConfigPart).withNullableCheck(Mockito.any());
79+
Mockito.verify(this.fieldConfigPart).withArrayMinItemsResolver(Mockito.any());
80+
Mockito.verify(this.fieldConfigPart).withArrayMaxItemsResolver(Mockito.any());
81+
Mockito.verify(this.fieldConfigPart).withStringMinLengthResolver(Mockito.any());
82+
Mockito.verify(this.fieldConfigPart).withStringMaxLengthResolver(Mockito.any());
83+
Mockito.verify(this.fieldConfigPart).withNumberInclusiveMinimumResolver(Mockito.any());
84+
Mockito.verify(this.fieldConfigPart).withNumberExclusiveMinimumResolver(Mockito.any());
85+
Mockito.verify(this.fieldConfigPart).withNumberInclusiveMaximumResolver(Mockito.any());
86+
Mockito.verify(this.fieldConfigPart).withNumberExclusiveMaximumResolver(Mockito.any());
87+
88+
Mockito.verify(this.methodConfigPart).withNullableCheck(Mockito.any());
89+
Mockito.verify(this.methodConfigPart).withArrayMinItemsResolver(Mockito.any());
90+
Mockito.verify(this.methodConfigPart).withArrayMaxItemsResolver(Mockito.any());
91+
Mockito.verify(this.methodConfigPart).withStringMinLengthResolver(Mockito.any());
92+
Mockito.verify(this.methodConfigPart).withStringMaxLengthResolver(Mockito.any());
93+
Mockito.verify(this.methodConfigPart).withNumberInclusiveMinimumResolver(Mockito.any());
94+
Mockito.verify(this.methodConfigPart).withNumberExclusiveMinimumResolver(Mockito.any());
95+
Mockito.verify(this.methodConfigPart).withNumberInclusiveMaximumResolver(Mockito.any());
96+
Mockito.verify(this.methodConfigPart).withNumberExclusiveMaximumResolver(Mockito.any());
97+
98+
Mockito.verifyNoMoreInteractions(this.configBuilder, this.fieldConfigPart, this.methodConfigPart);
99+
}
100+
101+
Object parametersForTestNullableCheck() {
102+
return new Object[][]{
103+
{"unannotatedField", null},
104+
{"notNullNumber", Boolean.FALSE},
105+
{"notEmptyList", Boolean.FALSE},
106+
{"notBlankString", Boolean.FALSE},
107+
{"nullField", Boolean.TRUE}
108+
};
109+
}
110+
111+
@Test
112+
@Parameters
113+
public void testNullableCheck(String fieldName, Boolean expectedResult) throws Exception {
114+
new JavaxValidationModule().applyToConfigBuilder(this.configBuilder);
115+
116+
ArgumentCaptor<BiFunction<Field, JavaType, Boolean>> captor = ArgumentCaptor.forClass(BiFunction.class);
117+
Mockito.verify(this.fieldConfigPart).withNullableCheck(captor.capture());
118+
119+
Boolean result = captor.getValue().apply(TestClassForNullableCheck.class.getDeclaredField(fieldName), null);
120+
Assert.assertEquals(expectedResult, result);
121+
}
122+
123+
Object parametersForTestArrayItemCountResolvers() {
124+
return new Object[][]{
125+
{"unannotatedArray", null, null},
126+
{"sizeTenToTwentyString", null, null},
127+
{"minSizeFiveArray", 5, null},
128+
{"maxSizeFiftyArray", null, 50},
129+
{"sizeTenToTwentySet", 10, 20},
130+
{"nonEmptyMaxSizeHundredList", 1, 100}
131+
};
132+
}
133+
134+
@Test
135+
@Parameters
136+
public void testArrayItemCountResolvers(String fieldName, Integer expectedMinItems, Integer expectedMaxItems) throws Exception {
137+
new JavaxValidationModule().applyToConfigBuilder(this.configBuilder);
138+
139+
Field field = TestClassForArrayItemCount.class.getDeclaredField(fieldName);
140+
JavaType fieldType = new JavaType(field.getGenericType(), TypeVariableContext.EMPTY_SCOPE);
141+
142+
ArgumentCaptor<BiFunction<Field, JavaType, Integer>> minItemCaptor = ArgumentCaptor.forClass(BiFunction.class);
143+
Mockito.verify(this.fieldConfigPart).withArrayMinItemsResolver(minItemCaptor.capture());
144+
Integer minItemCount = minItemCaptor.getValue().apply(field, fieldType);
145+
Assert.assertEquals(expectedMinItems, minItemCount);
146+
147+
ArgumentCaptor<BiFunction<Field, JavaType, Integer>> maxItemCaptor = ArgumentCaptor.forClass(BiFunction.class);
148+
Mockito.verify(this.fieldConfigPart).withArrayMaxItemsResolver(maxItemCaptor.capture());
149+
Integer maxItemCount = maxItemCaptor.getValue().apply(field, fieldType);
150+
Assert.assertEquals(expectedMaxItems, maxItemCount);
151+
}
152+
153+
Object parametersForTestStringLengthResolvers() {
154+
return new Object[][]{
155+
{"unannotatedString", null, null},
156+
{"sizeTenToTwentyArray", null, null},
157+
{"minSizeFiveSequence", 5, null},
158+
{"maxSizeFiftyString", null, 50},
159+
{"sizeTenToTwentyString", 10, 20},
160+
{"nonEmptyMaxSizeHundredString", 1, 100},
161+
{"nonBlankString", 1, null}
162+
};
163+
}
164+
165+
@Test
166+
@Parameters
167+
public void testStringLengthResolvers(String fieldName, Integer expectedMinLength, Integer expectedMaxLength) throws Exception {
168+
new JavaxValidationModule().applyToConfigBuilder(this.configBuilder);
169+
170+
Field field = TestClassForStringLength.class.getDeclaredField(fieldName);
171+
JavaType fieldType = new JavaType(field.getGenericType(), TypeVariableContext.EMPTY_SCOPE);
172+
173+
ArgumentCaptor<BiFunction<Field, JavaType, Integer>> minLengthCaptor = ArgumentCaptor.forClass(BiFunction.class);
174+
Mockito.verify(this.fieldConfigPart).withStringMinLengthResolver(minLengthCaptor.capture());
175+
Integer minLength = minLengthCaptor.getValue().apply(field, fieldType);
176+
Assert.assertEquals(expectedMinLength, minLength);
177+
178+
ArgumentCaptor<BiFunction<Field, JavaType, Integer>> maxLengthCaptor = ArgumentCaptor.forClass(BiFunction.class);
179+
Mockito.verify(this.fieldConfigPart).withStringMaxLengthResolver(maxLengthCaptor.capture());
180+
Integer maxLength = maxLengthCaptor.getValue().apply(field, fieldType);
181+
Assert.assertEquals(expectedMaxLength, maxLength);
182+
}
183+
184+
Object parametersForTestNumberMinMaxResolvers() {
185+
return new Object[][]{
186+
{"unannotatedInt", null, null, null, null},
187+
{"minMinusHundredLong", "-100", null, null, null},
188+
{"maxFiftyShort", null, null, "50", null},
189+
{"tenToTwentyInclusiveInteger", "10.1", null, "20.2", null},
190+
{"tenToTwentyExclusiveInteger", null, "10.1", null, "20.2"},
191+
{"positiveByte", null, BigDecimal.ZERO, null, null},
192+
{"positiveOrZeroBigInteger", BigDecimal.ZERO, null, null, null},
193+
{"negativeDecimal", null, null, null, BigDecimal.ZERO},
194+
{"negativeOrZeroLong", null, null, BigDecimal.ZERO, null}
195+
};
196+
}
197+
198+
@Test
199+
@Parameters
200+
public void testNumberMinMaxResolvers(String fieldName, BigDecimal expectedMinInclusive, BigDecimal expectedMinExclusive,
201+
BigDecimal expectedMaxInclusive, BigDecimal expectedMaxExclusive) throws Exception {
202+
new JavaxValidationModule().applyToConfigBuilder(this.configBuilder);
203+
204+
Field field = TestClassForNumberMinMax.class.getDeclaredField(fieldName);
205+
JavaType fieldType = new JavaType(field.getGenericType(), TypeVariableContext.EMPTY_SCOPE);
206+
207+
ArgumentCaptor<BiFunction<Field, JavaType, BigDecimal>> minInclusiveCaptor = ArgumentCaptor.forClass(BiFunction.class);
208+
Mockito.verify(this.fieldConfigPart).withNumberInclusiveMinimumResolver(minInclusiveCaptor.capture());
209+
BigDecimal minInclusive = minInclusiveCaptor.getValue().apply(field, fieldType);
210+
Assert.assertEquals(expectedMinInclusive, minInclusive);
211+
212+
ArgumentCaptor<BiFunction<Field, JavaType, BigDecimal>> minExclusiveCaptor = ArgumentCaptor.forClass(BiFunction.class);
213+
Mockito.verify(this.fieldConfigPart).withNumberExclusiveMinimumResolver(minExclusiveCaptor.capture());
214+
BigDecimal minExclusive = minExclusiveCaptor.getValue().apply(field, fieldType);
215+
Assert.assertEquals(expectedMinExclusive, minExclusive);
216+
217+
ArgumentCaptor<BiFunction<Field, JavaType, BigDecimal>> maxInclusiveCaptor = ArgumentCaptor.forClass(BiFunction.class);
218+
Mockito.verify(this.fieldConfigPart).withNumberInclusiveMaximumResolver(maxInclusiveCaptor.capture());
219+
BigDecimal maxInclusive = maxInclusiveCaptor.getValue().apply(field, fieldType);
220+
Assert.assertEquals(expectedMaxInclusive, maxInclusive);
221+
222+
ArgumentCaptor<BiFunction<Field, JavaType, BigDecimal>> maxExclusiveCaptor = ArgumentCaptor.forClass(BiFunction.class);
223+
Mockito.verify(this.fieldConfigPart).withNumberExclusiveMaximumResolver(maxExclusiveCaptor.capture());
224+
BigDecimal maxExclusive = maxExclusiveCaptor.getValue().apply(field, fieldType);
225+
Assert.assertEquals(expectedMaxExclusive, maxExclusive);
226+
}
227+
228+
private static class TestClassForNullableCheck {
229+
230+
Integer unannotatedField;
231+
@NotNull
232+
Double notNullNumber;
233+
@NotEmpty
234+
List<Object> notEmptyList;
235+
@NotBlank
236+
String notBlankString;
237+
@Null
238+
Object nullField;
239+
@Size(min = 5)
240+
int[] minSizeFiveArray;
241+
@Size(max = 50)
242+
long[] maxSizeFiftyArray;
243+
@Size(min = 10, max = 20)
244+
Set<Boolean> sizeTenToTwentySet;
245+
}
246+
247+
private static class TestClassForArrayItemCount {
248+
249+
String[] unannotatedArray;
250+
@Size(min = 10, max = 20)
251+
String sizeTenToTwentyString;
252+
@Size(min = 5)
253+
int[] minSizeFiveArray;
254+
@Size(max = 50)
255+
long[] maxSizeFiftyArray;
256+
@Size(min = 10, max = 20)
257+
Set<Boolean> sizeTenToTwentySet;
258+
@NotEmpty
259+
@Size(max = 100)
260+
List<Double> nonEmptyMaxSizeHundredList;
261+
}
262+
263+
private static class TestClassForStringLength {
264+
265+
String unannotatedString;
266+
@Size(min = 10, max = 20)
267+
int[] sizeTenToTwentyArray;
268+
@Size(min = 5)
269+
CharSequence minSizeFiveSequence;
270+
@Size(max = 50)
271+
String maxSizeFiftyString;
272+
@Size(min = 10, max = 20)
273+
String sizeTenToTwentyString;
274+
@NotEmpty
275+
@Size(max = 100)
276+
String nonEmptyMaxSizeHundredString;
277+
@NotBlank
278+
String nonBlankString;
279+
}
280+
281+
private static class TestClassForNumberMinMax {
282+
283+
int unannotatedInt;
284+
@Min(-100L)
285+
long minMinusHundredLong;
286+
@Max(50)
287+
short maxFiftyShort;
288+
@DecimalMin("10.1")
289+
@DecimalMax("20.2")
290+
Integer tenToTwentyInclusiveInteger;
291+
@DecimalMin(value = "10.1", inclusive = false)
292+
@DecimalMax(value = "20.2", inclusive = false)
293+
Integer tenToTwentyExclusiveInteger;
294+
@Positive
295+
byte positiveByte;
296+
@PositiveOrZero
297+
BigInteger positiveOrZeroBigInteger;
298+
@Negative
299+
BigDecimal negativeDecimal;
300+
@NegativeOrZero
301+
Long negativeOrZeroLong;
302+
}
303+
}

0 commit comments

Comments
 (0)