1
2
3
4 package de.powerstat.validation.values.strategies.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertEquals;
8 import static org.junit.jupiter.api.Assertions.assertFalse;
9 import static org.junit.jupiter.api.Assertions.assertNotNull;
10 import static org.junit.jupiter.api.Assertions.assertThrows;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import org.junit.jupiter.api.Test;
14
15 import de.powerstat.validation.values.strategies.IUsernameStrategy;
16 import de.powerstat.validation.values.strategies.UsernameConfigurableStrategy;
17 import de.powerstat.validation.values.strategies.UsernameConfigurableStrategy.HandleEMail;
18
19
20
21
22
23 final class UsernameConfigurableStrategyTests
24 {
25
26
27
28 private static final String PATTERN1 = "^[@./_0-9a-zA-Z-]+$";
29
30
31
32
33 private static final String PATTERN2 = "^[@./_0-9a-zA-Z-]*$";
34
35
36
37
38 private static final String USERNAME = "username";
39
40
41
42
43 private static final String USERNAME_EXAMPLE_COM = "username@example.com";
44
45
46
47
48 private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception expected";
49
50
51
52
53 private static final String CLEAN_STRATEGY_NOT_AS_EXPECTED = "cleanStrategy not as expected";
54
55
56
57
58 private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
59
60
61
62
63
64 UsernameConfigurableStrategyTests()
65 {
66 super();
67 }
68
69
70
71
72
73 @Test
74 void testConstructor1()
75 {
76 assertThrows(NullPointerException.class, () ->
77 {
78 UsernameConfigurableStrategy.of(0, 254, null, HandleEMail.EMAIL_POSSIBLE);
79 }, UsernameConfigurableStrategyTests.NULL_POINTER_EXCEPTION
80 );
81 }
82
83
84
85
86
87 @Test
88 void testConstructor2()
89 {
90 assertThrows(NullPointerException.class, () ->
91 {
92 UsernameConfigurableStrategy.of(0, 254, UsernameConfigurableStrategyTests.PATTERN2, null);
93 }, UsernameConfigurableStrategyTests.NULL_POINTER_EXCEPTION
94 );
95 }
96
97
98
99
100
101 @Test
102 void testMinLengthToShort()
103 {
104 assertThrows(IllegalArgumentException.class, () ->
105 {
106 UsernameConfigurableStrategy.of(-1, 254, UsernameConfigurableStrategyTests.PATTERN2, HandleEMail.EMAIL_POSSIBLE);
107 }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
108 );
109 }
110
111
112
113
114
115 @Test
116 void testMinLengthZero()
117 {
118 final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(0, 254, UsernameConfigurableStrategyTests.PATTERN2, HandleEMail.EMAIL_POSSIBLE);
119 assertNotNull(cleanStrategy, UsernameConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
120 }
121
122
123
124
125
126 @Test
127 void testMaxSmallerThanMinLength()
128 {
129 assertThrows(IllegalArgumentException.class, () ->
130 {
131 UsernameConfigurableStrategy.of(10, 9, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_POSSIBLE);
132 }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
133 );
134 }
135
136
137
138
139
140 @Test
141 void testMaxEqualMinLength()
142 {
143 final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(10, 10, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_POSSIBLE);
144 assertNotNull(cleanStrategy, UsernameConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
145 }
146
147
148
149
150
151 @Test
152 void testRegexpWrong1()
153 {
154 assertThrows(IllegalArgumentException.class, () ->
155 {
156 UsernameConfigurableStrategy.of(1, 254, "[@./_0-9a-zA-Z-]+", HandleEMail.EMAIL_REQUIRED);
157 }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
158 );
159 }
160
161
162
163
164
165 @Test
166 void testRegexpWrong2()
167 {
168 assertThrows(IllegalArgumentException.class, () ->
169 {
170 UsernameConfigurableStrategy.of(1, 254, "^[@./_0-9a-zA-Z-]+", HandleEMail.EMAIL_REQUIRED);
171 }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
172 );
173 }
174
175
176
177
178
179 @Test
180 void testUsernameEMailDenied1()
181 {
182 final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(1, 254, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_DENIED);
183 assertThrows(IllegalArgumentException.class, () ->
184 {
185 cleanStrategy.validationStrategy(UsernameConfigurableStrategyTests.USERNAME_EXAMPLE_COM);
186 }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
187 );
188 }
189
190
191
192
193
194 @Test
195 void testUsernameEMailDenied2()
196 {
197 final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(1, 254, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_DENIED);
198 final boolean email = cleanStrategy.validationStrategy(UsernameConfigurableStrategyTests.USERNAME);
199 assertFalse(email, "email validated");
200 }
201
202
203
204
205
206 @Test
207 void testUsernameEMailRequired1()
208 {
209 final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(1, 254, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_REQUIRED);
210 assertThrows(IllegalArgumentException.class, () ->
211 {
212 cleanStrategy.validationStrategy(UsernameConfigurableStrategyTests.USERNAME);
213 }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
214 );
215 }
216
217
218
219
220
221 @Test
222 void testUsernameEMailRequired2()
223 {
224 final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(1, 254, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_REQUIRED);
225 final boolean email = cleanStrategy.validationStrategy(UsernameConfigurableStrategyTests.USERNAME_EXAMPLE_COM);
226 assertTrue(email, "email not validated");
227 }
228
229
230
231
232
233 @Test
234 void testGetAction()
235 {
236 assertEquals(1, HandleEMail.EMAIL_REQUIRED.getAction(), "Action not as expected");
237 }
238
239 }