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

๐Ÿงช [Test] Admin ๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ ๋ฐ ๋ฐฉ ์ƒ์„ธ ์กฐํšŒ ๋ฐ ๋ฐฉ Status๋ณ€๊ฒฝ ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ #762

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import javax.validation.constraints.NotNull;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class RoomShowChangeReqDto {
@NotNull
private String status;

public RoomShowChangeReqDto(String status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class RoomAdminService {
* ๋ฐฉ Status ๋ณ€๊ฒฝ
* @param roomId ๋ฐฉ id
* @param newStatus ๋ฐ”๊ฟ€ status
* @exception RoomNotFoundException ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฐฉ ์ž…๋ ฅ
* @exception RoomSameStatusException ๊ฐ™์€ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ
* @exception RoomNotFoundException ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฐฉ ์ž…๋ ฅ - 404
* @exception RoomSameStatusException ๊ฐ™์€ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ -409
*/
@Transactional
public void modifyRoomStatus(Long roomId, RoomType newStatus) {
Expand Down Expand Up @@ -91,7 +91,8 @@ public AdminRoomDetailResDto findAdminDetailRoom(Long roomId) {

Optional<UserRoom> hostUserRoomOptional = userRoomRepository.findByUserIdAndRoomIdAndIsExistTrue(
room.getHost().getId(), roomId);
String hostNickname = hostUserRoomOptional.get().getNickname();
String hostNickname = hostUserRoomOptional.map(UserRoom::getNickname)
.orElse(null);

List<UserRoomResDto> roomUsers = userRoomRepository.findByRoomId(roomId).stream()
.filter(UserRoom::getIsExist)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
package gg.party.api.admin.room;

import static java.lang.Boolean.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.time.LocalDateTime;

import org.apache.http.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;

import gg.auth.utils.AuthTokenProvider;
import gg.data.party.Category;
import gg.data.party.PartyPenalty;
import gg.data.party.Room;
import gg.data.party.UserRoom;
import gg.data.party.type.RoomType;
import gg.data.user.User;
import gg.data.user.type.RacketType;
import gg.data.user.type.RoleType;
import gg.data.user.type.SnsType;
import gg.party.api.admin.room.controller.request.RoomShowChangeReqDto;
import gg.party.api.admin.room.controller.response.AdminCommentResDto;
import gg.party.api.admin.room.controller.response.AdminRoomDetailResDto;
import gg.party.api.admin.room.controller.response.AdminRoomListResDto;
import gg.party.api.user.room.controller.response.UserRoomResDto;
import gg.party.api.user.room.service.RoomManagementService;
import gg.repo.party.CategoryRepository;
import gg.repo.party.RoomRepository;
import gg.repo.party.UserRoomRepository;
import gg.utils.TestDataUtils;
import gg.utils.annotation.IntegrationTest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@IntegrationTest
@AutoConfigureMockMvc
@Transactional
@RequiredArgsConstructor
@Slf4j
public class RoomAdminControllerTest {
@Autowired
MockMvc mockMvc;
@Autowired
TestDataUtils testDataUtils;
@Autowired
ObjectMapper objectMapper;
@Autowired
AuthTokenProvider tokenProvider;
@Autowired
RoomRepository roomRepository;
@Autowired
UserRoomRepository userRoomRepository;
@Autowired
CategoryRepository categoryRepository;
@Autowired
RoomManagementService roomManagementService;
User userTester;
User adminTester;
User reportedTester;
String userAccessToken;
String adminAccessToken;
String reportedAccessToken;
Category testCategory;
Room openRoom;
Room startRoom;
Room finishRoom;
Room hiddenRoom;
Room failRoom;
Room pageTestRoom;
Room openReportedRoom;
Room startReportedRoom;
Room finishReportedRoom;
Room hiddenReportedRoom;
Room failReportedRoom;
Room[] rooms = {openRoom, startRoom, finishRoom, hiddenRoom, failRoom, openReportedRoom, startReportedRoom,
finishReportedRoom, hiddenReportedRoom, failReportedRoom, pageTestRoom};
RoomType[] roomTypes = {RoomType.OPEN, RoomType.START, RoomType.FINISH, RoomType.HIDDEN, RoomType.FAIL,
RoomType.OPEN, RoomType.START, RoomType.FINISH, RoomType.HIDDEN, RoomType.FAIL, RoomType.OPEN};

@Nested
@DisplayName("Admin ๋ฐฉ Status ๋ณ€๊ฒฝ ํ…Œ์ŠคํŠธ")
class ModifyRoomVisibility {
@BeforeEach
void beforeEach() {
userTester = testDataUtils.createNewUser("findTester", "findTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER);
userAccessToken = tokenProvider.createToken(userTester.getId());
adminTester = testDataUtils.createNewUser("adminTester", "adminTester",
RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN);
adminAccessToken = tokenProvider.createToken(adminTester.getId());
testCategory = testDataUtils.createNewCategory("category");
openRoom = testDataUtils.createNewRoom(userTester, userTester, testCategory, 1, 1, 3, 2, 180,
RoomType.OPEN);
}

@Test
@DisplayName("๋ฐฉ Status ๋ณ€๊ฒฝ ์„ฑ๊ณต 204")
public void success() throws Exception {
//given
String roomId = openRoom.getId().toString();
String url = "/party/admin/rooms/" + roomId;
RoomShowChangeReqDto roomShowChangeReqDto = new RoomShowChangeReqDto(RoomType.START.toString());
String jsonRequest = objectMapper.writeValueAsString(roomShowChangeReqDto);
//when
String contentAsString = mockMvc.perform(patch(url)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isNoContent())
.andReturn().getResponse().getContentAsString();
//then
assertThat(openRoom.getStatus()).isEqualTo(RoomType.START);
}

@Test
@DisplayName("๋ฐฉ์ด ์—†์Œ์œผ๋กœ ์ธํ•œ ์—๋Ÿฌ 404")
public void noRoomFail() throws Exception {
//given
String roomId = "1000";
String url = "/party/admin/rooms/" + roomId;
RoomShowChangeReqDto roomShowChangeReqDto = new RoomShowChangeReqDto(RoomType.START.toString());
String jsonRequest = objectMapper.writeValueAsString(roomShowChangeReqDto);
//when && then
String contentAsString = mockMvc.perform(patch(url)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isNotFound()).toString();
}

@Test
@DisplayName("๋™์ผํ•œ Status ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•œ ์—๋Ÿฌ 400")
public void sameStatusFail() throws Exception {
//given
String roomId = openRoom.getId().toString();
String url = "/party/admin/rooms/" + roomId;
RoomShowChangeReqDto roomShowChangeReqDto = new RoomShowChangeReqDto(RoomType.OPEN.toString());
String jsonRequest = objectMapper.writeValueAsString(roomShowChangeReqDto);
//when && then
String contentAsString = mockMvc.perform(patch(url)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isBadRequest()).toString();
}
}

@Nested
@DisplayName("Admin ๋ฐฉ ์ „์ฒด ์กฐํšŒ ํ…Œ์ŠคํŠธ")
class FindAllActiveRoomList {
@BeforeEach
void beforeEach() {
userTester = testDataUtils.createNewUser("findTester", "findTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER);
userAccessToken = tokenProvider.createToken(userTester.getId());
reportedTester = testDataUtils.createNewUser("reportedTester", "reportedTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER);
reportedAccessToken = tokenProvider.createToken(reportedTester.getId());
PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "test", "test",
LocalDateTime.now(), 60);
adminTester = testDataUtils.createNewUser("adminTester", "adminTester",
RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN);
adminAccessToken = tokenProvider.createToken(adminTester.getId());
testCategory = testDataUtils.createNewCategory("category");
for (int i = 0; i < rooms.length; i++) {
if (i < 5) {
rooms[i] = testDataUtils.createNewRoom(userTester, userTester, testCategory, i, 1, 3, 2, 180,
roomTypes[i]);
} else {
rooms[i] = testDataUtils.createNewRoom(reportedTester, reportedTester, testCategory, i, 1, 3, 2,
180, roomTypes[i]);
}
}
}

@Test
@DisplayName("์ฒซ ํŽ˜์ด์ง€ ์กฐํšŒ ์„ฑ๊ณต 200")
public void startPageSuccess() throws Exception {
//given
String currentPage = "1";
String pageSize = "10";
String url = "/party/admin/rooms?page=" + currentPage + "&size=" + pageSize;
//when
String contentAsString = mockMvc.perform(get(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
//then
AdminRoomListResDto arlrd = objectMapper.readValue(contentAsString, AdminRoomListResDto.class);
assertThat(arlrd.getAdminRoomList().size()).isEqualTo(10);
}

@Test
@DisplayName("๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ์กฐํšŒ ์„ฑ๊ณต 200")
public void lastPageSuccess() throws Exception {
//given
String currentPage = "2";
String pageSize = "10";
String url = "/party/admin/rooms?page=" + currentPage + "&size=" + pageSize;
//when
String contentAsString = mockMvc.perform(get(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
//then
AdminRoomListResDto arlrd = objectMapper.readValue(contentAsString, AdminRoomListResDto.class);
assertThat(arlrd.getAdminRoomList().size()).isEqualTo(1);
}
}

@Nested
@DisplayName("๋ฐฉ ์ƒ์„ธ ์กฐํšŒ ํ…Œ์ŠคํŠธ")
class AdminRoomDetailInfo {
@BeforeEach
void beforeEach() {
userTester = testDataUtils.createNewImageUser("findTester", "findTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER, "userImage");
userAccessToken = tokenProvider.createToken(userTester.getId());
reportedTester = testDataUtils.createNewImageUser("reportedTester", "reportedTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER, "reportedImage");
reportedAccessToken = tokenProvider.createToken(reportedTester.getId());
PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "test", "test",
LocalDateTime.now(), 60);
adminTester = testDataUtils.createNewImageUser("adminTester", "adminTester",
RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN, "adminImage");
adminAccessToken = tokenProvider.createToken(adminTester.getId());
testCategory = testDataUtils.createNewCategory("category");
for (int i = 0; i < rooms.length - 1; i++) {
if (i < 5) {
rooms[i] = testDataUtils.createNewRoom(userTester, userTester, testCategory, i, 1, 3, 2, 180,
roomTypes[i]);
} else {
rooms[i] = testDataUtils.createNewRoom(reportedTester, reportedTester, testCategory, i, 1, 3, 2,
180, roomTypes[i]);
}
UserRoom testUserRoom = testDataUtils.createNewUserRoom(userTester, rooms[i], "testNickname", TRUE);
UserRoom reportUserRoom = testDataUtils.createNewUserRoom(reportedTester, rooms[i],
"reportNickname", TRUE);
testDataUtils.createComment(userTester, testUserRoom, rooms[i], "testComment" + i);
testDataUtils.createReportComment(reportedTester, reportUserRoom, rooms[i], "reportComment" + i);

}
}

@Test
@DisplayName("๋ชจ๋“  ์ข…๋ฅ˜์— ๋Œ€ํ•œ ์ƒ์„ธ์ •๋ณด ์กฐํšŒ ์„ฑ๊ณต 200")
public void success() throws Exception {
for (int i = 0; i < rooms.length - 1; i++) { // pageTestRoom ์ œ์™ธ
//given
String roomId = (rooms[i].getId()).toString();
String url = "/party/admin/rooms/" + roomId;
//when
String contentAsString = mockMvc.perform(get(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
//then
AdminRoomDetailResDto ardrd = objectMapper.readValue(contentAsString, AdminRoomDetailResDto.class);
for (UserRoomResDto roomUser : ardrd.getRoomUsers()) {
assertThat(roomUser.getIntraId()).isNotNull();
assertThat(roomUser.getUserImage()).isNotNull();
}
for (AdminCommentResDto comment : ardrd.getComments()) {
assertThat(comment.getIntraId()).isNotNull();
}
}
}

@Test
@DisplayName("๋ฐฉ์— ์•„๋ฌด๋„ ์—†์–ด์„œ FAIL ๋œ ๋ฐฉ ์ƒ์„ธ์ •๋ณด ์กฐํšŒ ์„ฑ๊ณต 200")
public void failRoomSuccess() throws Exception {
//given
Room failToNoUserRoom = testDataUtils.createNewRoom(userTester, userTester, testCategory, 1, 0, 3, 2,
180,
RoomType.FAIL);
UserRoom testUserRoom = testDataUtils.createNewUserRoom(userTester, failToNoUserRoom, "testNickname",
FALSE);
String roomId = failToNoUserRoom.getId().toString();
String url = "/party/admin/rooms/" + roomId;
//when
String contentAsString = mockMvc.perform(get(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
//then
AdminRoomDetailResDto ardrd = objectMapper.readValue(contentAsString, AdminRoomDetailResDto.class);
for (UserRoomResDto roomUser : ardrd.getRoomUsers()) {
assertThat(roomUser.getIntraId()).isNotNull();
assertThat(roomUser.getUserImage()).isNotNull();
}
for (AdminCommentResDto comment : ardrd.getComments()) {
assertThat(comment.getIntraId()).isNotNull();
}
}

@Test
@DisplayName("์—†๋Š” ๋ฐฉ์œผ๋กœ ์ธํ•œ ์—๋Ÿฌ 404")
public void lastPageSuccess() throws Exception {
//given
String roomId = "1000";
String url = "/party/admin/rooms/" + roomId;
//when
String contentAsString = mockMvc.perform(get(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken))
.andExpect(status().isNotFound()).toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.DuplicationException;
import gg.utils.exception.custom.BusinessException;

public class RoomSameStatusException extends DuplicationException {
public class RoomSameStatusException extends BusinessException {
public RoomSameStatusException() {
super(ErrorCode.ROOM_SAME_STATUS.getMessage(), ErrorCode.ROOM_SAME_STATUS);
}
Expand Down
Loading
Loading