Commit 7603ab4d94a4d8adc0cd441290f590e94ed9ca56

Authored by Thomas Ziemer
1 parent 35cdab48

test: Tests (außer Delete-Tests) fertig gemacht

README.md
... ... @@ -222,4 +222,14 @@ Quelle: https://tools.jboss.org/downloads/jbosstools/neon/4.4.3.Final.html
222 222  
223 223 ## Projekt verwenden
224 224  
225   -Beim Deployen des Projekts auf den Application Server wird automatisch der Benutzer "admin" mit dem Kennwort "admin" erstellt und diesem die Rolle "Adminrolle" zugewiesen. Mit ihm kann man sich anmelden und erste Übungen vornehmen.
226 225 \ No newline at end of file
  226 +In der Entwicklungsumgebung:
  227 +
  228 +Beim Deployen des Projekts auf den Application Server wird durch einen Data Bootstrapping-Mechanismus automatisch der Benutzer "admin" mit dem Kennwort "admin" erstellt und diesem die Rolle "Adminrolle" zugewiesen. Mit ihm kann man sich anmelden und erste Übungen vornehmen. Hierzu verwendet man den REST-Client "Advanced Rest Client Application" für den Chrome Browser.
  229 +
  230 +Nach der Implementierung von JUnit-Tests auf der Kommandozeile:
  231 +
  232 +> mvn clean install
  233 +
  234 +Das Projekt wird gebaut und die Tests werden ausgeführt. Möchte man die Tests gelegentlich mal nicht ausführen, macht man:
  235 +
  236 +> mvn clean install -DskipTests
227 237 \ No newline at end of file
... ...
src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
1 1 package net.ziemers.swxercise.lg.user.service;
2 2  
3 3 import java.util.Arrays;
  4 +import java.util.Collection;
4 5 import java.util.HashSet;
5 6 import java.util.Set;
6 7 import javax.inject.Inject;
... ... @@ -27,6 +28,8 @@ import org.junit.runner.RunWith;
27 28 * To test classes in isolation we shouldn't be using their dependencies.
28 29 * Instead we should be using a mock. There are many mocking libraries out
29 30 * there, however CDI-Unit has extra support for Mockito @Mock annotations.
  31 + *
  32 + * Quelle: http://jglue.org/cdi-unit-user-guide/
