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.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   * Username configurable strategy tests.
22   */
23  final class UsernameConfigurableStrategyTests
24   {
25    /**
26     * Match pattern 1.
27     */
28    private static final String PATTERN1 = "^[@./_0-9a-zA-Z-]+$"; //$NON-NLS-1$
29  
30    /**
31     * Match pattern 2.
32     */
33    private static final String PATTERN2 = "^[@./_0-9a-zA-Z-]*$"; //$NON-NLS-1$
34  
35    /**
36     * Username constant.
37     */
38    private static final String USERNAME = "username"; //$NON-NLS-1$
39  
40    /**
41     * EMail address constant.
42     */
43    private static final String USERNAME_EXAMPLE_COM = "username@example.com"; //$NON-NLS-1$
44  
45    /**
46     * Illegal argument exception expected constant.
47     */
48    private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception expected"; //$NON-NLS-1$
49  
50    /**
51     * cleanStrategy not as expected constant.
52     */
53    private static final String CLEAN_STRATEGY_NOT_AS_EXPECTED = "cleanStrategy not as expected"; //$NON-NLS-1$
54  
55    /**
56     * Null pointer exception constanr.
57     */
58    private static final String NULL_POINTER_EXCEPTION = "Null pointer exception"; //$NON-NLS-1$
59  
60  
61    /**
62     * Default constructor.
63     */
64    /* default */ UsernameConfigurableStrategyTests()
65     {
66      super();
67     }
68  
69  
70    /**
71     * Test constructor.
72     */
73    @Test
74    /* default */ void testConstructor1()
75     {
76      assertThrows(NullPointerException.class, () ->
77       {
78        /* final IUsernameStrategy cleanStrategy = */ UsernameConfigurableStrategy.of(0, 254, null, HandleEMail.EMAIL_POSSIBLE);
79       }, UsernameConfigurableStrategyTests.NULL_POINTER_EXCEPTION
80      );
81     }
82  
83  
84    /**
85     * Test constructor.
86     */
87    @Test
88    /* default */ void testConstructor2()
89     {
90      assertThrows(NullPointerException.class, () ->
91       {
92        /* final IUsernameStrategy cleanStrategy = */ UsernameConfigurableStrategy.of(0, 254, UsernameConfigurableStrategyTests.PATTERN2, null);
93       }, UsernameConfigurableStrategyTests.NULL_POINTER_EXCEPTION
94      );
95     }
96  
97  
98    /**
99     * Test strategy with minLength to short.
100    */
101   @Test
102   /* default */ void testMinLengthToShort()
103    {
104     assertThrows(IllegalArgumentException.class, () ->
105      {
106       /* final IUsernameStrategy cleanStrategy = */ UsernameConfigurableStrategy.of(-1, 254, UsernameConfigurableStrategyTests.PATTERN2, HandleEMail.EMAIL_POSSIBLE);
107      }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
108     );
109    }
110 
111 
112   /**
113    * Test strategy with minLength zero.
114    */
115   @Test
116   /* default */ 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    * Test strategy with maxLength < minLength.
125    */
126   @Test
127   /* default */ void testMaxSmallerThanMinLength()
128    {
129     assertThrows(IllegalArgumentException.class, () ->
130      {
131       /* final IUsernameStrategy cleanStrategy = */ UsernameConfigurableStrategy.of(10, 9, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_POSSIBLE);
132      }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
133     );
134    }
135 
136 
137   /**
138    * Test strategy with maxLength = minLength.
139    */
140   @Test
141   /* default */ 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    * Test strategy with wrong regexp.
150    */
151   @Test
152   /* default */ void testRegexpWrong1()
153    {
154     assertThrows(IllegalArgumentException.class, () ->
155      {
156       /* final IUsernameStrategy cleanStrategy = */ UsernameConfigurableStrategy.of(1, 254, "[@./_0-9a-zA-Z-]+", HandleEMail.EMAIL_REQUIRED); //$NON-NLS-1$
157      }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
158     );
159    }
160 
161 
162   /**
163    * Test strategy with wrong regexp.
164    */
165   @Test
166   /* default */ void testRegexpWrong2()
167    {
168     assertThrows(IllegalArgumentException.class, () ->
169      {
170       /* final IUsernameStrategy cleanStrategy = */ UsernameConfigurableStrategy.of(1, 254, "^[@./_0-9a-zA-Z-]+", HandleEMail.EMAIL_REQUIRED); //$NON-NLS-1$
171      }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
172     );
173    }
174 
175 
176   /**
177    * Test strategy with email denied.
178    */
179   @Test
180   /* default */ void testUsernameEMailDenied1()
181    {
182     final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(1, 254, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_DENIED);
183     assertThrows(IllegalArgumentException.class, () ->
184      {
185       /* boolean email = */ cleanStrategy.validationStrategy(UsernameConfigurableStrategyTests.USERNAME_EXAMPLE_COM);
186      }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
187     );
188    }
189 
190 
191   /**
192    * Test strategy with email denied.
193    */
194   @Test
195   /* default */ 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"); //$NON-NLS-1$
200    }
201 
202 
203   /**
204    * Test strategy with email required.
205    */
206   @Test
207   /* default */ void testUsernameEMailRequired1()
208    {
209     final IUsernameStrategy cleanStrategy = UsernameConfigurableStrategy.of(1, 254, UsernameConfigurableStrategyTests.PATTERN1, HandleEMail.EMAIL_REQUIRED);
210     assertThrows(IllegalArgumentException.class, () ->
211      {
212       /* boolean email = */ cleanStrategy.validationStrategy(UsernameConfigurableStrategyTests.USERNAME);
213      }, UsernameConfigurableStrategyTests.ILLEGAL_ARGUMENT_EXCEPTION
214     );
215    }
216 
217 
218   /**
219    * Test strategy with email required.
220    */
221   @Test
222   /* default */ 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"); //$NON-NLS-1$
227    }
228 
229 
230   /**
231    * Test getAction of HandleEMail.
232    */
233   @Test
234   /* default */ void testGetAction()
235    {
236     assertEquals(1, HandleEMail.EMAIL_REQUIRED.getAction(), "Action not as expected"); //$NON-NLS-1$
237    }
238 
239  }