Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 행사 생성 API 예외 메세지 및 DTO 검증 애너테이션 추가 #103

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c: BillActionController에서는 @Valid를 뒤에 썼는데, @Valid@RequestBody 순서 어떻게 할지도 정해야겠네요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Valid @RequestBody 순서로 반영했습니다.

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()));
}
}
Loading