View Javadoc
1   /*
2    * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values.strategies.test;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertAll;
8   import static org.junit.jupiter.api.Assertions.assertNotNull;
9   import static org.junit.jupiter.api.Assertions.assertThrows;
10  
11  import org.junit.jupiter.api.Test;
12  
13  import de.powerstat.validation.values.strategies.IPasswordStrategy;
14  import de.powerstat.validation.values.strategies.PasswordConfigurableStrategy;
15  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16  
17  
18  /**
19   * Password configurable strategy tests.
20   */
21  @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
22  final class PasswordConfigurableStrategyTests
23   {
24    /**
25     * Password pattern.
26     */
27    private static final String PWD_PATTERN = "^[@./_0-9a-zA-Z-]+$";
28  
29    /**
30     * Password.
31     */
32    private static final String PWD_1234567890 = "1234567890";
33  
34    /**
35     * Clean strategy not as expected.
36     */
37    private static final String CLEAN_STRATEGY_NOT_AS_EXPECTED = "cleanStrategy not as expected";
38  
39    /**
40     * Illegal argument exception expected constant.
41     */
42    private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception expected"; //$NON-NLS-1$
43  
44    /**
45     * Dummy value 1.
46     */
47    private static final String DUMMY1 = "1111111111"; //$NON-NLS-1$
48  
49  
50    /**
51     * Default constructor.
52     */
53    /* default */ PasswordConfigurableStrategyTests()
54     {
55      super();
56     }
57  
58  
59    /**
60     * Test strategy with minLength to short.
61     */
62    @Test
63    /* default */ void testMinLengthToShort()
64     {
65      assertThrows(IllegalArgumentException.class, () ->
66       {
67        /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(0, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 0);
68       }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
69      );
70     }
71  
72  
73    /**
74     * Test strategy with maxLength < minLength.
75     */
76    @Test
77    /* default */ void testMaxSmallerThanMinLength()
78     {
79      assertThrows(IllegalArgumentException.class, () ->
80       {
81        /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(10, 9, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 0);
82       }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
83      );
84     }
85  
86  
87    /**
88     * Test strategy with maxLength = minLength.
89     */
90    @Test
91    /* default */ void testMaxEqualMinLength()
92     {
93      final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(10, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 0);
94      assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
95     }
96  
97  
98    /**
99     * Test strategy with caching.
100    */
101   @Test
102   /* default */ void testCache()
103    {
104     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(10, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 0);
105     final IPasswordStrategy cleanStrategy2 = PasswordConfigurableStrategy.of(10, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 0);
106     assertAll("testEquals", //$NON-NLS-1$
107       () -> assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED),
108       () -> assertNotNull(cleanStrategy2, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED)
109     );
110    }
111 
112 
113   /**
114    * Test strategy with wrong regexp.
115    */
116   @Test
117   /* default */ void testRegexpWrong1()
118    {
119     assertThrows(IllegalArgumentException.class, () ->
120      {
121       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, "[@./_0-9a-zA-Z-]+", 0, 0, 0, 0, 0, 0); //$NON-NLS-1$
122      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
123     );
124    }
125 
126 
127   /**
128    * Test strategy with wrong regexp.
129    */
130   @Test
131   /* default */ void testRegexpWrong2()
132    {
133     assertThrows(IllegalArgumentException.class, () ->
134      {
135       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, "^[@./_0-9a-zA-Z-]+", 0, 0, 0, 0, 0, 0); //$NON-NLS-1$
136      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
137     );
138    }
139 
140 
141   /**
142    * Test strategy with minNumeric negative.
143    */
144   @Test
145   /* default */ void testMinNumericNegative()
146    {
147     assertThrows(IllegalArgumentException.class, () ->
148      {
149       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, -1, 0, 0, 0, 0, 0);
150      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
151     );
152    }
153 
154 
155   /**
156    * Test strategy with minNumeric maximum.
157    */
158   @Test
159   /* default */ void testMinNumericMaximum()
160    {
161     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 10, 0, 0, 0, 0, 0);
162     cleanStrategy.validationStrategy(PasswordConfigurableStrategyTests.PWD_1234567890);
163     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
164    }
165 
166 
167   /**
168    * Test strategy with minNumeric greater than maxLength.
169    */
170   @Test
171   /* default */ void testMinNumericGreaterMaxLength()
172    {
173     assertThrows(IllegalArgumentException.class, () ->
174      {
175       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 11, 0, 0, 0, 0, 0);
176      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
177     );
178    }
179 
180 
181   /**
182    * Test strategy with minLower greater than maxLength.
183    */
184   @Test
185   /* default */ void testMinLowerGreaterMaxLength()
186    {
187     assertThrows(IllegalArgumentException.class, () ->
188      {
189       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 11, 0, 0, 0, 0);
190      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
191     );
192    }
193 
194 
195   /**
196    * Test strategy with minUpper greater than maxLength.
197    */
198   @Test
199   /* default */ void testMinUpperGreaterMaxLength()
200    {
201     assertThrows(IllegalArgumentException.class, () ->
202      {
203       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 11, 0, 0, 0);
204      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
205     );
206    }
207 
208 
209   /**
210    * Test strategy with minSpecial greater than maxLength.
211    */
212   @Test
213   /* default */ void testMinSpecialGreaterMaxLength()
214    {
215     assertThrows(IllegalArgumentException.class, () ->
216      {
217       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 11, 0, 0);
218      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
219     );
220    }
221 
222 
223   /**
224    * Test strategy with minUniue greater than maxLength.
225    */
226   @Test
227   /* default */ void testMinUnqiueGreaterMaxLength()
228    {
229     assertThrows(IllegalArgumentException.class, () ->
230      {
231       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 11, 0);
232      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
233     );
234    }
235 
236 
237   /**
238    * Test strategy with maxRepeated greater than maxLength.
239    */
240   @Test
241   /* default */ void testMaxRepeatedGreaterMaxLength()
242    {
243     assertThrows(IllegalArgumentException.class, () ->
244      {
245       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 11);
246      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
247     );
248    }
249 
250 
251   /**
252    * Test strategy with minNumeric failure.
253    */
254   @Test
255   /* default */ void testNumericFailure()
256    {
257     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 1, 0, 0, 0, 0, 0);
258     assertThrows(IllegalArgumentException.class, () ->
259      {
260       cleanStrategy.validationStrategy("abcdefghij"); //$NON-NLS-1$
261      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
262     );
263    }
264 
265 
266   /**
267    * Test strategy with minLower negative.
268    */
269   @Test
270   /* default */ void testMinLowerNegative()
271    {
272     assertThrows(IllegalArgumentException.class, () ->
273      {
274       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, -1, 0, 0, 0, 0);
275      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
276     );
277    }
278 
279 
280   /**
281    * Test strategy with minLower maximum.
282    */
283   @Test
284   /* default */ void testMinLowerMaximum()
285    {
286     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(1, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 10, 0, 0, 0, 0);
287     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
288    }
289 
290 
291   /**
292    * Test strategy with minLower failure.
293    */
294   @Test
295   /* default */ void testLowerFailure()
296    {
297     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 1, 0, 0, 0, 0);
298     assertThrows(IllegalArgumentException.class, () ->
299      {
300       cleanStrategy.validationStrategy(PasswordConfigurableStrategyTests.PWD_1234567890);
301      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
302     );
303    }
304 
305 
306   /**
307    * Test strategy with minUpper negative.
308    */
309   @Test
310   /* default */ void testMinUpperNegative()
311    {
312     assertThrows(IllegalArgumentException.class, () ->
313      {
314       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, -1, 0, 0, 0);
315      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
316     );
317    }
318 
319 
320   /**
321    * Test strategy with minUpper maximum.
322    */
323   @Test
324   /* default */ void testMinUpperMaximum()
325    {
326     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(1, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 10, 0, 0, 0);
327     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
328    }
329 
330 
331   /**
332    * Test strategy with minUpper failure.
333    */
334   @Test
335   /* default */ void testUpperFailure()
336    {
337     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 1, 0, 0, 0);
338     assertThrows(IllegalArgumentException.class, () ->
339      {
340       cleanStrategy.validationStrategy(PasswordConfigurableStrategyTests.PWD_1234567890);
341      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
342     );
343    }
344 
345 
346   /**
347    * Test strategy with minSpecial negative.
348    */
349   @Test
350   /* default */ void testMinSpecialNegative()
351    {
352     assertThrows(IllegalArgumentException.class, () ->
353      {
354       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, -1, 0, 0);
355      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
356     );
357    }
358 
359 
360   /**
361    * Test strategy with minSpecial maximum.
362    */
363   @Test
364   /* default */ void testMinSpecialMaximum()
365    {
366     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(1, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 10, 0, 0);
367     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
368    }
369 
370 
371   /**
372    * Test strategy with minSpecial failure.
373    */
374   @Test
375   /* default */ void testSpecialFailure()
376    {
377     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 1, 0, 0);
378     assertThrows(IllegalArgumentException.class, () ->
379      {
380       cleanStrategy.validationStrategy(PasswordConfigurableStrategyTests.PWD_1234567890);
381      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
382     );
383    }
384 
385 
386   /**
387    * Test strategy with minUnique negative.
388    */
389   @Test
390   /* default */ void testMinUnqiueNegative()
391    {
392     assertThrows(IllegalArgumentException.class, () ->
393      {
394       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, -1, 0);
395      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
396     );
397    }
398 
399 
400   /**
401    * Test strategy with minUnique maximum.
402    */
403   @Test
404   /* default */ void testMinUnqiueMaximum()
405    {
406     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(1, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 10, 0);
407     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
408    }
409 
410 
411   /**
412    * Test strategy with minUnique failure.
413    */
414   @Test
415   /* default */ void testUniqueFailure()
416    {
417     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 2, 0);
418     assertThrows(IllegalArgumentException.class, () ->
419      {
420       cleanStrategy.validationStrategy(PasswordConfigurableStrategyTests.DUMMY1);
421      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
422     );
423    }
424 
425 
426   /**
427    * Test strategy with maxRepeated negative.
428    */
429   @Test
430   /* default */ void testMaxRepeatedNegative()
431    {
432     assertThrows(IllegalArgumentException.class, () ->
433      {
434       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 254, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, -1);
435      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
436     );
437    }
438 
439 
440   /**
441    * Test strategy with maxRepeated maximum.
442    */
443   @Test
444   /* default */ void testMaxRepeatedMaximum()
445    {
446     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(1, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 10);
447     cleanStrategy.validationStrategy(PasswordConfigurableStrategyTests.DUMMY1);
448     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
449    }
450 
451 
452   /**
453    * Test strategy with (minNumeric + minLower + minUpper + minSpecial) > maxLength.
454    */
455   @Test
456   /* default */ void testSumWrong()
457    {
458     assertThrows(IllegalArgumentException.class, () ->
459      {
460       /* final IPasswordStrategy cleanStrategy = */ PasswordConfigurableStrategy.of(1, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 3, 3, 3, 3, 0, 0);
461      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
462     );
463    }
464 
465 
466   /**
467    * Test strategy with (minNumeric + minLower + minUpper + minSpecial) == maxLength.
468    */
469   @Test
470   /* default */ void testSumMaximum()
471    {
472     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(1, 12, PasswordConfigurableStrategyTests.PWD_PATTERN, 3, 3, 3, 3, 0, 0);
473     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
474    }
475 
476 
477   /**
478    * Test strategy.
479    */
480   @Test
481   /* default */ void testSuccess()
482    {
483     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 12, PasswordConfigurableStrategyTests.PWD_PATTERN, 1, 1, 1, 1, 4, 1);
484     cleanStrategy.validationStrategy("1aA@1aA@"); //$NON-NLS-1$
485     assertNotNull(cleanStrategy, PasswordConfigurableStrategyTests.CLEAN_STRATEGY_NOT_AS_EXPECTED);
486    }
487 
488 
489   /**
490    * Test strategy with repeated characters.
491    */
492   @Test
493   /* default */ void testRepeatedCharacters()
494    {
495     final IPasswordStrategy cleanStrategy = PasswordConfigurableStrategy.of(8, 10, PasswordConfigurableStrategyTests.PWD_PATTERN, 0, 0, 0, 0, 0, 2);
496     assertThrows(IllegalArgumentException.class, () ->
497      {
498       cleanStrategy.validationStrategy("aaaaaaaa"); //$NON-NLS-1$
499      }, PasswordConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
500     );
501    }
502 
503  }