Skip to content

Commit

Permalink
Merge pull request #11 from kjungw1025/test/acceptMission
Browse files Browse the repository at this point in the history
�test: 미션 인증글 승인에 대한 테스트 코드 작성
  • Loading branch information
kjungw1025 authored Mar 21, 2024
2 parents d144e16 + 739d8b6 commit 84d1e83
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.renew.sw.mentoring.domain.admin.service;

import com.renew.sw.mentoring.domain.completedmission.repository.CompletedMissionRepository;
import com.renew.sw.mentoring.domain.mission.model.entity.BonusMission;
import com.renew.sw.mentoring.domain.mission.model.entity.Mission;
import com.renew.sw.mentoring.domain.mission.repository.BonusMissionRepository;
import com.renew.sw.mentoring.domain.mission.repository.MissionRepository;
import com.renew.sw.mentoring.domain.post.exception.AlreadyMissionBoardAcceptedException;
import com.renew.sw.mentoring.domain.post.exception.MissionBoardNotInProgressException;
import com.renew.sw.mentoring.domain.post.model.entity.RegisterStatus;
import com.renew.sw.mentoring.domain.post.model.entity.type.MissionBoard;
import com.renew.sw.mentoring.domain.post.repository.MissionBoardRepository;
import com.renew.sw.mentoring.domain.team.model.entity.Team;
import com.renew.sw.mentoring.domain.team.repository.TeamRepository;
import com.renew.sw.mentoring.domain.user.model.UserRole;
import com.renew.sw.mentoring.domain.user.model.entity.User;
import com.renew.sw.mentoring.domain.user.repository.UserRepository;
import com.renew.sw.mentoring.mock.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class AdminServiceTest {

@Mock
private TeamRepository teamRepository;

@Mock
private UserRepository userRepository;

@Mock
private MissionBoardRepository missionBoardRepository;

@Mock
private MissionRepository missionRepository;

@Mock
private BonusMissionRepository bonusMissionRepository;

@Mock
private CompletedMissionRepository completedMissionRepository;

@Mock
private PasswordEncoder passwordEncoder;

@InjectMocks
private AdminService adminService;

@Test
@DisplayName("미션 인증글 승인 - 메인 미션만 성공한, 승인 대기중인 글이 잘 승인 되는지?")
void acceptMission_1() {
// given
Team team = TeamMock.create("팀1");
User user = UserMock.create(team, UserRole.MENTOR, passwordEncoder);
Mission mission = MissionMock.create();
MissionBoard missionBoard = MissionBoardMock.create(user, mission.getId(),false, RegisterStatus.IN_PROGRESS);
ReflectionTestUtils.setField(missionBoard, "id", 1L);

when(missionBoardRepository.findById(any())).thenReturn(Optional.of(missionBoard));
when(missionRepository.findById(any())).thenReturn(Optional.of(mission));

// when
adminService.acceptMission(UserRole.ADMIN, missionBoard.getId());

// then
assertEquals(team.getScore(), mission.getPoint());
assertEquals(missionBoard.getRegisterStatus(), RegisterStatus.ACCEPTED);
}

@Test
@DisplayName("미션 인증글 승인 - 메인, 보너스 미션 모두 성공한, 승인 대기중인 글이 잘 승인 되는지?")
void acceptMission_2() {
// given
Team team = TeamMock.create("팀1");
User user = UserMock.create(team, UserRole.MENTOR, passwordEncoder);
Mission mission = MissionMock.create();
BonusMission bonusMission = BonusMissionMock.create(mission);
MissionBoard missionBoard = MissionBoardMock.create(user, mission.getId(),true, RegisterStatus.IN_PROGRESS);
ReflectionTestUtils.setField(missionBoard, "id", 1L);

when(missionBoardRepository.findById(any())).thenReturn(Optional.of(missionBoard));
when(missionRepository.findById(any())).thenReturn(Optional.of(mission));
when(bonusMissionRepository.findAllByMissionId(any())).thenReturn(Collections.singletonList(bonusMission));

// when
adminService.acceptMission(UserRole.ADMIN, missionBoard.getId());

// then
assertEquals(team.getScore(), mission.getPoint() + bonusMission.getPoint());
assertEquals(missionBoard.getRegisterStatus(), RegisterStatus.ACCEPTED);
}

@Test
@DisplayName("미션 인증글 승인 - 승인 대기중인 글이 아닐 때, 오류 메시지를 잘 반환하는지?")
void acceptMission_4() {
// given
Team team = TeamMock.create("팀1");
User user = UserMock.create(team, UserRole.MENTOR, passwordEncoder);
Mission mission = MissionMock.create();
MissionBoard missionBoard = MissionBoardMock.create(user, mission.getId(),false, RegisterStatus.REJECTED);
ReflectionTestUtils.setField(missionBoard, "id", 1L);

when(missionBoardRepository.findById(any())).thenReturn(Optional.of(missionBoard));

// when & then
MissionBoardNotInProgressException exception = assertThrows(MissionBoardNotInProgressException.class, () -> adminService.acceptMission(UserRole.ADMIN, missionBoard.getId()));
assertEquals(exception.getMessageId(), "failed.mission-board.in-progress");
}

@Test
@DisplayName("미션 인증글 승인 - 이미 승인된 글일 때, 오류 메시지를 잘 반환하는지?")
void acceptMission_5() {
// given
Team team = TeamMock.create("팀1");
User user = UserMock.create(team, UserRole.MENTOR, passwordEncoder);
Mission mission = MissionMock.create();
MissionBoard missionBoard = MissionBoardMock.create(user, mission.getId(),false, RegisterStatus.ACCEPTED);
ReflectionTestUtils.setField(missionBoard, "id", 1L);

when(missionBoardRepository.findById(any())).thenReturn(Optional.of(missionBoard));

// when & then
AlreadyMissionBoardAcceptedException exception = assertThrows(AlreadyMissionBoardAcceptedException.class, () -> adminService.acceptMission(UserRole.ADMIN, missionBoard.getId()));
assertEquals(exception.getMessageId(), "already.mission-board.accepted");
}
}
16 changes: 16 additions & 0 deletions src/test/java/com/renew/sw/mentoring/mock/BonusMissionMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.renew.sw.mentoring.mock;

