1
+ package org .javaee8 .validation ;
2
+
3
+ import org .jboss .arquillian .container .test .api .Deployment ;
4
+ import org .jboss .arquillian .junit .Arquillian ;
5
+ import org .jboss .shrinkwrap .api .spec .JavaArchive ;
6
+ import org .jboss .shrinkwrap .api .spec .WebArchive ;
7
+ import org .junit .Before ;
8
+ import org .junit .Test ;
9
+ import org .junit .runner .RunWith ;
10
+
11
+ import javax .validation .ConstraintViolation ;
12
+ import javax .validation .Validation ;
13
+ import javax .validation .Validator ;
14
+ import javax .validation .groups .Default ;
15
+ import java .time .*;
16
+ import java .util .Arrays ;
17
+ import java .util .Optional ;
18
+ import java .util .Set ;
19
+
20
+ import static org .hamcrest .CoreMatchers .*;
21
+ import static org .jboss .shrinkwrap .api .ShrinkWrap .create ;
22
+ import static org .junit .Assert .assertThat ;
23
+
24
+ /**
25
+ * @author mertcaliskan
26
+ */
27
+ @ RunWith (Arquillian .class )
28
+ public class ConstraintViolationTest {
29
+
30
+ private Validator validator ;
31
+
32
+ @ Before
33
+ public void setUpValidator () {
34
+ validator = Validation
35
+ .byDefaultProvider ()
36
+ .configure ()
37
+ .clockProvider (
38
+ () -> Clock .fixed (
39
+ Instant .parse ("2017-01-01T00:00:00.00Z" ),
40
+ ZoneId .systemDefault ()))
41
+ .buildValidatorFactory ()
42
+ .getValidator ();
43
+ }
44
+
45
+ @ Deployment
46
+ public static WebArchive deploy () {
47
+ return create (WebArchive .class )
48
+ .addAsLibraries (
49
+ create (JavaArchive .class ).addClasses (Person .class ,
50
+ Admin .class ,
51
+ Country .class ,
52
+ Address .class ));
53
+ }
54
+
55
+ @ Test
56
+ public void validatingBirthDateFailsWithViolation () {
57
+ Person person = new Person ();
58
+ person .setYearOfBirth (Year .of (2018 ));
59
+
60
+ Set <ConstraintViolation <Person >> violations = validator .validate (person );
61
+
62
+ assertThat (violations , is (not (nullValue ())));
63
+ assertThat (violations .size (), is (1 ));
64
+ assertThat (violations .iterator ().next ().getMessage (),
65
+ is ("must be in the past" ));
66
+ }
67
+
68
+ public void validatingMarriageAnniversaryDateFailsWithViolation () {
69
+ Person person = new Person ();
70
+ person .setMarriageAnniversary (Optional .of (LocalDate .MAX ));
71
+
72
+ Set <ConstraintViolation <Person >> violations = validator .validate (person );
73
+
74
+ assertThat (violations , is (not (nullValue ())));
75
+ assertThat (violations .size (), is (1 ));
76
+ assertThat (violations .iterator ().next ().getMessage (),
77
+ is ("must be in the past" ));
78
+ }
79
+
80
+ @ Test
81
+ public void validatingEmailsFailsWithViolation () {
82
+ Person person = new Person ();
83
+ person .
setEmails (
Arrays .
asList (
"[email protected] " ,
"invalid_mail" ));
84
+
85
+ Set <ConstraintViolation <Person >> violations = validator .validate (person );
86
+
87
+ assertThat (violations , is (not (nullValue ())));
88
+ assertThat (violations .size (), is (1 ));
89
+ assertThat (violations .iterator ().next ().getMessage (),
90
+ is ("must be a well-formed email address" ));
91
+ }
92
+
93
+ @ Test
94
+ public void validatingPassswordWithDefaultGroupFailsWithViolation () {
95
+ Person person = new Person ();
96
+ person .setPassword ("1234567" );
97
+
98
+ Set <ConstraintViolation <Person >> violations = validator .validate (person , Default .class );
99
+
100
+ assertThat (violations , is (not (nullValue ())));
101
+ assertThat (violations .size (), is (1 ));
102
+ assertThat (violations .iterator ().next ().getMessage (),
103
+ is ("size must be between 8 and 2147483647" ));
104
+ }
105
+
106
+ @ Test
107
+ public void validatingPassswordWithAdminGroupFailsWithViolation () {
108
+ Person person = new Person ();
109
+ person .setPassword ("1234567" );
110
+
111
+ Set <ConstraintViolation <Person >> violations = validator .validate (person , Admin .class );
112
+
113
+ assertThat (violations , is (not (nullValue ())));
114
+ assertThat (violations .size (), is (1 ));
115
+ assertThat (violations .iterator ().next ().getMessage (),
116
+ is ("size must be between 12 and 2147483647" ));
117
+ }
118
+
119
+ @ Test
120
+ public void validatingAddressFailsWithViolation () {
121
+ Person person = new Person ();
122
+ Country country = new Country ();
123
+ country .setCountryCode ("ABC" );
124
+ Address address = new Address ();
125
+ address .setDetail ("" );
126
+ person .getAddressMap ().put (country , address );
127
+
128
+ Set <ConstraintViolation <Person >> violations = validator .validate (person );
129
+
130
+ assertThat (violations , is (not (nullValue ())));
131
+ assertThat (violations .size (), is (2 ));
132
+ assertThat (violations .iterator ().next ().getMessage (),
133
+ anyOf (is ("must not be empty" ), is ("size must be between 2 and 2" )));
134
+ assertThat (violations .iterator ().next ().getMessage (),
135
+ anyOf (is ("must not be empty" ), is ("size must be between 2 and 2" )));
136
+ }
137
+ }
0 commit comments