Commit 8cae1cc55371bbbce0d84ec024e41fc50944117b

Authored by Thomas Ziemer
1 parent 62d24433

test: ViewController- und ServiceTests optimiert/ergänzt

src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
... ... @@ -29,12 +29,13 @@ public class UserServiceTest extends JpaTestUtils {
29 29  
30 30 private static String USERNAME_TEST = "username_test";
31 31 private static String EXISTING_USERNAME_TEST = "username_profile";
  32 + private static Long EXISTING_USER_ID = 2L;
32 33  
33 34 private static boolean dbInitialized;
34 35  
35 36 private UserDto userDto;
36 37  
37   - private Long userId;
  38 + private boolean actual;
38 39  
39 40 @Inject
40 41 private UserDao userDao;
... ... @@ -74,7 +75,20 @@ public class UserServiceTest extends JpaTestUtils {
74 75 .createUser();
75 76  
76 77 then()
77   - .assertCreateFail();
  78 + .assertCreateFailure();
  79 + }
  80 +
  81 + @Test
  82 + public void testUpdateUserDoesntUpdateUsername() {
  83 +
  84 + given()
  85 + .userDto(USERNAME_TEST);
  86 +
  87 + when()
  88 + .updateUser(EXISTING_USER_ID);
  89 +
  90 + then()
  91 + .assertUpdateSuccess();
78 92 }
79 93  
80 94 // given
... ... @@ -85,7 +99,7 @@ public class UserServiceTest extends JpaTestUtils {
85 99  
86 100 private UserServiceTest userDto(final String username) {
87 101 userDto = new UserDtoTestDataBuilder()
88   - .withUsername(USERNAME_TEST)
  102 + .withUsername(username)
89 103 .build();
90 104 return this;
91 105 }
... ... @@ -98,7 +112,15 @@ public class UserServiceTest extends JpaTestUtils {
98 112  
99 113 private UserServiceTest createUser() {
100 114 txBegin();
101   - userId = underTest.createUser(userDto);
  115 + actual = underTest.createUser(userDto);
  116 + txCommit();
  117 +
  118 + return this;
  119 + }
  120 +
  121 + private UserServiceTest updateUser(final Long id) {
  122 + txBegin();
  123 + actual = underTest.updateUser(id, userDto);
102 124 txCommit();
103 125  
104 126 return this;
... ... @@ -111,14 +133,20 @@ public class UserServiceTest extends JpaTestUtils {
111 133 }
112 134  
113 135 private void assertCreateSuccess() {
114   - // wir suchen den soeben erstellten Benutzer; wenn er existiert, is alles gut
  136 + // wir suchen den soeben erstellten Benutzer; wenn er existiert, ist alles gut
115 137 final User user = userDao.findByUsername(USERNAME_TEST);
116 138 assertNotNull(user);
117 139 }
118 140  
119   - private void assertCreateFail() {
120   - // es darf kein neuer Benutzer mit identischem "username" erstellt worden sein
121   - assertNull(userId);
  141 + private void assertCreateFailure() {
  142 + // es darf kein neuer Benutzer mit identischem Benutzernamen erstellt worden sein
  143 + assertFalse(actual);
  144 + }
  145 +
  146 + private void assertUpdateSuccess() {
  147 + // wir suchen den soeben aktualisierten Benutzer; wenn sein Benutzername unverändert ist, ist alles gut
  148 + final User user = userDao.findById(EXISTING_USER_ID);
  149 + assertEquals(EXISTING_USERNAME_TEST, user.getProfile().getUsername());
122 150 }
123 151  
124 152 }
... ...
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
1 1 package net.ziemers.swxercise.ui;
2 2  
3   -import net.ziemers.swxercise.lg.model.user.User;
4 3 import net.ziemers.swxercise.lg.testdatabuilder.user.UserDtoTestDataBuilder;
5   -import net.ziemers.swxercise.lg.testdatabuilder.user.UserTestDataBuilder;
6 4 import net.ziemers.swxercise.lg.user.dto.UserDto;
7 5 import net.ziemers.swxercise.lg.user.service.UserService;
  6 +import net.ziemers.swxercise.ui.enums.ResponseState;
8 7 import org.junit.Test;
9 8 import org.junit.runner.RunWith;
10 9 import org.mockito.InjectMocks;
... ... @@ -33,8 +32,6 @@ public class UserViewControllerTest {
33 32  
34 33 private UserDto userDto;
35 34  
36   - private User user;
37   -
38 35 private RestResponse actual;
39 36  
40 37 @Test
... ... @@ -59,12 +56,54 @@ public class UserViewControllerTest {
59 56  
60 57 @Test
61 58 public void testCreateUserReturnsSuccess() {
62   - // TODO Test ist noch zu implementieren
  59 +
  60 + given()
  61 + .userDto();
  62 +
  63 + doing()
  64 + .createUser(true);
  65 +
  66 + then()
  67 + .assertCreateSuccess();
63 68 }
64 69  
65 70 @Test
66   - public void testUpdateUserReturnsSuccess() {
67   - // TODO Test ist noch zu implementieren
  71 + public void testCreateUserReturnsFailure() {
  72 +
  73 + given()
  74 + .userDto();
  75 +
  76 + doing()
  77 + .createUser(false);
  78 +
  79 + then()
  80 + .assertCreateFailure();
  81 + }
  82 +
  83 + @Test
  84 + public void testUpdateLoggedInUserReturnsSuccess() {
  85 +
  86 + given()
  87 + .userDto();
  88 +
  89 + doing()
  90 + .updateUser();
  91 +
  92 + then()
  93 + .assertUpdateSuccess();
  94 + }
  95 +
  96 + @Test
  97 + public void testUpdateSpecificUserReturnsSuccess() {
  98 +
  99 + given()
  100 + .userDto();
  101 +
  102 + doing()
  103 + .updateUser(1L);
  104 +
  105 + then()
  106 + .assertUpdateSuccess();
68 107 }
69 108  
70 109 @Test
... ... @@ -81,17 +120,29 @@ public class UserViewControllerTest {
81 120 public void testLoginUserReturnsSuccess() {
82 121  
83 122 given()
84   - .userDto()
85   - .user();
  123 + .userDto();
86 124  
87 125 doing()
88   - .loginUser();
  126 + .loginUser(true);
89 127  
90 128 then()
91 129 .assertLoginSuccess();
92 130 }
93 131  
94 132 @Test
  133 + public void testLoginNonExistingUserReturnsFailure() {
  134 +
  135 + given()
  136 + .userDto();
  137 +
  138 + doing()
  139 + .loginUser(false);
  140 +
  141 + then()
  142 + .assertLoginFailure();
  143 + }
  144 +
  145 + @Test
95 146 public void testLogoutUserReturnsSuccess() {
96 147  
97 148 doing()
... ... @@ -112,23 +163,36 @@ public class UserViewControllerTest {
112 163 return this;
113 164 }
114 165  
115   - private UserViewControllerTest user() {
116   - user = new UserTestDataBuilder().build();
117   - return this;
118   - }
119   -
120 166 // doing
121 167  
122 168 private UserViewControllerTest doing() {
123 169 return this;
124 170 }
125 171  
126   - private void loginUser() {
  172 + private void createUser(final boolean result) {
127 173 /*
128 174 * The when().thenReturn() method chain is used to specify
129 175 * a return value for a method call with pre-defined parameters.
130 176 */
131   - when(userService.loginUser(userDto)).thenReturn(true);
  177 + when(userService.createUser(userDto)).thenReturn(result);
  178 +
  179 + actual = underTest.createUser(userDto);
  180 + }
  181 +
  182 + private void updateUser() {
  183 + when(userService.updateUser(userDto)).thenReturn(true);
  184 +
  185 + actual = underTest.updateUser(userDto);
  186 + }
  187 +
  188 + private void updateUser(final Long id) {
  189 + when(userService.updateUser(id, userDto)).thenReturn(true);
  190 +
  191 + actual = underTest.updateUser(id, userDto);
  192 + }
  193 +
  194 + private void loginUser(final boolean result) {
  195 + when(userService.loginUser(userDto)).thenReturn(result);
132 196  
133 197 actual = underTest.loginUser(userDto);
134 198 }
... ... @@ -145,11 +209,31 @@ public class UserViewControllerTest {
145 209 return this;
146 210 }
147 211  
  212 + private void assertCreateSuccess() {
  213 + final RestResponse expected = new RestResponse();
  214 + assertEquals(expected, actual);
  215 + }
  216 +
  217 + private void assertCreateFailure() {
  218 + final RestResponse expected = new RestResponse(ResponseState.ALREADY_EXISTING);
  219 + assertEquals(expected, actual);
  220 + }
  221 +
  222 + private void assertUpdateSuccess() {
  223 + final RestResponse expected = new RestResponse();
  224 + assertEquals(expected, actual);
  225 + }
  226 +
148 227 private void assertLoginSuccess() {
149 228 final RestResponse expected = new RestResponse();
150 229 assertEquals(expected, actual);
151 230 }
152 231  
  232 + private void assertLoginFailure() {
  233 + final RestResponse expected = new RestResponse(ResponseState.FAILED);
  234 + assertEquals(expected, actual);
  235 + }
  236 +
153 237 private void assertLogoutSuccess() {
154 238 final RestResponse expected = new RestResponse();
155 239 assertEquals(expected, actual);
... ...
src/test/resources/net/ziemers/swxercise/testdata/UserServiceTestData.xml
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <dataset>
3 3 <!-- die Grosz-/Kleinschreibung scheint unter bestimmten Bedingungen von Bedeutung zu sein! -->
4   - <Profile id="1" username="username_profile" passwordHash="passwordHash_profile" hashAlgorithm="hashAlgorithm_profile" salt="salt_profile"/>
  4 + <Profile id="1" username="username_profile" passwordHash="passwordHash_profile" hashAlgorithm="SHA512" salt="salt_profile"/>
5 5 <User id="2" firstname="firstname_user" lastname="lastname_user" profile_id="1"/>
6 6 </dataset>
... ...