1616
1717import static org .assertj .core .api .Assertions .assertThat ;
1818import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
19- import static org .assertj .core .api .Assertions .fail ;
2019
2120import build .buf .protovalidate .exceptions .ValidationException ;
2221import com .example .imports .validationtest .ExampleFieldRules ;
@@ -33,52 +32,44 @@ public class ValidatorConstructionTest {
3332
3433 // Tests validation works as planned with default builder.
3534 @ Test
36- public void testDefaultBuilder () {
37- Map <Integer , Integer > testMap = new HashMap <Integer , Integer >();
35+ public void testDefaultBuilder () throws ValidationException {
36+ Map <Integer , Integer > testMap = new HashMap <>();
3837 testMap .put (42 , 42 );
3938 FieldExpressionMapInt32 msg = FieldExpressionMapInt32 .newBuilder ().putAllVal (testMap ).build ();
4039
4140 Validator validator = ValidatorFactory .newBuilder ().build ();
42- try {
43- ValidationResult result = validator .validate (msg );
44- assertThat (result .isSuccess ()).isFalse ();
45- assertThat (result .getViolations ().size ()).isEqualTo (1 );
46- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
47- .isEqualTo ("all map values must equal 1" );
48- } catch (ValidationException e ) {
49- fail ("unexpected exception thrown" , e );
50- }
41+ ValidationResult result = validator .validate (msg );
42+ assertThat (result .isSuccess ()).isFalse ();
43+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
44+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
45+ .isEqualTo ("all map values must equal 1" );
5146 }
5247
5348 // Tests validation works as planned with default builder and config
5449 @ Test
55- public void testDefaultBuilderWithConfig () {
56- Map <Integer , Integer > testMap = new HashMap <Integer , Integer >();
50+ public void testDefaultBuilderWithConfig () throws ValidationException {
51+ Map <Integer , Integer > testMap = new HashMap <>();
5752 testMap .put (42 , 42 );
5853 FieldExpressionMapInt32 msg = FieldExpressionMapInt32 .newBuilder ().putAllVal (testMap ).build ();
5954
6055 Config cfg = Config .newBuilder ().setFailFast (true ).build ();
6156 Validator validator = ValidatorFactory .newBuilder ().withConfig (cfg ).build ();
62- try {
63- ValidationResult result = validator .validate (msg );
64- assertThat (result .isSuccess ()).isFalse ();
65- assertThat (result .getViolations ().size ()).isEqualTo (1 );
66- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
67- .isEqualTo ("all map values must equal 1" );
68- } catch (ValidationException e ) {
69- fail ("unexpected exception thrown" , e );
70- }
57+ ValidationResult result = validator .validate (msg );
58+ assertThat (result .isSuccess ()).isFalse ();
59+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
60+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
61+ .isEqualTo ("all map values must equal 1" );
7162 }
7263
7364 // Tests that if the correct seed descriptors are provided and lazy is disabled,
7465 // validation works as planned.
7566 @ Test
76- public void testSeedDescriptorsLazyDisabled () {
77- Map <Integer , Integer > testMap = new HashMap <Integer , Integer >();
67+ public void testSeedDescriptorsLazyDisabled () throws ValidationException {
68+ Map <Integer , Integer > testMap = new HashMap <>();
7869 testMap .put (42 , 42 );
7970 FieldExpressionMapInt32 msg = FieldExpressionMapInt32 .newBuilder ().putAllVal (testMap ).build ();
8071
81- List <Descriptor > seedDescriptors = new ArrayList <Descriptor >();
72+ List <Descriptor > seedDescriptors = new ArrayList <>();
8273 FieldExpressionMapInt32 reg = FieldExpressionMapInt32 .newBuilder ().build ();
8374 seedDescriptors .add (reg .getDescriptorForType ());
8475
@@ -87,179 +78,142 @@ public void testSeedDescriptorsLazyDisabled() {
8778 // Note that buildWithDescriptors throws the exception so the validator builder
8879 // can be created ahead of time without having to catch an exception.
8980 ValidatorFactory .ValidatorBuilder bldr = ValidatorFactory .newBuilder ().withConfig (cfg );
90- try {
91- Validator validator = bldr .buildWithDescriptors (seedDescriptors , true );
92- ValidationResult result = validator .validate (msg );
93- assertThat (result .isSuccess ()).isFalse ();
94- assertThat (result .getViolations ().size ()).isEqualTo (1 );
95- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
96- .isEqualTo ("all map values must equal 1" );
97- } catch (ValidationException e ) {
98- fail ("unexpected exception thrown" , e );
99- }
81+ Validator validator = bldr .buildWithDescriptors (seedDescriptors , true );
82+ ValidationResult result = validator .validate (msg );
83+ assertThat (result .isSuccess ()).isFalse ();
84+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
85+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
86+ .isEqualTo ("all map values must equal 1" );
10087 }
10188
10289 // Tests that the seed descriptor list is immutable inside the validator and that if
10390 // a descriptor is removed after the validator is created, validation still works as planned.
10491 @ Test
105- public void testSeedDescriptorsImmutable () {
106- Map <Integer , Integer > testMap = new HashMap <Integer , Integer >();
92+ public void testSeedDescriptorsImmutable () throws ValidationException {
93+ Map <Integer , Integer > testMap = new HashMap <>();
10794 testMap .put (42 , 42 );
10895 FieldExpressionMapInt32 msg = FieldExpressionMapInt32 .newBuilder ().putAllVal (testMap ).build ();
10996
110- List <Descriptor > seedDescriptors = new ArrayList <Descriptor >();
97+ List <Descriptor > seedDescriptors = new ArrayList <>();
11198 seedDescriptors .add (msg .getDescriptorForType ());
11299
113100 Config cfg = Config .newBuilder ().setFailFast (true ).build ();
114- try {
115- Validator validator =
116- ValidatorFactory .newBuilder ().withConfig (cfg ).buildWithDescriptors (seedDescriptors , true );
101+ Validator validator =
102+ ValidatorFactory .newBuilder ().withConfig (cfg ).buildWithDescriptors (seedDescriptors , true );
117103
118- // Remove descriptor from list after the validator is created to verify validation still works
119- seedDescriptors .clear ();
104+ // Remove descriptor from list after the validator is created to verify validation still works
105+ seedDescriptors .clear ();
120106
121- ValidationResult result = validator .validate (msg );
122- assertThat (result .isSuccess ()).isFalse ();
123- assertThat (result .getViolations ().size ()).isEqualTo (1 );
124- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
125- .isEqualTo ("all map values must equal 1" );
126- } catch (ValidationException e ) {
127- fail ("unexpected exception thrown" , e );
128- }
107+ ValidationResult result = validator .validate (msg );
108+ assertThat (result .isSuccess ()).isFalse ();
109+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
110+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
111+ .isEqualTo ("all map values must equal 1" );
129112 }
130113
131114 // Tests that if a message is attempted to be validated and it wasn't in the initial
132115 // list of seed descriptors AND lazy is disabled, that a message is returned that
133116 // no evaluator is available.
134117 @ Test
135- public void testSeedDescriptorsWithWrongDescriptorAndLazyDisabled () {
136- Map <Integer , Integer > testMap = new HashMap <Integer , Integer >();
118+ public void testSeedDescriptorsWithWrongDescriptorAndLazyDisabled () throws ValidationException {
119+ Map <Integer , Integer > testMap = new HashMap <>();
137120 testMap .put (42 , 42 );
138121 FieldExpressionMapInt32 msg = FieldExpressionMapInt32 .newBuilder ().putAllVal (testMap ).build ();
139122
140- List <Descriptor > seedDescriptors = new ArrayList <Descriptor >();
123+ List <Descriptor > seedDescriptors = new ArrayList <>();
141124 ExampleFieldRules wrong = ExampleFieldRules .newBuilder ().build ();
142125 seedDescriptors .add (wrong .getDescriptorForType ());
143126
144127 Config cfg = Config .newBuilder ().setFailFast (true ).build ();
145- try {
146- Validator validator =
147- ValidatorFactory .newBuilder ().withConfig (cfg ).buildWithDescriptors (seedDescriptors , true );
148- ValidationResult result = validator .validate (msg );
149- assertThat (result .isSuccess ()).isFalse ();
150- assertThat (result .getViolations ().size ()).isEqualTo (1 );
151- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
152- .isEqualTo ("No evaluator available for " + msg .getDescriptorForType ().getFullName ());
153- } catch (ValidationException e ) {
154- fail ("unexpected exception thrown" , e );
155- }
128+ Validator validator =
129+ ValidatorFactory .newBuilder ().withConfig (cfg ).buildWithDescriptors (seedDescriptors , true );
130+ ValidationResult result = validator .validate (msg );
131+ assertThat (result .isSuccess ()).isFalse ();
132+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
133+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
134+ .isEqualTo ("No evaluator available for " + msg .getDescriptorForType ().getFullName ());
156135 }
157136
158137 // Tests that an IllegalStateException is thrown if an empty descriptor list is given
159138 // and lazy is disabled.
160139 @ Test
161140 public void testEmptySeedDescriptorsInvalidState () {
162- List <Descriptor > seedDescriptors = new ArrayList <Descriptor >();
141+ List <Descriptor > seedDescriptors = new ArrayList <>();
163142 assertThatExceptionOfType (IllegalStateException .class )
164143 .isThrownBy (
165- () -> {
166- ValidatorFactory .newBuilder ().buildWithDescriptors (seedDescriptors , true );
167- });
144+ () -> ValidatorFactory .newBuilder ().buildWithDescriptors (seedDescriptors , true ));
168145 }
169146
170147 // Tests that an IllegalStateException is thrown if a null descriptor list is given
171148 // and lazy is disabled.
172149 @ Test
173150 public void testNullSeedDescriptorsInvalidState () {
174151 assertThatExceptionOfType (IllegalStateException .class )
175- .isThrownBy (
176- () -> {
177- ValidatorFactory .newBuilder ().buildWithDescriptors (null , true );
178- });
152+ .isThrownBy (() -> ValidatorFactory .newBuilder ().buildWithDescriptors (null , true ));
179153 }
180154
181155 // Tests that when an empty list of seed descriptors is provided and lazy is enabled
182156 // that the missing message descriptor is successfully built and validation works as planned.
183157 @ Test
184- public void testEmptySeedDescriptorsLazyEnabled () {
185- Map <Integer , Integer > testMap = new HashMap <Integer , Integer >();
158+ public void testEmptySeedDescriptorsLazyEnabled () throws ValidationException {
159+ Map <Integer , Integer > testMap = new HashMap <>();
186160 testMap .put (42 , 42 );
187161 FieldExpressionMapInt32 msg = FieldExpressionMapInt32 .newBuilder ().putAllVal (testMap ).build ();
188162
189- List <Descriptor > seedDescriptors = new ArrayList <Descriptor >();
163+ List <Descriptor > seedDescriptors = new ArrayList <>();
190164 Config cfg = Config .newBuilder ().setFailFast (true ).build ();
191- try {
192- Validator validator =
193- ValidatorFactory .newBuilder ()
194- .withConfig (cfg )
195- .buildWithDescriptors (seedDescriptors , false );
196- ValidationResult result = validator .validate (msg );
197- assertThat (result .isSuccess ()).isFalse ();
198- assertThat (result .getViolations ().size ()).isEqualTo (1 );
199- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
200- .isEqualTo ("all map values must equal 1" );
201- } catch (ValidationException e ) {
202- fail ("unexpected exception thrown" , e );
203- }
165+ Validator validator =
166+ ValidatorFactory .newBuilder ().withConfig (cfg ).buildWithDescriptors (seedDescriptors , false );
167+ ValidationResult result = validator .validate (msg );
168+ assertThat (result .isSuccess ()).isFalse ();
169+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
170+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
171+ .isEqualTo ("all map values must equal 1" );
204172 }
205173
206174 // Tests that when a null list of seed descriptors is provided, a NullPointerException
207175 // is thrown with a message that descriptors cannot be null.
208176 @ Test
209177 public void testNullSeedDescriptorsLazyEnabled () {
210178 assertThatExceptionOfType (NullPointerException .class )
211- .isThrownBy (
212- () -> {
213- ValidatorFactory .newBuilder ().buildWithDescriptors (null , false );
214- })
179+ .isThrownBy (() -> ValidatorFactory .newBuilder ().buildWithDescriptors (null , false ))
215180 .withMessageContaining ("descriptors must not be null" );
216- ;
217181 }
218182
219183 // Tests that the config is applied when building a validator.
220184 @ Test
221- public void testConfigApplied () {
185+ public void testConfigApplied () throws ValidationException {
222186 // Value must be at most 5 characters and must be lowercase alpha chars or numbers.
223187 FieldExpressionMultiple msg = FieldExpressionMultiple .newBuilder ().setVal ("INVALID" ).build ();
224188
225189 // Set fail fast to true, so we exit after the first validation failure.
226190 Config cfg = Config .newBuilder ().setFailFast (true ).build ();
227- try {
228- Validator validator = ValidatorFactory .newBuilder ().withConfig (cfg ).build ();
229- ValidationResult result = validator .validate (msg );
230- assertThat (result .isSuccess ()).isFalse ();
231- assertThat (result .getViolations ().size ()).isEqualTo (1 );
232- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
233- .isEqualTo ("value length must be at most 5 characters" );
234- } catch (ValidationException e ) {
235- fail ("unexpected exception thrown" , e );
236- }
191+ Validator validator = ValidatorFactory .newBuilder ().withConfig (cfg ).build ();
192+ ValidationResult result = validator .validate (msg );
193+ assertThat (result .isSuccess ()).isFalse ();
194+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
195+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
196+ .isEqualTo ("value length must be at most 5 characters" );
237197 }
238198
239199 // Tests that the config is applied when building a validator with seed descriptors.
240200 @ Test
241- public void testConfigAppliedWithSeedDescriptors () {
201+ public void testConfigAppliedWithSeedDescriptors () throws ValidationException {
242202 // Value must be at most 5 characters and must be lowercase alpha chars or numbers.
243203 FieldExpressionMultiple msg = FieldExpressionMultiple .newBuilder ().setVal ("INVALID" ).build ();
244204
245205 FieldExpressionMultiple desc = FieldExpressionMultiple .newBuilder ().build ();
246- List <Descriptor > seedDescriptors = new ArrayList <Descriptor >();
206+ List <Descriptor > seedDescriptors = new ArrayList <>();
247207 seedDescriptors .add (desc .getDescriptorForType ());
248208
249209 // Set fail fast to true, so we exit after the first validation failure.
250210 Config cfg = Config .newBuilder ().setFailFast (true ).build ();
251- try {
252- Validator validator =
253- ValidatorFactory .newBuilder ()
254- .withConfig (cfg )
255- .buildWithDescriptors (seedDescriptors , false );
256- ValidationResult result = validator .validate (msg );
257- assertThat (result .isSuccess ()).isFalse ();
258- assertThat (result .getViolations ().size ()).isEqualTo (1 );
259- assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
260- .isEqualTo ("value length must be at most 5 characters" );
261- } catch (ValidationException e ) {
262- fail ("unexpected exception thrown" , e );
263- }
211+ Validator validator =
212+ ValidatorFactory .newBuilder ().withConfig (cfg ).buildWithDescriptors (seedDescriptors , false );
213+ ValidationResult result = validator .validate (msg );
214+ assertThat (result .isSuccess ()).isFalse ();
215+ assertThat (result .getViolations ().size ()).isEqualTo (1 );
216+ assertThat (result .getViolations ().get (0 ).toProto ().getMessage ())
217+ .isEqualTo ("value length must be at most 5 characters" );
264218 }
265219}
0 commit comments