Commit 00097b5c80083237d0072d086ca8f6498f4dfb8b
1 parent
7c0c2ad7
test: UserService-Tests vervollständigt
Showing
3 changed files
with
96 additions
and
10 deletions
src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
1 | 1 | package net.ziemers.swxercise.lg.user.service; |
2 | 2 | |
3 | +import java.util.Arrays; | |
4 | +import java.util.HashSet; | |
5 | +import java.util.Set; | |
3 | 6 | import javax.inject.Inject; |
4 | 7 | |
5 | 8 | import net.ziemers.swxercise.db.dao.user.UserDao; |
... | ... | @@ -167,18 +170,80 @@ public class UserServiceTest extends JpaTestUtils { |
167 | 170 | } |
168 | 171 | |
169 | 172 | @Test |
170 | - public void testDeleteUserReturnsSuccess() { | |
173 | + public void testDeleteUnknownUserByIdReturnsFailure() { | |
171 | 174 | // TODO Test ist noch zu implementieren |
172 | 175 | } |
173 | 176 | |
174 | 177 | @Test |
175 | - public void testIsUserAllowedReturnsSuccess() { | |
178 | + public void testDeleteSessionUserReturnsSuccess() { | |
176 | 179 | // TODO Test ist noch zu implementieren |
177 | 180 | } |
178 | 181 | |
179 | 182 | @Test |
180 | - public void testIsUserAllowedReturnsFailure() { | |
181 | - // TODO Test ist noch zu implementieren | |
183 | + @InRequestScope | |
184 | + public void testIsNotLoggedInUserAllowedReturnsSuccess() { | |
185 | + | |
186 | + when() | |
187 | + .isUserAllowed(new HashSet<String>(Arrays.asList("NOT_LOGGED_IN"))); | |
188 | + | |
189 | + then() | |
190 | + .assertIsUserAllowedSuccess(); | |
191 | + } | |
192 | + | |
193 | + @Test | |
194 | + @InRequestScope | |
195 | + public void testIsNotLoggedInUserAllowedReturnsFailure() { | |
196 | + | |
197 | + when() | |
198 | + .isUserAllowed(new HashSet<String>(Arrays.asList("LOGGED_IN"))); | |
199 | + | |
200 | + then() | |
201 | + .assertIsUserAllowedFailure(); | |
202 | + } | |
203 | + | |
204 | + @Test | |
205 | + @InRequestScope | |
206 | + public void testIsSessionUserAllowedReturnsSuccess() { | |
207 | + | |
208 | + given() | |
209 | + .userDto(EXISTING_USERNAME_TEST) | |
210 | + .loginUser(EXISTING_PASSWORD_TEST); | |
211 | + | |
212 | + when() | |
213 | + .isUserAllowed(new HashSet<String>(Arrays.asList("LOGGED_IN"))); | |
214 | + | |
215 | + then() | |
216 | + .assertIsUserAllowedSuccess(); | |
217 | + } | |
218 | + | |
219 | + @Test | |
220 | + @InRequestScope | |
221 | + public void testIsAdminUserAllowedReturnsSuccess() { | |
222 | + | |
223 | + given() | |
224 | + .userDto(EXISTING_USERNAME_TEST) | |
225 | + .loginUser(EXISTING_PASSWORD_TEST); | |
226 | + | |
227 | + when() | |
228 | + .isUserAllowed(new HashSet<String>(Arrays.asList("ADMIN"))); | |
229 | + | |
230 | + then() | |
231 | + .assertIsUserAllowedSuccess(); | |
232 | + } | |
233 | + | |
234 | + @Test | |
235 | + @InRequestScope | |
236 | + public void testIsUserWithUnknownRoleAllowedReturnsFailure() { | |
237 | + | |
238 | + given() | |
239 | + .userDto(EXISTING_USERNAME_TEST) | |
240 | + .loginUser(EXISTING_PASSWORD_TEST); | |
241 | + | |
242 | + when() | |
243 | + .isUserAllowed(new HashSet<String>(Arrays.asList("UNKNOWN_ROLE"))); | |
244 | + | |
245 | + then() | |
246 | + .assertIsUserAllowedFailure(); | |
182 | 247 | } |
183 | 248 | |
184 | 249 | // given |
... | ... | @@ -237,6 +302,11 @@ public class UserServiceTest extends JpaTestUtils { |
237 | 302 | return this; |
238 | 303 | } |
239 | 304 | |
305 | + private UserServiceTest isUserAllowed(final Set<String> rightsSet) { | |
306 | + actual = underTest.isUserAllowed(rightsSet); | |
307 | + return this; | |
308 | + } | |
309 | + | |
240 | 310 | // then |
241 | 311 | |
242 | 312 | private UserServiceTest then() { |
... | ... | @@ -276,4 +346,12 @@ public class UserServiceTest extends JpaTestUtils { |
276 | 346 | assertEquals(EXISTING_USERNAME_TEST, user.getProfile().getUsername()); |
277 | 347 | } |
278 | 348 | |
349 | + private void assertIsUserAllowedSuccess() { | |
350 | + assertTrue(actual); | |
351 | + } | |
352 | + | |
353 | + private void assertIsUserAllowedFailure() { | |
354 | + assertFalse(actual); | |
355 | + } | |
356 | + | |
279 | 357 | } | ... | ... |
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
... | ... | @@ -77,7 +77,7 @@ public class UserViewControllerTest { |
77 | 77 | } |
78 | 78 | |
79 | 79 | @Test |
80 | - public void testGetUserReturnsUserJson() { | |
80 | + public void testGetSessionUserReturnsUserJson() { | |
81 | 81 | |
82 | 82 | doing() |
83 | 83 | .getUser(); |
... | ... | @@ -113,7 +113,7 @@ public class UserViewControllerTest { |
113 | 113 | } |
114 | 114 | |
115 | 115 | @Test |
116 | - public void testUpdateLoggedInUserReturnsSuccess() { | |
116 | + public void testUpdateSessionUserReturnsSuccess() { | |
117 | 117 | |
118 | 118 | given() |
119 | 119 | .userDto(); |
... | ... | @@ -126,7 +126,7 @@ public class UserViewControllerTest { |
126 | 126 | } |
127 | 127 | |
128 | 128 | @Test |
129 | - public void testUpdateSpecificUserReturnsSuccess() { | |
129 | + public void testUpdateUserByIdReturnsSuccess() { | |
130 | 130 | |
131 | 131 | given() |
132 | 132 | .userDto(); |
... | ... | @@ -144,6 +144,11 @@ public class UserViewControllerTest { |
144 | 144 | } |
145 | 145 | |
146 | 146 | @Test |
147 | + public void testDeleteUnknownUserByIdReturnsFailure() { | |
148 | + // TODO Test ist noch zu implementieren | |
149 | + } | |
150 | + | |
151 | + @Test | |
147 | 152 | public void testDeleteSessionUserReturnsSuccess() { |
148 | 153 | // TODO Test ist noch zu implementieren |
149 | 154 | } |
... | ... | @@ -162,7 +167,7 @@ public class UserViewControllerTest { |
162 | 167 | } |
163 | 168 | |
164 | 169 | @Test |
165 | - public void testLoginNonExistingUserReturnsFailure() { | |
170 | + public void testLoginUserReturnsFailure() { | |
166 | 171 | |
167 | 172 | given() |
168 | 173 | .userDto(); |
... | ... | @@ -195,7 +200,7 @@ public class UserViewControllerTest { |
195 | 200 | return this; |
196 | 201 | } |
197 | 202 | |
198 | - // doing | |
203 | + // doing (when) | |
199 | 204 | |
200 | 205 | private UserViewControllerTest doing() { |
201 | 206 | return this; | ... | ... |
src/test/resources/net/ziemers/swxercise/testdata/UserServiceTestData.xml
1 | 1 | <?xml version="1.0" encoding="UTF-8"?> |
2 | 2 | <dataset> |
3 | + <Role id="3" name="name_role_empty"/> | |
4 | + <Role id="4" name="name_role_admin"/> | |
5 | + <Role_rights Role_id="4" rights="ADMIN"/> | |
3 | 6 | <!-- die Gross-/Kleinschreibung scheint unter bestimmten Bedingungen von Bedeutung zu sein! --> |
4 | 7 | <!-- das Kennwort ist "secret" --> |
5 | - <Profile id="1" username="username_profile" passwordHash="9EOZEsheQcXixUO/XW/vPwRlZh9NMyHppli2qKe3U9+Y0EHhtJU1AA56pkduCGsSbp8VZ7so/62+3cftxBhJGg==" hashAlgorithm="SHA512" salt="FuQll2Av59se1r1Dc4WEPDmzZw4="/> | |
8 | + <Profile id="1" role_id="4" username="username_profile" passwordHash="9EOZEsheQcXixUO/XW/vPwRlZh9NMyHppli2qKe3U9+Y0EHhtJU1AA56pkduCGsSbp8VZ7so/62+3cftxBhJGg==" hashAlgorithm="SHA512" salt="FuQll2Av59se1r1Dc4WEPDmzZw4="/> | |
6 | 9 | <User id="2" firstname="firstname_user" lastname="lastname_user" profile_id="1"/> |
7 | 10 | </dataset> | ... | ... |