30 33 */
31 34 @RunWith(CdiRunner.class)
32 35 public class UserServiceTest extends JpaTestUtils {
... ... @@ -42,6 +45,8 @@ public class UserServiceTest extends JpaTestUtils {
42 45  
43 46 private boolean actual;
44 47  
  48 + private Collection<User> actualUsers;
  49 +
45 50 private User actualUser;
46 51  
47 52 @Inject
... ... @@ -65,10 +70,11 @@ public class UserServiceTest extends JpaTestUtils {
65 70 /*
66 71 * In order to inject @SessionScoped beans, one has to annotate the function or class with @InRequestScope
67 72 * as only this annotation guarantees to have the session scope active always.
  73 + * Quelle: https://github.com/BrynCooke/cdi-unit/issues/82
68 74 */
69 75 @Test
70 76 @InRequestScope
71   - public void testLoginUserReturnsSuccess() {
  77 + public void testLoginUserSucceeds() {
72 78  
73 79 given()
74 80 .userDto(EXISTING_USERNAME_TEST);
... ... @@ -77,12 +83,12 @@ public class UserServiceTest extends JpaTestUtils {
77 83 .loginUser(EXISTING_PASSWORD_TEST);
78 84  
79 85 then()
80   - .assertLoginSuccess();
  86 + .assertLoginUserSucceeded();
81 87 }
82 88  
83 89 @Test
84 90 @InRequestScope
85   - public void testLogoutUserSuccess() {
  91 + public void testLogoutUserSucceeds() {
86 92  
87 93 given()
88 94 .userDto(EXISTING_USERNAME_TEST)
... ... @@ -92,22 +98,22 @@ public class UserServiceTest extends JpaTestUtils {
92 98 .logoutUser();
93 99  
94 100 then()
95   - .assertLogoutSuccess();
  101 + .assertLogoutUserSucceeded();
96 102 }
97 103  
98 104 @Test
99   - public void testFindUserById() {
  105 + public void testFindUserByIdSucceeds() {
100 106  
101 107 when()
102 108 .findUser(EXISTING_USER_ID);
103 109  
104 110 then()
105   - .assertFindUserByIdSuccess(EXISTING_USER_ID);
  111 + .assertFindUserByIdSucceeded(EXISTING_USER_ID);
106 112 }
107 113  
108 114 @Test
109 115 @InRequestScope
110   - public void testFindUser() {
  116 + public void testFindSessionUserSucceeds() {
111 117  
112 118 given()
113 119 .userDto(EXISTING_USERNAME_TEST)
... ... @@ -117,16 +123,21 @@ public class UserServiceTest extends JpaTestUtils {
117 123 .findUser();
118 124  
119 125 then()
120   - .assertFindUserByIdSuccess(EXISTING_USER_ID);
  126 + .assertFindUserByIdSucceeded(EXISTING_USER_ID);
121 127 }
122 128  
123 129 @Test
124   - public void testFindAllUsers() {
125   - // TODO Test ist noch zu implementieren
  130 + public void testFindAllUsersSucceeds() {
  131 +
  132 + when()
  133 + .findAllUsers();
  134 +
  135 + then()
  136 + .assertFindAllUsersSucceeded();
126 137 }
127 138  
128 139 @Test
129   - public void testCreateUserReturnsSuccess() {
  140 + public void testCreateUserSucceeds() {
130 141  
131 142 given()
132 143 .userDto(USERNAME_TEST);
... ... @@ -135,11 +146,11 @@ public class UserServiceTest extends JpaTestUtils {
135 146 .createUser();
136 147  
137 148 then()
138   - .assertCreateSuccess();
  149 + .assertCreateUserSucceeded();
139 150 }
140 151  
141 152 @Test
142   - public void testCreateUserReturnsFailure() {
  153 + public void testCreateAlreadyExistingUserFails() {
143 154  
144 155 given()
145 156 .userDto(EXISTING_USERNAME_TEST);
... ... @@ -148,7 +159,7 @@ public class UserServiceTest extends JpaTestUtils {
148 159 .createUser();
149 160  
150 161 then()
151   - .assertCreateFailure();
  162 + .assertCreateUserFailed();
152 163 }
153 164  
154 165 @Test
... ... @@ -161,49 +172,49 @@ public class UserServiceTest extends JpaTestUtils {
161 172 .updateUser(EXISTING_USER_ID);
162 173  
163 174 then()
164   - .assertUpdateSuccess();
  175 + .assertUpdateUserSucceeded();
165 176 }
166 177  
167 178 @Test
168   - public void testDeleteUserByIdReturnsSuccess() {
  179 + public void testDeleteUserByIdSucceeds() {
169 180 // TODO Test ist noch zu implementieren
170 181 }
171 182  
172 183 @Test
173   - public void testDeleteUnknownUserByIdReturnsFailure() {
  184 + public void testDeleteUnknownUserByIdFails() {
174 185 // TODO Test ist noch zu implementieren
175 186 }
176 187  
177 188 @Test
178   - public void testDeleteSessionUserReturnsSuccess() {
  189 + public void testDeleteSessionUserSucceeds() {
179 190 // TODO Test ist noch zu implementieren
180 191 }
181 192  
182 193 @Test
183 194 @InRequestScope
184   - public void testIsNotLoggedInUserAllowedReturnsSuccess() {
  195 + public void testIsNotLoggedInUserAllowedWithNotLoggedInRightSucceeds() {
185 196  
186 197 when()
187 198 .isUserAllowed(new HashSet<String>(Arrays.asList("NOT_LOGGED_IN")));
188 199  
189 200 then()
190   - .assertIsUserAllowedSuccess();
  201 + .assertIsUserAllowedSucceeded();
191 202 }
192 203  
193 204 @Test
194 205 @InRequestScope
195   - public void testIsNotLoggedInUserAllowedReturnsFailure() {
  206 + public void testIsNotLoggedInUserAllowedWithLoggedInRightFails() {
196 207  
197 208 when()
198 209 .isUserAllowed(new HashSet<String>(Arrays.asList("LOGGED_IN")));
199 210  
200 211 then()
201   - .assertIsUserAllowedFailure();
  212 + .assertIsUserAllowedFailed();
202 213 }
203 214  
204 215 @Test
205 216 @InRequestScope
206   - public void testIsSessionUserAllowedReturnsSuccess() {
  217 + public void testIsSessionUserAllowedWithLoggedInRightSucceeds() {
207 218  
208 219 given()
209 220 .userDto(EXISTING_USERNAME_TEST)
... ... @@ -213,12 +224,27 @@ public class UserServiceTest extends JpaTestUtils {
213 224 .isUserAllowed(new HashSet<String>(Arrays.asList("LOGGED_IN")));
214 225  
215 226 then()
216   - .assertIsUserAllowedSuccess();
  227 + .assertIsUserAllowedSucceeded();
217 228 }
218 229  
219 230 @Test
220 231 @InRequestScope
221   - public void testIsAdminUserAllowedReturnsSuccess() {
  232 + public void testIsSessionUserAllowedWithNotLoggedInRightFails() {
  233 +
  234 + given()
  235 + .userDto(EXISTING_USERNAME_TEST)
  236 + .loginUser(EXISTING_PASSWORD_TEST);
  237 +
  238 + when()
  239 + .isUserAllowed(new HashSet<String>(Arrays.asList("NOT_LOGGED_IN")));
  240 +
  241 + then()
  242 + .assertIsUserAllowedFailed();
  243 + }
  244 +
  245 + @Test
  246 + @InRequestScope
  247 + public void testIsAdminUserAllowedWithAdminRightSucceeds() {
222 248  
223 249 given()
224 250 .userDto(EXISTING_USERNAME_TEST)
... ... @@ -228,12 +254,12 @@ public class UserServiceTest extends JpaTestUtils {
228 254 .isUserAllowed(new HashSet<String>(Arrays.asList("ADMIN")));
229 255  
230 256 then()
231   - .assertIsUserAllowedSuccess();
  257 + .assertIsUserAllowedSucceeded();
232 258 }
233 259  
234 260 @Test
235 261 @InRequestScope
236   - public void testIsUserWithUnknownRoleAllowedReturnsFailure() {
  262 + public void testIsUserWithUnknownRoleAllowedFails() {
237 263  
238 264 given()
239 265 .userDto(EXISTING_USERNAME_TEST)
... ... @@ -243,7 +269,7 @@ public class UserServiceTest extends JpaTestUtils {
243 269 .isUserAllowed(new HashSet<String>(Arrays.asList("UNKNOWN_ROLE")));
244 270  
245 271 then()
246   - .assertIsUserAllowedFailure();
  272 + .assertIsUserAllowedFailed();
247 273 }
248 274  
249 275 // given
... ... @@ -302,6 +328,11 @@ public class UserServiceTest extends JpaTestUtils {
302 328 return this;
303 329 }
304 330  
  331 + private UserServiceTest findAllUsers() {
  332 + actualUsers = underTest.findAllUsers();
  333 + return this;
  334 + }
  335 +
305 336 private UserServiceTest isUserAllowed(final Set<String> rightsSet) {
306 337 actual = underTest.isUserAllowed(rightsSet);
307 338 return this;
... ... @@ -313,44 +344,50 @@ public class UserServiceTest extends JpaTestUtils {
313 344 return this;
314 345 }
315 346  
316   - private void assertLoginSuccess() {
  347 + private void assertLoginUserSucceeded() {
317 348 // die Login-Methode lieferte Erfolg zurück, und es gibt einen angemeldeten Benutzer
318 349 assertTrue(actual);
319 350 assertNotNull(sessionContext.getUser());
320 351 }
321 352  
322   - private void assertLogoutSuccess() {
  353 + private void assertLogoutUserSucceeded() {
323 354 // die Logout-Methode lieferte Erfolg zurück, und es gibt keinen angemeldeten Benutzer
324 355 assertTrue(actual);
325 356 assertNull(sessionContext.getUser());
326 357 }
327 358  
328   - private void assertFindUserByIdSuccess(final Long expectedId) {
  359 + private void assertFindUserByIdSucceeded(final Long expectedId) {
329 360 assertEquals(expectedId, actualUser.getId());
330 361 }
331 362  
332   - private void assertCreateSuccess() {
  363 + private void assertFindAllUsersSucceeded() {
  364 + // in unseren Testdaten befinden sich zwei Benutzer
  365 + final int EXISTING_USERS_COUNT = 2;
  366 + assertEquals(EXISTING_USERS_COUNT, actualUsers.size());
  367 + }
  368 +
  369 + private void assertCreateUserSucceeded() {
333 370 // wir suchen den soeben erstellten Benutzer; wenn er existiert, ist alles gut
334 371 final User user = userDao.findByUsername(USERNAME_TEST);
335 372 assertNotNull(user);
336 373 }
337 374  
338   - private void assertCreateFailure() {
  375 + private void assertCreateUserFailed() {
339 376 // es darf kein neuer Benutzer mit identischem Benutzernamen erstellt worden sein
340 377 assertFalse(actual);
341 378 }
342 379  
343   - private void assertUpdateSuccess() {
  380 + private void assertUpdateUserSucceeded() {
344 381 // wir suchen den soeben aktualisierten Benutzer; wenn sein Benutzername unverändert ist, ist alles gut
345 382 final User user = userDao.findById(EXISTING_USER_ID);
346 383 assertEquals(EXISTING_USERNAME_TEST, user.getProfile().getUsername());
347 384 }
348 385  
349   - private void assertIsUserAllowedSuccess() {
  386 + private void assertIsUserAllowedSucceeded() {
350 387 assertTrue(actual);
351 388 }
352 389  
353   - private void assertIsUserAllowedFailure() {
  390 + private void assertIsUserAllowedFailed() {
354 391 assertFalse(actual);
355 392 }
356 393  
... ...
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
... ... @@ -47,10 +47,13 @@ public class UserViewControllerTest {
47 47 private User actualUser;
48 48  
49 49 @Test
50   - public void testJUnitFrameworkReturnsTrue() {
  50 + public void testJUnitFrameworkSucceeds() {
51 51 assertTrue(true);
52 52 }
53 53  
  54 + /**
  55 + * Hier sieht man eine erwartete Exception
  56 + */
54 57 @Test(expected = AssertionError.class)
55 58 public void testJUnitFrameworkThrowsException() {
56 59 assertTrue(false);
... ... @@ -63,7 +66,7 @@ public class UserViewControllerTest {
63 66 .getAllUsers();
64 67  
65 68 then()
66   - .assertGetAllUsersSuccess();
  69 + .assertGetAllUsersSucceeded();
67 70 }
68 71  
69 72 @Test
... ... @@ -73,21 +76,21 @@ public class UserViewControllerTest {
73 76 .getUserById(EXISTING_USER_ID);
74 77  
75 78 then()
76   - .assertGetUserByIdSuccess();
  79 + .assertGetUserByIdSucceeded();
77 80 }
78 81  
79 82 @Test
80   - public void testGetSessionUserReturnsUserJson() {
  83 + public void testGetSessionUserReturnsSessionUserJson() {
81 84  
82 85 doing()
83 86 .getUser();
84 87  
85 88 then()
86   - .assertGetUserSuccess();
  89 + .assertGetUserSucceeded();
87 90 }
88 91  
89 92 @Test
90   - public void testCreateUserReturnsSuccess() {
  93 + public void testCreateUserSucceeds() {
91 94  
92 95 given()
93 96 .userDto();
... ... @@ -96,11 +99,11 @@ public class UserViewControllerTest {
96 99 .createUser(true);
97 100  
98 101 then()
99   - .assertCreateSuccess();
  102 + .assertCreateUserSucceeded();
100 103 }
101 104  
102 105 @Test
103   - public void testCreateUserReturnsFailure() {
  106 + public void testCreateUserFails() {
104 107  
105 108 given()
106 109 .userDto();
... ... @@ -109,11 +112,11 @@ public class UserViewControllerTest {
109 112 .createUser(false);
110 113  
111 114 then()
112   - .assertCreateFailure();
  115 + .assertCreateUserFailed();
113 116 }
114 117  
115 118 @Test
116   - public void testUpdateSessionUserReturnsSuccess() {
  119 + public void testUpdateSessionUserSucceeds() {
117 120  
118 121 given()
119 122 .userDto();
... ... @@ -122,11 +125,11 @@ public class UserViewControllerTest {
122 125 .updateUser();
123 126  
124 127 then()
125   - .assertUpdateSuccess();
  128 + .assertUpdateUserSucceeded();
126 129 }
127 130  
128 131 @Test
129   - public void testUpdateUserByIdReturnsSuccess() {
  132 + public void testUpdateUserByIdSucceeds() {
130 133  
131 134 given()
132 135 .userDto();
... ... @@ -135,26 +138,26 @@ public class UserViewControllerTest {
135 138 .updateUser(EXISTING_USER_ID);
136 139  
137 140 then()
138   - .assertUpdateSuccess();
  141 + .assertUpdateUserSucceeded();
139 142 }
140 143  
141 144 @Test
142   - public void testDeleteUserByIdReturnsSuccess() {
  145 + public void testDeleteUserByIdSucceeds() {
143 146 // TODO Test ist noch zu implementieren
144 147 }
145 148  
146 149 @Test
147   - public void testDeleteUnknownUserByIdReturnsFailure() {
  150 + public void testDeleteUnknownUserByIdFails() {
148 151 // TODO Test ist noch zu implementieren
149 152 }
150 153  
151 154 @Test
152   - public void testDeleteSessionUserReturnsSuccess() {
  155 + public void testDeleteSessionUserSucceeds() {
153 156 // TODO Test ist noch zu implementieren
154 157 }
155 158  
156 159 @Test
157   - public void testLoginUserReturnsSuccess() {
  160 + public void testLoginUserSucceeds() {
158 161  
159 162 given()
160 163 .userDto();
... ... @@ -163,11 +166,11 @@ public class UserViewControllerTest {
163 166 .loginUser(true);
164 167  
165 168 then()
166   - .assertLoginSuccess();
  169 + .assertLoginUserSucceeded();
167 170 }
168 171  
169 172 @Test
170   - public void testLoginUserReturnsFailure() {
  173 + public void testLoginUserFails() {
171 174  
172 175 given()
173 176 .userDto();
... ... @@ -176,17 +179,17 @@ public class UserViewControllerTest {
176 179 .loginUser(false);
177 180  
178 181 then()
179   - .assertLoginFailure();
  182 + .assertLoginUserFailed();
180 183 }
181 184  
182 185 @Test
183   - public void testLogoutUserReturnsSuccess() {
  186 + public void testLogoutUserSucceeds() {
184 187  
185 188 doing()
186 189 .logoutUser();
187 190  
188 191 then()
189   - .assertLogoutSuccess();
  192 + .assertLogoutUserSucceeded();
190 193 }
191 194  
192 195 // given
... ... @@ -264,45 +267,45 @@ public class UserViewControllerTest {
264 267 return this;
265 268 }
266 269  
267   - private void assertCreateSuccess() {
  270 + private void assertCreateUserSucceeded() {
268 271 final RestResponse expected = new RestResponse();
269 272 assertEquals(expected, actual);
270 273 }
271 274  
272   - private void assertCreateFailure() {
  275 + private void assertCreateUserFailed() {
273 276 final RestResponse expected = new RestResponse(ResponseState.ALREADY_EXISTING);
274 277 assertEquals(expected, actual);
275 278 }
276 279  
277   - private void assertGetAllUsersSuccess() {
  280 + private void assertGetAllUsersSucceeded() {
278 281 final Collection<User> expectedCollection = new ArrayList<>();
279 282 assertEquals(expectedCollection, actualCollection);
280 283 }
281 284  
282   - private void assertGetUserByIdSuccess() {
  285 + private void assertGetUserByIdSucceeded() {
283 286 assertEquals(USER_FIRSTNAME, actualUser.getFirstname());
284 287 }
285 288  
286   - private void assertGetUserSuccess() {
  289 + private void assertGetUserSucceeded() {
287 290 assertEquals(USER_FIRSTNAME, actualUser.getFirstname());
288 291 }
289 292  
290   - private void assertUpdateSuccess() {
  293 + private void assertUpdateUserSucceeded() {
291 294 final RestResponse expected = new RestResponse();
292 295 assertEquals(expected, actual);
293 296 }
294 297  
295   - private void assertLoginSuccess() {
  298 + private void assertLoginUserSucceeded() {
296 299 final RestResponse expected = new RestResponse();
297 300 assertEquals(expected, actual);
298 301 }
299 302  
300   - private void assertLoginFailure() {
  303 + private void assertLoginUserFailed() {
301 304 final RestResponse expected = new RestResponse(ResponseState.FAILED);
302 305 assertEquals(expected, actual);
303 306 }
304 307  
305   - private void assertLogoutSuccess() {
  308 + private void assertLogoutUserSucceeded() {
306 309 final RestResponse expected = new RestResponse();
307 310 assertEquals(expected, actual);
308 311 }
... ...
src/test/resources/net/ziemers/swxercise/testdata/UserServiceTestData.xml
... ... @@ -7,4 +7,5 @@
7 7 <!-- das Kennwort ist "secret" -->
8 8 <Profile id="1" role_id="4" username="username_profile" passwordHash="9EOZEsheQcXixUO/XW/vPwRlZh9NMyHppli2qKe3U9+Y0EHhtJU1AA56pkduCGsSbp8VZ7so/62+3cftxBhJGg==" hashAlgorithm="SHA512" salt="FuQll2Av59se1r1Dc4WEPDmzZw4="/>
9 9 <User id="2" firstname="firstname_user" lastname="lastname_user" profile_id="1"/>
  10 + <User id="5" firstname="firstname_user2" lastname="lastname_user2"/>
10 11 </dataset>
... ...