Skip to content

Commit

Permalink
Merge pull request #298 from Hous-Release/test/#297
Browse files Browse the repository at this point in the history
cascade 외래키 조건 삭제를 위해 관련 서비스 함수 테스트 코드 추가
  • Loading branch information
hyejungg authored Feb 24, 2023
2 parents a98bddb + c5423c4 commit 374412e
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/hous/server/service/rule/RuleServiceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static void existsRuleByRoomRules(Room room, List<String> requestRules) {
List<String> rules = room.getRules().stream().map(Rule::getName).collect(Collectors.toList());
for (String ruleName : requestRules) {
if (rules.contains(ruleName)) {
throw new ConflictException(String.format("방 (%s) 에 이미 존재하는 규칙 (%s) 입니다.", room.getId(), ruleName), CONFLICT_RULE_EXCEPTION);
throw new ConflictException(String.format("방 (%s) 에 이미 존재하는 ruleName (%s) 입니다.", room.getId(), ruleName), CONFLICT_RULE_EXCEPTION);

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import hous.server.domain.common.Constraint;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
Expand All @@ -14,10 +11,18 @@
@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
public class DeleteRuleReqeustDto {

@ApiModelProperty(value = "규칙 id 배열", example = "[12, 13, 14, ...]")
@NotNull(message = "{rule.list.notNull}")
@Size(min = Constraint.RULE_LIST_MIN, message = "{rule.list.min}")
private List<Long> rulesIdList;

public static DeleteRuleReqeustDto of(List<Long> rulesIdList) {
return DeleteRuleReqeustDto.builder()
.rulesIdList(rulesIdList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
public class DeleteUserRequestDto {

@ApiModelProperty(value = "사유", example = "DONE_LIVING_TOGETHER")
Expand All @@ -22,4 +23,11 @@ public class DeleteUserRequestDto {
@Size(max = Constraint.FEEDBACK_COMMENT_MAX, message = "{user.comment.max}")
@NotNull(message = "{user.comment.notNull}")
private String comment;

public static DeleteUserRequestDto of(FeedbackType feedbackType, String comment) {
return DeleteUserRequestDto.builder()
.feedbackType(feedbackType)
.comment(comment)
.build();
}
}
297 changes: 297 additions & 0 deletions src/test/java/hous/server/service/room/RoomServiceTest.java
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();
}
}
Loading

0 comments on commit 374412e

Please sign in to comment.