Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JDBC 라이브러리 구현 3,4단계] 리오(오영택) 미션 제출합니다. #574

Merged
merged 12 commits into from
Oct 10, 2023
6 changes: 6 additions & 0 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ public User findByAccount(String account) {
}
return user.get();
}

public void deleteAll() {
Jaeyoung22 marked this conversation as resolved.
Show resolved Hide resolved
String sql = "delete from users";

jdbcTemplate.update(sql);
}
}
36 changes: 36 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,36 @@
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) {
User user = findById(id);
user.changePassword(newPassword);

userDao.update(user);
userHistoryDao.log(new UserHistory(user, createBy));
}
}
34 changes: 34 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,34 @@
package com.techcourse.service;

import com.techcourse.domain.User;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.concurrent.atomic.AtomicReference;

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(long id) {
Copy link

@somsom13 somsom13 Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findById는 TransactionTemplate 으로 실행시키지 않으신 이유가 궁금해요~!!

AppUserService의 changePassword 내에서 findById를 호출하는 걸 보면, findById도 트랜잭션 내에서 실행시켜줘야 하지 않을까 싶은데 리오는 어떻게 생각하시나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇네요 제가 readonly=False의 느낌으로 트랜잭션을 설정했었는데, 바론 말대로 한 트랜잭션 내에서 진행이 되어야 겠군요... 감사합니다!

AtomicReference<User> user = new AtomicReference<>();
transactionTemplate.execute(() -> user.set(userService.findById(id)));
return user.get();
}

@Override
public void insert(User user) {
transactionTemplate.execute(() -> userService.insert(user));
}

@Override
public void changePassword(long id, String newPassword, String createBy) {
transactionTemplate.execute(() -> userService.changePassword(id, newPassword, createBy));
}
}
28 changes: 4 additions & 24 deletions app/src/main/java/com/techcourse/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
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 UserService {
public interface UserService {

private final UserDao userDao;
private final UserHistoryDao userHistoryDao;
User findById(long id);

public UserService(UserDao userDao, UserHistoryDao userHistoryDao) {
this.userDao = userDao;
this.userHistoryDao = userHistoryDao;
}
void insert(User user);

public User findById(long id) {
return userDao.findById(id);
}

public void insert(User user) {
userDao.insert(user);
}

public void changePassword(long id, String newPassword, String createBy) {
User user = findById(id);
user.changePassword(newPassword);
userDao.update(user);
userHistoryDao.log(new UserHistory(user, createBy));
}
void changePassword(long id, String newPassword, String createBy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static void execute(DataSource dataSource) {
URL url = DatabasePopulatorUtils.class.getClassLoader().getResource("schema.sql");
File file = new File(url.getFile());
String sql = Files.readString(file.toPath());

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute(sql);
} catch (IOException | NullPointerException e) {
Expand Down
12 changes: 8 additions & 4 deletions app/src/test/java/com/techcourse/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
class UserDaoTest {

private UserDao userDao;
private User user;

@BeforeEach
void setup() {
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance());

userDao = new UserDao(DataSourceConfig.getInstance());
userDao.deleteAll();
final var user = new User("gugu", "password", "[email protected]");
userDao.insert(user);
this.user = userDao.findByAccount("gugu");
}

@Test
Expand All @@ -30,7 +33,7 @@ void findAll() {

@Test
void findById() {
final var user = userDao.findById(1L);
final var user = userDao.findById(this.user.getId());

assertThat(user.getAccount()).isEqualTo("gugu");
}
Expand All @@ -48,21 +51,22 @@ void insert() {
final var account = "insert-gugu";
final var user = new User(account, "password", "[email protected]");
userDao.insert(user);
User insertedUser = userDao.findByAccount(account);

final var actual = userDao.findById(2L);
final var actual = userDao.findById(insertedUser.getId());

assertThat(actual.getAccount()).isEqualTo(account);
}

@Test
void update() {
final var newPassword = "password99";
final var user = userDao.findById(1L);
final var user = userDao.findById(this.user.getId());
user.changePassword(newPassword);

userDao.update(user);

final var actual = userDao.findById(1L);
final var actual = userDao.findById(user.getId());

assertThat(actual.getPassword()).isEqualTo(newPassword);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,41 @@
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 static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@Disabled
class UserServiceTest {
class AppUserServiceTest {

private JdbcTemplate jdbcTemplate;
private UserDao userDao;
private User user;

@BeforeEach
void setUp() {
this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance());
this.userDao = new UserDao(jdbcTemplate);

DatabasePopulatorUtils.execute(DataSourceConfig.getInstance());
userDao.deleteAll();

final var user = new User("gugu", "password", "[email protected]");
userDao.insert(user);
this.user = userDao.findByAccount("gugu");
}

@Test
void testChangePassword() {
final var userHistoryDao = new UserHistoryDao(jdbcTemplate);
final var userService = new UserService(userDao, userHistoryDao);
final var userService = new AppUserService(userDao, userHistoryDao);

final var newPassword = "qqqqq";
final var createBy = "gugu";
userService.changePassword(1L, newPassword, createBy);
userService.changePassword(user.getId(), newPassword, createBy);

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

assertThat(actual.getPassword()).isEqualTo(newPassword);
}

@Test
void testTransactionRollback() {
// 트랜잭션 롤백 테스트를 위해 mock으로 교체
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate);
final var userService = new UserService(userDao, userHistoryDao);

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

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

assertThat(actual.getPassword()).isNotEqualTo(newPassword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.techcourse.dao.UserHistoryDao;
import com.techcourse.domain.UserHistory;
import org.springframework.dao.DataAccessException;
import org.springframework.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class MockUserHistoryDao extends UserHistoryDao {
Expand All @@ -13,6 +13,7 @@ public MockUserHistoryDao(final JdbcTemplate jdbcTemplate) {

@Override
public void log(final UserHistory userHistory) {
System.out.println("asdf");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 ㅎㅎ 🙈

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗! 이대로 머지가 되다니... 슬프군요... 죄송합니다....

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗.. 이럴 수가.. DM으로 의견 여쭤보고 머지할걸 그랬네용.. 저도 죄송합니다 🥲

throw new DataAccessException();
}
}
58 changes: 58 additions & 0 deletions app/src/test/java/com/techcourse/service/TxUserServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.techcourse.service;

import com.techcourse.config.DataSourceConfig;
import com.techcourse.dao.UserDao;
import com.techcourse.domain.User;
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TxUserServiceTest {

private JdbcTemplate jdbcTemplate;
private UserDao userDao;
private TransactionTemplate transactionTemplate;
private User user;

@BeforeEach
void setUp() {
DataSource dataSource = DataSourceConfig.getInstance();

DatabasePopulatorUtils.execute(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.userDao = new UserDao(jdbcTemplate);
this.transactionTemplate = new TransactionTemplate(dataSource);

userDao.deleteAll();


final var user = new User("gugu", "password", "[email protected]");
userDao.insert(user);
this.user = userDao.findByAccount("gugu");
}

@Test
void testTransactionRollback() {
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate);
final var appUserService = new AppUserService(userDao, userHistoryDao);
final var userService = new TxUserService(appUserService, transactionTemplate);

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

assertThrows(DataAccessException.class,
() -> userService.changePassword(user.getId(), newPassword, createBy));

final var actual = userService.findById(user.getId());

assertThat(actual.getPassword()).isNotEqualTo(newPassword);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.dao;
package org.springframework;

public class DataAccessException extends RuntimeException {

Expand Down
Loading
Loading