Skip to content

Commit

Permalink
test: 댓글 작성 시, 멘션 ID에 Null일 경우 테스트 코드 작성 (#338)
Browse files Browse the repository at this point in the history
* chore: spring-validation 의존성 추가 (#337)

* test: 댓글 작성 시, 멘션 ID에 Null일 경우 테스트 코드 작성 (#337)
  • Loading branch information
kdkdhoho authored Jan 20, 2025
1 parent a8ea8ab commit ab44930
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-validation'

// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.12.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum ErrorCode {

// Http Request
METHOD_ARGUMENT_TYPE_MISMATCH(BAD_REQUEST, "요청 한 값 타입이 잘못되어 binding에 실패하였습니다."),
METHOD_ARGUMENT_NOT_VALID_EXCEPTION(BAD_REQUEST, "요청에 담긴 값에 문제가 있습니다."),
RESOURCE_NOT_FOUND(NOT_FOUND, "대상이 존재하지 않습니다."),
RESOURCES_EMPTY(NOT_FOUND, "해당 대상들이 존재하지 않습니다."),
ELASTICSEARCH_REQUEST_FAILED(BAD_REQUEST, "Elasticsearch 검색 요청에 실패했습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.listywave.common.exception;

import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS_TOKEN;
import static com.listywave.common.exception.ErrorCode.METHOD_ARGUMENT_TYPE_MISMATCH;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static com.listywave.common.exception.ErrorCode.METHOD_ARGUMENT_NOT_VALID_EXCEPTION;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@Slf4j
Expand All @@ -32,20 +35,30 @@ ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
@ExceptionHandler(Exception.class)
protected ResponseEntity<String> handleException(Exception e) {
log.error("[InternalServerError] : {}", e.getMessage(), e);
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(e.getMessage());
return ResponseEntity.internalServerError().body(e.getMessage());
}

@ExceptionHandler(IllegalArgumentException.class)
ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
log.error("[IllegalArgumentException] : {}", e.getMessage(), e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
return ResponseEntity.badRequest().body(e.getMessage());
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error("[MethodArgumentTypeMismatchException] : {}", e.getMessage(), e);
CustomException customException = new CustomException(METHOD_ARGUMENT_TYPE_MISMATCH);
return ErrorResponse.toResponseEntity(customException);
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException e,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request
) {
log.error("[MethodArgumentNotValidException] : {}", e.getMessage(), e);
String errorMessage = e.getFieldErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(", "));
ErrorCode errorCode = METHOD_ARGUMENT_NOT_VALID_EXCEPTION;
ErrorResponse errorResponse = new ErrorResponse(status.value(), errorMessage, errorCode.name(), errorCode.getDetail(), errorMessage);
return ResponseEntity.badRequest().body(errorResponse);
}

@ExceptionHandler(SignatureException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.listywave.list.application.service.CommentService;
import com.listywave.list.presentation.dto.request.comment.CommentCreateRequest;
import com.listywave.list.presentation.dto.request.comment.CommentUpdateRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -29,7 +30,7 @@ public class CommentController {
ResponseEntity<CommentCreateResponse> create(
@PathVariable("listId") Long listId,
@Auth Long writerId,
@RequestBody CommentCreateRequest request
@RequestBody @Valid CommentCreateRequest request
) {
CommentCreateResponse response = commentService.create(listId, writerId, request.content(), request.mentionIds());
return ResponseEntity.status(CREATED).body(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.listywave.list.presentation.dto.request.comment;

import jakarta.validation.constraints.NotNull;
import java.util.List;

public record CommentCreateRequest(
String content,
List<Long> mentionIds
@NotNull(message = "댓글 작성 시 멘션 ID에 Null이 될 수 없습니다.") List<Long> mentionIds
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import static com.listywave.user.fixture.UserFixture.정수;
import static java.util.Collections.EMPTY_LIST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import com.listywave.acceptance.common.AcceptanceTest;
import com.listywave.list.application.dto.response.CommentFindResponse;
import com.listywave.list.application.dto.response.ListCreateResponse;
import com.listywave.list.presentation.dto.request.ReplyCreateRequest;
import com.listywave.list.presentation.dto.request.comment.CommentCreateRequest;
import com.listywave.list.presentation.dto.request.comment.CommentUpdateRequest;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -49,6 +51,23 @@ public class CommentAcceptanceTest extends AcceptanceTest {
assertThat(결과.totalCount()).isEqualTo(11);
}

@Test
void 멘션ID를_포함하지_않고_요청을_보내면_예외가_발생한다() {
// given
var 동호 = 회원을_저장한다(동호());
var 동호_액세스_토큰 = 액세스_토큰을_발급한다(동호);
var 동호_리스트_ID = 리스트_저장_API_호출(가장_좋아하는_견종_TOP3_생성_요청_데이터(List.of()), 동호_액세스_토큰)
.as(ListCreateResponse.class)
.listId();

// when
var 멘션ID를_포함하지_않은_요청 = new CommentCreateRequest("댓글", null);
var response = 댓글_저장_API_호출(동호_액세스_토큰, 동호_리스트_ID, 멘션ID를_포함하지_않은_요청);

// then
assertThat(response.statusCode()).isEqualTo(BAD_REQUEST.value());
}

@Nested
class 댓글_수정 {

Expand Down

0 comments on commit ab44930

Please sign in to comment.