diff --git a/backend/src/test/java/codezap/category/service/CategoryServiceTest.java b/backend/src/test/java/codezap/category/service/CategoryServiceTest.java index 57fea81e7..f1c9c7d18 100644 --- a/backend/src/test/java/codezap/category/service/CategoryServiceTest.java +++ b/backend/src/test/java/codezap/category/service/CategoryServiceTest.java @@ -17,8 +17,6 @@ import codezap.category.domain.Category; import codezap.category.dto.request.CreateCategoryRequest; -import codezap.category.dto.request.DeleteAllCategoriesRequest; -import codezap.category.dto.request.DeleteCategoryRequest; import codezap.category.dto.request.UpdateAllCategoriesRequest; import codezap.category.dto.request.UpdateCategoryRequest; import codezap.category.dto.response.CreateCategoryResponse; @@ -47,7 +45,7 @@ class CreateCategoryTest { void createCategorySuccess() { Member member = memberRepository.save(MemberFixture.getFirstMember()); String categoryName = "categoryName"; - CreateCategoryRequest request = new CreateCategoryRequest(categoryName); + CreateCategoryRequest request = new CreateCategoryRequest(categoryName, 0L); CreateCategoryResponse response = sut.create(member, request); @@ -65,9 +63,9 @@ void createCategorySuccessWithOtherMemberAndSameName() { Member member = memberRepository.save(MemberFixture.getFirstMember()); Member otherMember = memberRepository.save(MemberFixture.createFixture("otherMember")); String duplicatedCategoryName = "category"; - categoryRepository.save(new Category(duplicatedCategoryName, member, 1L)); + categoryRepository.save(new Category(duplicatedCategoryName, member, 0L)); - CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest(duplicatedCategoryName); + CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest(duplicatedCategoryName, 0L); CreateCategoryResponse createCategoryResponse = sut.create(otherMember, createCategoryRequest); Category savedCategory = categoryRepository.fetchById(createCategoryResponse.id()); @@ -83,17 +81,17 @@ void createCategorySuccessWithOtherMemberAndSameName() { void createCategorySuccessWithLastOrdinalByMember() { Member member1 = memberRepository.save(MemberFixture.getFirstMember()); Member member2 = memberRepository.save(MemberFixture.getSecondMember()); - categoryRepository.save(new Category("category1", member1, 1L)); - categoryRepository.save(new Category("category2", member1, 2L)); - categoryRepository.save(new Category("category3", member2, 1L)); + categoryRepository.save(new Category("category1", member1, 0L)); + categoryRepository.save(new Category("category2", member1, 1L)); + categoryRepository.save(new Category("category3", member2, 0L)); String categoryName = "category4"; - CreateCategoryRequest request = new CreateCategoryRequest(categoryName); + CreateCategoryRequest request = new CreateCategoryRequest(categoryName, 2L); CreateCategoryResponse response = sut.create(member1, request); Category savedCategory = categoryRepository.fetchById(response.id()); - assertThat(savedCategory.getOrdinal()).isEqualTo(3L); + assertThat(savedCategory.getOrdinal()).isEqualTo(2L); } @Test @@ -103,7 +101,7 @@ void createCategoryFailWithSameMemberAndDuplicateName() { String duplicatedCategoryName = "category"; categoryRepository.save(new Category(duplicatedCategoryName, member, 1L)); - CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest(duplicatedCategoryName); + CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest(duplicatedCategoryName, 2L); assertThatThrownBy(() -> sut.create(member, createCategoryRequest)) .isInstanceOf(CodeZapException.class) @@ -195,201 +193,210 @@ void failWithNotSavedId() { } @Nested - @DisplayName("카테고리 수정 테스트") + @DisplayName("카테고리 편집 테스트") class UpdateCategoryTest { @Test - @DisplayName("카테고리 수정 성공") + @DisplayName("카테고리 편집 성공") @Disabled - void updateCategorySuccess() { + void updateCategoriesSuccess() { + String createCategoryName = "createName"; String updateCategoryName = "updateName1"; - String notUpdateCategoryName = "category2"; Member member = memberRepository.save(MemberFixture.getFirstMember()); Category category1 = categoryRepository.save(new Category("category1", member, 1L)); - Category category2 = categoryRepository.save(new Category(notUpdateCategoryName, member, 2L)); - UpdateCategoryRequest request1 = new UpdateCategoryRequest(category1.getId(), updateCategoryName, category1.getOrdinal()); - UpdateCategoryRequest request2 = new UpdateCategoryRequest(category2.getId(), notUpdateCategoryName, category2.getOrdinal()); + Category category2 = categoryRepository.save(new Category("category2", member, 2L)); + + CreateCategoryRequest createRequest = new CreateCategoryRequest(createCategoryName, 1L); + UpdateCategoryRequest updateRequest = new UpdateCategoryRequest(category1.getId(), updateCategoryName, 2L); - sut.updateCategories(member, new UpdateAllCategoriesRequest(List.of(request1, request2))); + sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(createRequest), + List.of(updateRequest), + List.of(category2.getId()))); assertAll( () -> assertThat(categoryRepository.fetchById(category1.getId()).getName()).isEqualTo( updateCategoryName), () -> assertThat(categoryRepository.fetchById(category1.getId()).getOrdinal()).isEqualTo(2L), - () -> assertThat(categoryRepository.fetchById(category2.getId()).getName()).isEqualTo( - notUpdateCategoryName), - () -> assertThat(categoryRepository.fetchById(category2.getId()).getOrdinal()).isEqualTo(1L) + () -> assertThat(categoryRepository.fetchById(3L).getName()).isEqualTo( + createCategoryName), + () -> assertThat(categoryRepository.fetchById(3L).getOrdinal()).isEqualTo(1L) ); } @Test - @DisplayName("카테고리 수정 실패: 권한 없음") - void updateCategoryFailWithUnauthorized() { + @DisplayName("카테고리 편집 실패: 수정 권한 없음") + void updateCategoriesFailWithUnauthorizedUpdate() { Member member = memberRepository.save(MemberFixture.getFirstMember()); Category category = categoryRepository.save(new Category("category1", member, 1L)); Member otherMember = memberRepository.save(MemberFixture.createFixture("otherMember")); - UpdateCategoryRequest request = new UpdateCategoryRequest(category.getId(), "updateName", category.getOrdinal()); + UpdateCategoryRequest request = new UpdateCategoryRequest(category.getId(), "updateName", + category.getOrdinal()); assertThatThrownBy( - () -> sut.updateCategories(otherMember, new UpdateAllCategoriesRequest(List.of(request)))) + () -> sut.updateCategories(otherMember, new UpdateAllCategoriesRequest( + List.of(), + List.of(request), + List.of()))) .isInstanceOf(CodeZapException.class) .hasMessage("해당 카테고리를 수정 또는 삭제할 권한이 없는 유저입니다."); } @Test - @DisplayName("카테고리 수정 실패: 이미 존재하는 카테고리 이름") - void duplicatedCategoryName() { + @DisplayName("카테고리 편집 실패: 기본 카테고리 수정") + void updateCategoriesFailWithDefaultCategory() { Member member = memberRepository.save(MemberFixture.getFirstMember()); - Category category1 = categoryRepository.save(new Category("category1", member, 1L)); - Category category2 = categoryRepository.save(new Category("category2", member, 2L)); - UpdateCategoryRequest request = new UpdateCategoryRequest(category2.getId(), category1.getName(), category2.getOrdinal()); + Category category = categoryRepository.save(Category.createDefaultCategory(member)); + Member otherMember = memberRepository.save(MemberFixture.createFixture("otherMember")); + + UpdateCategoryRequest request = new UpdateCategoryRequest(category.getId(), "updateName", + category.getOrdinal()); - assertThatThrownBy(() -> sut.updateCategories(member, new UpdateAllCategoriesRequest(List.of(request)))) + assertThatThrownBy( + () -> sut.updateCategories(otherMember, new UpdateAllCategoriesRequest( + List.of(), + List.of(request), + List.of()))) .isInstanceOf(CodeZapException.class) - .hasMessage("이름이 " + category1.getName() + "인 카테고리가 이미 존재합니다."); + .hasMessage("기본 카테고리는 수정 및 삭제할 수 없습니다."); } + @Test - @DisplayName("카테고리 수정 실패: 현재와 동일한 카테고리 이름") - void notChangedCategoryName() { + @DisplayName("카테고리 편집 실패: 중복된 카테고리 이름") + void duplicatedCategoryName() { + String duplicatedName = "duplicatedName"; Member member = memberRepository.save(MemberFixture.getFirstMember()); - Category category = categoryRepository.save(new Category("category", member, 1L)); - UpdateCategoryRequest request = new UpdateCategoryRequest(category.getId(), category.getName(), category.getOrdinal()); + Category category = categoryRepository.save(new Category(duplicatedName, member, 1L)); + + CreateCategoryRequest createRequest = new CreateCategoryRequest(duplicatedName, 2L); + UpdateCategoryRequest updateRequest = new UpdateCategoryRequest(category.getId(), category.getName(), + category.getOrdinal()); - assertThatThrownBy(() -> sut.updateCategories(member, new UpdateAllCategoriesRequest(List.of(request)))) + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(createRequest), + List.of(updateRequest), + List.of()))) .isInstanceOf(CodeZapException.class) - .hasMessage("이름이 " + category.getName() + "인 카테고리가 이미 존재합니다."); + .hasMessage("요청에 중복된 카테고리 이름이 존재합니다."); } @Test - @DisplayName("카테고리 수정 실패: 존재하지 않는 카테고리 id") + @DisplayName("카테고리 편집 실패: 존재하지 않는 카테고리 수정") void notSavedCategoryId() { Member member = memberRepository.save(MemberFixture.getFirstMember()); long notSavedId = 100L; UpdateCategoryRequest request = new UpdateCategoryRequest(notSavedId, "categoryName", 1L); - assertThatThrownBy(() -> sut.updateCategories(member, new UpdateAllCategoriesRequest(List.of(request)))) + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(), + List.of(request), + List.of()))) .isInstanceOf(CodeZapException.class) .hasMessage("식별자 " + notSavedId + "에 해당하는 카테고리가 존재하지 않습니다."); } @Test - @DisplayName("카테고리 수정 실패: 중복된 순서") + @DisplayName("카테고리 편집 실패: 중복된 순서") void duplicatedCategoryOrdinal() { Member member = memberRepository.save(MemberFixture.getFirstMember()); Category category1 = categoryRepository.save(new Category("category1", member, 1L)); Category category2 = categoryRepository.save(new Category("category2", member, 2L)); - Category category3 = categoryRepository.save(new Category("category3", member, 3L)); - UpdateCategoryRequest request1 = new UpdateCategoryRequest(category2.getId(), "newCategory2", 2L); - UpdateCategoryRequest request2 = new UpdateCategoryRequest(category3.getId(), "newCategory3", 1L); - - assertThatThrownBy(() -> sut.updateCategories(member, new UpdateAllCategoriesRequest(List.of(request1, request2)))) - .isInstanceOf(CodeZapException.class) - .hasMessage("템플릿 순서가 중복됩니다."); - } - } - - @Nested - @DisplayName("카테고리 삭제 테스트") - class DeleteById { - @Test - @DisplayName("카테고리 삭제 성공") - void deleteCategorySuccess() { - Member member = memberRepository.save(MemberFixture.getFirstMember()); - Category savedCategory = categoryRepository.save(new Category("category1", member, 1L)); - int beforeDeleteSize = categoryRepository.findAllByMemberIdOrderById(member.getId()).size(); - var categoryRequest = new DeleteCategoryRequest(savedCategory.getId(), savedCategory.getOrdinal()); - var request = new DeleteAllCategoriesRequest(List.of(categoryRequest)); - - sut.deleteCategories(member, request); + CreateCategoryRequest createRequest = new CreateCategoryRequest("category3", category2.getOrdinal()); + UpdateCategoryRequest request1 = new UpdateCategoryRequest(category1.getId(), category1.getName(), 2L); + UpdateCategoryRequest request2 = new UpdateCategoryRequest(category2.getId(), category2.getName(), 1L); - assertAll( - () -> assertThat(categoryRepository.findAllByMemberIdOrderById(member.getId())) - .hasSize(beforeDeleteSize - 1), - () -> assertThatThrownBy(() -> categoryRepository.fetchById(savedCategory.getId())) - .isInstanceOf(CodeZapException.class) - .hasMessage("식별자 1에 해당하는 카테고리가 존재하지 않습니다.") - ); + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(createRequest), + List.of(request1, request2), + List.of()))) + .isInstanceOf(CodeZapException.class) + .hasMessage("카테고리 순서가 연속적이지 않습니다."); } @Test - @DisplayName("카테고리 삭제 성공: 뒷 순서 카테고리 순서 재정렬") - void deleteCategorySuccessWithReorderOrdinal() { + @DisplayName("카테고리 편집 실패: 연속되지 않는 순서") + void nonSequentialCategoryOrdinal() { Member member = memberRepository.save(MemberFixture.getFirstMember()); Category category1 = categoryRepository.save(new Category("category1", member, 1L)); Category category2 = categoryRepository.save(new Category("category2", member, 2L)); - Category category3 = categoryRepository.save(new Category("category3", member, 3L)); - Category category4 = categoryRepository.save(new Category("category4", member, 4L)); - Category category5 = categoryRepository.save(new Category("category5", member, 5L)); - var categoryRequest1 = new DeleteCategoryRequest(category2.getId(), category2.getOrdinal()); - var categoryRequest2 = new DeleteCategoryRequest(category4.getId(), category4.getOrdinal()); - var request = new DeleteAllCategoriesRequest(List.of(categoryRequest1, categoryRequest2)); - - sut.deleteCategories(member, request); - entityManager.refresh(category1); - entityManager.refresh(category3); - entityManager.refresh(category5); + CreateCategoryRequest createRequest = new CreateCategoryRequest("category3", 4L); + UpdateCategoryRequest request1 = new UpdateCategoryRequest(category1.getId(), category1.getName(), 2L); + UpdateCategoryRequest request2 = new UpdateCategoryRequest(category2.getId(), category2.getName(), 1L); - assertAll( - () -> assertThat(categoryRepository.fetchById(category1.getId()).getOrdinal()).isEqualTo(1L), - () -> assertThat(categoryRepository.fetchById(category3.getId()).getOrdinal()).isEqualTo(2L), - () -> assertThat(categoryRepository.fetchById(category5.getId()).getOrdinal()).isEqualTo(3L) - ); + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(createRequest), + List.of(request1, request2), + List.of()))) + .isInstanceOf(CodeZapException.class) + .hasMessage("카테고리 순서가 연속적이지 않습니다."); } @Test - @DisplayName("카테고리 삭제 실패: 권한 없음") - void deleteCategoryFailWithUnauthorized() { + @DisplayName("카테고리 편집 실패: 삭제 권한 없음") + void updateCategoriesFailWithUnauthorizedDelete() { Member member = memberRepository.save(MemberFixture.getFirstMember()); + Category category = categoryRepository.save(new Category("category1", member, 1L)); Member otherMember = memberRepository.save(MemberFixture.createFixture("otherMember")); - Category savedCategory = categoryRepository.save(new Category("category1", member, 1L)); - var categoryRequest = new DeleteCategoryRequest(savedCategory.getId(), savedCategory.getOrdinal()); - var request = new DeleteAllCategoriesRequest(List.of(categoryRequest)); - assertThatCode(() -> sut.deleteCategories(otherMember, request)) + assertThatThrownBy( + () -> sut.updateCategories(otherMember, new UpdateAllCategoriesRequest( + List.of(), + List.of(), + List.of(1L)))) .isInstanceOf(CodeZapException.class) .hasMessage("해당 카테고리를 수정 또는 삭제할 권한이 없는 유저입니다."); } @Test - @DisplayName("카테고리 삭제 실패: 존재하지 않는 카테고리는 삭제할 수 없음") + @DisplayName("카테고리 삭제 실패: 존재하지 않는 카테고리 삭제") void deleteCategoryFailWithNotExistCategory() { Member member = memberRepository.save(MemberFixture.getFirstMember()); - long notSavedCategoryId = 100L; - var categoryRequest = new DeleteCategoryRequest(notSavedCategoryId, 1L); - var request = new DeleteAllCategoriesRequest(List.of(categoryRequest)); + long notSavedId = 100L; - assertThatCode(() -> sut.deleteCategories(member, request)) + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(), + List.of(), + List.of(notSavedId)))) .isInstanceOf(CodeZapException.class) - .hasMessage("식별자 " + notSavedCategoryId + "에 해당하는 카테고리가 존재하지 않습니다."); + .hasMessage("식별자 " + notSavedId + "에 해당하는 카테고리가 존재하지 않습니다."); } @Test - @DisplayName("카테고리 아이디로 카테고리 삭제 실패 : 해당 카테고리에 속한 템플릿이 존재하면 삭제할 수 없음") + @DisplayName("카테고리 삭제 실패: 템플릿이 존재하는 카테고리 삭제") void deleteByIdFailExistsTemplate() { Member member = memberRepository.save(MemberFixture.getFirstMember()); Category category = categoryRepository.save(new Category("카테고리 1", member, 1L)); templateRepository.save(new Template(member, "title", "desciption", category)); - var categoryRequest = new DeleteCategoryRequest(category.getId(), category.getOrdinal()); - var request = new DeleteAllCategoriesRequest(List.of(categoryRequest)); - assertThatThrownBy(() -> sut.deleteCategories(member, request)) + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(), + List.of(), + List.of(category.getId())))) .isInstanceOf(CodeZapException.class) .hasMessage("템플릿이 존재하는 카테고리는 삭제할 수 없습니다."); } @Test - @DisplayName("카테고리 아이디로 카테고리 삭제 실패 : 기본 카테고리는 삭제할 수 없음") + @DisplayName("카테고리 삭제 실패: 기본 카테고리 삭제") void deleteByIdFailDefaultCategory() { Member member = memberRepository.save(MemberFixture.getFirstMember()); Category defaultCategory = categoryRepository.save(Category.createDefaultCategory(member)); - var categoryRequest = new DeleteCategoryRequest(defaultCategory.getId(), defaultCategory.getOrdinal()); - var request = new DeleteAllCategoriesRequest(List.of(categoryRequest)); - assertThatThrownBy(() -> sut.deleteCategories(member, request)) + assertThatThrownBy( + () -> sut.updateCategories(member, new UpdateAllCategoriesRequest( + List.of(), + List.of(), + List.of(defaultCategory.getId())))) .isInstanceOf(CodeZapException.class) .hasMessage("기본 카테고리는 수정 및 삭제할 수 없습니다."); }