-
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.
* 패키지 위치 변경 및 코드 정리 * feat: 트랜잭션 required 형태 전파 * refactor: exception 처리 수정 --------- Co-authored-by: kang-hyungu <[email protected]>
- Loading branch information
1 parent
7e8c6d3
commit 393ace4
Showing
21 changed files
with
313 additions
and
149 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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/techcourse/service/AppUserService.java
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
|
||
public class AppUserService implements UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public AppUserService(UserDao userDao, UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/techcourse/service/TxUserService.java
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
public TxUserService(UserService userService, TransactionTemplate transactionTemplate) { | ||
this.userService = userService; | ||
this.transactionTemplate = transactionTemplate; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionTemplate.execute(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionTemplate.execute(() -> userService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionTemplate.execute(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
} |
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,81 +1,12 @@ | ||
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 interface UserService { | ||
|
||
public class UserService { | ||
User findById(final long id); | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserService.class); | ||
void insert(final User user); | ||
|
||
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) { | ||
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) { | ||
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) { | ||
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()); | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
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 |
---|---|---|
|
@@ -35,27 +35,27 @@ void setup() { | |
|
||
userDao = new UserDao(new JdbcTemplate(DataSourceConfig.getInstance())); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(conn, user); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(conn); | ||
final var users = userDao.findAll(); | ||
|
||
assertThat(users).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void findById() { | ||
final var user = userDao.findById(conn, 1L); | ||
final var user = userDao.findById(1L); | ||
|
||
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void findByAccount() { | ||
final var account = "gugu"; | ||
final var user = userDao.findByAccount(conn, account); | ||
final var user = userDao.findByAccount(account); | ||
|
||
assertThat(user.getAccount()).isEqualTo(account); | ||
} | ||
|
@@ -64,39 +64,39 @@ void findByAccount() { | |
void insert() { | ||
final var account = "insert-gugu"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(conn, user); | ||
userDao.insert(user); | ||
|
||
final var actual = userDao.findById(conn, 2L); | ||
final var actual = userDao.findById(2L); | ||
|
||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void update() { | ||
final var newPassword = "password99"; | ||
final var user = userDao.findById(conn, 1L); | ||
final var user = userDao.findById(1L); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(conn, user); | ||
userDao.update(user); | ||
|
||
final var actual = userDao.findById(conn, 1L); | ||
final var actual = userDao.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
||
@Test | ||
void deleteAll() { | ||
userDao.deleteAll(conn); | ||
userDao.deleteAll(); | ||
|
||
List<User> users = userDao.findAll(conn); | ||
List<User> users = userDao.findAll(); | ||
assertThat(users).isEmpty(); | ||
} | ||
|
||
@Test | ||
void findObjectReturnNull() { | ||
userDao.deleteAll(conn); | ||
userDao.deleteAll(); | ||
|
||
assertThatThrownBy(() -> userDao.findById(conn, 1L)) | ||
assertThatThrownBy(() -> userDao.findById(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 |
---|---|---|
|
@@ -7,31 +7,35 @@ | |
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import java.sql.SQLException; | ||
import org.springframework.transaction.support.TransactionManager; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class UserServiceTest { | ||
class TxUserServiceTest { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setUp() throws SQLException { | ||
void setUp() { | ||
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(DataSourceConfig.getInstance().getConnection(), user); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void testChangePassword() { | ||
final var transactionTemplate = new TransactionTemplate(new TransactionManager(DataSourceConfig.getInstance())); | ||
final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(userDao, userHistoryDao); | ||
final var appUserService = new AppUserService(userDao, userHistoryDao); | ||
final var userService = new TxUserService(appUserService, transactionTemplate); | ||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
|
@@ -43,15 +47,19 @@ void testChangePassword() { | |
} | ||
|
||
@Test | ||
void testTransactionRollback() { | ||
void testTransactionRollback2() { | ||
// 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(userDao, userHistoryDao); | ||
// 애플리케이션 서비스 | ||
final var appUserService = new AppUserService(userDao, userHistoryDao); | ||
// 트랜잭션 서비스 추상화 | ||
final var userService = new TxUserService(appUserService, new TransactionTemplate(new TransactionManager(DataSourceConfig.getInstance()))); | ||
|
||
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
userService.changePassword(1L, newPassword, createBy); | ||
assertThrows(DataAccessException.class, | ||
() -> userService.changePassword(1L, newPassword, createBy)); | ||
|
||
final var actual = userService.findById(1L); | ||
|
||
|
Oops, something went wrong.