diff --git a/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java b/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java index 25976ca8..8ce11aa9 100644 --- a/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java +++ b/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java @@ -45,7 +45,7 @@ public static class CareerDto{ public static class CareerGroupedByCategoryDto extends CareerGroupedByResponse { @Schema(description = "카테고리명",example = "동아리",type = "String") private String categoryName; - @Schema(description = "카테고리 내 활동 개수 (카테고리가 동아리인 활동의 개수)",example = "2",type="int") + @Schema(description = "카테고리 내 활동 개수 ( 예 : 카테고리가 동아리인 활동의 개수 )",example = "2",type="int") private int count; @Schema(description = "해당 카테고리 내 활동 목록") private List careers; @@ -59,7 +59,7 @@ public static class CareerGroupedByCategoryDto extends CareerGroupedByResponse { public static class CareerGroupedByYearDto extends CareerGroupedByResponse{ @Schema(description = "연도", example = "2024", type = "int") private int year; - @Schema(description = "연도 내 활동 개수 (2024년에 진행된 활동읠 개수)", example = "2", type = "int") + @Schema(description = "연도 내 활동 개수 ( 예 : 2024년에 진행된 활동의 개수 )", example = "2", type = "int") private int count; @Schema(description = "해당 연도 내 활동 목록") private List careers; diff --git a/src/main/java/umc/kkijuk/server/career/service/CareerService.java b/src/main/java/umc/kkijuk/server/career/service/CareerService.java index 11a87933..fc269b55 100644 --- a/src/main/java/umc/kkijuk/server/career/service/CareerService.java +++ b/src/main/java/umc/kkijuk/server/career/service/CareerService.java @@ -3,7 +3,6 @@ import umc.kkijuk.server.career.controller.response.CareerGroupedByResponse; import umc.kkijuk.server.career.domain.Career; import umc.kkijuk.server.career.dto.CareerRequestDto; -import umc.kkijuk.server.career.dto.CareerResponseDto; import java.util.List; import java.util.Optional; diff --git a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java index 0fadfc97..28f4c5e3 100644 --- a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java @@ -68,6 +68,7 @@ public Career updateCareer(Long careerId, CareerRequestDto.UpdateCareerDto reque career.setCategory(categoryRepository.findById(Long.valueOf(request.getCategory())).get()); } return careerRepository.save(career); + } @Override diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java index 9dfa0b5b..4bb070e9 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java @@ -12,15 +12,18 @@ import org.springframework.test.annotation.DirtiesContext; import umc.kkijuk.server.career.controller.exception.CareerExceptionControllerAdvice; import umc.kkijuk.server.career.controller.exception.CareerValidationException; +import umc.kkijuk.server.career.controller.response.CareerGroupedByResponse; import umc.kkijuk.server.career.controller.response.CareerResponse; import umc.kkijuk.server.career.controller.response.CareerResponseMessage; import umc.kkijuk.server.career.domain.Career; import umc.kkijuk.server.career.domain.Category; import umc.kkijuk.server.career.dto.CareerRequestDto; +import umc.kkijuk.server.career.dto.CareerResponseDto; import umc.kkijuk.server.career.repository.CareerRepository; import umc.kkijuk.server.career.repository.CategoryRepository; import java.time.LocalDate; +import java.util.List; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; @@ -28,7 +31,7 @@ import static org.mockito.Mockito.*; @SpringBootTest -@DirtiesContext +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) public class CareerServiceTest { @Autowired private CareerService careerService; @@ -40,30 +43,25 @@ public class CareerServiceTest { private CareerExceptionControllerAdvice careerExceptionControllerAdvice; private Career career1; private Career career2; + private Category category1; + private Category category2; @BeforeEach void init() { - Category category1 = Category.builder() + category1 = Category.builder() .name("동아리") .build(); - Category category2 = Category.builder() + category2 = Category.builder() .name("대외활동") .build(); - Category category3 = Category.builder() - .name("공모전/대회") - .build(); - Category category4 = Category.builder() - .name("프로젝트") - .build(); categoryRepository.save(category1); categoryRepository.save(category2); - categoryRepository.save(category3); - categoryRepository.save(category4); - career1 = Career.builder().id(1L).name("tet1") + career1 = Career.builder() + .name("test1") .alias("alias1") .summary("summary1") .current(false) @@ -73,7 +71,8 @@ void init() { .category(category1) .build(); - career2 = Career.builder().id(2L).name("test2") + career2 = Career.builder() + .name("test2") .alias("alias2") .summary("summary2") .current(false) @@ -86,25 +85,6 @@ void init() { careerRepository.save(career1); careerRepository.save(career2); } - @Test - void update_기존_career_수정_null_입력시_기존값_유지() { - //given - CareerRequestDto.UpdateCareerDto updateCareerDto = CareerRequestDto.UpdateCareerDto.builder().build(); - //when - Career updateCareer = careerService.updateCareer(career2.getId(), updateCareerDto); - //then - assertAll( - () -> assertThat(updateCareer.getId()).isEqualTo(career2.getId()), - () -> assertThat(updateCareer.getName()).isEqualTo(career2.getName()), - () -> assertThat(updateCareer.getAlias()).isEqualTo(career2.getAlias()), - () -> assertThat(updateCareer.getSummary()).isEqualTo(career2.getSummary()), - () -> assertThat(updateCareer.getCurrent()).isEqualTo(career2.getCurrent()), - () -> assertThat(updateCareer.getStartdate()).isEqualTo(career2.getStartdate()), - () -> assertThat(updateCareer.getEnddate()).isEqualTo(career2.getEnddate()), - () -> assertThat(updateCareer.getYear()).isEqualTo(career2.getYear()), - () -> assertThat(updateCareer.getCategory().getId()).isEqualTo(career2.getCategory().getId()) - ); - } @Test void create_새로운_career_만들기() { @@ -116,7 +96,7 @@ void init() { .summary("summary3") .startDate(LocalDate.of(2024, 4, 10)) .endDate(LocalDate.of(2024, 7, 20)) - .category(1) + .category(Math.toIntExact(category1.getId())) .build(); //when Career newCareer = careerService.createCareer(careerCreateDto); @@ -124,14 +104,31 @@ void init() { assertAll( () -> assertThat(newCareer.getId()).isEqualTo(3L), () -> assertThat(newCareer.getName()).isEqualTo("test3"), - () -> assertThat(newCareer.getAlias()).isEqualTo("alias3"), - () -> assertThat(newCareer.getSummary()).isEqualTo("summary3"), - () -> assertThat(newCareer.getCurrent()).isEqualTo(false), - () -> assertThat(newCareer.getStartdate()).isEqualTo(LocalDate.of(2024, 4, 10)), - () -> assertThat(newCareer.getEnddate()).isEqualTo(LocalDate.of(2024, 7, 20)), - () -> assertThat(newCareer.getYear()).isEqualTo(2024), - () -> assertThat(newCareer.getCategory().getId()).isEqualTo(1L), - () -> assertThat(newCareer.getCategory().getName()).isEqualTo("동아리") + () ->assertThat(newCareer.getAlias()).isEqualTo("alias3"), + () ->assertThat(newCareer.getSummary()).isEqualTo("summary3"), + () ->assertThat(newCareer.getStartdate()).isEqualTo(LocalDate.of(2024,4,10)), + () ->assertThat(newCareer.getEnddate()).isEqualTo(LocalDate.of(2024,7,20)), + () ->assertThat(newCareer.getYear()).isEqualTo(2024), + () ->assertThat(newCareer.getCategory().getId()).isEqualTo(1L) + ); + } + @Test + void update_기존_career_수정_null_입력시_기존값_유지() { + //given + CareerRequestDto.UpdateCareerDto updateCareerDto = CareerRequestDto.UpdateCareerDto.builder().build(); + //when + Career updateCareer = careerService.updateCareer(2L, updateCareerDto); + //then + assertAll( + () -> assertThat(updateCareer.getId()).isEqualTo(2L), + () -> assertThat(updateCareer.getName()).isEqualTo("test2"), + () -> assertThat(updateCareer.getAlias()).isEqualTo("alias2"), + () -> assertThat(updateCareer.getSummary()).isEqualTo("summary2"), + () -> assertThat(updateCareer.getCurrent()).isEqualTo(false), + () -> assertThat(updateCareer.getStartdate()).isEqualTo(LocalDate.of(2024, 4, 10)), + () -> assertThat(updateCareer.getEnddate()).isEqualTo(LocalDate.of(2024, 7, 20)), + () -> assertThat(updateCareer.getYear()).isEqualTo(2024), + () -> assertThat(updateCareer.getCategory().getId()).isEqualTo(2L) ); } @Test @@ -142,11 +139,12 @@ void init() { .summary("update summary") .alias("update alias") .isCurrent(true) - .startDate(LocalDate.of(2021,01,01)) .category(2) + .startDate(LocalDate.of(2021,01,01)) .build(); + //when - Career updateCareer = careerService.updateCareer(career2.getId(), updateCareerDto); + Career updateCareer = careerService.updateCareer(2L, updateCareerDto); //then assertAll( () -> assertThat(updateCareer.getId()).isEqualTo(2L), @@ -156,9 +154,8 @@ void init() { () -> assertThat(updateCareer.getCurrent()).isEqualTo(true), () -> assertThat(updateCareer.getStartdate()).isEqualTo(LocalDate.of(2021,01,01)), () -> assertThat(updateCareer.getEnddate()).isEqualTo(LocalDate.now()), - () -> assertThat(updateCareer.getYear()).isEqualTo(LocalDate.now().getYear()), () -> assertThat(updateCareer.getCategory().getId()).isEqualTo(2L), - () -> assertThat(updateCareer.getCategory().getName()).isEqualTo("대외활동") + () -> assertThat(updateCareer.getYear()).isEqualTo(LocalDate.now().getYear()) ); } @@ -186,7 +183,7 @@ void init() { .alias("update alias") .isCurrent(true) .startDate(LocalDate.of(2024543,01,01)) - .category(2) + .category(Math.toIntExact(category2.getId())) .build(); //when //then @@ -204,22 +201,22 @@ void init() { .isCurrent(false) .startDate(LocalDate.of(2024,01,01)) .endDate(LocalDate.of(2023,01,01)) - .category(2) + .category(Math.toIntExact(category2.getId())) .build(); //when //then assertThrows(CareerValidationException.class, () -> careerService.updateCareer(career2.getId(), updateCareerDto)); - } @Test void delete_기존_career_삭제(){ //given - careerService.deleteCareer(career1.getId()); + Long targetId = career1.getId(); //when + careerService.deleteCareer(targetId); //then - Optional deletedCareer = careerRepository.findById(career1.getId()); + Optional deletedCareer = careerRepository.findById(targetId); assertThat(deletedCareer).isEmpty(); } @@ -230,6 +227,52 @@ void init() { //then assertThrows(CareerValidationException.class, () -> careerService.deleteCareer(999L)); } + + + @Test + void read_조회시_없는_queryString_요청은_에러() { + //given + String status = "test"; + //when + //then + assertThrows(IllegalArgumentException.class, () -> careerService.getCareerGroupedBy(status)); + } + + @Test + void read_queryString값이_category일때_조회결과_검증() { + //given + String status = "category"; + //when + List groupedCareerList = careerService.getCareerGroupedBy(status); + //then + CareerResponseDto.CareerGroupedByCategoryDto groupedResult1 = (CareerResponseDto.CareerGroupedByCategoryDto) groupedCareerList.get(0); + CareerResponseDto.CareerGroupedByCategoryDto groupedResult2 = (CareerResponseDto.CareerGroupedByCategoryDto) groupedCareerList.get(1); + + assertThat(groupedCareerList).isNotEmpty(); + assertThat(groupedCareerList.size()).isEqualTo(2); + + assertThat(groupedResult1.getCategoryName()).isEqualTo("대외활동"); + assertThat(groupedResult2.getCategoryName()).isEqualTo("동아리"); + + assertThat(groupedResult1.getCount()).isEqualTo(1); + assertThat(groupedResult2.getCount()).isEqualTo(1); + + } + @Test + void read_queryString_값이_year_일때_조회결과_검증() { + //given + String status = "year"; + //when + List groupedCareerList = careerService.getCareerGroupedBy(status); + //then + assertThat(groupedCareerList).isNotEmpty(); + assertThat(groupedCareerList.size()).isEqualTo(1); + + CareerResponseDto.CareerGroupedByYearDto groupedResult = (CareerResponseDto.CareerGroupedByYearDto) groupedCareerList.get(0); + assertThat(groupedResult.getYear()).isEqualTo(2024); + assertThat(groupedResult.getCount()).isEqualTo(2); + } + @Test void CareerExceptionControllerAdvice가_올바른_예외_응답을_반환하는지_검증() { //given diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java index a0f77072..1b2f479d 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java @@ -10,9 +10,14 @@ import umc.kkijuk.server.career.domain.Career; import umc.kkijuk.server.career.domain.Category; import umc.kkijuk.server.career.dto.CareerRequestDto; +import umc.kkijuk.server.career.dto.CareerResponseDto; import umc.kkijuk.server.career.repository.CareerRepository; import java.time.LocalDate; +import java.util.Arrays; +import java.util.List; import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.*; @@ -23,7 +28,8 @@ public class CareerServiceUnitTest { private CareerRepository careerRepository; @InjectMocks private CareerServiceImpl careerService; - private Career career; + private Career career1; + private Career career2; private Category category1; private Category category2; @BeforeEach @@ -38,7 +44,7 @@ void init() { .name("대외활동") .build(); - career = Career.builder() + career1 = Career.builder() .id(1L) .name("test") .alias("alias") @@ -47,17 +53,41 @@ void init() { .category(category1) .startdate(LocalDate.of(2024, 4, 10)) .enddate(LocalDate.of(2024, 7, 20)) + .year(2024) .build(); - } + career2 = Career.builder() + .id(2L) + .name("test2") + .alias("alias2") + .summary("summary2") + .current(false) + .category(category2) + .startdate(LocalDate.of(2024, 4, 10)) + .enddate(LocalDate.of(2024, 7, 20)) + .year(2024) + .build(); + } + @Test + void read_getCareerGroupedBy_Category_성공() { + //given + List careerList = Arrays.asList(career1, career2); + when(careerRepository.findAll()).thenReturn(careerList); + //when + List result = (List) careerService.getCareerGroupedBy("category"); + //then + assertThat(result).isNotEmpty(); + assertThat(result.size()).isEqualTo(2); + verify(careerRepository, times(1)).findAll(); + } @Test void delete_기존_career_삭제() { //given - when(careerRepository.findById(anyLong())).thenReturn(Optional.of(career)); + when(careerRepository.findById(anyLong())).thenReturn(Optional.of(career1)); //when careerService.deleteCareer(1L); //then - verify(careerRepository,times(1)).delete(career); + verify(careerRepository,times(1)).delete(career1); } @Test