Commit 86ec838eda8df4f20f7e306e5a9860f3a218eb4f

Authored by Thomas Ziemer
1 parent d0c0b80b

test: Testdatenbank mit XML-Testdaten füllen

.gitignore
... ... @@ -2,5 +2,6 @@
2 2 .project
3 3 .settings/
4 4 .idea/
  5 +.DS_Store
5 6 target/
6 7 *.iml
... ...
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
... ... @@ -40,13 +40,17 @@ public class UserService {
40 40 }
41 41  
42 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 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 40  
41 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 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
1   -package net.ziemers.swxercise.lg.testdata.testdatabuilder;
  1 +package net.ziemers.swxercise.lg.testdatabuilder;
2 2  
3 3 import javax.persistence.EntityManager;
4 4  
... ...
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 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 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 4 import net.ziemers.swxercise.lg.user.dto.UserDto;
5 5  
6 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 3 import net.ziemers.swxercise.lg.model.user.Address;
4 4 import net.ziemers.swxercise.lg.model.user.Profile;
5 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 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 5 import net.ziemers.swxercise.db.dao.user.UserDao;
6 6 import net.ziemers.swxercise.db.utils.JpaTestUtils;
7 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 9 import net.ziemers.swxercise.lg.user.dto.UserDto;
10 10  
11 11 import static org.junit.Assert.*;
... ... @@ -28,11 +28,14 @@ import org.junit.runner.RunWith;
28 28 public class UserServiceTest extends JpaTestUtils {
29 29  
30 30 private static String USERNAME_TEST = "username_test";
  31 + private static String EXISTING_USERNAME_TEST = "username_profile";
31 32  
32 33 private static boolean dbInitialized;
33 34  
34 35 private UserDto userDto;
35 36  
  37 + private Long userId;
  38 +
36 39 @Inject
37 40 private UserDao userDao;
38 41  
... ... @@ -43,7 +46,7 @@ public class UserServiceTest extends JpaTestUtils {
43 46 public void setUp() throws Exception {
44 47 if (!dbInitialized) {
45 48 cleanDb();
46   - //initDbWith("UserService.xml");
  49 + initDbWith("UserServiceTestData.xml");
47 50 dbInitialized = true;
48 51 }
49 52 }
... ... @@ -52,7 +55,7 @@ public class UserServiceTest extends JpaTestUtils {
52 55 public void testCreateUserReturnsSuccess() {
53 56  
54 57 given()
55   - .userDto();
  58 + .userDto(USERNAME_TEST);
56 59  
57 60 when()
58 61 .createUser();
... ... @@ -61,13 +64,26 @@ public class UserServiceTest extends JpaTestUtils {
61 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 80 // given
65 81  
66 82 private UserServiceTest given() {
67 83 return this;
68 84 }
69 85  
70   - private UserServiceTest userDto() {
  86 + private UserServiceTest userDto(final String username) {
71 87 userDto = new UserDtoTestDataBuilder()
72 88 .withUsername(USERNAME_TEST)
73 89 .build();
... ... @@ -82,7 +98,7 @@ public class UserServiceTest extends JpaTestUtils {
82 98  
83 99 private UserServiceTest createUser() {
84 100 txBegin();
85   - underTest.createUser(userDto);
  101 + userId = underTest.createUser(userDto);
86 102 txCommit();
87 103  
88 104 return this;
... ... @@ -95,8 +111,14 @@ public class UserServiceTest extends JpaTestUtils {
95 111 }
96 112  
97 113 private void assertCreateSuccess() {
  114 + // wir suchen den soeben erstellten Benutzer; wenn er existiert, is alles gut
98 115 final User user = userDao.findByUsername(USERNAME_TEST);
99 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 1 package net.ziemers.swxercise.ui;
2 2  
3 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 6 import net.ziemers.swxercise.lg.user.dto.UserDto;
7 7 import net.ziemers.swxercise.lg.user.service.UserService;
8 8 import org.junit.Test;
... ...
src/test/resources/net/ziemers/swxercise/testdata/UserServiceTestData.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<dataset>
  3 + <profile id="1" username="username_profile" passwordHash="passwordHash_profile" hashAlgorithm="hashAlgorithm_profile" salt="salt_profile"/>
  4 + <user id="2" firstname="firstname_user" lastname="lastname_user" profile_id="1"/>
  5 +</dataset>
... ...