|
16 | 16 |
|
17 | 17 | package org.springframework.restdocs.constraints; |
18 | 18 |
|
19 | | -import java.math.BigDecimal; |
20 | | -import java.util.Date; |
21 | | - |
22 | | -import javax.validation.constraints.AssertFalse; |
23 | | -import javax.validation.constraints.AssertTrue; |
24 | | -import javax.validation.constraints.DecimalMax; |
25 | | -import javax.validation.constraints.DecimalMin; |
26 | | -import javax.validation.constraints.Digits; |
27 | | -import javax.validation.constraints.Future; |
28 | | -import javax.validation.constraints.Max; |
29 | | -import javax.validation.constraints.Min; |
30 | | -import javax.validation.constraints.NotNull; |
31 | | -import javax.validation.constraints.Null; |
32 | | -import javax.validation.constraints.Past; |
33 | | -import javax.validation.constraints.Pattern; |
34 | | -import javax.validation.constraints.Size; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
35 | 21 |
|
36 | 22 | import org.junit.Test; |
37 | 23 |
|
| 24 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 25 | +import static org.hamcrest.CoreMatchers.is; |
38 | 26 | import static org.hamcrest.Matchers.contains; |
39 | | -import static org.hamcrest.Matchers.hasSize; |
40 | 27 | import static org.junit.Assert.assertThat; |
| 28 | +import static org.mockito.BDDMockito.given; |
| 29 | +import static org.mockito.Mockito.mock; |
41 | 30 |
|
42 | 31 | /** |
43 | 32 | * Tests for {@link ConstraintDescriptions}. |
|
46 | 35 | */ |
47 | 36 | public class ConstraintDescriptionsTests { |
48 | 37 |
|
49 | | -private final ConstraintDescriptions constraintDescriptions = new ConstraintDescriptions( |
50 | | -Constrained.class); |
51 | | - |
52 | | -@Test |
53 | | -public void assertFalse() { |
54 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("assertFalse"), |
55 | | -contains("Must be false")); |
56 | | -} |
| 38 | +private final ConstraintResolver constraintResolver = mock(ConstraintResolver.class); |
57 | 39 |
|
58 | | -@Test |
59 | | -public void assertTrue() { |
60 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("assertTrue"), |
61 | | -contains("Must be true")); |
62 | | -} |
| 40 | +private final ConstraintDescriptionResolver constraintDescriptionResolver = mock( |
| 41 | +ConstraintDescriptionResolver.class); |
63 | 42 |
|
64 | | -@Test |
65 | | -public void decimalMax() { |
66 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("decimalMax"), |
67 | | -contains("Must be at most 9.875")); |
68 | | -} |
69 | | - |
70 | | -@Test |
71 | | -public void decimalMin() { |
72 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("decimalMin"), |
73 | | -contains("Must be at least 1.5")); |
74 | | -} |
75 | | - |
76 | | -@Test |
77 | | -public void digits() { |
78 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("digits"), |
79 | | -contains("Must have at most 2 integral digits and 5 fractional digits")); |
80 | | -} |
81 | | - |
82 | | -@Test |
83 | | -public void future() { |
84 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("future"), |
85 | | -contains("Must be in the future")); |
86 | | -} |
87 | | - |
88 | | -@Test |
89 | | -public void max() { |
90 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("max"), |
91 | | -contains("Must be at most 10")); |
92 | | -} |
93 | | - |
94 | | -@Test |
95 | | -public void min() { |
96 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("min"), |
97 | | -contains("Must be at least 5")); |
98 | | -} |
99 | | - |
100 | | -@Test |
101 | | -public void notNull() { |
102 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("notNull"), |
103 | | -contains("Must not be null")); |
104 | | -} |
105 | | - |
106 | | -@Test |
107 | | -public void nul() { |
108 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("nul"), |
109 | | -contains("Must be null")); |
110 | | -} |
111 | | - |
112 | | -@Test |
113 | | -public void past() { |
114 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("past"), |
115 | | -contains("Must be in the past")); |
116 | | -} |
117 | | - |
118 | | -@Test |
119 | | -public void pattern() { |
120 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("pattern"), |
121 | | -contains("Must match the regular expression `[A-Z][a-z]+`")); |
122 | | -} |
123 | | - |
124 | | -@Test |
125 | | -public void size() { |
126 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("size"), |
127 | | -contains("Size must be between 0 and 10 inclusive")); |
128 | | -} |
129 | | - |
130 | | -@Test |
131 | | -public void sizeList() { |
132 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("sizeList"), |
133 | | -contains("Size must be between 1 and 4 inclusive", |
134 | | -"Size must be between 8 and 10 inclusive")); |
135 | | -} |
| 43 | +private final ConstraintDescriptions constraintDescriptions = new ConstraintDescriptions( |
| 44 | +Constrained.class, this.constraintResolver, |
| 45 | +this.constraintDescriptionResolver); |
136 | 46 |
|
137 | 47 | @Test |
138 | | -public void unconstrained() { |
139 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("unconstrained"), |
140 | | -hasSize(0)); |
| 48 | +public void descriptionsForConstraints() { |
| 49 | +Constraint constraint1 = new Constraint("constraint1", |
| 50 | +Collections.<String, Object>emptyMap()); |
| 51 | +Constraint constraint2 = new Constraint("constraint2", |
| 52 | +Collections.<String, Object>emptyMap()); |
| 53 | +given(this.constraintResolver.resolveForProperty("foo", Constrained.class)) |
| 54 | +.willReturn(Arrays.asList(constraint1, constraint2)); |
| 55 | +given(this.constraintDescriptionResolver.resolveDescription(constraint1)) |
| 56 | +.willReturn("Bravo"); |
| 57 | +given(this.constraintDescriptionResolver.resolveDescription(constraint2)) |
| 58 | +.willReturn("Alpha"); |
| 59 | +assertThat(this.constraintDescriptions.descriptionsForProperty("foo"), |
| 60 | +contains("Alpha", "Bravo")); |
141 | 61 | } |
142 | 62 |
|
143 | 63 | @Test |
144 | | -public void nonExistentProperty() { |
145 | | -assertThat(this.constraintDescriptions.descriptionsForProperty("doesNotExist"), |
146 | | -hasSize(0)); |
| 64 | +public void emptyListOfDescriptionsWhenThereAreNoConstraints() { |
| 65 | +given(this.constraintResolver.resolveForProperty("foo", Constrained.class)) |
| 66 | +.willReturn(Collections.<Constraint>emptyList()); |
| 67 | +assertThat(this.constraintDescriptions.descriptionsForProperty("foo").size(), |
| 68 | +is(equalTo(0))); |
147 | 69 | } |
148 | 70 |
|
149 | 71 | private static class Constrained { |
150 | 72 |
|
151 | | -@AssertFalse |
152 | | -private boolean assertFalse; |
153 | | - |
154 | | -@AssertTrue |
155 | | -private boolean assertTrue; |
156 | | - |
157 | | -@DecimalMax("9.875") |
158 | | -private BigDecimal decimalMax; |
159 | | - |
160 | | -@DecimalMin("1.5") |
161 | | -private BigDecimal decimalMin; |
162 | | - |
163 | | -@Digits(fraction = 5, integer = 2) |
164 | | -private BigDecimal digits; |
165 | | - |
166 | | -@Future |
167 | | -private Date future; |
168 | | - |
169 | | -@NotNull |
170 | | -private String notNull; |
171 | | - |
172 | | -@Max(10) |
173 | | -private int max; |
174 | | - |
175 | | -@Min(5) |
176 | | -private int min; |
177 | | - |
178 | | -@Null |
179 | | -private String nul; |
180 | | - |
181 | | -@Past |
182 | | -private Date past; |
183 | | - |
184 | | -@Pattern(regexp = "[A-Z][a-z]+") |
185 | | -private String pattern; |
186 | | - |
187 | | -@Size(min = 0, max = 10) |
188 | | -private String size; |
189 | | - |
190 | | -@Size.List({ @Size(min = 1, max = 4), @Size(min = 8, max = 10) }) |
191 | | -private String sizeList; |
192 | | - |
193 | | -@SuppressWarnings("unused") |
194 | | -private String unconstrained; |
195 | 73 | } |
196 | 74 |
|
197 | 75 | } |
0 commit comments