Skip to content

Commit

Permalink
refactor: 행사 생성 API 예외 메세지 및 DTO 검증 애너테이션 추가 (#103)
Browse files Browse the repository at this point in the history
* refactor: 행사 생성 API 예외 메세지 추가 및 DTO 검증 애너테이션 추가

* refactor: 행사 생성 API EventSaveRequest name을 eventName으로 변경

* refactor: 행사 생성 요청 객체 검증 애너테이션 @SiZe 제거

* refactor: 연속된 공백 검증 및 예외 메세지 상수 사용하도록 리팩터링
  • Loading branch information
kunsanglee authored Jul 25, 2024
1 parent 2610de8 commit 3e2c5b0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
26 changes: 26 additions & 0 deletions server/src/main/java/server/haengdong/domain/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import server.haengdong.exception.HaengdongErrorCode;
import server.haengdong.exception.HaengdongException;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class Event {

private static final int MIN_NAME_LENGTH = 2;
private static final int MAX_NAME_LENGTH = 20;
private static final String SPACES = " ";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -22,7 +28,27 @@ public class Event {
private String token;

public Event(String name, String token) {
validateName(name);
this.name = name;
this.token = token;
}

private void validateName(String name) {
int nameLength = name.length();
if (nameLength < MIN_NAME_LENGTH || MAX_NAME_LENGTH < nameLength) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
String.format("행사 이름은 %d자 이상 %d자 이하만 입력 가능합니다. 입력한 이름 길이 : %d",
MIN_NAME_LENGTH,
MAX_NAME_LENGTH,
name.length()));
}
if (isBlankContinuous(name)) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
String.format("행사 이름에는 공백 문자가 연속될 수 없습니다. 입력한 이름 : %s", name));
}
}

private boolean isBlankContinuous(String name) {
return name.contains(SPACES);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package server.haengdong.presentation;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -19,7 +20,7 @@ public class EventController {
private final EventService eventService;

@PostMapping("/api/events")
public ResponseEntity<EventResponse> saveEvent(@RequestBody EventSaveRequest request) {
public ResponseEntity<EventResponse> saveEvent(@Valid @RequestBody EventSaveRequest request) {
EventResponse eventResponse = EventResponse.of(eventService.saveEvent(request.toAppRequest()));

return ResponseEntity.ok(eventResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package server.haengdong.presentation.request;

import jakarta.validation.constraints.NotBlank;
import server.haengdong.application.request.EventAppRequest;

public record EventSaveRequest(String name) {
public record EventSaveRequest(

@NotBlank
String eventName
) {

public EventAppRequest toAppRequest() {
return new EventAppRequest(name);
return new EventAppRequest(eventName);
}
}
37 changes: 37 additions & 0 deletions server/src/test/java/server/haengdong/domain/event/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package server.haengdong.domain.event;

import static org.assertj.core.api.Assertions.assertThatCode;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import server.haengdong.exception.HaengdongException;

class EventTest {

@DisplayName("공백 문자가 연속되지 않고, 이름이 2자 이상 20자 이하인 행사를 생성하면 예외가 발생하지 않는다.")
@ParameterizedTest
@ValueSource(strings = {"12", "12345678901234567890", "공 백", " 공백", "공백 ", " 공 백 "})
void createSuccessTest(String eventName) {
assertThatCode(() -> new Event(eventName, "TEST_TOKEN"))
.doesNotThrowAnyException();
}

@DisplayName("공백 문자가 연속되면 예외가 발생한다.")
@ParameterizedTest
@ValueSource(strings = {" 공백", "공백 ", "공백 연속", "공 백"})
void createFailTest1(String eventName) {
assertThatCode(() -> new Event(eventName, "TEST_TOKEN"))
.isInstanceOf(HaengdongException.class)
.hasMessage(String.format("행사 이름에는 공백 문자가 연속될 수 없습니다. 입력한 이름 : %s", eventName));
}

@DisplayName("이름이 2자 미만이거나 20자 초과인 경우 예외가 발생한다.")
@ParameterizedTest
@ValueSource(strings = {"", " ", "123456789012345678901"})
void createFilTest2(String eventName) {
assertThatCode(() -> new Event(eventName, "TEST_TOKEN"))
.isInstanceOf(HaengdongException.class)
.hasMessage(String.format("행사 이름은 2자 이상 20자 이하만 입력 가능합니다. 입력한 이름 길이 : %d", eventName.length()));
}
}

0 comments on commit 3e2c5b0

Please sign in to comment.