diff --git a/backend/.gitignore b/backend/.gitignore index 31e6907a9..ae484e0c4 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -4,4 +4,4 @@ build/ !**/src/main/**/build/ !**/src/test/**/build/ .idea - +out diff --git a/backend/build.gradle b/backend/build.gradle index 1da24c5b0..f3b0b606e 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -24,18 +24,24 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + // web implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' + + // lombok compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + + // database + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2' runtimeOnly 'com.mysql:mysql-connector-j' - annotationProcessor 'org.projectlombok:lombok' + // test testImplementation 'io.rest-assured:rest-assured:5.3.1' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - - implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' } tasks.named('test') { diff --git a/backend/src/main/java/mouda/backend/exception/GlobalExceptionHandler.java b/backend/src/main/java/mouda/backend/exception/GlobalExceptionHandler.java index 35dff91a0..94ed81d0a 100644 --- a/backend/src/main/java/mouda/backend/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/mouda/backend/exception/GlobalExceptionHandler.java @@ -1,14 +1,26 @@ package mouda.backend.exception; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +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.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @RestControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { + @Override + protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException exception, + HttpHeaders headers, HttpStatusCode status, WebRequest request) { + String error = exception.getBindingResult().getFieldErrors().get(0).getDefaultMessage(); + + return ResponseEntity.badRequest().body(new ErrorResponse(error)); + } + @ExceptionHandler(MoudaException.class) public ResponseEntity handleMoudaException(MoudaException exception) { return ResponseEntity.status(exception.getHttpStatus()).body(new ErrorResponse(exception.getMessage())); diff --git a/backend/src/main/java/mouda/backend/member/domain/Member.java b/backend/src/main/java/mouda/backend/member/domain/Member.java index ad183d9f9..0a0aedcc7 100644 --- a/backend/src/main/java/mouda/backend/member/domain/Member.java +++ b/backend/src/main/java/mouda/backend/member/domain/Member.java @@ -1,5 +1,7 @@ package mouda.backend.member.domain; +import org.springframework.http.HttpStatus; + import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -10,12 +12,16 @@ import lombok.Getter; import lombok.NoArgsConstructor; import mouda.backend.moim.domain.Moim; +import mouda.backend.moim.exception.MoimErrorMessage; +import mouda.backend.moim.exception.MoimException; @Entity @Getter @NoArgsConstructor public class Member { + private static final int NICKNAME_MAX_LENGTH = 10; + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -27,9 +33,19 @@ public class Member { @Builder public Member(String nickname) { + validateNickname(nickname); this.nickname = nickname; } + private void validateNickname(String nickname) { + if (nickname.isBlank()) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MEMBER_NICKNAME_NOT_EXISTS); + } + if (nickname.length() >= NICKNAME_MAX_LENGTH) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MEMBER_NICKNAME_TOO_LONG); + } + } + public void joinMoim(Moim moim) { this.moim = moim; } diff --git a/backend/src/main/java/mouda/backend/moim/controller/MoimController.java b/backend/src/main/java/mouda/backend/moim/controller/MoimController.java index c56a535c0..1fd5082ff 100644 --- a/backend/src/main/java/mouda/backend/moim/controller/MoimController.java +++ b/backend/src/main/java/mouda/backend/moim/controller/MoimController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import mouda.backend.common.RestResponse; import mouda.backend.moim.domain.Moim; @@ -27,7 +28,7 @@ public class MoimController implements MoimSwagger { @Override @PostMapping - public ResponseEntity> createMoim(@RequestBody MoimCreateRequest moimCreateRequest) { + public ResponseEntity> createMoim(@Valid @RequestBody MoimCreateRequest moimCreateRequest) { Moim moim = moimService.createMoim(moimCreateRequest); return ResponseEntity.ok().body(new RestResponse<>(moim.getId())); @@ -43,7 +44,7 @@ public ResponseEntity> findAllMoim() { @Override @GetMapping("/{moimId}") - public ResponseEntity> findMoimDetails(@PathVariable Long moimId) { + public ResponseEntity> findMoimDetails(@PathVariable("moimId") Long moimId) { MoimDetailsFindResponse moimDetailsFindResponse = moimService.findMoimDetails(moimId); return ResponseEntity.ok().body(new RestResponse<>(moimDetailsFindResponse)); diff --git a/backend/src/main/java/mouda/backend/moim/domain/Moim.java b/backend/src/main/java/mouda/backend/moim/domain/Moim.java index 617469712..18e589638 100644 --- a/backend/src/main/java/mouda/backend/moim/domain/Moim.java +++ b/backend/src/main/java/mouda/backend/moim/domain/Moim.java @@ -1,10 +1,12 @@ package mouda.backend.moim.domain; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.LocalTime; import org.springframework.http.HttpStatus; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -22,18 +24,30 @@ @NoArgsConstructor public class Moim { + private static final int TITLE_MAX_LENGTH = 30; + private static final int PLACE_MAX_LENGTH = 100; + private static final int MAX_PEOPLE_LOWER_BOUND = 1; + private static final int MAX_PEOPLE_UPPER_BOUND = 99; + private static final int AUTHOR_NICKNAME_MAX_LENGTH = 10; + private static final int DESCRIPTION_MAX_LENGTH = 1000; + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(nullable = false) private String title; + @Column(nullable = false) private LocalDate date; + @Column(nullable = false) private LocalTime time; + @Column(nullable = false) private String place; + @Column(nullable = false) private int maxPeople; private String description; @@ -52,6 +66,14 @@ public Moim( int maxPeople, String description ) { + validateTitle(title); + validateDate(date); + validateTime(time); + validateMoimIsFuture(date, time); + validatePlace(place); + validateMaxPeople(maxPeople); + validateDescription(description); + this.title = title; this.date = date; this.time = time; @@ -60,6 +82,58 @@ public Moim( this.description = description; } + private void validateTitle(String title) { + if (title.isBlank()) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.TITLE_NOT_EXIST); + } + if (title.length() > TITLE_MAX_LENGTH) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.TITLE_TOO_LONG); + } + } + + private void validateDate(LocalDate date) { + if (date == null) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.DATE_NOT_EXIST); + } + } + + private void validateTime(LocalTime time) { + if (time == null) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.TIME_NOT_EXIST); + } + } + + private void validateMoimIsFuture(LocalDate date, LocalTime time) { + LocalDateTime moimDateTime = LocalDateTime.of(date, time); + if (moimDateTime.isBefore(LocalDateTime.now())) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.PAST_DATE_TIME); + } + } + + private void validatePlace(String place) { + if (place.isBlank()) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.PLACE_NOT_EXIST); + } + if (place.length() > PLACE_MAX_LENGTH) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.PLACE_TOO_LONG); + } + } + + private void validateMaxPeople(int maxPeople) { + if (maxPeople < MAX_PEOPLE_LOWER_BOUND) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MAX_PEOPLE_IS_POSITIVE); + } + if (maxPeople > MAX_PEOPLE_UPPER_BOUND) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MAX_PEOPLE_TOO_MANY); + } + } + + private void validateDescription(String description) { + if (description != null && description.length() > DESCRIPTION_MAX_LENGTH) { + throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.DESCRIPTION_TOO_LONG); + } + } + public void validateAlreadyFullMoim(int currentPeople) { if (currentPeople > maxPeople) { throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MAX_PEOPLE); diff --git a/backend/src/main/java/mouda/backend/moim/dto/request/MoimCreateRequest.java b/backend/src/main/java/mouda/backend/moim/dto/request/MoimCreateRequest.java index 800d578f2..6e4424835 100644 --- a/backend/src/main/java/mouda/backend/moim/dto/request/MoimCreateRequest.java +++ b/backend/src/main/java/mouda/backend/moim/dto/request/MoimCreateRequest.java @@ -3,15 +3,29 @@ import java.time.LocalDate; import java.time.LocalTime; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import mouda.backend.moim.domain.Moim; public record MoimCreateRequest( + @NotBlank String title, + + @NotNull LocalDate date, + + @NotNull LocalTime time, + + @NotBlank String place, + + @NotNull Integer maxPeople, + + @NotBlank String authorNickname, + String description ) { diff --git a/backend/src/main/java/mouda/backend/moim/dto/request/MoimJoinRequest.java b/backend/src/main/java/mouda/backend/moim/dto/request/MoimJoinRequest.java index 54b80007b..2239695bf 100644 --- a/backend/src/main/java/mouda/backend/moim/dto/request/MoimJoinRequest.java +++ b/backend/src/main/java/mouda/backend/moim/dto/request/MoimJoinRequest.java @@ -1,7 +1,15 @@ package mouda.backend.moim.dto.request; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; + public record MoimJoinRequest( + @NotNull + @Positive Long moimId, + + @NotBlank String nickname ) { } diff --git a/backend/src/main/java/mouda/backend/moim/exception/MoimErrorMessage.java b/backend/src/main/java/mouda/backend/moim/exception/MoimErrorMessage.java index d62d84e87..f2d9c97ef 100644 --- a/backend/src/main/java/mouda/backend/moim/exception/MoimErrorMessage.java +++ b/backend/src/main/java/mouda/backend/moim/exception/MoimErrorMessage.java @@ -9,7 +9,20 @@ public enum MoimErrorMessage { NOT_FOUND("모임이 존재하지 않습니다."), MAX_PEOPLE("모임 최대 인원 수를 초과합니다."), - ; + PAST_DATE_TIME("모임 날짜를 현재 시점 이후로 입력해주세요."), + TITLE_NOT_EXIST("모임 제목을 입력해주세요."), + TITLE_TOO_LONG("모임 제목을 조금 더 짧게 입력해주세요."), + DATE_NOT_EXIST("모임 날짜를 입력해주세요."), + TIME_NOT_EXIST("모임 시간을 입력해주세요."), + PLACE_NOT_EXIST("모임 장소를 입력해주세요."), + PLACE_TOO_LONG("모임 장소를 조금 더 짧게 입력해주세요."), + MAX_PEOPLE_IS_POSITIVE("모임 최대 인원은 양수여야 합니다."), + MAX_PEOPLE_TOO_MANY("모임 최대 인원을 조금 더 적게 입력해주세요."), + AUTHOR_NICKNAME_NOT_EXIST("모임 생성자 닉네임을 입력해주세요."), + AUTHOR_NICKNAME_TOO_LONG("모임 생성자 이름을 조금 더 짧게 입력해주세요."), + DESCRIPTION_TOO_LONG("모임 설명을 조금 더 짧게 입력해주세요."), + MEMBER_NICKNAME_NOT_EXISTS("모임 참여자 닉네임을 입력해주세요."), + MEMBER_NICKNAME_TOO_LONG("모임 참여자 닉네임을 조금 더 짧게 입력해주세요."); private final String message; } diff --git a/backend/src/test/java/mouda/backend/config/JacksonConfigTest.java b/backend/src/test/java/mouda/backend/config/JacksonConfigTest.java index e064d7983..741360d35 100644 --- a/backend/src/test/java/mouda/backend/config/JacksonConfigTest.java +++ b/backend/src/test/java/mouda/backend/config/JacksonConfigTest.java @@ -4,6 +4,7 @@ import java.time.LocalDate; import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.Map; import org.junit.jupiter.api.AfterEach; @@ -24,6 +25,8 @@ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class JacksonConfigTest { + private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + @Autowired private MoimRepository moimRepository; @@ -50,9 +53,11 @@ class DateFormatTest { @DisplayName("yyyy-MM-dd 형식의 날짜를 역직렬화한다.") @Test void deserialize() { + LocalDate date = LocalDate.now().plusDays(1); + Map params = Map.of( "title", "title", - "date", "2024-07-19", + "date", date.format(formatter), "time", "12:30", "place", "place", "maxPeople", 10, @@ -90,9 +95,10 @@ void deserialize_when_invalidDateFormat() { @DisplayName("날짜는 yyyy-MM-dd 형식으로 직렬화된다.") @Test void serialize() { + LocalDate date = LocalDate.now().plusDays(1); Moim moim = Moim.builder() .title("title") - .date(LocalDate.parse("2024-07-19")) + .date(date) .time(LocalTime.parse("12:30")) .place("place") .maxPeople(10) @@ -104,7 +110,7 @@ void serialize() { RestAssured.given() .when().get("/v1/moim/" + saved.getId()) .then().statusCode(is(HttpStatus.OK.value())) - .body("data.date", is("2024-07-19")); + .body("data.date", is(date.format(formatter))); } } @@ -115,9 +121,11 @@ class TimeFormatTest { @DisplayName("HH:mm 형식의 시간을 역직렬화한다.") @Test void deserialize() { + LocalDate date = LocalDate.now().plusDays(1); + Map params = Map.of( "title", "title", - "date", "2024-07-19", + "date", date.format(formatter), "time", "12:30", "place", "place", "maxPeople", 10, @@ -135,9 +143,11 @@ void deserialize() { @DisplayName("HH:mm 형식이 아닌 시간이 입력되면 예외가 발생한다.") @Test void deserialize_when_invalidTimeFormat() { + LocalDate date = LocalDate.now().plusDays(1); + Map params = Map.of( "title", "title", - "date", "2024-07-19", + "date", date.format(formatter), "time", "12-30", "place", "place", "maxPeople", 10, @@ -155,9 +165,11 @@ void deserialize_when_invalidTimeFormat() { @DisplayName("시간은 HH:mm 형식으로 직렬화된다.") @Test void serialize() { + LocalDate date = LocalDate.now().plusDays(1); + Moim moim = Moim.builder() .title("title") - .date(LocalDate.now().plusDays(1)) + .date(date) .time(LocalTime.parse("12:30")) .place("place") .maxPeople(10) diff --git a/backend/src/test/java/mouda/backend/member/domain/MemberTest.java b/backend/src/test/java/mouda/backend/member/domain/MemberTest.java new file mode 100644 index 000000000..1ca75ae11 --- /dev/null +++ b/backend/src/test/java/mouda/backend/member/domain/MemberTest.java @@ -0,0 +1,38 @@ +package mouda.backend.member.domain; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import mouda.backend.moim.exception.MoimException; + +class MemberTest { + private static final String NICNKAME = "안나"; + + @DisplayName("회원을 정상적으로 생성한다.") + @Test + void createMember() { + assertDoesNotThrow(() -> Member.builder() + .nickname(NICNKAME) + .build()); + } + + @DisplayName("닉네임을 입력하지 않아 회원 생성에 실패한다.") + @Test + void failToCreateMemberWhenNicknameIsBlank() { + Assertions.assertThrows(MoimException.class, () -> Member.builder() + .nickname("") + .build()); + } + + @DisplayName("닉네임이 제한 길이를 초과하여 회원 생성에 실패한다.") + @Test + void failToCreateMemberWhenNicknameIsTooLong() { + String longNickname = "a".repeat(11); + Assertions.assertThrows(MoimException.class, () -> Member.builder() + .nickname(longNickname) + .build()); + } +} diff --git a/backend/src/test/java/mouda/backend/member/repository/MemberRepositoryTest.java b/backend/src/test/java/mouda/backend/member/repository/MemberRepositoryTest.java index 7128727e5..f0a78e8fe 100644 --- a/backend/src/test/java/mouda/backend/member/repository/MemberRepositoryTest.java +++ b/backend/src/test/java/mouda/backend/member/repository/MemberRepositoryTest.java @@ -1,5 +1,7 @@ package mouda.backend.member.repository; +import java.time.LocalDate; +import java.time.LocalTime; import java.util.List; import org.assertj.core.api.Assertions; @@ -36,6 +38,12 @@ void cleanUp() { void findNickNamesByMoimId() { Member member = new Member("tehah"); Moim moim = Moim.builder() + .title("모임 제목") + .date(LocalDate.now().plusDays(1)) + .time(LocalTime.now().minusHours(1)) + .place("서울시 강북구 중앙로 2길 25") + .maxPeople(10) + .description("모임 설명입니다.") .build(); Moim saveMoim = moimRepository.save(moim); member.joinMoim(saveMoim); diff --git a/backend/src/test/java/mouda/backend/moim/domain/MoimTest.java b/backend/src/test/java/mouda/backend/moim/domain/MoimTest.java new file mode 100644 index 000000000..dc2558e10 --- /dev/null +++ b/backend/src/test/java/mouda/backend/moim/domain/MoimTest.java @@ -0,0 +1,195 @@ +package mouda.backend.moim.domain; + +import static org.junit.jupiter.api.Assertions.*; + +import java.time.LocalDate; +import java.time.LocalTime; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import mouda.backend.moim.exception.MoimException; + +class MoimTest { + + private static final String TITLE = "이번 주에 축구하실 분 구함"; + private static final LocalDate DATE = LocalDate.now().plusDays(1); + private static final LocalTime TIME = LocalTime.now().plusHours(1); + private static final String PLACE = "서울시 동작구 강원대로 10길 5"; + private static final int MAX_PEOPLE = 11; + private static final String AUTHOR_NICKNAME = "안나"; + private static final String DESCRIPTION = "이번 주 금요일에 퇴근하고 축구하실 분 계신가요? 끝나고 치맥도 할 생각입니다."; + + @DisplayName("모임 객체를 정상적으로 생성한다.") + @Test + void createMoim() { + Assertions.assertDoesNotThrow(() -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("제목 길이가 제한을 초과하면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenTitleIsTooLong() { + String longTitle = "a".repeat(31); + assertThrows(MoimException.class, () -> Moim.builder() + .title(longTitle) + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("제목이 빈 문자열이면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenTitleDoesNotExists() { + assertThrows(MoimException.class, () -> Moim.builder() + .title("") + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("날짜가 null이면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenDateIsNull() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(null) + .time(TIME) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("시간이 null이면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenTimeIsNull() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(null) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("모임 날짜가 현재보다 과거이면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenDateIsPast() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(LocalDate.now().minusDays(1)) + .time(LocalTime.now().plusHours(1)) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("모임 시간이 현재보다 과거이면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenTimeIsPast() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(LocalDate.now()) + .time(LocalTime.now().minusHours(1)) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("장소가 빈 문자열이면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenPlaceIsBlank() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(LocalDate.now()) + .time(LocalTime.now().minusHours(1)) + .place("") + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("장소 길이가 제한을 초과하면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenPlaceIsTooLong() { + String longPlace = "a".repeat(101); + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(TIME) + .place(longPlace) + .maxPeople(MAX_PEOPLE) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("모임 최대 인원이 0보다 작으면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenMaxPeopleIsTooSmall() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(-1) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("모임 최대 인원이 제한을 초과하면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenMaxPeopleIsTooMany() { + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(100) + .description(DESCRIPTION) + .build()); + } + + @DisplayName("설명이 null이면 모임 객체 생성에 성공한다.") + @Test + void createMoimWhenDescriptionIsNull() { + assertDoesNotThrow(() -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(null) + .build()); + } + + @DisplayName("설명의 길이가 길면 모임 객체 생성에 실패한다.") + @Test + void failToCreateMoimWhenDescriptionIsTooLong() { + String longDescription = "a".repeat(1001); + assertThrows(MoimException.class, () -> Moim.builder() + .title(TITLE) + .date(DATE) + .time(TIME) + .place(PLACE) + .maxPeople(MAX_PEOPLE) + .description(longDescription) + .build()); + } +} diff --git a/backend/src/test/java/mouda/backend/moim/service/MoimServiceTest.java b/backend/src/test/java/mouda/backend/moim/service/MoimServiceTest.java index a72f750d4..1174b9560 100644 --- a/backend/src/test/java/mouda/backend/moim/service/MoimServiceTest.java +++ b/backend/src/test/java/mouda/backend/moim/service/MoimServiceTest.java @@ -49,7 +49,7 @@ void cleanUp() { @Test void createMoim() { MoimCreateRequest moimCreateRequest = new MoimCreateRequest( - "title", LocalDate.now(), LocalTime.now(), "place", + "title", LocalDate.now().plusDays(1), LocalTime.now(), "place", 10, "안나", "설명" ); @@ -62,7 +62,7 @@ void createMoim() { @Test void findAllMoim() { MoimCreateRequest moimCreateRequest = new MoimCreateRequest( - "title", LocalDate.now(), LocalTime.now(), "place", + "title", LocalDate.now().plusDays(1), LocalTime.now(), "place", 10, "안나", "설명" ); moimService.createMoim(moimCreateRequest); @@ -77,7 +77,7 @@ void findAllMoim() { @Test void findMoimDetails() { MoimCreateRequest moimCreateRequest = new MoimCreateRequest( - "title", LocalDate.now(), LocalTime.now(), "place", + "title", LocalDate.now().plusDays(1), LocalTime.now(), "place", 10, "안나", "설명" ); moimService.createMoim(moimCreateRequest); @@ -91,7 +91,7 @@ void findMoimDetails() { @Test void joinMoim() { MoimCreateRequest moimCreateRequest = new MoimCreateRequest( - "title", LocalDate.now(), LocalTime.now(), "place", + "title", LocalDate.now().plusDays(1), LocalTime.now(), "place", 10, "안나", "설명" ); Moim moim = moimService.createMoim(moimCreateRequest); @@ -109,7 +109,7 @@ void joinMoim() { @Test void deleteMoim() { MoimCreateRequest moimCreateRequest = new MoimCreateRequest( - "title", LocalDate.now(), LocalTime.now(), "place", + "title", LocalDate.now().plusDays(1), LocalTime.now(), "place", 10, "안나", "설명" ); moimService.createMoim(moimCreateRequest); @@ -124,7 +124,7 @@ void deleteMoim() { @Test void failToJoinMoimWhenExceedMaxPeople() { MoimCreateRequest moimCreateRequest = new MoimCreateRequest( - "title", LocalDate.now(), LocalTime.now(), "place", + "title", LocalDate.now().plusDays(1), LocalTime.now(), "place", 2, "안나", "설명" );