1
2
3
4 package de.powerstat.validation.values.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertAll;
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.params.ParameterizedTest;
16 import org.junit.jupiter.params.provider.ValueSource;
17
18 import de.powerstat.validation.values.Password;
19 import de.powerstat.validation.values.strategies.IPasswordStrategy;
20 import de.powerstat.validation.values.strategies.PasswordConfigurableStrategy;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23
24
25
26
27 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
28 final class PasswordTests
29 {
30
31
32
33 private static final String PASSWORD = "password";
34
35
36
37
38 private static final String PASSWORD2 = "password2";
39
40
41
42
43 private static final String PASSWORD3 = "password4";
44
45
46
47
48 private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected";
49
50
51
52
53 private static final String PASSWORD_NOT_AS_EXPECTED = "Password not as expected";
54
55
56
57
58 private static final String SECRET_PASSWORD = "********";
59
60
61
62
63
64 PasswordTests()
65 {
66 super();
67 }
68
69
70
71
72
73
74
75 @ParameterizedTest
76 @ValueSource(strings = {"username", "username@example.com", "a2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234"})
77 void testPasswordOk0(final String password)
78 {
79 final Password cleanPassword = Password.of(password);
80 assertTrue(cleanPassword.verifyPassword(password), PasswordTests.PASSWORD_NOT_AS_EXPECTED);
81 }
82
83
84
85
86
87 @Test
88 void testPasswordWrongValidation()
89 {
90 final Password cleanPassword = Password.of(PasswordTests.PASSWORD);
91 assertFalse(cleanPassword.verifyPassword("wrongPassword"), "Password verification not as expected");
92 }
93
94
95
96
97
98
99
100 @ParameterizedTest
101 @ValueSource(strings = {"", "a234567", "a23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"})
102 void testPasswordLength(final String password)
103 {
104 assertThrows(IllegalArgumentException.class, () ->
105 {
106 Password.of(password);
107 }, PasswordTests.ILLEGAL_ARGUMENT
108 );
109 }
110
111
112
113
114
115 @Test
116 void testPasswordWithIllegalCharacters0()
117 {
118 final IPasswordStrategy strategy = PasswordConfigurableStrategy.of(2, 254, "^[!§$%&/()=?öäüÖÄÜ,.:;_@0-9a-zA-Z-]+$", 0, 0, 0, 0, 0, 0);
119 assertThrows(IllegalArgumentException.class, () ->
120 {
121 Password.of(strategy, "\"€");
122 }, PasswordTests.ILLEGAL_ARGUMENT
123 );
124 }
125
126
127
128
129
130 @Test
131 void testStringValueRead()
132 {
133 final Password password = Password.of(PasswordTests.PASSWORD);
134 assertEquals(PasswordTests.PASSWORD, password.stringValue(), PasswordTests.PASSWORD_NOT_AS_EXPECTED);
135 }
136
137
138
139
140
141 @Test
142 void testStringValueNoRead()
143 {
144 final Password password = Password.of(PasswordTests.PASSWORD3, true);
145 assertEquals(SECRET_PASSWORD, password.stringValue(), PasswordTests.PASSWORD_NOT_AS_EXPECTED);
146 }
147
148
149
150
151
152 @Test
153 void testHashCode()
154 {
155 final Password password1 = Password.of(PasswordTests.PASSWORD);
156 final Password password2 = Password.of(PasswordTests.PASSWORD);
157 final Password password3 = Password.of(PasswordTests.PASSWORD2);
158 assertAll("testHashCode",
159 () -> assertEquals(password1.hashCode(), password2.hashCode(), "hashCodes are not equal"),
160 () -> assertNotEquals(password1.hashCode(), password3.hashCode(), "hashCodes are equal")
161 );
162 }
163
164
165
166
167
168 @Test
169 @SuppressWarnings("java:S5785")
170 void testEquals()
171 {
172 final Password password1 = Password.of(PasswordTests.PASSWORD);
173 final Password password2 = Password.of(PasswordTests.PASSWORD);
174 final Password password3 = Password.of(PasswordTests.PASSWORD2);
175 final Password password4 = Password.of(PasswordTests.PASSWORD);
176 assertAll("testEquals",
177 () -> assertTrue(password1.equals(password1), "password11 is not equal"),
178 () -> assertTrue(password1.equals(password2), "password12 are not equal"),
179 () -> assertTrue(password2.equals(password1), "password21 are not equal"),
180 () -> assertTrue(password2.equals(password4), "password24 are not equal"),
181 () -> assertTrue(password1.equals(password4), "password14 are not equal"),
182 () -> assertFalse(password1.equals(password3), "password13 are equal"),
183 () -> assertFalse(password3.equals(password1), "password31 are equal"),
184 () -> assertFalse(password1.equals(null), "password10 is equal")
185 );
186 }
187
188
189
190
191
192 @Test
193 void testToString()
194 {
195 final Password password = Password.of(PasswordTests.PASSWORD);
196 assertEquals("Password[password=********]", password.toString(), "toString not equal");
197 }
198
199
200
201
202
203 @Test
204 @SuppressWarnings("java:S5785")
205 void testCompareTo()
206 {
207 final Password password1 = Password.of(PasswordTests.PASSWORD);
208 final Password password2 = Password.of(PasswordTests.PASSWORD);
209 final Password password3 = Password.of(PasswordTests.PASSWORD2);
210 final Password password4 = Password.of("password3");
211 final Password password5 = Password.of(PasswordTests.PASSWORD);
212 assertAll("testCompareTo",
213 () -> assertTrue(password1.compareTo(password2) == -password2.compareTo(password1), "reflexive1"),
214 () -> assertTrue(password1.compareTo(password3) == -password3.compareTo(password1), "reflexive2"),
215 () -> assertTrue((password4.compareTo(password3) > 0) && (password3.compareTo(password1) > 0) && (password4.compareTo(password1) > 0), "transitive1"),
216 () -> assertTrue((password1.compareTo(password2) == 0) && (Math.abs(password1.compareTo(password5)) == Math.abs(password2.compareTo(password5))), "sgn1"),
217 () -> assertTrue((password1.compareTo(password2) == 0) && password1.equals(password2), "equals")
218 );
219 }
220
221 }