Skip to content

Commit

Permalink
Test : Career 카테고리, 연도별 활동 조회 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonda02 committed Jul 24, 2024
1 parent 9388d70 commit 4d9a2b8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CareerDto> careers;
Expand All @@ -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<CareerDto> careers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
143 changes: 93 additions & 50 deletions src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@
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;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@SpringBootTest
@DirtiesContext
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class CareerServiceTest {
@Autowired
private CareerService careerService;
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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_만들기() {
Expand All @@ -116,22 +96,39 @@ 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);
//then
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
Expand All @@ -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),
Expand All @@ -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())
);

}
Expand Down Expand Up @@ -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
Expand All @@ -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<Career> deletedCareer = careerRepository.findById(career1.getId());
Optional<Career> deletedCareer = careerRepository.findById(targetId);
assertThat(deletedCareer).isEmpty();

}
Expand All @@ -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<? extends CareerGroupedByResponse> 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<? extends CareerGroupedByResponse> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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
Expand All @@ -38,7 +44,7 @@ void init() {
.name("대외활동")
.build();

career = Career.builder()
career1 = Career.builder()
.id(1L)
.name("test")
.alias("alias")
Expand All @@ -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<Career> careerList = Arrays.asList(career1, career2);
when(careerRepository.findAll()).thenReturn(careerList);
//when
List<? extends CareerResponseDto.CareerGroupedByCategoryDto> result = (List<? extends CareerResponseDto.CareerGroupedByCategoryDto>) 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
Expand Down

0 comments on commit 4d9a2b8

Please sign in to comment.