diff --git a/src/main/java/com/somemore/domains/review/repository/ReviewRepositoryImpl.java b/src/main/java/com/somemore/domains/review/repository/ReviewRepositoryImpl.java index 2e3d8d7ee..eab2d2a89 100644 --- a/src/main/java/com/somemore/domains/review/repository/ReviewRepositoryImpl.java +++ b/src/main/java/com/somemore/domains/review/repository/ReviewRepositoryImpl.java @@ -27,9 +27,9 @@ public class ReviewRepositoryImpl implements ReviewRepository { private final ReviewJpaRepository reviewJpaRepository; private final JPAQueryFactory queryFactory; - private final static QReview review = QReview.review; - private final static QVolunteerApply volunteerApply = QVolunteerApply.volunteerApply; - private final static QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; + private static final QReview review = QReview.review; + private static final QVolunteerApply volunteerApply = QVolunteerApply.volunteerApply; + private static final QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; @Override public Review save(Review review) { @@ -55,31 +55,31 @@ public boolean existsByVolunteerApplyId(Long volunteerApplyId) { public Page findAllByVolunteerIdAndSearch(UUID volunteerId, ReviewSearchCondition condition) { - BooleanExpression predicate = review.volunteerId.eq(volunteerId) - .and(eqVolunteerCategory(condition.category())) + BooleanExpression exp = volunteerIdEq(volunteerId) + .and(volunteerCategoryEq(condition.category())) .and(isNotDeleted()); - return getReviews(condition, predicate); + return getReviews(condition, exp); } @Override public Page findAllByCenterIdAndSearch(UUID centerId, ReviewSearchCondition condition) { - BooleanExpression predicate = recruitBoard.centerId.eq(centerId) - .and(eqVolunteerCategory(condition.category())) + BooleanExpression exp = centerIdEq(centerId) + .and(volunteerCategoryEq(condition.category())) .and(isNotDeleted()); - return getReviews(condition, predicate); + return getReviews(condition, exp); } @NotNull - private Page getReviews(ReviewSearchCondition condition, BooleanExpression predicate) { + private Page getReviews(ReviewSearchCondition condition, BooleanExpression exp) { List content = queryFactory.select(review) .from(review) .join(volunteerApply).on(review.volunteerApplyId.eq(volunteerApply.id)) .join(recruitBoard).on(recruitBoard.id.eq(volunteerApply.recruitBoardId)) - .where(predicate) + .where(exp) .offset(condition.pageable().getOffset()) .limit(condition.pageable().getPageSize()) .orderBy(toOrderSpecifiers(condition.pageable().getSort())) @@ -90,16 +90,24 @@ private Page getReviews(ReviewSearchCondition condition, BooleanExpressi .from(review) .join(volunteerApply).on(review.volunteerApplyId.eq(volunteerApply.id)) .join(recruitBoard).on(recruitBoard.id.eq(volunteerApply.recruitBoardId)) - .where(predicate); + .where(exp); return PageableExecutionUtils.getPage(content, condition.pageable(), countQuery::fetchOne); } + private static BooleanExpression volunteerIdEq(UUID volunteerId) { + return review.volunteerId.eq(volunteerId); + } + + private static BooleanExpression centerIdEq(UUID centerId) { + return recruitBoard.centerId.eq(centerId); + } + private BooleanExpression isNotDeleted() { return review.deleted.isFalse(); } - private BooleanExpression eqVolunteerCategory(VolunteerCategory category) { + private BooleanExpression volunteerCategoryEq(VolunteerCategory category) { return category != null ? recruitBoard.recruitmentInfo.volunteerCategory.eq(category) : null; } diff --git a/src/main/java/com/somemore/domains/review/service/CreateReviewService.java b/src/main/java/com/somemore/domains/review/service/CreateReviewService.java index 9c169a66d..139f22785 100644 --- a/src/main/java/com/somemore/domains/review/service/CreateReviewService.java +++ b/src/main/java/com/somemore/domains/review/service/CreateReviewService.java @@ -11,6 +11,7 @@ import com.somemore.domains.review.dto.request.ReviewCreateRequestDto; import com.somemore.domains.review.usecase.CreateReviewUseCase; +import com.somemore.global.exception.DuplicateException; import java.util.UUID; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -27,7 +28,7 @@ public class CreateReviewService implements CreateReviewUseCase { @Override public Long createReview(ReviewCreateRequestDto requestDto, UUID volunteerId, String imgUrl) { VolunteerApply apply = getVolunteerApply(requestDto.recruitBoardId(), volunteerId); - validateReviewNotExist(apply); + validateDuplicateReview(apply); validateActivityCompletion(apply); Review review = requestDto.toEntity(apply, volunteerId, imgUrl); @@ -38,9 +39,9 @@ private VolunteerApply getVolunteerApply(Long recruitBoardId, UUID volunteerId) return volunteerApplyQueryUseCase.getByRecruitIdAndVolunteerId(recruitBoardId, volunteerId); } - private void validateReviewNotExist(VolunteerApply apply) { + private void validateDuplicateReview(VolunteerApply apply) { if (reviewRepository.existsByVolunteerApplyId(apply.getId())) { - throw new BadRequestException(REVIEW_ALREADY_EXISTS.getMessage()); + throw new DuplicateException(REVIEW_ALREADY_EXISTS); } } @@ -48,6 +49,6 @@ private void validateActivityCompletion(VolunteerApply apply) { if (apply.isVolunteerActivityCompleted()) { return; } - throw new BadRequestException(REVIEW_RESTRICTED_TO_ATTENDED.getMessage()); + throw new BadRequestException(REVIEW_RESTRICTED_TO_ATTENDED); } } diff --git a/src/main/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryService.java b/src/main/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryService.java index f32580f81..1b08198b0 100644 --- a/src/main/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryService.java +++ b/src/main/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryService.java @@ -8,8 +8,7 @@ import com.somemore.domains.volunteerapply.dto.response.VolunteerApplySummaryResponseDto; import com.somemore.domains.volunteerapply.repository.VolunteerApplyRepository; import com.somemore.domains.volunteerapply.usecase.VolunteerApplyQueryUseCase; -import com.somemore.global.exception.BadRequestException; - +import com.somemore.global.exception.NoSuchElementException; import java.util.List; import java.util.UUID; import lombok.RequiredArgsConstructor; @@ -35,7 +34,7 @@ public List getVolunteerIdsByRecruitIds(List recruitIds) { public VolunteerApply getByRecruitIdAndVolunteerId(Long recruitId, UUID volunteerId) { return volunteerApplyRepository.findByRecruitIdAndVolunteerId(recruitId, volunteerId) .orElseThrow( - () -> new BadRequestException(NOT_EXISTS_VOLUNTEER_APPLY)); + () -> new NoSuchElementException(NOT_EXISTS_VOLUNTEER_APPLY)); } @Override @@ -49,7 +48,7 @@ public VolunteerApplySummaryResponseDto getSummaryByRecruitId(Long recruitId) { @Override public VolunteerApplyResponseDto getVolunteerApplyByRecruitIdAndVolunteerId(Long recruitId, - UUID volunteerId) { + UUID volunteerId) { VolunteerApply apply = getByRecruitIdAndVolunteerId(recruitId, volunteerId); return VolunteerApplyResponseDto.from(apply); diff --git a/src/main/java/com/somemore/global/exception/DuplicateException.java b/src/main/java/com/somemore/global/exception/DuplicateException.java index 0a752c189..f9ae30685 100644 --- a/src/main/java/com/somemore/global/exception/DuplicateException.java +++ b/src/main/java/com/somemore/global/exception/DuplicateException.java @@ -2,6 +2,9 @@ public class DuplicateException extends RuntimeException{ + public DuplicateException(final ExceptionMessage message) { + super(message.getMessage()); + } public DuplicateException(final String message) { super(message); } diff --git a/src/main/java/com/somemore/global/exception/handler/GlobalExceptionHandler.java b/src/main/java/com/somemore/global/exception/handler/GlobalExceptionHandler.java index 97150c26a..ce29c81d4 100644 --- a/src/main/java/com/somemore/global/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/somemore/global/exception/handler/GlobalExceptionHandler.java @@ -49,10 +49,10 @@ ProblemDetail handleDuplicateException(final DuplicateException e) { @ExceptionHandler(MethodArgumentNotValidException.class) ProblemDetail handleMethodArgumentNotValid(final MethodArgumentNotValidException e) { - ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage()); + String errorMessage = e.getBindingResult().getAllErrors().getFirst().getDefaultMessage(); + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, errorMessage); problemDetail.setTitle("유효성 예외"); - problemDetail.setDetail("입력 데이터 유효성 검사가 실패했습니다. 각 필드를 확인해주세요."); return problemDetail; } diff --git a/src/test/java/com/somemore/domains/center/controller/CenterProfileCommandApiControllerTest.java b/src/test/java/com/somemore/domains/center/controller/CenterProfileCommandApiControllerTest.java index ef0ee432a..c2886d8fa 100644 --- a/src/test/java/com/somemore/domains/center/controller/CenterProfileCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/center/controller/CenterProfileCommandApiControllerTest.java @@ -1,42 +1,31 @@ package com.somemore.domains.center.controller; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willDoNothing; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.center.dto.request.CenterProfileUpdateRequestDto; import com.somemore.domains.center.usecase.command.UpdateCenterProfileUseCase; import com.somemore.global.imageupload.usecase.ImageUploadUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.RequestPostProcessor; -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willDoNothing; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - class CenterProfileCommandApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - - @Autowired - private ObjectMapper objectMapper; - @MockBean private UpdateCenterProfileUseCase updateCenterProfileUseCase; @@ -92,7 +81,6 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) .header("Authorization", "Bearer access-token")) //then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").isEmpty()) diff --git a/src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java b/src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java index a67d5c9f0..ea408c8f6 100644 --- a/src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java +++ b/src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java @@ -1,25 +1,25 @@ package com.somemore.domains.center.controller; +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.center.dto.response.CenterProfileResponseDto; import com.somemore.domains.center.usecase.query.CenterQueryUseCase; import com.somemore.global.exception.BadRequestException; import com.somemore.support.ControllerTestSupport; +import java.util.List; +import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import java.util.List; -import java.util.UUID; - -import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER; -import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - class CenterQueryApiControllerTest extends ControllerTestSupport { @MockBean @@ -53,16 +53,18 @@ void getCenterProfile() throws Exception { get("/api/center/profile/{centerId}", centerId) .contentType(MediaType.APPLICATION_JSON) ) - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value("200")) .andExpect(jsonPath("$.message").value("기관 프로필 조회 성공")) .andExpect(jsonPath("$.data.center_id").value(centerId.toString())) // center_id로 수정 .andExpect(jsonPath("$.data.name").value("Test Center")) - .andExpect(jsonPath("$.data.contact_number").value("010-1234-5678")) // contact_number로 수정 - .andExpect(jsonPath("$.data.img_url").value("http://example.com/image.jpg")) // img_url로 수정 + .andExpect(jsonPath("$.data.contact_number").value( + "010-1234-5678")) // contact_number로 수정 + .andExpect(jsonPath("$.data.img_url").value( + "http://example.com/image.jpg")) // img_url로 수정 .andExpect(jsonPath("$.data.introduce").value("This is a test center.")) - .andExpect(jsonPath("$.data.homepage_link").value("http://example.com")) // homepage_link로 수정 + .andExpect(jsonPath("$.data.homepage_link").value( + "http://example.com")) // homepage_link로 수정 .andExpect(jsonPath("$.data.prefer_items").isArray()); // prefer_items로 수정 verify(centerQueryUseCase, times(1)).getCenterProfileByCenterId(centerId); @@ -81,7 +83,6 @@ void getCenterProfile_NotFound() throws Exception { get("/api/center/profile/{centerId}", nonExistentCenterId) .contentType(MediaType.APPLICATION_JSON) ) - .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.status").value("400")) .andExpect(jsonPath("$.detail").value("존재하지 않는 기관입니다.")); diff --git a/src/test/java/com/somemore/domains/center/controller/PreferItemCommandApiControllerTest.java b/src/test/java/com/somemore/domains/center/controller/PreferItemCommandApiControllerTest.java index 398aeee80..97520a8a9 100644 --- a/src/test/java/com/somemore/domains/center/controller/PreferItemCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/center/controller/PreferItemCommandApiControllerTest.java @@ -1,5 +1,13 @@ package com.somemore.domains.center.controller; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.center.dto.request.PreferItemCreateRequestDto; import com.somemore.domains.center.dto.response.PreferItemCreateResponseDto; import com.somemore.domains.center.usecase.command.CreatePreferItemUseCase; @@ -7,21 +15,12 @@ import com.somemore.global.exception.BadRequestException; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import java.util.UUID; - -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.verify; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - class PreferItemCommandApiControllerTest extends ControllerTestSupport { @MockBean diff --git a/src/test/java/com/somemore/domains/community/controller/CommunityBoardCommandApiControllerTest.java b/src/test/java/com/somemore/domains/community/controller/CommunityBoardCommandApiControllerTest.java index 1f93d0b40..7cacaac34 100644 --- a/src/test/java/com/somemore/domains/community/controller/CommunityBoardCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/community/controller/CommunityBoardCommandApiControllerTest.java @@ -1,6 +1,15 @@ package com.somemore.domains.community.controller; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willDoNothing; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.community.dto.request.CommunityBoardCreateRequestDto; import com.somemore.domains.community.dto.request.CommunityBoardUpdateRequestDto; import com.somemore.domains.community.usecase.board.CreateCommunityBoardUseCase; @@ -9,38 +18,18 @@ import com.somemore.global.imageupload.usecase.ImageUploadUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.RequestPostProcessor; -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willDoNothing; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - public class CommunityBoardCommandApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - - @Autowired - private ObjectMapper objectMapper; - @MockBean private CreateCommunityBoardUseCase createCommunityBoardUseCase; @@ -86,10 +75,10 @@ void createCommunityBoard_success() throws Exception { //when mockMvc.perform(multipart("/api/community-board") - .file(requestData) - .file(imageFile) - .contentType(MULTIPART_FORM_DATA) - .header("Authorization", "Bearer access-token")) + .file(requestData) + .file(imageFile) + .contentType(MULTIPART_FORM_DATA) + .header("Authorization", "Bearer access-token")) //then .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(201)) @@ -138,13 +127,12 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) //when mockMvc.perform(builder - .file(requestData) - .file(imageFile) - .contentType(MULTIPART_FORM_DATA) - .header("Authorization", "Bearer access-token")) + .file(requestData) + .file(imageFile) + .contentType(MULTIPART_FORM_DATA) + .header("Authorization", "Bearer access-token")) //then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").isEmpty()) @@ -157,13 +145,13 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) void deleteCommunityBoard_success() throws Exception { //given Long communityBoardId = 1L; - willDoNothing().given(deleteCommunityBoardUseCase).deleteCommunityBoard(any(UUID.class), any()); + willDoNothing().given(deleteCommunityBoardUseCase) + .deleteCommunityBoard(any(UUID.class), any()); //when mockMvc.perform(delete("/api/community-board/{id}", communityBoardId) - .header("Authorization", "Bearer access-token")) + .header("Authorization", "Bearer access-token")) //then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.message").value("커뮤니티 게시글 삭제 성공")) diff --git a/src/test/java/com/somemore/domains/community/controller/CommunityBoardQueryApiControllerTest.java b/src/test/java/com/somemore/domains/community/controller/CommunityBoardQueryApiControllerTest.java index 351567ff7..3ae460295 100644 --- a/src/test/java/com/somemore/domains/community/controller/CommunityBoardQueryApiControllerTest.java +++ b/src/test/java/com/somemore/domains/community/controller/CommunityBoardQueryApiControllerTest.java @@ -1,35 +1,29 @@ package com.somemore.domains.community.controller; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.community.dto.response.CommunityBoardDetailResponseDto; import com.somemore.domains.community.dto.response.CommunityBoardResponseDto; import com.somemore.domains.community.usecase.board.CommunityBoardQueryUseCase; import com.somemore.support.ControllerTestSupport; +import java.util.Collections; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.Collections; -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; public class CommunityBoardQueryApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - @MockBean private CommunityBoardQueryUseCase communityBoardQueryUseCase; @@ -48,7 +42,7 @@ void getAll() throws Exception { //when //then mockMvc.perform(get("/api/community-boards") - .accept(MediaType.APPLICATION_JSON)) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").exists()) @@ -88,7 +82,8 @@ void getByWriterId() throws Exception { void getById() throws Exception { //given Long communityBoardId = 1L; - CommunityBoardDetailResponseDto responseDto = CommunityBoardDetailResponseDto.builder().build(); + CommunityBoardDetailResponseDto responseDto = CommunityBoardDetailResponseDto.builder() + .build(); given(communityBoardQueryUseCase.getCommunityBoardDetail(any())) .willReturn(responseDto); diff --git a/src/test/java/com/somemore/domains/community/controller/CommunityCommentCommandApiControllerTest.java b/src/test/java/com/somemore/domains/community/controller/CommunityCommentCommandApiControllerTest.java index e6dc7f240..59356fbd1 100644 --- a/src/test/java/com/somemore/domains/community/controller/CommunityCommentCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/community/controller/CommunityCommentCommandApiControllerTest.java @@ -1,6 +1,16 @@ package com.somemore.domains.community.controller; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willDoNothing; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.community.dto.request.CommunityCommentCreateRequestDto; import com.somemore.domains.community.dto.request.CommunityCommentUpdateRequestDto; import com.somemore.domains.community.usecase.comment.CreateCommunityCommentUseCase; @@ -8,32 +18,14 @@ import com.somemore.domains.community.usecase.comment.UpdateCommunityCommentUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willDoNothing; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; public class CommunityCommentCommandApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - - @Autowired - private ObjectMapper objectMapper; - @MockBean private CreateCommunityCommentUseCase createCommunityCommentUseCase; @@ -85,7 +77,8 @@ void updateCommunityComment_success() throws Exception { .updateCommunityComment(any(), any(), any(UUID.class), any()); //when - mockMvc.perform(put("/api/community-board/{boardId}/comment/{id}", communityBoardId, communityCommentId) + mockMvc.perform(put("/api/community-board/{boardId}/comment/{id}", communityBoardId, + communityCommentId) .content(objectMapper.writeValueAsString(requestDto)) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer access-token")) @@ -102,13 +95,14 @@ void updateCommunityComment_success() throws Exception { @WithMockCustomUser void deleteCommunityComment_success() throws Exception { //given - willDoNothing().given(deleteCommunityCommentUseCase).deleteCommunityComment(any(UUID.class), any(), any()); + willDoNothing().given(deleteCommunityCommentUseCase) + .deleteCommunityComment(any(UUID.class), any(), any()); //when - mockMvc.perform(delete("/api/community-board/{boardId}/comment/{id}", communityBoardId, communityCommentId) + mockMvc.perform(delete("/api/community-board/{boardId}/comment/{id}", communityBoardId, + communityCommentId) .header("Authorization", "Bearer access-token")) //then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.message").value("커뮤니티 댓글 삭제 성공")) diff --git a/src/test/java/com/somemore/domains/community/controller/CommunityCommentQueryApiControllerTest.java b/src/test/java/com/somemore/domains/community/controller/CommunityCommentQueryApiControllerTest.java index 7cc433034..1447d2140 100644 --- a/src/test/java/com/somemore/domains/community/controller/CommunityCommentQueryApiControllerTest.java +++ b/src/test/java/com/somemore/domains/community/controller/CommunityCommentQueryApiControllerTest.java @@ -1,33 +1,27 @@ package com.somemore.domains.community.controller; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.community.dto.response.CommunityCommentResponseDto; import com.somemore.domains.community.usecase.comment.CommunityCommentQueryUseCase; import com.somemore.support.ControllerTestSupport; +import java.util.Collections; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.Collections; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; public class CommunityCommentQueryApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - @MockBean private CommunityCommentQueryUseCase communityCommentQueryUseCase; @@ -44,7 +38,7 @@ void getByBoardId() throws Exception { //when //then mockMvc.perform(get("/api/community-board/{boardId}/comments", communityBoardId) - .accept(MediaType.APPLICATION_JSON)) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").exists()) diff --git a/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardCommandApiControllerTest.java b/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardCommandApiControllerTest.java index 6218ad2b7..e95e44a4a 100644 --- a/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardCommandApiControllerTest.java @@ -1,6 +1,20 @@ package com.somemore.domains.recruitboard.controller; -import com.fasterxml.jackson.databind.ObjectMapper; +import static com.somemore.domains.recruitboard.domain.RecruitStatus.CLOSED; +import static com.somemore.domains.recruitboard.domain.VolunteerCategory.OTHER; +import static com.somemore.support.fixture.LocalDateTimeFixture.createStartDateTime; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willDoNothing; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.location.dto.request.LocationCreateRequestDto; import com.somemore.domains.recruitboard.domain.RecruitStatus; import com.somemore.domains.recruitboard.dto.request.RecruitBoardCreateRequestDto; @@ -13,42 +27,20 @@ import com.somemore.global.imageupload.usecase.ImageUploadUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.RequestPostProcessor; -import java.math.BigDecimal; -import java.time.LocalDateTime; -import java.util.UUID; - -import static com.somemore.domains.recruitboard.domain.RecruitStatus.CLOSED; -import static com.somemore.domains.recruitboard.domain.VolunteerCategory.OTHER; -import static com.somemore.support.fixture.LocalDateTimeFixture.createStartDateTime; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willDoNothing; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - class RecruitBoardCommandApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - - @Autowired - private ObjectMapper objectMapper; - @MockBean private CreateRecruitBoardUseCase createRecruitBoardUseCase; @@ -176,7 +168,6 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) .contentType(MULTIPART_FORM_DATA) .header("Authorization", "Bearer access-token")) //then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").isEmpty()) @@ -206,7 +197,6 @@ void updateRecruitBoardLocation() throws Exception { .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer access-token")) // then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").isEmpty()) @@ -231,7 +221,6 @@ void updateRecruitBoardStatus() throws Exception { .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer access-token")) //then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.message").value("봉사 활동 모집글 상태 수정 성공")) @@ -250,7 +239,6 @@ void deleteRecruitBoard() throws Exception { mockMvc.perform(delete("/api/recruit-board/{id}", recruitBoardId) .header("Authorization", "Bearer access-token")) // then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.message").value("봉사 활동 모집글 삭제 성공")) diff --git a/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardQueryApiControllerTest.java b/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardQueryApiControllerTest.java index c499427f8..77e73bc6d 100644 --- a/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardQueryApiControllerTest.java +++ b/src/test/java/com/somemore/domains/recruitboard/controller/RecruitBoardQueryApiControllerTest.java @@ -1,5 +1,15 @@ package com.somemore.domains.recruitboard.controller; +import static com.somemore.domains.recruitboard.domain.VolunteerCategory.ADMINISTRATIVE_SUPPORT; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.recruitboard.dto.condition.RecruitBoardNearByCondition; import com.somemore.domains.recruitboard.dto.condition.RecruitBoardSearchCondition; import com.somemore.domains.recruitboard.dto.response.RecruitBoardDetailResponseDto; @@ -8,31 +18,17 @@ import com.somemore.domains.recruitboard.dto.response.RecruitBoardWithLocationResponseDto; import com.somemore.domains.recruitboard.usecase.query.RecruitBoardQueryUseCase; import com.somemore.support.ControllerTestSupport; +import java.util.Collections; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.Collections; -import java.util.UUID; - -import static com.somemore.domains.recruitboard.domain.VolunteerCategory.ADMINISTRATIVE_SUPPORT; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class RecruitBoardQueryApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - @MockBean private RecruitBoardQueryUseCase recruitBoardQueryUseCase; diff --git a/src/test/java/com/somemore/domains/review/controller/ReviewCommandApiControllerTest.java b/src/test/java/com/somemore/domains/review/controller/ReviewCommandApiControllerTest.java index 4deef4e1a..dae38a879 100644 --- a/src/test/java/com/somemore/domains/review/controller/ReviewCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/review/controller/ReviewCommandApiControllerTest.java @@ -1,44 +1,35 @@ package com.somemore.domains.review.controller; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.fasterxml.jackson.core.JsonProcessingException; import com.somemore.domains.review.dto.request.ReviewCreateRequestDto; import com.somemore.domains.review.usecase.CreateReviewUseCase; import com.somemore.global.imageupload.usecase.ImageUploadUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.BDDMockito.given; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class ReviewCommandApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - - @Autowired - private ObjectMapper objectMapper; - @MockBean private ImageUploadUseCase imageUploadUseCase; @MockBean private CreateReviewUseCase createReviewUseCase; - @DisplayName("리뷰 생성") + @DisplayName("리뷰 생성 성공") @Test @WithMockCustomUser() void createReview() throws Exception { @@ -56,12 +47,7 @@ void createReview() throws Exception { "test image content".getBytes() ); - MockMultipartFile requestData = new MockMultipartFile( - "data", - "", - MediaType.APPLICATION_JSON_VALUE, - objectMapper.writeValueAsBytes(requestDto) - ); + MockMultipartFile requestData = getRequestData(requestDto); String imgUrl = "https://example.com/image/test-image.jpg"; Long reviewId = 1L; @@ -82,4 +68,92 @@ void createReview() throws Exception { .andExpect(jsonPath("$.data").value(reviewId)) .andExpect(jsonPath("$.message").value("리뷰 등록 성공")); } + + @DisplayName("리뷰 생성 유효성 테스트 - 모집글 아이디") + @Test + @WithMockCustomUser() + void createReviewValidateTestRecruitBoardId() throws Exception { + // given + ReviewCreateRequestDto requestDto = ReviewCreateRequestDto.builder() + .title("리뷰 제목") + .content("리뷰 내용") + .build(); + + MockMultipartFile requestData = getRequestData(requestDto); + + given(imageUploadUseCase.uploadImage(any())).willReturn(""); + + // when + mockMvc.perform(multipart("/api/review") + .file(requestData) + .contentType(MULTIPART_FORM_DATA) + .header("Authorization", "Bearer access-token")) + // then + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.title").value("유효성 예외")) + .andExpect(jsonPath("$.detail").value("봉사 모집글 아이디는 필수 값입니다.")); + } + + @DisplayName("리뷰 생성 유효성 테스트 - 제목") + @Test + @WithMockCustomUser() + void createReviewValidateTestTitle() throws Exception { + // given + ReviewCreateRequestDto requestDto = ReviewCreateRequestDto.builder() + .recruitBoardId(1L) + .content("리뷰 내용") + .build(); + + MockMultipartFile requestData = getRequestData(requestDto); + + given(imageUploadUseCase.uploadImage(any())).willReturn(""); + + // when + mockMvc.perform(multipart("/api/review") + .file(requestData) + .contentType(MULTIPART_FORM_DATA) + .header("Authorization", "Bearer access-token")) + // then + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.title").value("유효성 예외")) + .andExpect(jsonPath("$.detail").value("리뷰 제목은 필수 값입니다.")); + } + + @DisplayName("리뷰 생성 유효성 테스트 - 내용") + @Test + @WithMockCustomUser() + void createReviewValidateTestContent() throws Exception { + // given + ReviewCreateRequestDto requestDto = ReviewCreateRequestDto.builder() + .recruitBoardId(1L) + .title("리뷰 제목") + .build(); + + MockMultipartFile requestData = getRequestData(requestDto); + + given(imageUploadUseCase.uploadImage(any())).willReturn(""); + + // when + mockMvc.perform(multipart("/api/review") + .file(requestData) + .contentType(MULTIPART_FORM_DATA) + .header("Authorization", "Bearer access-token")) + // then + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.title").value("유효성 예외")) + .andExpect(jsonPath("$.detail").value("리뷰 내용은 필수 값입니다.")); + } + + private MockMultipartFile getRequestData(ReviewCreateRequestDto requestDto) + throws JsonProcessingException { + return new MockMultipartFile( + "data", + "", + MediaType.APPLICATION_JSON_VALUE, + objectMapper.writeValueAsBytes(requestDto) + ); + } } diff --git a/src/test/java/com/somemore/domains/review/service/CreateReviewServiceTest.java b/src/test/java/com/somemore/domains/review/service/CreateReviewServiceTest.java index 071f17739..35636fa54 100644 --- a/src/test/java/com/somemore/domains/review/service/CreateReviewServiceTest.java +++ b/src/test/java/com/somemore/domains/review/service/CreateReviewServiceTest.java @@ -6,6 +6,7 @@ import com.somemore.domains.volunteerapply.domain.VolunteerApply; import com.somemore.domains.volunteerapply.repository.VolunteerApplyRepository; import com.somemore.global.exception.BadRequestException; +import com.somemore.global.exception.DuplicateException; import com.somemore.support.IntegrationTestSupport; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -124,7 +125,7 @@ void createReviewWhenExistsReview() { // then assertThatThrownBy( () -> createReviewService.createReview(requestDto, volunteerId, "") - ).isInstanceOf(BadRequestException.class) + ).isInstanceOf(DuplicateException.class) .hasMessage(REVIEW_ALREADY_EXISTS.getMessage()); } diff --git a/src/test/java/com/somemore/domains/volunteer/controller/VolunteerProfileCommandControllerTest.java b/src/test/java/com/somemore/domains/volunteer/controller/VolunteerProfileCommandControllerTest.java index 0958bc328..2ccfd9994 100644 --- a/src/test/java/com/somemore/domains/volunteer/controller/VolunteerProfileCommandControllerTest.java +++ b/src/test/java/com/somemore/domains/volunteer/controller/VolunteerProfileCommandControllerTest.java @@ -1,40 +1,30 @@ package com.somemore.domains.volunteer.controller; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willDoNothing; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.volunteer.dto.request.VolunteerProfileUpdateRequestDto; import com.somemore.domains.volunteer.usecase.UpdateVolunteerProfileUseCase; import com.somemore.global.imageupload.usecase.ImageUploadUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import java.util.UUID; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.willDoNothing; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - class VolunteerProfileCommandControllerTest extends ControllerTestSupport { - @Autowired - private ObjectMapper objectMapper; - @MockBean private UpdateVolunteerProfileUseCase updateVolunteerProfileUseCase; @@ -69,7 +59,6 @@ void updateVolunteerProfile() throws Exception { .contentType(MULTIPART_FORM_DATA) .header("Authorization", "Bearer access-token")) // then - .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data").isEmpty()) @@ -98,15 +87,9 @@ void testNicknameValidation() throws Exception { .contentType(MULTIPART_FORM_DATA) .header("Authorization", "Bearer access-token")) .andExpect(status().isBadRequest()) - .andDo(result -> { - String responseBody = result.getResponse().getContentAsString(StandardCharsets.UTF_8); - - Map jsonResponse = objectMapper.readValue(responseBody, new TypeReference<>() { - }); - String detail = (String) jsonResponse.get("detail"); - - assertThat(detail).isEqualTo("입력 데이터 유효성 검사가 실패했습니다. 각 필드를 확인해주세요."); - }); + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.title").value("유효성 예외")) + .andExpect(jsonPath("$.detail").value("닉네임은 최대 10자까지 입력 가능합니다.")); } @DisplayName("봉사자 프로필 수정의 소개 유효성 검사 실패 테스트") @@ -138,15 +121,9 @@ void testIntroduceValidation() throws Exception { .contentType(MULTIPART_FORM_DATA) .header("Authorization", "Bearer access-token")) .andExpect(status().isBadRequest()) - .andDo(result -> { - String responseBody = result.getResponse().getContentAsString(StandardCharsets.UTF_8); - - Map jsonResponse = objectMapper.readValue(responseBody, new TypeReference<>() { - }); - String detail = (String) jsonResponse.get("detail"); - - assertThat(detail).isEqualTo("입력 데이터 유효성 검사가 실패했습니다. 각 필드를 확인해주세요."); - }); + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.title").value("유효성 예외")) + .andExpect(jsonPath("$.detail").value("소개글은 최대 100자까지 입력 가능합니다.")); } private MockMultipartFile createMockImageFile() { diff --git a/src/test/java/com/somemore/domains/volunteerapply/controller/CenterVolunteerApplyCommandApiControllerTest.java b/src/test/java/com/somemore/domains/volunteerapply/controller/CenterVolunteerApplyCommandApiControllerTest.java index 3c2851ecc..b3502cd87 100644 --- a/src/test/java/com/somemore/domains/volunteerapply/controller/CenterVolunteerApplyCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/volunteerapply/controller/CenterVolunteerApplyCommandApiControllerTest.java @@ -1,34 +1,27 @@ package com.somemore.domains.volunteerapply.controller; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willDoNothing; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.volunteerapply.dto.request.VolunteerApplySettleRequestDto; import com.somemore.domains.volunteerapply.usecase.ApproveVolunteerApplyUseCase; import com.somemore.domains.volunteerapply.usecase.RejectVolunteerApplyUseCase; import com.somemore.domains.volunteerapply.usecase.SettleVolunteerApplyFacadeUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.List; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.List; -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.willDoNothing; -import static org.springframework.http.MediaType.APPLICATION_JSON; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class CenterVolunteerApplyCommandApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - @MockBean private ApproveVolunteerApplyUseCase approveVolunteerApplyUseCase; @@ -38,9 +31,6 @@ class CenterVolunteerApplyCommandApiControllerTest extends ControllerTestSupport @MockBean private SettleVolunteerApplyFacadeUseCase settleVolunteerApplyFacadeUseCase; - @Autowired - private ObjectMapper objectMapper; - @Test @DisplayName("봉사 활동 지원 승인 성공 테스트") @WithMockCustomUser(role = "CENTER") diff --git a/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyCommandApiControllerTest.java b/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyCommandApiControllerTest.java index 18a3b00f5..4d6499b74 100644 --- a/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyCommandApiControllerTest.java +++ b/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyCommandApiControllerTest.java @@ -1,19 +1,5 @@ package com.somemore.domains.volunteerapply.controller; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.somemore.domains.volunteerapply.dto.request.VolunteerApplyCreateRequestDto; -import com.somemore.domains.volunteerapply.usecase.ApplyVolunteerApplyUseCase; -import com.somemore.domains.volunteerapply.usecase.WithdrawVolunteerApplyUseCase; -import com.somemore.support.ControllerTestSupport; -import com.somemore.support.annotation.WithMockCustomUser; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.UUID; - import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willDoNothing; @@ -23,13 +9,17 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -class VolunteerApplyCommandApiControllerTest extends ControllerTestSupport { - - @Autowired - private MockMvc mockMvc; +import com.somemore.domains.volunteerapply.dto.request.VolunteerApplyCreateRequestDto; +import com.somemore.domains.volunteerapply.usecase.ApplyVolunteerApplyUseCase; +import com.somemore.domains.volunteerapply.usecase.WithdrawVolunteerApplyUseCase; +import com.somemore.support.ControllerTestSupport; +import com.somemore.support.annotation.WithMockCustomUser; +import java.util.UUID; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; - @Autowired - private ObjectMapper objectMapper; +class VolunteerApplyCommandApiControllerTest extends ControllerTestSupport { @MockBean private ApplyVolunteerApplyUseCase applyVolunteerApplyUseCase; @@ -68,7 +58,7 @@ void apply() throws Exception { @Test @DisplayName("봉사 활동 철회 성공 테스트") @WithMockCustomUser - void withdraw () throws Exception { + void withdraw() throws Exception { // given Long id = 1L; diff --git a/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyQueryApiControllerTest.java b/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyQueryApiControllerTest.java index faf2fb0f9..454a4b90e 100644 --- a/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyQueryApiControllerTest.java +++ b/src/test/java/com/somemore/domains/volunteerapply/controller/VolunteerApplyQueryApiControllerTest.java @@ -1,5 +1,13 @@ package com.somemore.domains.volunteerapply.controller; +import static com.somemore.domains.volunteerapply.domain.ApplyStatus.WAITING; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.somemore.domains.volunteerapply.dto.condition.VolunteerApplySearchCondition; import com.somemore.domains.volunteerapply.dto.response.VolunteerApplyRecruitInfoResponseDto; import com.somemore.domains.volunteerapply.dto.response.VolunteerApplyResponseDto; @@ -9,30 +17,16 @@ import com.somemore.domains.volunteerapply.usecase.VolunteerApplyQueryUseCase; import com.somemore.support.ControllerTestSupport; import com.somemore.support.annotation.WithMockCustomUser; +import java.util.Collections; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.Collections; -import java.util.UUID; - -import static com.somemore.domains.volunteerapply.domain.ApplyStatus.WAITING; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.springframework.http.MediaType.APPLICATION_JSON; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class VolunteerApplyQueryApiControllerTest extends ControllerTestSupport { - @Autowired - private MockMvc mockMvc; - @MockBean private VolunteerApplyQueryUseCase volunteerApplyQueryUseCase; diff --git a/src/test/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryServiceTest.java b/src/test/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryServiceTest.java index 9366cf31f..4b4387e41 100644 --- a/src/test/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryServiceTest.java +++ b/src/test/java/com/somemore/domains/volunteerapply/service/VolunteerApplyQueryServiceTest.java @@ -1,12 +1,22 @@ package com.somemore.domains.volunteerapply.service; +import static com.somemore.domains.volunteerapply.domain.ApplyStatus.APPROVED; +import static com.somemore.domains.volunteerapply.domain.ApplyStatus.REJECTED; +import static com.somemore.domains.volunteerapply.domain.ApplyStatus.WAITING; +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_VOLUNTEER_APPLY; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import com.somemore.domains.volunteerapply.domain.ApplyStatus; import com.somemore.domains.volunteerapply.domain.VolunteerApply; import com.somemore.domains.volunteerapply.dto.condition.VolunteerApplySearchCondition; import com.somemore.domains.volunteerapply.dto.response.VolunteerApplyResponseDto; import com.somemore.domains.volunteerapply.dto.response.VolunteerApplySummaryResponseDto; import com.somemore.domains.volunteerapply.repository.VolunteerApplyRepository; +import com.somemore.global.exception.NoSuchElementException; import com.somemore.support.IntegrationTestSupport; +import java.util.List; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -15,12 +25,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.transaction.annotation.Transactional; -import java.util.List; -import java.util.UUID; - -import static com.somemore.domains.volunteerapply.domain.ApplyStatus.*; -import static org.assertj.core.api.Assertions.assertThat; - @Transactional class VolunteerApplyQueryServiceTest extends IntegrationTestSupport { @@ -193,6 +197,22 @@ void getAllByIds() { .containsExactlyInAnyOrderElementsOf(ids); } + @DisplayName("존재하지 않는 봉사 지원을 조회할 경우 에러가 발생한다.") + @Test + void getByRecruitIdAndVolunteerIdWhenNotExist() { + // given + Long wrongBoardId = 999L; + UUID wrongVolunteerId = UUID.randomUUID(); + + // when + // then + assertThatThrownBy( + () -> volunteerApplyQueryService.getByRecruitIdAndVolunteerId(wrongBoardId, + wrongVolunteerId) + ).isInstanceOf(NoSuchElementException.class) + .hasMessage(NOT_EXISTS_VOLUNTEER_APPLY.getMessage()); + } + private VolunteerApply createApply(UUID volunteerId, Long recruitId) { return createApply(volunteerId, recruitId, WAITING); } diff --git a/src/test/java/com/somemore/support/ControllerTestSupport.java b/src/test/java/com/somemore/support/ControllerTestSupport.java index 4edfe5bc7..80bdbc257 100644 --- a/src/test/java/com/somemore/support/ControllerTestSupport.java +++ b/src/test/java/com/somemore/support/ControllerTestSupport.java @@ -18,4 +18,5 @@ public abstract class ControllerTestSupport { @Autowired protected ObjectMapper objectMapper; + }