import com.renew.sw.mentoring.domain.mission.model.entity.BonusMission;
import com.renew.sw.mentoring.domain.mission.model.entity.Mission;

public class BonusMissionMock {

public static BonusMission create(Mission mission) {
return BonusMission.builder()
.mission(mission)
.name("bonusMission")
.description("description")
.point(20)
.build();
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/renew/sw/mentoring/mock/MissionBoardMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.renew.sw.mentoring.mock;

import com.renew.sw.mentoring.domain.post.model.entity.RegisterStatus;
import com.renew.sw.mentoring.domain.post.model.entity.type.MissionBoard;
import com.renew.sw.mentoring.domain.user.model.entity.User;

public class MissionBoardMock {
public static MissionBoard create(User user,
Long missionId,
boolean isBonusMissionSuccessful,
RegisterStatus registerStatus) {
return MissionBoard.builder()
.user(user)
.title("title")
.body("body")
.missionId(missionId)
.isBonusMissionSuccessful(isBonusMissionSuccessful)
.registerStatus(registerStatus)
.build();
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/renew/sw/mentoring/mock/MissionMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.renew.sw.mentoring.mock;

import com.renew.sw.mentoring.domain.mission.model.Difficulty;
import com.renew.sw.mentoring.domain.mission.model.MissionStatus;
import com.renew.sw.mentoring.domain.mission.model.entity.Mission;

public class MissionMock {

public static Mission create() {
return Mission.builder()
.name("mission")
.description("description")
.point(30)
.difficulty(Difficulty.EASY)
.missionStatus(MissionStatus.MAIN)
.build();
}
}
11 changes: 11 additions & 0 deletions src/test/java/com/renew/sw/mentoring/mock/TeamMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.renew.sw.mentoring.mock;

import com.renew.sw.mentoring.domain.team.model.entity.Team;

public class TeamMock {
public static Team create(String teamName) {
return Team.builder()
.teamName(teamName)
.build();
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/renew/sw/mentoring/mock/UserMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.renew.sw.mentoring.mock;

import com.renew.sw.mentoring.domain.team.model.entity.Team;
import com.renew.sw.mentoring.domain.user.model.UserRole;
import com.renew.sw.mentoring.domain.user.model.entity.User;
import org.springframework.security.crypto.password.PasswordEncoder;

public class UserMock {
public static final String STUDENT_ID = "12345678";
public static final String PASSWORD = "abcdabab";

public static final String NAME = "username";
public static final String NICKNAME = "nickname";
public static User create(Team team, UserRole userRole, PasswordEncoder passwordEncoder) {
String password = PASSWORD;

if (passwordEncoder != null) {
password = passwordEncoder.encode(password);
}

return User.builder()
.team(team)
.studentId(STUDENT_ID)
.password(password)
.name(NAME)
.nickname(NICKNAME)
.userRole(userRole)
.build();
}
}

0 comments on commit 84d1e83

Please sign in to comment.