Skip to content

Commit

Permalink
test: CategoryControllerTest 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
kyum-q committed Oct 15, 2024
1 parent d45adc3 commit 5d7545d
Showing 1 changed file with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ class CreateCategoryTest {
@Test
@DisplayName("카테고리 생성 성공")
void createCategorySuccess() throws Exception {
// given
long categoryId = 1L;
CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest("category");

when(categoryService.create(
MemberFixture.memberFixture(), createCategoryRequest))
.thenReturn(new CreateCategoryResponse(1L, "category"));

// when & then
mvc.perform(post("/categories")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createCategoryRequest)))
Expand All @@ -60,43 +62,52 @@ void createCategorySuccess() throws Exception {
}

@Test
@DisplayName("카테고리 생성 실패: 로그인을 하지 않은 유저")
@DisplayName("카테고리 생성 실패: 로그인을 하지 않은 회원")
void createCategoryFailWithNotLogin() throws Exception {
// given
CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest("category");

doThrow(new CodeZapException(ErrorCode.UNAUTHORIZED_USER, "인증에 대한 쿠키가 없어서 회원 정보를 찾을 수 없습니다. 다시 로그인해주세요."))
.when(credentialProvider).extractMember(anyString());

// when & then
mvc.perform(post("/categories")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createCategoryRequest)))
.andExpect(status().isUnauthorized());
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.detail").value("인증에 대한 쿠키가 없어서 회원 정보를 찾을 수 없습니다. 다시 로그인해주세요."))
.andExpect(jsonPath("$.errorCode").value(1301));
}

@Test
@DisplayName("카테고리 생성 실패: 카테고리 이름 길이 초과")
void createCategoryFailWithLongName() throws Exception {
// given
CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest("a".repeat(MAX_LENGTH + 1));

// when & then
mvc.perform(post("/categories")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createCategoryRequest)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("카테고리 이름은 최대 15자까지 입력 가능합니다."));
.andExpect(jsonPath("$.detail").value("카테고리 이름은 최대 15자까지 입력 가능합니다."))
.andExpect(jsonPath("$.errorCode").value(1101));
}
}

@Test
@DisplayName("카테고리 전체 조회 성공")
void findAllCategoriesSuccess() throws Exception {
// given
Member member = MemberFixture.memberFixture();
List<Category> categories = List.of(new Category("category1", member), new Category("category1", member));
FindAllCategoriesResponse findAllCategoriesResponse = FindAllCategoriesResponse.from(categories);

when(categoryService.findAllByMemberId(any())).thenReturn(findAllCategoriesResponse);

// when & then
mvc.perform(get("/categories")
.param("memberId", "1")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -114,9 +125,11 @@ class UpdateCategoryTest {
@Test
@DisplayName("카테고리 수정 성공")
void updateCategorySuccess() throws Exception {
// given
long categoryId = 1L;
UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest("updateCategory");

// when & then
mvc.perform(put("/categories/" + categoryId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -127,12 +140,14 @@ void updateCategorySuccess() throws Exception {
@Test
@DisplayName("카테고리 수정 실패: 권한 없음")
void updateCategoryFailWithUnauthorized() throws Exception {
// given
long categoryId = 1L;
UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest("a".repeat(MAX_LENGTH));

doThrow(new CodeZapException(ErrorCode.UNAUTHORIZED_USER, "인증에 대한 쿠키가 없어서 회원 정보를 찾을 수 없습니다. 다시 로그인해주세요."))
.when(credentialProvider).extractMember(anyString());

// when & then
mvc.perform(put("/categories/" + categoryId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -143,32 +158,38 @@ void updateCategoryFailWithUnauthorized() throws Exception {
@Test
@DisplayName("카테고리 수정 실패: 카테고리 이름 길이 초과")
void updateCategoryFailWithlongName() throws Exception {
// given
long categoryId = 1L;
UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest("a".repeat(MAX_LENGTH + 1));

// when & then
mvc.perform(put("/categories/" + categoryId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updateCategoryRequest)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("카테고리 이름은 최대 255자까지 입력 가능합니다."));
.andExpect(jsonPath("$.detail").value("카테고리 이름은 최대 255자까지 입력 가능합니다."))
.andExpect(jsonPath("$.errorCode").value(1101));
}

@Test
@DisplayName("카테고리 수정 실패: 중복된 이름의 카테고리 존재")
void updateCategoryFailWithDuplicatedName() throws Exception {
// given
long categoryId = 1L;
String duplicatedName = "duplicatedName";
UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest(duplicatedName);

doThrow(new CodeZapException(ErrorCode.DUPLICATE_CATEGORY, "이름이 " + duplicatedName + "인 카테고리가 이미 존재합니다."))
.when(categoryService).update(any(), any(), any());

// when & then
mvc.perform(put("/categories/" + categoryId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updateCategoryRequest)))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.detail").value("이름이 " + duplicatedName + "인 카테고리가 이미 존재합니다."));
.andExpect(jsonPath("$.detail").value("이름이 " + duplicatedName + "인 카테고리가 이미 존재합니다."))
.andExpect(jsonPath("$.errorCode").value(1203));
}
}

Expand All @@ -179,28 +200,35 @@ class DeleteCategoryTest {
@Test
@DisplayName("카테고리 삭제 성공")
void deleteCategorySuccess() throws Exception {
// given
long categoryId = 1L;

// when
mvc.perform(delete("/categories/" + categoryId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());

// then
verify(categoryService, times(1))
.deleteById(MemberFixture.memberFixture(), categoryId);
}

@Test
@DisplayName("카테고리 삭제 실패: 권한 없음")
void deleteCategoryFailWithUnauthorized() throws Exception {
// given
long categoryId = 1L;
doThrow(new CodeZapException(ErrorCode.UNAUTHORIZED_USER, "인증에 대한 쿠키가 없어서 회원 정보를 찾을 수 없습니다. 다시 로그인해주세요."))
.when(credentialProvider).extractMember(anyString());

// when & then
mvc.perform(delete("/categories/" + categoryId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.detail").value("인증에 대한 쿠키가 없어서 회원 정보를 찾을 수 없습니다. 다시 로그인해주세요."))
.andExpect(jsonPath("$.errorCode").value(1301));
}

@Test
Expand All @@ -215,7 +243,8 @@ void deleteCategoryFailWithDuplicatedName() throws Exception {
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.detail").value("식별자 " + id + "에 해당하는 카테고리가 존재하지 않습니다."));
.andExpect(jsonPath("$.detail").value("식별자 " + id + "에 해당하는 카테고리가 존재하지 않습니다."))
.andExpect(jsonPath("$.errorCode").value(1201));
}

@Test
Expand All @@ -229,7 +258,8 @@ void deleteCategoryFailWithlongName() throws Exception {
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("템플릿이 존재하는 카테고리는 삭제할 수 없습니다."));
.andExpect(jsonPath("$.detail").value("템플릿이 존재하는 카테고리는 삭제할 수 없습니다."))
.andExpect(jsonPath("$.errorCode").value(1102));
}
}
}

0 comments on commit 5d7545d

Please sign in to comment.