Skip to content

Commit

Permalink
refactor(team-building): 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
박다예/NSC스튜디오/NE committed Jul 4, 2024
1 parent 4bbe242 commit df87130
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum ExceptionInfo {
INVALID_TEAM_BUILDING_UUID("팀빌딩을 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
INVALID_TEAM_UUID("팀을 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
INVALID_USER_UUID("유저를 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
BAD_REQUEST_FOR_USER_PICK("해당 유저를 선택할 수 없습니다.", HttpStatus.BAD_REQUEST),
BAD_REQUEST_FOR_USER_PICK("유효하지 않은 유저 선택 요청입니다.", HttpStatus.BAD_REQUEST),
ALREADY_JOINED_USER("다른 팀에 배정되어 있는 유저입니다.", HttpStatus.BAD_REQUEST),
COMPLETED_TEAM_BUILDING("이미 종료된 팀빌딩입니다.", HttpStatus.BAD_REQUEST),
INVALID_ADJUST_REQUEST("조정 라운드에서만 조정 가능합니다.", HttpStatus.BAD_REQUEST),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.nexters.moyeomoyeo.team_building.controller.dto.request.UserRequest;
import com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamBuildingResponse;
import com.nexters.moyeomoyeo.team_building.controller.dto.response.UserInfo;
import com.nexters.moyeomoyeo.team_building.service.TeamBuildingCoreService;
import com.nexters.moyeomoyeo.team_building.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -28,7 +29,7 @@
public class UserSurveyController {

private final UserService userService;
private final TeamBuildingService teamBuildingService;
private final TeamBuildingCoreService teamBuildingCoreService;

@Operation(summary = "회원 생성 요청", description = """
회원이 생성됩니다. \s
Expand All @@ -48,6 +49,6 @@ public ResponseEntity<UserInfo> createUser(@PathVariable(value = "teamBuildingUu
@GetMapping("/{teamBuildingUuid}/teams")
public ResponseEntity<TeamBuildingResponse> findTeamBuildingAndTeams(
@PathVariable(value = "teamBuildingUuid") String teamBuildingUuid) {
return ResponseEntity.ok(teamBuildingService.findTeamBuildingAndTeams(teamBuildingUuid));
return ResponseEntity.ok(teamBuildingCoreService.findTeamBuilding(teamBuildingUuid));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.nexters.moyeomoyeo.team_building.domain.constant;

import java.util.List;
import lombok.Getter;

public enum RoundStatus {
COMPLETE(6, null),
ADJUSTED_ROUND(5, COMPLETE),
FORTH_ROUND(4, ADJUSTED_ROUND),
THIRD_ROUND(3, FORTH_ROUND),
FOURTH_ROUND(4, ADJUSTED_ROUND),
THIRD_ROUND(3, FOURTH_ROUND),
SECOND_ROUND(2, THIRD_ROUND),
FIRST_ROUND(1, SECOND_ROUND),
START(0, FIRST_ROUND)
Expand All @@ -22,4 +23,8 @@ public enum RoundStatus {
this.order = order;
this.nextStatus = nextStatus;
}

public static boolean isPickUserPossible(RoundStatus roundStatus) {
return List.of(FIRST_ROUND, SECOND_ROUND, THIRD_ROUND, FOURTH_ROUND).contains(roundStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ public void nextRound() {

public void addTeamBuilding(TeamBuilding teamBuilding) {
this.teamBuilding = teamBuilding;
this.teamBuilding.getTeams().add(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public UserPickResponse pickUsers(String teamBuildingUuid, String teamUuid, User
validateRequest(teamBuildingRoundStatus, team, pickedUsers);

addUserAndMoveTeamRound(team, pickedUsers);

moveTeamBuildingRoundIfAllSelected(teamBuilding);

broadcastPickedUsers(teamBuilding.getUuid(), team.getUuid(), team.getName(), userUuids);
Expand Down Expand Up @@ -132,8 +131,8 @@ private static void addUserAndMoveTeamRound(Team targetTeam, List<User> pickedUs
}

private static void validateRequest(RoundStatus teamBuildingRoundStatus, Team targetTeam, List<User> pickedUsers) {
if (RoundStatus.COMPLETE == teamBuildingRoundStatus) {
throw ExceptionInfo.COMPLETED_TEAM_BUILDING.exception();
if (!RoundStatus.isPickUserPossible(teamBuildingRoundStatus)) {
throw ExceptionInfo.BAD_REQUEST_FOR_USER_PICK.exception();
}

if (isSelectDone(teamBuildingRoundStatus, targetTeam.getRoundStatus())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UserService {
private final NotificationService notificationService;
private final UserRepository userRepository;

public static User makeUser(String teamBuildingUuid, UserRequest request) {
private static User makeUser(String teamBuildingUuid, UserRequest request) {
return User.builder()
.name(request.getName())
.position(request.getPosition())
Expand All @@ -44,13 +44,11 @@ public UserInfo createUser(String teamBuildingUuid, UserRequest request) {
return userInfo;
}

@Transactional
public void deleteUser(String teamBuildingUuid, String userUuid) {
final User targetUser = findByUuid(userUuid);

userRepository.delete(targetUser);
notificationService.broadcast(teamBuildingUuid, "delete-user", userUuid);

}

public User findByUuid(String userUuid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nexters.moyeomoyeo.team_building.fixture;

import com.nexters.moyeomoyeo.team_building.domain.constant.Position;
import com.nexters.moyeomoyeo.team_building.domain.constant.RoundStatus;
import com.nexters.moyeomoyeo.team_building.domain.entity.Team;
import com.nexters.moyeomoyeo.team_building.domain.entity.TeamBuilding;
import java.util.List;

public class FixtureTeamBuilding {

public static TeamBuilding teamBuilding(RoundStatus roundStatus) {
return TeamBuilding.builder()
.roundStatus(roundStatus)
.name("넥스터즈 팀빌딩")
.teams(List.of(
Team.builder()
.name("team1")
.pmPosition(Position.IOS)
.roundStatus(roundStatus)
.build(),
Team.builder()
.name("team2")
.pmPosition(Position.ANDROID)
.roundStatus(roundStatus)
.build(),
Team.builder()
.name("team3")
.pmPosition(Position.FRONT_END)
.roundStatus(roundStatus)
.build(),
Team.builder()
.name("team4")
.pmPosition(Position.BACK_END)
.roundStatus(roundStatus)
.build()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.nexters.moyeomoyeo.team_building.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;

import com.nexters.moyeomoyeo.MoyeoMoyeoTest;
import com.nexters.moyeomoyeo.common.constant.ExceptionInfo;
import com.nexters.moyeomoyeo.common.exception.vo.CustomException;
import com.nexters.moyeomoyeo.team_building.controller.dto.request.TeamBuildingRequest;
import com.nexters.moyeomoyeo.team_building.controller.dto.request.TeamRequest;
import com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamBuildingResponse;
import com.nexters.moyeomoyeo.team_building.controller.dto.response.UserInfo;
import com.nexters.moyeomoyeo.team_building.domain.constant.Position;
import com.nexters.moyeomoyeo.team_building.domain.constant.RoundStatus;
import com.nexters.moyeomoyeo.team_building.domain.entity.Team;
import com.nexters.moyeomoyeo.team_building.domain.entity.TeamBuilding;
import com.nexters.moyeomoyeo.team_building.domain.entity.User;
import com.nexters.moyeomoyeo.team_building.domain.repository.TeamBuildingRepository;
import com.nexters.moyeomoyeo.team_building.domain.repository.UserRepository;
import com.nexters.moyeomoyeo.team_building.fixture.FixtureTeamBuilding;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@MoyeoMoyeoTest
class TeamBuildingAdminServiceTest {

@Autowired
private TeamBuildingAdminService sut;

@Autowired
private TeamBuildingRepository teamBuildingRepository;

@Autowired
private UserRepository userRepository;

@Test
void createTeamBuilding() {
final TeamBuildingResponse teamBuildingResponse = sut.createTeamBuilding(TeamBuildingRequest.builder()
.name("넥스터즈 팀빌딩이에요")
.teams(List.of(
TeamRequest.builder()
.name("team1")
.pmName("pm1")
.pmPosition(Position.FRONT_END)
.build(),
TeamRequest.builder()
.name("team2")
.pmName("pm2")
.pmPosition(Position.BACK_END)
.build()))
.build());;

assertThat(teamBuildingResponse.getTeamBuildingInfo().getTeamBuildingName()).isEqualTo("넥스터즈 팀빌딩이에요");
assertThat(teamBuildingResponse.getTeamInfoList().get(0).getTeamName()).isEqualTo("team1");
assertThat(teamBuildingResponse.getTeamInfoList().get(0).getPmPosition()).isEqualTo(Position.FRONT_END);
}


@Transactional
@Test
@DisplayName("조정 단계에서 팀1 -> 팀2로 옮겨졌을 때")
void adjustUserToDifferentTeam() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.ADJUSTED_ROUND));
final Team team1 = teamBuilding.getTeams().get(0);
final Team team2 = teamBuilding.getTeams().get(1);

final User user = userRepository.save(User.builder()
.name("넥터사람")
.position(Position.BACK_END)
.choices(List.of("1", "2", "3", "4"))
.team(team1)
.build());

final UserInfo adjustedUser = sut.adjustUser(teamBuilding.getUuid(), user.getUuid(), team2.getUuid());
assertThat(adjustedUser.getJoinedTeamUuid()).isEqualTo(team2.getUuid());
assertThat(adjustedUser.getSelectedRound()).isEqualTo(RoundStatus.ADJUSTED_ROUND);
}

@Transactional
@Test
@DisplayName("조정 단계에서 할당됭 팀을 해제했을 때")
void adjustUserToNotAssigned() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.ADJUSTED_ROUND));
final Team team1 = teamBuilding.getTeams().get(0);

final User user = userRepository.save(User.builder()
.name("넥터사람")
.position(Position.BACK_END)
.choices(List.of("1", "2", "3", "4"))
.team(team1)
.build());

final UserInfo adjustedUser = sut.adjustUser(teamBuilding.getUuid(), user.getUuid(), null);
assertThat(adjustedUser.getJoinedTeamUuid()).isNull();
assertThat(adjustedUser.getSelectedRound()).isNull();
}

@Test
void deleteTeamBuildingUser() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.START));

final User user = userRepository.save(User.builder()
.name("넥터사람")
.position(Position.BACK_END)
.choices(List.of("1", "2", "3", "4"))
.teamBuildingUuid(teamBuilding.getUuid())
.build());

sut.deleteTeamBuildingUser(teamBuilding.getUuid(), user.getUuid());

assertThat(userRepository.findByUuid(user.getUuid())).isEmpty();
}

@Test
@Transactional
void finishTeamBuilding() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.ADJUSTED_ROUND));

sut.finishTeamBuilding(teamBuilding.getUuid());

assertThat(teamBuilding.getRoundStatus()).isEqualTo(RoundStatus.COMPLETE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.nexters.moyeomoyeo.team_building.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.nexters.moyeomoyeo.MoyeoMoyeoTest;
import com.nexters.moyeomoyeo.team_building.controller.dto.request.UserPickRequest;
import com.nexters.moyeomoyeo.team_building.controller.dto.response.UserPickResponse;
import com.nexters.moyeomoyeo.team_building.domain.constant.Position;
import com.nexters.moyeomoyeo.team_building.domain.constant.RoundStatus;
import com.nexters.moyeomoyeo.team_building.domain.entity.Team;
import com.nexters.moyeomoyeo.team_building.domain.entity.TeamBuilding;
import com.nexters.moyeomoyeo.team_building.domain.entity.User;
import com.nexters.moyeomoyeo.team_building.domain.repository.TeamBuildingRepository;
import com.nexters.moyeomoyeo.team_building.domain.repository.UserRepository;
import com.nexters.moyeomoyeo.team_building.fixture.FixtureTeamBuilding;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@MoyeoMoyeoTest
class TeamBuildingCoreServiceTest {

@Autowired
private TeamBuildingCoreService sut;

@Autowired
private TeamBuildingRepository teamBuildingRepository;

@Autowired
private UserRepository userRepository;

@Transactional
@Test
@DisplayName("첫번째 라운드에서 한 팀이 유저 선택 완료후 다음라운드로 넘어가는지")
void pickUserOne() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.FIRST_ROUND));

final List<Team> teams = teamBuilding.getTeams();
final Team sutTeam = teams.get(0);
final User user = userRepository.save(User.builder()
.name("넥터사람")
.position(Position.BACK_END)
.choices(List.of(sutTeam.getUuid(), teams.get(1).getUuid(), teams.get(2).getUuid(), teams.get(3).getUuid()))
.teamBuildingUuid(teamBuilding.getUuid())
.build());

final UserPickResponse userPickResponse = sut.pickUsers(teamBuilding.getUuid(), sutTeam.getUuid(), UserPickRequest.builder()
.userUuids(List.of(user.getUuid()))
.build());

assertThat(userPickResponse.getUserInfoList().get(0).getUserName()).isEqualTo("넥터사람");
assertThat(userPickResponse.getUserInfoList().get(0).getPosition()).isEqualTo(Position.BACK_END);
assertThat(userPickResponse.getUserInfoList().get(0).getSelectedRound()).isEqualTo(RoundStatus.FIRST_ROUND);
assertThat(sutTeam.getRoundStatus()).isEqualTo(RoundStatus.SECOND_ROUND);
}

@Transactional
@Test
@DisplayName("첫번째 라운드에서 한 팀이 유저 선택을 아무도 하지 않은 후 다음라운드로 넘어가는지")
void pickUserNoOne() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.FIRST_ROUND));

final List<Team> teams = teamBuilding.getTeams();
final Team sutTeam = teams.get(0);

final UserPickResponse userPickResponse = sut.pickUsers(teamBuilding.getUuid(), sutTeam.getUuid(), UserPickRequest.builder()
.userUuids(List.of())
.build());

assertThat(userPickResponse.getUserInfoList()).isEmpty();
assertThat(sutTeam.getRoundStatus()).isEqualTo(RoundStatus.SECOND_ROUND);
}

@Transactional
@Test
@DisplayName("첫번째 라운드에서 모든 팀이 유저 선택 완료 후 팀빌딩이 다음라운드로 넘어가는지")
void pickUserALL() {
final TeamBuilding teamBuilding = teamBuildingRepository.save(FixtureTeamBuilding.teamBuilding(RoundStatus.FIRST_ROUND));

final List<Team> teams = teamBuilding.getTeams();

final User user1 = userRepository.save(User.builder()
.name("넥터사람1")
.position(Position.BACK_END)
.choices(List.of(teams.get(0).getUuid(), teams.get(1).getUuid(), teams.get(2).getUuid(), teams.get(3).getUuid()))
.teamBuildingUuid(teamBuilding.getUuid())
.build());

final User user2 = userRepository.save(User.builder()
.name("넥터사람2")
.position(Position.FRONT_END)
.choices(List.of(teams.get(0).getUuid(), teams.get(1).getUuid(), teams.get(2).getUuid(), teams.get(3).getUuid()))
.teamBuildingUuid(teamBuilding.getUuid())
.build());

sut.pickUsers(teamBuilding.getUuid(), teams.get(0).getUuid(), UserPickRequest.builder()
.userUuids(List.of(user1.getUuid(), user2.getUuid()))
.build());

sut.pickUsers(teamBuilding.getUuid(), teams.get(1).getUuid(), UserPickRequest.builder()
.userUuids(List.of())
.build());

sut.pickUsers(teamBuilding.getUuid(), teams.get(2).getUuid(), UserPickRequest.builder()
.userUuids(List.of())
.build());

sut.pickUsers(teamBuilding.getUuid(), teams.get(3).getUuid(), UserPickRequest.builder()
.userUuids(List.of())
.build());

assertThat(teamBuilding.getRoundStatus()).isEqualTo(RoundStatus.SECOND_ROUND);
}
}
Loading

0 comments on commit df87130

Please sign in to comment.