-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 패키지 위치 변경 및 코드 정리 * test: 학습 테스트 * refactor: row 한개 조회 로직 수정 * feat: 트랜잭션 적용 * refactor: 클래스 제거 --------- Co-authored-by: kang-hyungu <[email protected]>
- Loading branch information
1 parent
737e2d4
commit 7e8c6d3
Showing
11 changed files
with
303 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,81 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.SQLException; | ||
|
||
public class UserService { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserService.class); | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource = DataSourceConfig.getInstance(); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
try (final var conn = dataSource.getConnection()) { | ||
try { | ||
conn.setAutoCommit(false); | ||
|
||
return userDao.findById(conn, id); | ||
} catch (Exception e) { | ||
conn.rollback(); | ||
} finally { | ||
conn.setAutoCommit(true); | ||
} | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
log.info(e.getMessage()); | ||
} | ||
throw new RuntimeException(); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
try (final var conn = dataSource.getConnection()) { | ||
try { | ||
conn.setAutoCommit(false); | ||
|
||
userDao.insert(conn, user); | ||
} catch (Exception e) { | ||
conn.rollback(); | ||
} finally { | ||
conn.setAutoCommit(true); | ||
} | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
log.info(e.getMessage()); | ||
} | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
try (final var conn = dataSource.getConnection()) { | ||
try { | ||
conn.setAutoCommit(false); | ||
|
||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
} catch (Exception e) { | ||
conn.rollback(); | ||
} finally { | ||
conn.setAutoCommit(true); | ||
} | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
log.info(e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
package com.techcourse.dao; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.exception.DataAccessException; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class UserDaoTest { | ||
|
||
private static final Connection conn; | ||
|
||
static { | ||
try { | ||
conn = DataSourceConfig.getInstance().getConnection(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
|
@@ -20,27 +35,27 @@ void setup() { | |
|
||
userDao = new UserDao(new JdbcTemplate(DataSourceConfig.getInstance())); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
userDao.insert(conn, user); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(); | ||
final var users = userDao.findAll(conn); | ||
|
||
assertThat(users).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void findById() { | ||
final var user = userDao.findById(1L); | ||
final var user = userDao.findById(conn, 1L); | ||
|
||
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void findByAccount() { | ||
final var account = "gugu"; | ||
final var user = userDao.findByAccount(account); | ||
final var user = userDao.findByAccount(conn, account); | ||
|
||
assertThat(user.getAccount()).isEqualTo(account); | ||
} | ||
|
@@ -49,39 +64,39 @@ void findByAccount() { | |
void insert() { | ||
final var account = "insert-gugu"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(user); | ||
userDao.insert(conn, user); | ||
|
||
final var actual = userDao.findById(2L); | ||
final var actual = userDao.findById(conn, 2L); | ||
|
||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void update() { | ||
final var newPassword = "password99"; | ||
final var user = userDao.findById(1L); | ||
final var user = userDao.findById(conn, 1L); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(user); | ||
userDao.update(conn, user); | ||
|
||
final var actual = userDao.findById(1L); | ||
final var actual = userDao.findById(conn, 1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
||
@Test | ||
void deleteAll() { | ||
userDao.deleteAll(); | ||
userDao.deleteAll(conn); | ||
|
||
List<User> users = userDao.findAll(); | ||
List<User> users = userDao.findAll(conn); | ||
assertThat(users).isEmpty(); | ||
} | ||
|
||
@Test | ||
void findObjectReturnNull() { | ||
userDao.deleteAll(); | ||
userDao.deleteAll(conn); | ||
|
||
User user = userDao.findById(1L); | ||
assertThat(user).isNull(); | ||
assertThatThrownBy(() -> userDao.findById(conn, 1L)) | ||
.isInstanceOf(DataAccessException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,29 +5,27 @@ | |
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@Disabled | ||
class UserServiceTest { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
void setUp() throws SQLException { | ||
this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
|
||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
userDao.insert(DataSourceConfig.getInstance().getConnection(), user); | ||
} | ||
|
||
@Test | ||
|
@@ -53,8 +51,7 @@ void testTransactionRollback() { | |
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
assertThrows(DataAccessException.class, | ||
() -> userService.changePassword(1L, newPassword, createBy)); | ||
userService.changePassword(1L, newPassword, createBy); | ||
|
||
final var actual = userService.findById(1L); | ||
|
||
|
Oops, something went wrong.