Skip to content

Commit

Permalink
feat: UserService를 인터페이스로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchannn committed Oct 10, 2023
1 parent afa48cb commit 002529c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 64 deletions.
72 changes: 72 additions & 0 deletions app/src/main/java/com/techcourse/service/AppUserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.techcourse.service;

import com.techcourse.dao.UserDao;
import com.techcourse.dao.UserHistoryDao;
import com.techcourse.domain.User;
import com.techcourse.domain.UserHistory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.NoSuchElementException;

public class AppUserService implements UserService {

private final UserDao userDao;
private final UserHistoryDao userHistoryDao;
private final DataSource dataSource;

public AppUserService(
final UserDao userDao,
final UserHistoryDao userHistoryDao,
final DataSource dataSource
) {
this.userDao = userDao;
this.userHistoryDao = userHistoryDao;
this.dataSource = dataSource;
}

@Override
public User findById(final long id) throws SQLException {
return userDao.findById(dataSource.getConnection(), id)
.orElseThrow(() -> new NoSuchElementException("해당 아이디의 사용자가 존재하지 않습니다."));
}

@Override
public void insert(final User user) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
try {
userDao.insert(conn, user);

conn.commit();
} catch (SQLException e) {
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}

@Override
public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
final var user = findById(id);
user.changePassword(newPassword);
try {
userDao.update(conn, user);
userHistoryDao.log(conn, new UserHistory(user, createBy));

conn.commit();
} catch (SQLException | DataAccessException e) {
System.out.println("e = " + e);
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
}
71 changes: 71 additions & 0 deletions app/src/main/java/com/techcourse/service/TxUserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.techcourse.service;

import com.techcourse.domain.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class TxUserService implements UserService {

private final DataSource dataSource;
private final UserService userService;

public TxUserService(
final DataSource dataSource,
final UserService userService) {
this.dataSource = dataSource;
this.userService = userService;
}

@Override
public User findById(final long id) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
try {
final User user = userService.findById(id);

conn.commit();
return user;
} catch (SQLException e) {
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}

@Override
public void insert(final User user) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
try {
userService.insert(user);

conn.commit();
} catch (SQLException e) {
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}

@Override
public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
try {
userService.changePassword(id, newPassword, createBy);

conn.commit();
} catch (SQLException e) {
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
}
63 changes: 4 additions & 59 deletions app/src/main/java/com/techcourse/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,14 @@
package com.techcourse.service;

import com.techcourse.dao.UserDao;
import com.techcourse.dao.UserHistoryDao;
import com.techcourse.domain.User;
import com.techcourse.domain.UserHistory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.NoSuchElementException;

public class UserService {
public interface UserService {

private final UserDao userDao;
private final UserHistoryDao userHistoryDao;
private final DataSource dataSource;
User findById(final long id) throws SQLException;

public UserService(
final UserDao userDao,
final UserHistoryDao userHistoryDao,
final DataSource dataSource
) {
this.userDao = userDao;
this.userHistoryDao = userHistoryDao;
this.dataSource = dataSource;
}
void insert(final User user) throws SQLException;

public User findById(final long id) throws SQLException {
return userDao.findById(dataSource.getConnection(), id)
.orElseThrow(() -> new NoSuchElementException("해당 아이디의 사용자가 존재하지 않습니다."));
}

public void insert(final User user) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
try {
userDao.insert(conn, user);

conn.commit();
} catch (SQLException e) {
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}

public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException {
final Connection conn = DataSourceUtils.getConnection(dataSource);
conn.setAutoCommit(false);
final var user = findById(id);
user.changePassword(newPassword);
try {
userDao.update(conn, user);
userHistoryDao.log(conn, new UserHistory(user, createBy));

conn.commit();
} catch (SQLException | DataAccessException e) {
System.out.println("e = " + e);
conn.rollback();
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
void changePassword(final long id, final String newPassword, final String createBy) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class UserServiceTest {
class AppUserServiceTest {

private JdbcTemplate jdbcTemplate;
private UserDao userDao;
Expand All @@ -33,7 +33,7 @@ void setUp() throws SQLException {
@Test
void testChangePassword() throws SQLException {
final var userHistoryDao = new UserHistoryDao(jdbcTemplate);
final var userService = new UserService(userDao, userHistoryDao, jdbcTemplate.getDataSource());
final var userService = new AppUserService(userDao, userHistoryDao, jdbcTemplate.getDataSource());

final var newPassword = "qqqqq";
final var createBy = "gugu";
Expand All @@ -48,18 +48,18 @@ void testChangePassword() throws SQLException {
void testTransactionRollback() throws SQLException {
// 트랜잭션 롤백 테스트를 위해 mock으로 교체
final MockUserHistoryDao userHistoryDao = new MockUserHistoryDao(jdbcTemplate);
final UserService userService = new UserService(userDao, userHistoryDao, jdbcTemplate.getDataSource());
final AppUserService appUserService = new AppUserService(userDao, userHistoryDao, jdbcTemplate.getDataSource());

final var newPassword = "newPassword";
final var createBy = "gugu";

// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다.
assertThrows(
DataAccessException.class,
() -> userService.changePassword(1L, newPassword, createBy)
() -> appUserService.changePassword(1L, newPassword, createBy)
);

final var actual = userService.findById(1L);
final var actual = appUserService.findById(1L);

assertThat(actual.getPassword()).isNotEqualTo(newPassword);
}
Expand Down

0 comments on commit 002529c

Please sign in to comment.