Commit 86ec838eda8df4f20f7e306e5a9860f3a218eb4f
1 parent
d0c0b80b
test: Testdatenbank mit XML-Testdaten füllen
Showing
10 changed files
with
52 additions
and
20 deletions
.gitignore
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
@@ -40,13 +40,17 @@ public class UserService { | @@ -40,13 +40,17 @@ public class UserService { | ||
40 | } | 40 | } |
41 | 41 | ||
42 | public Long createUser(final UserDto userDto) { | 42 | public Long createUser(final UserDto userDto) { |
43 | - final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword()); | 43 | + User user = dao.findByUsername(userDto.getUsername()); |
44 | + if (user == null) { | ||
45 | + final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword()); | ||
44 | 46 | ||
45 | - // wir füllen das User-Objekt mit Method Chaining | ||
46 | - final User user = new User(userDto.getFirstname(), userDto.getLastname()) | ||
47 | - .withProfile(profile); | 47 | + // wir füllen das User-Objekt mit Method Chaining |
48 | + user = new User(userDto.getFirstname(), userDto.getLastname()) | ||
49 | + .withProfile(profile); | ||
48 | 50 | ||
49 | - return dao.save(user); | 51 | + return dao.save(user); |
52 | + } | ||
53 | + return null; | ||
50 | } | 54 | } |
51 | 55 | ||
52 | public void deleteUser(final Long id) { | 56 | public void deleteUser(final Long id) { |
src/test/java/net/ziemers/swxercise/db/utils/JpaTestUtils.java
@@ -40,7 +40,7 @@ public class JpaTestUtils { | @@ -40,7 +40,7 @@ public class JpaTestUtils { | ||
40 | 40 | ||
41 | private static final String PERSISTENCE_UNIT_TEST = "swXerciseTestPU"; | 41 | private static final String PERSISTENCE_UNIT_TEST = "swXerciseTestPU"; |
42 | 42 | ||
43 | - private static final String NET_ZIEMERS_SWXERCISE_PKG = "net/ziemers/swxercise/"; | 43 | + private static final String NET_ZIEMERS_SWXERCISE_PKG = "net/ziemers/swxercise/testdata/"; |
44 | 44 | ||
45 | private static EntityManagerFactory emf = null; | 45 | private static EntityManagerFactory emf = null; |
46 | 46 |
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/AbstractTestDataBuilder.java renamed to src/test/java/net/ziemers/swxercise/lg/testdatabuilder/AbstractTestDataBuilder.java
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/ProfileTestDataBuilder.java renamed to src/test/java/net/ziemers/swxercise/lg/testdatabuilder/user/ProfileTestDataBuilder.java
1 | -package net.ziemers.swxercise.lg.testdata.testdatabuilder.user; | 1 | +package net.ziemers.swxercise.lg.testdatabuilder.user; |
2 | 2 | ||
3 | import net.ziemers.swxercise.lg.model.user.Profile; | 3 | import net.ziemers.swxercise.lg.model.user.Profile; |
4 | -import net.ziemers.swxercise.lg.testdata.testdatabuilder.AbstractTestDataBuilder; | 4 | +import net.ziemers.swxercise.lg.testdatabuilder.AbstractTestDataBuilder; |
5 | 5 | ||
6 | import javax.persistence.EntityManager; | 6 | import javax.persistence.EntityManager; |
7 | 7 |
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/UserDtoTestDataBuilder.java renamed to src/test/java/net/ziemers/swxercise/lg/testdatabuilder/user/UserDtoTestDataBuilder.java
1 | -package net.ziemers.swxercise.lg.testdata.testdatabuilder.user; | 1 | +package net.ziemers.swxercise.lg.testdatabuilder.user; |
2 | 2 | ||
3 | -import net.ziemers.swxercise.lg.testdata.testdatabuilder.AbstractTestDataBuilder; | 3 | +import net.ziemers.swxercise.lg.testdatabuilder.AbstractTestDataBuilder; |
4 | import net.ziemers.swxercise.lg.user.dto.UserDto; | 4 | import net.ziemers.swxercise.lg.user.dto.UserDto; |
5 | 5 | ||
6 | import javax.persistence.EntityManager; | 6 | import javax.persistence.EntityManager; |
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/UserTestDataBuilder.java renamed to src/test/java/net/ziemers/swxercise/lg/testdatabuilder/user/UserTestDataBuilder.java
1 | -package net.ziemers.swxercise.lg.testdata.testdatabuilder.user; | 1 | +package net.ziemers.swxercise.lg.testdatabuilder.user; |
2 | 2 | ||
3 | import net.ziemers.swxercise.lg.model.user.Address; | 3 | import net.ziemers.swxercise.lg.model.user.Address; |
4 | import net.ziemers.swxercise.lg.model.user.Profile; | 4 | import net.ziemers.swxercise.lg.model.user.Profile; |
5 | import net.ziemers.swxercise.lg.model.user.User; | 5 | import net.ziemers.swxercise.lg.model.user.User; |
6 | -import net.ziemers.swxercise.lg.testdata.testdatabuilder.AbstractTestDataBuilder; | 6 | +import net.ziemers.swxercise.lg.testdatabuilder.AbstractTestDataBuilder; |
7 | 7 | ||
8 | import javax.persistence.EntityManager; | 8 | import javax.persistence.EntityManager; |
9 | 9 |
src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
@@ -5,7 +5,7 @@ import javax.inject.Inject; | @@ -5,7 +5,7 @@ import javax.inject.Inject; | ||
5 | import net.ziemers.swxercise.db.dao.user.UserDao; | 5 | import net.ziemers.swxercise.db.dao.user.UserDao; |
6 | import net.ziemers.swxercise.db.utils.JpaTestUtils; | 6 | import net.ziemers.swxercise.db.utils.JpaTestUtils; |
7 | import net.ziemers.swxercise.lg.model.user.User; | 7 | import net.ziemers.swxercise.lg.model.user.User; |
8 | -import net.ziemers.swxercise.lg.testdata.testdatabuilder.user.UserDtoTestDataBuilder; | 8 | +import net.ziemers.swxercise.lg.testdatabuilder.user.UserDtoTestDataBuilder; |
9 | import net.ziemers.swxercise.lg.user.dto.UserDto; | 9 | import net.ziemers.swxercise.lg.user.dto.UserDto; |
10 | 10 | ||
11 | import static org.junit.Assert.*; | 11 | import static org.junit.Assert.*; |
@@ -28,11 +28,14 @@ import org.junit.runner.RunWith; | @@ -28,11 +28,14 @@ import org.junit.runner.RunWith; | ||
28 | public class UserServiceTest extends JpaTestUtils { | 28 | public class UserServiceTest extends JpaTestUtils { |
29 | 29 | ||
30 | private static String USERNAME_TEST = "username_test"; | 30 | private static String USERNAME_TEST = "username_test"; |
31 | + private static String EXISTING_USERNAME_TEST = "username_profile"; | ||
31 | 32 | ||
32 | private static boolean dbInitialized; | 33 | private static boolean dbInitialized; |
33 | 34 | ||
34 | private UserDto userDto; | 35 | private UserDto userDto; |
35 | 36 | ||
37 | + private Long userId; | ||
38 | + | ||
36 | @Inject | 39 | @Inject |
37 | private UserDao userDao; | 40 | private UserDao userDao; |
38 | 41 | ||
@@ -43,7 +46,7 @@ public class UserServiceTest extends JpaTestUtils { | @@ -43,7 +46,7 @@ public class UserServiceTest extends JpaTestUtils { | ||
43 | public void setUp() throws Exception { | 46 | public void setUp() throws Exception { |
44 | if (!dbInitialized) { | 47 | if (!dbInitialized) { |
45 | cleanDb(); | 48 | cleanDb(); |
46 | - //initDbWith("UserService.xml"); | 49 | + initDbWith("UserServiceTestData.xml"); |
47 | dbInitialized = true; | 50 | dbInitialized = true; |
48 | } | 51 | } |
49 | } | 52 | } |
@@ -52,7 +55,7 @@ public class UserServiceTest extends JpaTestUtils { | @@ -52,7 +55,7 @@ public class UserServiceTest extends JpaTestUtils { | ||
52 | public void testCreateUserReturnsSuccess() { | 55 | public void testCreateUserReturnsSuccess() { |
53 | 56 | ||
54 | given() | 57 | given() |
55 | - .userDto(); | 58 | + .userDto(USERNAME_TEST); |
56 | 59 | ||
57 | when() | 60 | when() |
58 | .createUser(); | 61 | .createUser(); |
@@ -61,13 +64,26 @@ public class UserServiceTest extends JpaTestUtils { | @@ -61,13 +64,26 @@ public class UserServiceTest extends JpaTestUtils { | ||
61 | .assertCreateSuccess(); | 64 | .assertCreateSuccess(); |
62 | } | 65 | } |
63 | 66 | ||
67 | + @Test | ||
68 | + public void testCreateUserReturnsFail() { | ||
69 | + | ||
70 | + given() | ||
71 | + .userDto(EXISTING_USERNAME_TEST); | ||
72 | + | ||
73 | + when() | ||
74 | + .createUser(); | ||
75 | + | ||
76 | + then() | ||
77 | + .assertCreateFail(); | ||
78 | + } | ||
79 | + | ||
64 | // given | 80 | // given |
65 | 81 | ||
66 | private UserServiceTest given() { | 82 | private UserServiceTest given() { |
67 | return this; | 83 | return this; |
68 | } | 84 | } |
69 | 85 | ||
70 | - private UserServiceTest userDto() { | 86 | + private UserServiceTest userDto(final String username) { |
71 | userDto = new UserDtoTestDataBuilder() | 87 | userDto = new UserDtoTestDataBuilder() |
72 | .withUsername(USERNAME_TEST) | 88 | .withUsername(USERNAME_TEST) |
73 | .build(); | 89 | .build(); |
@@ -82,7 +98,7 @@ public class UserServiceTest extends JpaTestUtils { | @@ -82,7 +98,7 @@ public class UserServiceTest extends JpaTestUtils { | ||
82 | 98 | ||
83 | private UserServiceTest createUser() { | 99 | private UserServiceTest createUser() { |
84 | txBegin(); | 100 | txBegin(); |
85 | - underTest.createUser(userDto); | 101 | + userId = underTest.createUser(userDto); |
86 | txCommit(); | 102 | txCommit(); |
87 | 103 | ||
88 | return this; | 104 | return this; |
@@ -95,8 +111,14 @@ public class UserServiceTest extends JpaTestUtils { | @@ -95,8 +111,14 @@ public class UserServiceTest extends JpaTestUtils { | ||
95 | } | 111 | } |
96 | 112 | ||
97 | private void assertCreateSuccess() { | 113 | private void assertCreateSuccess() { |
114 | + // wir suchen den soeben erstellten Benutzer; wenn er existiert, is alles gut | ||
98 | final User user = userDao.findByUsername(USERNAME_TEST); | 115 | final User user = userDao.findByUsername(USERNAME_TEST); |
99 | assertNotNull(user); | 116 | assertNotNull(user); |
100 | } | 117 | } |
101 | 118 | ||
119 | + private void assertCreateFail() { | ||
120 | + // es darf kein neuer Benutzer mit identischem "username" erstellt worden sein | ||
121 | + assertNull(userId); | ||
122 | + } | ||
123 | + | ||
102 | } | 124 | } |
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
1 | package net.ziemers.swxercise.ui; | 1 | package net.ziemers.swxercise.ui; |
2 | 2 | ||
3 | import net.ziemers.swxercise.lg.model.user.User; | 3 | import net.ziemers.swxercise.lg.model.user.User; |
4 | -import net.ziemers.swxercise.lg.testdata.testdatabuilder.user.UserDtoTestDataBuilder; | ||
5 | -import net.ziemers.swxercise.lg.testdata.testdatabuilder.user.UserTestDataBuilder; | 4 | +import net.ziemers.swxercise.lg.testdatabuilder.user.UserDtoTestDataBuilder; |
5 | +import net.ziemers.swxercise.lg.testdatabuilder.user.UserTestDataBuilder; | ||
6 | import net.ziemers.swxercise.lg.user.dto.UserDto; | 6 | import net.ziemers.swxercise.lg.user.dto.UserDto; |
7 | import net.ziemers.swxercise.lg.user.service.UserService; | 7 | import net.ziemers.swxercise.lg.user.service.UserService; |
8 | import org.junit.Test; | 8 | import org.junit.Test; |
src/test/resources/net/ziemers/swxercise/testdata/UserServiceTestData.xml
0 → 100644