Skip to content

Commit

Permalink
๐Ÿงช [Test] Admin ์นดํ…Œ๊ณ ๋ฆฌ ์ถ”๊ฐ€ ๋ฐ ์ œ๊ฑฐ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
AreSain authored Mar 25, 2024
1 parent a482455 commit 9fe55a7
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface CategoryAdminRepository extends JpaRepository<Category, Long> {
Boolean existsByName(String categoryName);

Category findByName(String categoryName);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gg.party.api.admin.category.controller;

import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -25,7 +27,7 @@ public class CategoryAdminController {
* @return ์ถ”๊ฐ€ ์„ฑ๊ณต ์—ฌ๋ถ€
*/
@PostMapping
public ResponseEntity<Void> categoryAdd(@RequestBody CategoryAddAdminReqDto reqDto) {
public ResponseEntity<Void> categoryAdd(@RequestBody @Valid CategoryAddAdminReqDto reqDto) {
categoryAdminService.addCategory(reqDto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package gg.party.api.admin.category.controller.request;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
public class CategoryAddAdminReqDto {
@NotBlank(message = "์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์€ ๋น„์–ด ์žˆ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
@Size(min = 1, max = 10, message = "์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์€ 1์ž ์ด์ƒ 10์ž ์ดํ•˜์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
private String categoryName;

public CategoryAddAdminReqDto(String category) {
this.categoryName = category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CategoryAdminService {
/**
* ์นดํ…Œ๊ณ ๋ฆฌ ์ถ”๊ฐ€
* @param reqDto ์ถ”๊ฐ€ํ•  ์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„
* @exception CategoryDuplicateException ์ค‘๋ณต๋œ ์นดํ…Œ๊ณ ๋ฆฌ
* @exception CategoryDuplicateException ์ค‘๋ณต๋œ ์นดํ…Œ๊ณ ๋ฆฌ - 409
*/
@Transactional
public void addCategory(CategoryAddAdminReqDto reqDto) {
Expand All @@ -35,20 +35,20 @@ public void addCategory(CategoryAddAdminReqDto reqDto) {

/**
* ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ
* ์‚ญ์ œ ์‹œ ๊ธฐ์กด์— room์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋˜ ์นดํ…Œ๊ณ ๋ฆฌ๋Š” default(1) ๋กœ ๋ณ€๊ฒฝ
* ์‚ญ์ œ ์‹œ ๊ธฐ์กด์— room์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋˜ ์นดํ…Œ๊ณ ๋ฆฌ๋Š” etc ๋กœ ๋ณ€๊ฒฝ
* @param categoryId ์‚ญ์ œํ•  ์นดํ…Œ๊ณ ๋ฆฌ id
* @exception CategoryNotFoundException ์œ ํšจํ•˜์ง€ ์•Š์€ ์นดํ…Œ๊ณ ๋ฆฌ
* @exception DefaultCategoryNeedException default ์นดํ…Œ๊ณ ๋ฆฌ ์กด์žฌ x ๋˜๋Š” default ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ์š”์ฒญ
* @exception CategoryNotFoundException ์œ ํšจํ•˜์ง€ ์•Š์€ ์นดํ…Œ๊ณ ๋ฆฌ - 404
* @exception DefaultCategoryNeedException default ์นดํ…Œ๊ณ ๋ฆฌ ์กด์žฌ x ๋˜๋Š” default ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ์š”์ฒญ - 400
*/
@Transactional
public void removeCategory(Long categoryId) {
Category category = categoryAdminRepository.findById(categoryId)
.orElseThrow(CategoryNotFoundException::new);

Category defaultCategory = categoryAdminRepository.findById(DefaultCategoryNeedException.DEFAULT_CATEGORY_ID)
.orElseThrow(DefaultCategoryNeedException::new);
Category defaultCategory = categoryAdminRepository.findByName(
DefaultCategoryNeedException.DEFAULT_CATEGORY_NAME);

if (category.equals(defaultCategory)) {
if (defaultCategory == null || category.equals(defaultCategory)) {
throw new DefaultCategoryNeedException();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package gg.party.api.admin.category;

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

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.Room;
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.category.controller.request.CategoryAddAdminReqDto;
import gg.repo.party.CategoryRepository;
import gg.repo.party.RoomRepository;
import gg.utils.TestDataUtils;
import gg.utils.annotation.IntegrationTest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@IntegrationTest
@AutoConfigureMockMvc
@Transactional
@RequiredArgsConstructor
@Slf4j
public class CategoryAdminControllerTest {
@Autowired
MockMvc mockMvc;
@Autowired
TestDataUtils testDataUtils;
@Autowired
ObjectMapper objectMapper;
@Autowired
AuthTokenProvider tokenProvider;
@Autowired
RoomRepository roomRepository;
@Autowired
CategoryRepository categoryRepository;
User userTester;
String userAccessToken;
Category defaultCategory;
Category testCategory;
Room testRoom;

@Nested
@DisplayName("์นดํ…Œ๊ณ ๋ฆฌ ์ถ”๊ฐ€ ํ…Œ์ŠคํŠธ")
class CategoryAdd {
@BeforeEach
void beforeEach() {
userTester = testDataUtils.createNewUser("adminTester", "adminTester",
RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN);
userAccessToken = tokenProvider.createToken(userTester.getId());
defaultCategory = testDataUtils.createNewCategory("etc");
}

@Test
@DisplayName("์นดํ…Œ๊ณ ๋ฆฌ ์ถ”๊ฐ€ ์„ฑ๊ณต 201")
public void success() throws Exception {
//given
String url = "/party/admin/categories";
CategoryAddAdminReqDto categoryAddAdminReqDto = new CategoryAddAdminReqDto("category");
String jsonRequest = objectMapper.writeValueAsString(categoryAddAdminReqDto);
//when
String contentAsString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
//then
assertThat(categoryRepository.findAll()).size().isEqualTo(2);
}

@Test
@DisplayName("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ์ธํ•œ ์—๋Ÿฌ 409")
public void fail() throws Exception {
//given
String url = "/party/admin/categories";
testDataUtils.createNewCategory("category");
CategoryAddAdminReqDto categoryAddAdminReqDto = new CategoryAddAdminReqDto("category");
String jsonRequest = objectMapper.writeValueAsString(categoryAddAdminReqDto);
//when & then
String contentAsString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken))
.andExpect(status().isConflict()).toString();
}
}

@Nested
@DisplayName("์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ํ…Œ์ŠคํŠธ")
class CategoryRemove {
@BeforeEach
void beforeEach() {
userTester = testDataUtils.createNewUser("adminTester", "adminTester",
RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN);
userAccessToken = tokenProvider.createToken(userTester.getId());
testCategory = testDataUtils.createNewCategory("test");
testRoom = testDataUtils.createNewRoom(userTester, userTester, testCategory, 1, 1,
3, 2, 180, RoomType.OPEN);
defaultCategory = testDataUtils.createNewCategory("etc");
}

@Test
@DisplayName("์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ์„ฑ๊ณต 204")
public void success() throws Exception {
//given
String categoryID = testCategory.getId().toString();
String url = "/party/admin/categories/" + categoryID;
//when
String contentAsString = mockMvc.perform(delete(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken))
.andExpect(status().isNoContent())
.andReturn().getResponse().getContentAsString();
//then
assertThat(categoryRepository.findAll()).size().isEqualTo(1);
assertThat(roomRepository.findById(testRoom.getId()).get().getCategory().getName()).isEqualTo("etc");
}

@Test
@DisplayName("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ์ธํ•œ ์—๋Ÿฌ 404")
public void noCategoryFail() throws Exception {
//given
String categoryID = "10";
String url = "/party/admin/categories/" + categoryID;
//when & then
String contentAsString = mockMvc.perform(delete(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken))
.andExpect(status().isNotFound()).toString();
}

@Test
@DisplayName("default ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ์š”์ฒญ์— ๋Œ€ํ•œ ์—๋Ÿฌ 400")
public void fail() throws Exception {
//given
String categoryID = defaultCategory.getId().toString();
String url = "/party/admin/categories/" + categoryID;
//when & then
String contentAsString = mockMvc.perform(delete(url)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken))
.andExpect(status().isBadRequest()).toString();
}
}
}
4 changes: 2 additions & 2 deletions gg-utils/src/main/java/gg/utils/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ public enum ErrorCode {
ROOM_SAME_STATUS(400, "PT204", "์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ๋ฐฉ ์ž…๋‹ˆ๋‹ค."),
ROOM_NOT_OPEN(400, "PT205", "๋ชจ์ง‘์ค‘์ธ ๋ฐฉ์ด ์•„๋‹™๋‹ˆ๋‹ค."),
ROOM_NOT_PARTICIPANT(400, "PT206", "์ฐธ์—ฌํ•˜์ง€ ์•Š์€ ๋ฐฉ ์ž…๋‹ˆ๋‹ค."),
CATEGORY_DUPLICATE(400, "PT207", "์ค‘๋ณต๋œ ์นดํ…Œ๊ณ ๋ฆฌ ์ž…๋‹ˆ๋‹ค."),
ROOM_MIN_MAX_PEOPLE(400, "PT208", "์ตœ์†Œ์ธ์›์ด ์ตœ๋Œ€์ธ์›๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค."),
ROOM_MIN_MAX_PEOPLE(400, "PT207", "์ตœ์†Œ์ธ์›์ด ์ตœ๋Œ€์ธ์›๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค."),
USER_ALREADY_IN_ROOM(409, "PT301", "์ด๋ฏธ ์ฐธ์—ฌํ•œ ๋ฐฉ ์ž…๋‹ˆ๋‹ค."),
ALREADY_REPORTED(409, "PT302", "์ด๋ฏธ ์‹ ๊ณ ํ•œ ์š”์ฒญ์ž…๋‹ˆ๋‹ค."),
SELF_REPORT(409, "PT303", "์ž์‹ ์„ ์‹ ๊ณ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
CATEGORY_DUPLICATE(409, "PT304", "์ค‘๋ณต๋œ ์นดํ…Œ๊ณ ๋ฆฌ ์ž…๋‹ˆ๋‹ค."),
ON_PENALTY(403, "PT501", "ํŒจ๋„ํ‹ฐ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.");

private final int status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gg.utils.exception.party;

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

public class DefaultCategoryNeedException extends NotExistException {
public static final Long DEFAULT_CATEGORY_ID = 1L;
public class DefaultCategoryNeedException extends BusinessException {
public static final String DEFAULT_CATEGORY_NAME = "etc";

public DefaultCategoryNeedException() {
super(ErrorCode.DEFAULT_CATEGORY_NEED.getMessage(), ErrorCode.DEFAULT_CATEGORY_NEED);
Expand Down

0 comments on commit 9fe55a7

Please sign in to comment.