Commit e807d225028ef49d78958042336ed51022dbda86

Authored by Thomas Ziemer
1 parent 1a369053

test: realistischere Testdaten und loginUser()-Test (WIP)

src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
@@ -10,12 +10,14 @@ import net.ziemers.swxercise.lg.user.dto.UserDto; @@ -10,12 +10,14 @@ import net.ziemers.swxercise.lg.user.dto.UserDto;
10 10
11 import static org.junit.Assert.*; 11 import static org.junit.Assert.*;
12 12
  13 +import org.jglue.cdiunit.AdditionalClasses;
13 import org.jglue.cdiunit.CdiRunner; 14 import org.jglue.cdiunit.CdiRunner;
  15 +import org.jglue.cdiunit.InSessionScope;
14 import org.junit.Before; 16 import org.junit.Before;
15 import org.junit.Test; 17 import org.junit.Test;
16 import org.junit.runner.RunWith; 18 import org.junit.runner.RunWith;
17 19
18 -/* 20 +/**
19 * Testing your Java CDI application with CDI-Unit couldn't be easier. 21 * Testing your Java CDI application with CDI-Unit couldn't be easier.
20 * Just specify @RunWith(CdiRunner.class) on your JUnit test class 22 * Just specify @RunWith(CdiRunner.class) on your JUnit test class
21 * to enable injection directly into the test class. 23 * to enable injection directly into the test class.
@@ -25,10 +27,12 @@ import org.junit.runner.RunWith; @@ -25,10 +27,12 @@ import org.junit.runner.RunWith;
25 * there, however CDI-Unit has extra support for Mockito @Mock annotations. 27 * there, however CDI-Unit has extra support for Mockito @Mock annotations.
26 */ 28 */
27 @RunWith(CdiRunner.class) 29 @RunWith(CdiRunner.class)
  30 +@AdditionalClasses(SessionContext.class)
28 public class UserServiceTest extends JpaTestUtils { 31 public class UserServiceTest extends JpaTestUtils {
29 32
30 private static String USERNAME_TEST = "username_test"; 33 private static String USERNAME_TEST = "username_test";
31 private static String EXISTING_USERNAME_TEST = "username_profile"; 34 private static String EXISTING_USERNAME_TEST = "username_profile";
  35 + private static String EXISTING_PASSWORD_TEST = "secret";
32 private static Long EXISTING_USER_ID = 2L; 36 private static Long EXISTING_USER_ID = 2L;
33 37
34 private static boolean dbInitialized; 38 private static boolean dbInitialized;
@@ -52,6 +56,20 @@ public class UserServiceTest extends JpaTestUtils { @@ -52,6 +56,20 @@ public class UserServiceTest extends JpaTestUtils {
52 } 56 }
53 } 57 }
54 58
  59 +// @Test
  60 +// @InSessionScope
  61 +// public void testLoginUserReturnsSuccess() {
  62 +//
  63 +// given()
  64 +// .userDto(EXISTING_USERNAME_TEST);
  65 +//
  66 +// when()
  67 +// .loginUser(EXISTING_PASSWORD_TEST);
  68 +//
  69 +// then()
  70 +// .assertLoginSuccess();
  71 +// }
  72 +
55 @Test 73 @Test
56 public void testCreateUserReturnsSuccess() { 74 public void testCreateUserReturnsSuccess() {
57 75
@@ -66,7 +84,7 @@ public class UserServiceTest extends JpaTestUtils { @@ -66,7 +84,7 @@ public class UserServiceTest extends JpaTestUtils {
66 } 84 }
67 85
68 @Test 86 @Test
69 - public void testCreateUserReturnsFail() { 87 + public void testCreateUserReturnsFailure() {
70 88
71 given() 89 given()
72 .userDto(EXISTING_USERNAME_TEST); 90 .userDto(EXISTING_USERNAME_TEST);
@@ -110,6 +128,12 @@ public class UserServiceTest extends JpaTestUtils { @@ -110,6 +128,12 @@ public class UserServiceTest extends JpaTestUtils {
110 return this; 128 return this;
111 } 129 }
112 130
  131 + private UserServiceTest loginUser(final String password) {
  132 + userDto.withPassword(password);
  133 + actual = underTest.loginUser(userDto);
  134 + return this;
  135 + }
  136 +
113 private UserServiceTest createUser() { 137 private UserServiceTest createUser() {
114 txBegin(); 138 txBegin();
115 actual = underTest.createUser(userDto); 139 actual = underTest.createUser(userDto);
@@ -132,6 +156,10 @@ public class UserServiceTest extends JpaTestUtils { @@ -132,6 +156,10 @@ public class UserServiceTest extends JpaTestUtils {
132 return this; 156 return this;
133 } 157 }
134 158
  159 + private void assertLoginSuccess() {
  160 + assertTrue(actual);
  161 + }
  162 +
135 private void assertCreateSuccess() { 163 private void assertCreateSuccess() {
136 // wir suchen den soeben erstellten Benutzer; wenn er existiert, ist alles gut 164 // wir suchen den soeben erstellten Benutzer; wenn er existiert, ist alles gut
137 final User user = userDao.findByUsername(USERNAME_TEST); 165 final User user = userDao.findByUsername(USERNAME_TEST);
src/test/resources/net/ziemers/swxercise/testdata/UserServiceTestData.xml
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <dataset> 2 <dataset>
3 - <!-- die Grosz-/Kleinschreibung scheint unter bestimmten Bedingungen von Bedeutung zu sein! -->  
4 - <Profile id="1" username="username_profile" passwordHash="passwordHash_profile" hashAlgorithm="SHA512" salt="salt_profile"/> 3 + <!-- die Gross-/Kleinschreibung scheint unter bestimmten Bedingungen von Bedeutung zu sein! -->
  4 + <!-- das Kennwort ist "secret" -->
  5 + <Profile id="1" username="username_profile" passwordHash="9EOZEsheQcXixUO/XW/vPwRlZh9NMyHppli2qKe3U9+Y0EHhtJU1AA56pkduCGsSbp8VZ7so/62+3cftxBhJGg==" hashAlgorithm="SHA512" salt="FuQll2Av59se1r1Dc4WEPDmzZw4="/>
5 <User id="2" firstname="firstname_user" lastname="lastname_user" profile_id="1"/> 6 <User id="2" firstname="firstname_user" lastname="lastname_user" profile_id="1"/>
6 </dataset> 7 </dataset>