-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
cascade 외래키 조건 삭제를 위해 관련 서비스 함수 테스트 코드 추가
- Loading branch information
Showing
7 changed files
with
636 additions
and
5 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
297 changes: 297 additions & 0 deletions
297
src/test/java/hous/server/service/room/RoomServiceTest.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,297 @@ | ||
package hous.server.service.room; | ||
|
||
import hous.server.domain.badge.Represent; | ||
import hous.server.domain.badge.repository.AcquireRepository; | ||
import hous.server.domain.badge.repository.RepresentRepository; | ||
import hous.server.domain.notification.repository.NotificationRepository; | ||
import hous.server.domain.personality.PersonalityColor; | ||
import hous.server.domain.personality.repository.PersonalityRepository; | ||
import hous.server.domain.room.Participate; | ||
import hous.server.domain.room.Room; | ||
import hous.server.domain.room.repository.ParticipateRepository; | ||
import hous.server.domain.room.repository.RoomRepository; | ||
import hous.server.domain.rule.repository.RuleRepository; | ||
import hous.server.domain.todo.DayOfWeek; | ||
import hous.server.domain.todo.Redo; | ||
import hous.server.domain.todo.Take; | ||
import hous.server.domain.todo.Todo; | ||
import hous.server.domain.todo.repository.RedoRepository; | ||
import hous.server.domain.todo.repository.TakeRepository; | ||
import hous.server.domain.todo.repository.TodoRepository; | ||
import hous.server.domain.user.Onboarding; | ||
import hous.server.domain.user.TestScore; | ||
import hous.server.domain.user.User; | ||
import hous.server.domain.user.UserSocialType; | ||
import hous.server.domain.user.repository.OnboardingRepository; | ||
import hous.server.domain.user.repository.TestScoreRepository; | ||
import hous.server.domain.user.repository.UserRepository; | ||
import hous.server.service.room.dto.request.SetRoomNameRequestDto; | ||
import hous.server.service.todo.TodoService; | ||
import hous.server.service.todo.dto.request.TodoInfoRequestDto; | ||
import hous.server.service.user.UserService; | ||
import hous.server.service.user.dto.request.CreateUserRequestDto; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles(value = "local") | ||
public class RoomServiceTest { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private OnboardingRepository onboardingRepository; | ||
|
||
@Autowired | ||
private RoomRepository roomRepository; | ||
|
||
@Autowired | ||
private ParticipateRepository participateRepository; | ||
|
||
@Autowired | ||
private RedoRepository redoRepository; | ||
|
||
@Autowired | ||
private TakeRepository takeRepository; | ||
|
||
@Autowired | ||
private TodoRepository todoRepository; | ||
|
||
@Autowired | ||
private AcquireRepository acquireRepository; | ||
|
||
@Autowired | ||
private NotificationRepository notificationRepository; | ||
|
||
@Autowired | ||
private RepresentRepository representRepository; | ||
|
||
@Autowired | ||
private TestScoreRepository testScoreRepository; | ||
|
||
@Autowired | ||
private PersonalityRepository personalityRepository; | ||
|
||
@Autowired | ||
private RuleRepository ruleRepository; | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private RoomService roomService; | ||
|
||
@Autowired | ||
private TodoService todoService; | ||
|
||
@BeforeEach | ||
public void reset() { | ||
redoRepository.deleteAllInBatch(); | ||
takeRepository.deleteAllInBatch(); | ||
todoRepository.deleteAllInBatch(); | ||
participateRepository.deleteAllInBatch(); | ||
acquireRepository.deleteAllInBatch(); | ||
ruleRepository.deleteAllInBatch(); | ||
representRepository.deleteAllInBatch(); | ||
testScoreRepository.deleteAllInBatch(); | ||
roomRepository.deleteAllInBatch(); | ||
notificationRepository.deleteAllInBatch(); | ||
onboardingRepository.deleteAllInBatch(); | ||
userRepository.deleteAllInBatch(); | ||
} | ||
|
||
@Test | ||
@DisplayName("방에 들어가자마자 나가기 성공") | ||
@Transactional | ||
public void leave_room_success() { | ||
// given | ||
CreateUserRequestDto createUserRequestDto = CreateUserRequestDto.of( | ||
"socialId1", UserSocialType.KAKAO, "fcmToken1", "nickname1", "2022-01-01", true); | ||
Long userId = userService.registerUser(createUserRequestDto); | ||
User user = userRepository.findUserById(userId); | ||
|
||
SetRoomNameRequestDto setRoomNameRequestDto = SetRoomNameRequestDto.of("room1"); | ||
roomService.createRoom(setRoomNameRequestDto, userId); | ||
|
||
TodoInfoRequestDto todoInfoRequestDto = TodoInfoRequestDto.of("todo1", true, | ||
List.of(TodoInfoRequestDto.TodoUser.builder() | ||
.onboardingId(user.getOnboarding().getId()) | ||
.dayOfWeeks(List.of(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY)) | ||
.build())); | ||
todoService.createTodo(todoInfoRequestDto, userId); | ||
|
||
// when | ||
roomService.leaveRoom(userId); | ||
|
||
// then | ||
List<Room> rooms = roomRepository.findAll(); | ||
assertThat(rooms).isEmpty(); | ||
|
||
// 방을 나간 후 내가 추가한 할일이 삭제되었는지 확인 | ||
List<Todo> todos = todoRepository.findAll(); | ||
List<Take> takes = takeRepository.findAll(); | ||
List<Redo> redos = redoRepository.findAll(); | ||
assertThat(todos).isEmpty(); | ||
assertThat(takes).isEmpty(); | ||
assertThat(redos).isEmpty(); | ||
|
||
// 방을 나간 후 나의 정보들이 삭제되었는지 확인 | ||
List<Onboarding> onboardings = onboardingRepository.findAll(); | ||
assertThat(onboardings).hasSize(1); | ||
Onboarding onboarding = onboardings.get(0); | ||
|
||
List<Represent> represents = representRepository.findAll(); | ||
assertThat(represents).isEmpty(); | ||
List<TestScore> testScores = testScoreRepository.findAll(); | ||
assertThat(testScores).isEmpty(); | ||
assertThat(onboarding.getPersonality()).isEqualTo(personalityRepository.findPersonalityByColor(PersonalityColor.GRAY)); | ||
assertThat(onboarding.isPublic()).isFalse(); | ||
assertThat(onboarding.getMbti()).isNull(); | ||
assertThat(onboarding.getJob()).isNull(); | ||
assertThat(onboarding.getIntroduction()).isNull(); | ||
assertThat(onboarding.getTestScore()).isNull(); | ||
assertThat(onboarding.getRepresent()).isNull(); | ||
} | ||
|
||
@Test | ||
@DisplayName("방에 여러명 참여할 때 주인이 아닌 사람이 나가기 성공") | ||
@Transactional | ||
public void leave_room_by_participate_success() { | ||
// given | ||
CreateUserRequestDto createUserRequestDto1 = CreateUserRequestDto.of( | ||
"socialId1", UserSocialType.KAKAO, "fcmToken1", "nickname1", "2022-01-01", true); | ||
Long owner = userService.registerUser(createUserRequestDto1); | ||
User ownerUser = userRepository.findUserById(owner); | ||
|
||
CreateUserRequestDto createUserRequestDto2 = CreateUserRequestDto.of( | ||
"socialId2", UserSocialType.KAKAO, "fcmToken2", "nickname2", "2022-01-01", true); | ||
Long userId2 = userService.registerUser(createUserRequestDto2); | ||
User user2 = userRepository.findUserById(userId2); | ||
|
||
CreateUserRequestDto createUserRequestDto3 = CreateUserRequestDto.of( | ||
"socialId3", UserSocialType.KAKAO, "fcmToken3", "nickname3", "2022-01-01", true); | ||
Long userId3 = userService.registerUser(createUserRequestDto3); | ||
|
||
SetRoomNameRequestDto setRoomNameRequestDto = SetRoomNameRequestDto.of("room1"); | ||
Long roomId = roomService.createRoom(setRoomNameRequestDto, owner).getRoomId(); | ||
roomService.joinRoom(roomId, userId2); | ||
roomService.joinRoom(roomId, userId3); | ||
|
||
TodoInfoRequestDto todoInfoRequestDto = TodoInfoRequestDto.of("todo1", true, | ||
List.of(TodoInfoRequestDto.TodoUser.builder() | ||
.onboardingId(ownerUser.getOnboarding().getId()) | ||
.dayOfWeeks(List.of(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY)) | ||
.build(), | ||
TodoInfoRequestDto.TodoUser.builder() | ||
.onboardingId(user2.getOnboarding().getId()) | ||
.dayOfWeeks(List.of(DayOfWeek.TUESDAY, DayOfWeek.THURSDAY)) | ||
.build())); | ||
todoService.createTodo(todoInfoRequestDto, userId2); | ||
|
||
// when | ||
roomService.leaveRoom(userId2); | ||
|
||
// then | ||
List<Room> rooms = roomRepository.findAll(); | ||
assertThat(rooms).hasSize(1); | ||
List<Participate> participates = participateRepository.findAll(); | ||
assertThat(participates).hasSize(2); | ||
assertThat(owner).isEqualTo(rooms.get(0).getOwner().getUser().getId()); | ||
|
||
// 방을 나간 후 나간 유저가(userId2) 담당자에서 빠졌는지 확인 | ||
List<Todo> todos = todoRepository.findAll(); | ||
List<Take> takes = takeRepository.findAll(); | ||
List<Redo> redos = redoRepository.findAll(); | ||
assertThat(todos).hasSize(1); | ||
assertThat(takes).hasSize(1); | ||
assertThat(redos).hasSize(3); | ||
assertThat(takes.get(0).getOnboarding().getUser().getId()).isEqualTo(owner); | ||
|
||
// 방을 나간 후 나의 정보들이 삭제되었는지 확인 | ||
User user = userRepository.findUserById(userId2); | ||
Onboarding onboarding = user.getOnboarding(); | ||
assertThat(onboarding.getPersonality()).isEqualTo(personalityRepository.findPersonalityByColor(PersonalityColor.GRAY)); | ||
assertThat(onboarding.isPublic()).isFalse(); | ||
assertThat(onboarding.getMbti()).isNull(); | ||
assertThat(onboarding.getJob()).isNull(); | ||
assertThat(onboarding.getIntroduction()).isNull(); | ||
assertThat(onboarding.getTestScore()).isNull(); | ||
assertThat(onboarding.getRepresent()).isNull(); | ||
} | ||
|
||
@Test | ||
@DisplayName("방에 여러명 참여할 때 주인이 나가기 성공 후 owner 넘겨주기 성공") | ||
@Transactional | ||
public void leave_room_by_owner_success() { | ||
// given | ||
CreateUserRequestDto createUserRequestDto1 = CreateUserRequestDto.of( | ||
"socialId1", UserSocialType.KAKAO, "fcmToken1", "nickname1", "2022-01-01", true); | ||
Long owner = userService.registerUser(createUserRequestDto1); | ||
User ownerUser = userRepository.findUserById(owner); | ||
|
||
CreateUserRequestDto createUserRequestDto2 = CreateUserRequestDto.of( | ||
"socialId2", UserSocialType.KAKAO, "fcmToken2", "nickname2", "2022-01-01", true); | ||
Long userId2 = userService.registerUser(createUserRequestDto2); | ||
User user2 = userRepository.findUserById(userId2); | ||
|
||
CreateUserRequestDto createUserRequestDto3 = CreateUserRequestDto.of( | ||
"socialId3", UserSocialType.KAKAO, "fcmToken3", "nickname3", "2022-01-01", true); | ||
Long userId3 = userService.registerUser(createUserRequestDto3); | ||
|
||
SetRoomNameRequestDto setRoomNameRequestDto = SetRoomNameRequestDto.of("room1"); | ||
Long roomId = roomService.createRoom(setRoomNameRequestDto, owner).getRoomId(); | ||
roomService.joinRoom(roomId, userId2); | ||
roomService.joinRoom(roomId, userId3); | ||
|
||
TodoInfoRequestDto todoInfoRequestDto = TodoInfoRequestDto.of("todo1", true, | ||
List.of(TodoInfoRequestDto.TodoUser.builder() | ||
.onboardingId(ownerUser.getOnboarding().getId()) | ||
.dayOfWeeks(List.of(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY)) | ||
.build(), | ||
TodoInfoRequestDto.TodoUser.builder() | ||
.onboardingId(user2.getOnboarding().getId()) | ||
.dayOfWeeks(List.of(DayOfWeek.TUESDAY, DayOfWeek.THURSDAY)) | ||
.build())); | ||
todoService.createTodo(todoInfoRequestDto, userId2); | ||
|
||
// when | ||
roomService.leaveRoom(owner); | ||
|
||
// then | ||
List<Room> rooms = roomRepository.findAll(); | ||
assertThat(rooms).hasSize(1); | ||
List<Participate> participates = rooms.get(0).getParticipates(); | ||
assertThat(participates).hasSize(2); | ||
assertThat(userId2).isEqualTo(rooms.get(0).getOwner().getUser().getId()); // 두번째로 들어간 사람이 주인이 된다. | ||
|
||
// 방을 나간 후 나간 유저가(owner) 담당자에서 빠졌는지 확인 | ||
List<Todo> todos = todoRepository.findAll(); | ||
List<Take> takes = takeRepository.findAll(); | ||
List<Redo> redos = redoRepository.findAll(); | ||
assertThat(todos).hasSize(1); | ||
assertThat(takes).hasSize(1); | ||
assertThat(redos).hasSize(2); | ||
assertThat(takes.get(0).getOnboarding().getUser().getId()).isEqualTo(userId2); | ||
|
||
// 방을 나간 후 나의 정보들이 삭제되었는지 확인 | ||
User user = userRepository.findUserById(owner); | ||
Onboarding onboarding = user.getOnboarding(); | ||
assertThat(onboarding.getPersonality()).isEqualTo(personalityRepository.findPersonalityByColor(PersonalityColor.GRAY)); | ||
assertThat(onboarding.isPublic()).isFalse(); | ||
assertThat(onboarding.getMbti()).isNull(); | ||
assertThat(onboarding.getJob()).isNull(); | ||
assertThat(onboarding.getIntroduction()).isNull(); | ||
assertThat(onboarding.getTestScore()).isNull(); | ||
assertThat(onboarding.getRepresent()).isNull(); | ||
} | ||
} |
Oops, something went wrong.