From b0e9977f3d0c445f945bd9000e165c01f5e4bedf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=99=8D=EC=8B=9D?= <90020593+Daolove0323@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:29:49 +0900 Subject: [PATCH] [Weekly/11] Image/delete (#114) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 해시태그 네이밍 통일 * feat: 이벤트 응답 수정 * feat: testData 수정 --- .../AdvertisementImageService.java | 2 +- .../Image/application/ImageService.java | 7 +- .../Image/persist/AdvertisementImage.java | 4 +- .../wouldyouin/_common/error/ErrorCode.java | 2 + .../_common/error/GlobalExceptionHandler.java | 10 + .../wouldyouin/_common/vo/Location.java | 18 +- .../api/dto/CurationCreateRequest.java | 4 +- .../curation/api/dto/CurationEditRequest.java | 2 +- .../curation/api/dto/CurationResponse.java | 4 +- .../wouldyouin/curation/persist/Curation.java | 12 +- .../event/api/dto/EventCreateRequest.java | 2 + .../event/api/dto/EventEditRequest.java | 2 + .../event/api/dto/EventResponse.java | 6 +- .../event/api/dto/EventSliceResponse.java | 6 +- .../event/application/EventService.java | 24 +- .../cokaen/wouldyouin/_global/TestData.java | 295 +----------------- .../cokaen/wouldyouin/_global/TestUtil.java | 1 - .../_global/mockMember/WithMockCurator.java | 4 +- .../_global/mockMember/WithMockHost.java | 4 +- .../_global/mockMember/WithMockMember.java | 4 +- .../mockMember/WithMockWelcomeMember.java | 4 +- .../_global/testdata/CurationData.java | 108 +++++++ .../_global/testdata/ImageData.java | 135 ++++++++ .../_global/testdata/MemberData.java | 101 ++++++ .../curation/CurationControllerUnitTest.java | 77 ++--- .../curation/CurationServiceTest.java | 111 +++++++ .../event/EventControllerUnitTest.java | 17 +- .../wouldyouin/image/ImageControllerTest.java | 122 +++++--- .../BaseMemberServiceUnitTest.java | 16 +- .../application/CuratorServiceUnitTest.java | 9 +- .../application/HostServiceUnitTest.java | 6 +- .../application/MemberServiceUnitTest.java | 8 +- .../ReservationControllerUnitTest.java | 22 +- 33 files changed, 716 insertions(+), 433 deletions(-) create mode 100644 src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/CurationData.java create mode 100644 src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/ImageData.java create mode 100644 src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/MemberData.java create mode 100644 src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationServiceTest.java diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java index ad8308a1..ae49285c 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java @@ -39,7 +39,7 @@ protected String getChildPath() { @Override protected AdvertisementImage toEntity(ImageRequest imageRequest) { return AdvertisementImage.builder() - .name(imageRequest.getUrl()) + .url(imageRequest.getUrl()) .size(imageRequest.getSize()) .extension(imageRequest.getExtension()) .build(); diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java index a056d22d..be55e1a5 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java @@ -53,7 +53,12 @@ public T getById(Long id) { } protected ImageResponse create(ImageRequest imageRequest) { - return ImageResponse.from(getImageRepository().save(toEntity(imageRequest)), UriUtil.assembleFullUrl(apiUrl, getChildPath())); + T image = toEntity(imageRequest); + return ImageResponse.from(getImageRepository().save(image), getImageUrl(image)); + } + + public String getImageUrl(T image) { + return UriUtil.assembleFullUrl(apiUrl, getChildPath(), image.getName()); } protected void delete(Long id) { diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/AdvertisementImage.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/AdvertisementImage.java index 2fd5c836..5c01ffd3 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/AdvertisementImage.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/AdvertisementImage.java @@ -19,8 +19,8 @@ public class AdvertisementImage extends Image { private Advertisement advertisement; @Builder - public AdvertisementImage(String name, Long size, String extension, Advertisement advertisement) { - super(name, size, extension); + public AdvertisementImage(String url, Long size, String extension, Advertisement advertisement) { + super(url, size, extension); this.advertisement = advertisement; } } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/ErrorCode.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/ErrorCode.java index 6e5f7737..47e4e115 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/ErrorCode.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/ErrorCode.java @@ -28,6 +28,8 @@ public enum ErrorCode { URL_PARSING_FAILED(HttpStatus.BAD_REQUEST.value(), "-20400", "URL 파싱에 실패했습니다."), + INVALID_IMAGE_DOMAIN(HttpStatus.BAD_REQUEST.value(), "-20400", "해당 이미지 도메인에 대한 서비스가 존재하지 않습니다."), + CURRENT_LOCATION_EMPTY(HttpStatus.BAD_REQUEST.value(), "-20400", "현재 위치 정보를 찾을 수 없습니다."); private final Integer status; diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/GlobalExceptionHandler.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/GlobalExceptionHandler.java index 4c62df26..185ba406 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/GlobalExceptionHandler.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/error/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ import java.util.stream.Collectors; +import org.ktc2.cokaen.wouldyouin.Image.api.ImageDomain; import org.ktc2.cokaen.wouldyouin._common.api.ApiResponse; import org.ktc2.cokaen.wouldyouin._common.api.ApiResponseBody; import org.ktc2.cokaen.wouldyouin._common.exception.BusinessException; @@ -10,6 +11,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; @ControllerAdvice public class GlobalExceptionHandler { @@ -19,6 +21,14 @@ public ResponseEntity> handleBusinessException(BusinessExc return ApiResponse.error(e.getErrorCode(), e.getMessage()); } + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + public ResponseEntity> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { + if (ex.getRequiredType() == ImageDomain.class) { + return ApiResponse.error(ErrorCode.INVALID_IMAGE_DOMAIN, "해당 이미지 도메인에 대한 서비스가 존재하지 않습니다."); + } + return ApiResponse.error(ErrorCode.INVALID_INPUT_VALUE, "잘못된 요청 값입니다."); + } + @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { return ApiResponse.error(ErrorCode.INVALID_INPUT_VALUE, e.getBindingResult().getFieldErrors().stream() diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/vo/Location.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/vo/Location.java index 459f2f7b..ad551893 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/vo/Location.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/vo/Location.java @@ -1,6 +1,10 @@ package org.ktc2.cokaen.wouldyouin._common.vo; import jakarta.persistence.Embeddable; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -12,7 +16,17 @@ @AllArgsConstructor @Embeddable public class Location { - Double longitude; - Double latitude; + + @NotNull(message = "위도 값은 필수입니다.") + @Min(value = -90, message = "위도는 -90 이상이어야 합니다.") + @Max(value = 90, message = "위도는 90 이하여야 합니다.") + private Double latitude; + + @NotNull(message = "경도 값은 필수입니다.") + @Min(value = -180, message = "경도는 -180 이상이어야 합니다.") + @Max(value = 180, message = "경도는 180 이하여야 합니다.") + private Double longitude; + + @NotEmpty(message = "상세 주소는 필수입니다.") String detailAddress; } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCreateRequest.java b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCreateRequest.java index 5000fcec..f4f21f45 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCreateRequest.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCreateRequest.java @@ -30,7 +30,7 @@ public class CurationCreateRequest { @NotNull(message = "지역은 필수입니다.") private Area area; - private List hashTags; + private List hashtags; private List eventIds; @@ -49,7 +49,7 @@ public Curation toEntity(Curator curator, List curationCards, List .content(this.content) .curationCards(curationCards) .area(this.area) - .hashTags(this.hashTags) + .hashtags(this.hashtags) .events(events) .build(); } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationEditRequest.java b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationEditRequest.java index 1d4051d5..a113f851 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationEditRequest.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationEditRequest.java @@ -26,7 +26,7 @@ public class CurationEditRequest { @NotNull(message = "지역은 필수입니다.") private Area area; - private List hashTags; + private List hashtags; private List eventIds; diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationResponse.java index 98675d27..7277ed77 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationResponse.java @@ -21,8 +21,8 @@ public class CurationResponse { private final Area area; private final List hashTag; private final List eventsInfo; - private LocalDateTime modifiedDate; private final LocalDateTime createdTime; + private final LocalDateTime modifiedDate; public static CurationResponse from(Curation curation) { return CurationResponse.builder() @@ -33,7 +33,7 @@ public static CurationResponse from(Curation curation) { .curationCards(curation.getCurationCards().stream() .map(CurationCardResponse::from).toList()) .area(curation.getArea()) - .hashTag(curation.getHashTags()) + .hashTag(curation.getHashtags()) .eventsInfo(curation.getEvents().stream() .map(CurationEventResponse::from).toList()) .createdTime(curation.getCreatedDate()) diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/persist/Curation.java b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/persist/Curation.java index 216ca700..e8d86244 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/persist/Curation.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/persist/Curation.java @@ -60,7 +60,7 @@ public class Curation { private String content; @OneToMany(mappedBy = "curation", fetch = FetchType.LAZY) - private List curationCards = new ArrayList<>(); + private List curationCards; @NotNull @Enumerated(EnumType.STRING) @@ -69,7 +69,7 @@ public class Curation { @Column(name = "hashtag") @Convert(converter = HashtagConverter.class) - private List hashTags = new ArrayList<>(); + private List hashtags; @ManyToMany(fetch = FetchType.LAZY) @JoinTable( @@ -77,7 +77,7 @@ public class Curation { joinColumns = @JoinColumn(name = "curation_id"), inverseJoinColumns = @JoinColumn(name = "event_id") ) - private List events = new ArrayList<>(); + private List events; @CreatedDate @Column(name = "created_date") @@ -88,14 +88,14 @@ public class Curation { private LocalDateTime modifiedDate; @Builder - public Curation(Curator curator, String title, String content, List curationCards, Area area, List hashTags, + public Curation(Curator curator, String title, String content, List curationCards, Area area, List hashtags, List events) { this.curator = curator; this.title = title; this.content = content; Optional.ofNullable(curationCards).ifPresent(this::setCurationCards); this.area = area; - Optional.ofNullable(hashTags).ifPresent(this::setHashTags); + Optional.ofNullable(hashtags).ifPresent(this::setHashtags); Optional.ofNullable(events).ifPresent(this::setEvents); } @@ -103,7 +103,7 @@ public void updateFrom(CurationEditRequest curationEditRequest, List images; private EventHostResponse host; private Area area; private Location location; @@ -28,12 +31,13 @@ public class EventResponse { private Category category; private Boolean expired; - public static EventResponse from(Event event) { + public static EventResponse from(Event event, List imageUrls) { Host host = event.getHost(); return EventResponse.builder() .id(event.getId()) .title(event.getTitle()) .content(event.getContent()) + .images(imageUrls) .host(EventHostResponse.from(host)) .area(event.getArea()) .location(event.getLocation()) diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/EventSliceResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/EventSliceResponse.java index 5221024c..b9870f98 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/EventSliceResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/EventSliceResponse.java @@ -3,6 +3,7 @@ import java.util.List; import lombok.Builder; import lombok.Getter; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse; import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo; import org.ktc2.cokaen.wouldyouin.event.persist.Event; import org.springframework.data.domain.Slice; @@ -14,10 +15,9 @@ public class EventSliceResponse { private List events; private SliceInfo sliceInfo; - public static EventSliceResponse from(Slice reservations, int size, Long lastId) { + public static EventSliceResponse from(List events, int size, Long lastId) { return EventSliceResponse.builder() - .events(reservations.stream() - .map(EventResponse::from).toList()) + .events(events) .sliceInfo(SliceInfo.builder() .sliceSize(size) .lastId(lastId) diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java index a5c6c976..706d6472 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java @@ -2,6 +2,7 @@ import java.util.List; import lombok.RequiredArgsConstructor; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse; import org.ktc2.cokaen.wouldyouin.Image.application.EventImageService; import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage; import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException; @@ -39,7 +40,8 @@ public Event getByIdOrThrow(Long id) throws EntityNotFoundException { @Transactional(readOnly = true) public EventResponse getById(Long id) { - return EventResponse.from(getByIdOrThrow(id)); + Event event = getByIdOrThrow(id); + return EventResponse.from(event, getImageUrl(event)); } @Transactional(readOnly = true) @@ -51,14 +53,16 @@ public EventSliceResponse getAllByFilterOrderByDistanceAsc(LocationFilter locati title, category, area, pageable ); Long newLastId = getLastId(events, beforeLastId); - return EventSliceResponse.from(events, events.getSize(), newLastId); + List responses = events.stream().map(this::getEventResponse).toList(); + return EventSliceResponse.from(responses, events.getSize(), newLastId); } @Transactional(readOnly = true) public EventSliceResponse getAllByHostIdOrderByCreatedDateDesc(Long hostId, Pageable pageable, Long beforeLastId) { Slice events = eventRepository.findAllByHostIdOrderByEventIdDesc(hostId, beforeLastId, pageable); Long newLastId = getLastId(events, beforeLastId); - return EventSliceResponse.from(events, events.getSize(), newLastId); + List responses = events.stream().map(this::getEventResponse).toList(); + return EventSliceResponse.from(responses, events.getSize(), newLastId); } @Transactional @@ -68,7 +72,7 @@ public EventResponse create(Long hostId, EventCreateRequest eventCreateRequest) .map(eventImageService::getById).toList(); Event event = eventRepository.save(eventCreateRequest.toEntity(host, images)); images.forEach(image -> eventImageService.setEvent(image, event)); - return EventResponse.from(event); + return getEventResponse(event); } @Transactional @@ -80,7 +84,7 @@ public EventResponse update(Long hostId, Long eventId, EventEditRequest eventEdi .map(eventImageService::getById).toList(); event.updateFrom(eventEditRequest, images); images.forEach(image -> eventImageService.setEvent(image, event)); - return EventResponse.from(event); + return getEventResponse(event); } @Transactional @@ -110,4 +114,14 @@ public void validateHostId(Long hostId, Event event) { throw new UnauthorizedException("호스트 ID가 행사의 호스트 ID와 일치하지 않습니다."); } } + + private EventResponse getEventResponse(Event event) { + return EventResponse.from(event, getImageUrl(event)); + } + + private List getImageUrl(Event event) { + return event.getImages().stream() + .map(eventImageService::getImageUrl) + .toList(); + } } \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java index 15e07439..38f06c9f 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java @@ -1,41 +1,24 @@ package org.ktc2.cokaen.wouldyouin._global; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidReservationMemberResponse; + import java.time.LocalDateTime; import java.util.List; -import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageRequest; -import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse; -import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage; -import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage; -import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo; import org.ktc2.cokaen.wouldyouin._common.vo.Area; import org.ktc2.cokaen.wouldyouin._common.vo.Category; import org.ktc2.cokaen.wouldyouin._common.vo.Location; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardRequest; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardResponse; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationEditRequest; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationResponse; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationSliceResponse; -import org.ktc2.cokaen.wouldyouin.curation.persist.Curation; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventCreateRequest; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventEditRequest; import org.ktc2.cokaen.wouldyouin.event.api.dto.relationResonse.CurationEventResponse; import org.ktc2.cokaen.wouldyouin.event.api.dto.relationResonse.ReservationEventResponse; import org.ktc2.cokaen.wouldyouin.event.persist.Event; -import org.ktc2.cokaen.wouldyouin.member.api.dto.relation.ReservationMemberResponse; -import org.ktc2.cokaen.wouldyouin.member.api.dto.relationResponse.CurationCuratorResponse; -import org.ktc2.cokaen.wouldyouin.member.persist.AccountType; -import org.ktc2.cokaen.wouldyouin.member.persist.Curator; -import org.ktc2.cokaen.wouldyouin.member.persist.Host; -import org.ktc2.cokaen.wouldyouin.member.persist.Member; -import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; import org.ktc2.cokaen.wouldyouin.reservation.api.dto.ReservationRequest; import org.ktc2.cokaen.wouldyouin.reservation.api.dto.ReservationResponse; import org.ktc2.cokaen.wouldyouin.reservation.api.dto.ReservationSliceResponse; import org.ktc2.cokaen.wouldyouin.reservation.persist.Reservation; -import org.springframework.http.MediaType; -import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.util.ReflectionTestUtils; public class TestData { @@ -47,164 +30,6 @@ public static SliceInfo createSliceInfo() { .build(); } - // Todo: 클래스이름 Domain말고 Data는 어떤지..?, ImageDomain이 중복이라 사용할 수 없음 ㅜ - public static class ImageData { - - public static MemberImage createValidMemberImage(Long id) { - MemberImage ret = MemberImage.builder() - .url("memberImageUrl") - .size(10L) - .extension(".jpg") - .build(); - ReflectionTestUtils.setField(ret, "id", id); - return ret; - } - - public static EventImage createValidEventImage(Long id) { - EventImage ret = EventImage.builder() - .url("eventImageUrl") - .size(10L) - .extension(".jpg") - .build(); - ReflectionTestUtils.setField(ret, "id", id); - return ret; - } - - public static CurationImage createValidCurationImage(Long id) { - CurationImage ret = CurationImage.builder() - .url("curationImageUrl.jpg") - .size(10L) - .extension("jpg") - .build(); - ReflectionTestUtils.setField(ret, "id", id); - return ret; - } - - public static ImageRequest createValidImageRequest() { - return ImageRequest.builder() - .url("memberImage.png") - .size(20L) - .extension("png") - .build(); - } - - public static ImageResponse createValidImageResponse1() { - return ImageResponse.builder() - .id(1L) - .url("curationImageUrl.jpg") - .size(10L) - .extension(".jpg") - .createdDate(LocalDateTime.of(2024, 3, 23, 0, 0)) - .build(); - } - - public static ImageResponse createValidImageResponse2() { - return ImageResponse.builder() - .id(2L) - .url("memberImage.png") - .size(20L) - .extension(".png") - .createdDate(LocalDateTime.of(2024, 3, 23, 0, 0)) - .build(); - } - - public static MockMultipartFile createValidMultipartFile1() { - return new MockMultipartFile("images", "image1.jpg", MediaType.IMAGE_JPEG_VALUE, "imageData1".getBytes()); - } - - public static MockMultipartFile createValidMultipartFile2() { - return new MockMultipartFile("images", "image2.png", MediaType.IMAGE_JPEG_VALUE, "imageData2".getBytes()); - } - } - - public static class MemberDomain { - - public static final long validMemberId = 1L; - public static final long validCuratorId = 2L; - public static final long validHostId = 3L; - public static final long validWelcomeMemberId = 4L; - - public static Member createValidMember() { - MemberImage memberImage = ImageData.createValidMemberImage(validMemberId); - Member ret = Member.builder() - .accountType(AccountType.kakao) - .email("member1@example.com") - .nickname("nick_normal_123") - .phone("010-1112-2233") - .profileImage(memberImage) - .area(Area.광주) - .gender("Men") - .socialId("100100100100100") - .build(); - ReflectionTestUtils.setField(ret, "Id", validMemberId); - ReflectionTestUtils.setField(ret, "memberType", MemberType.normal); - ReflectionTestUtils.setField(memberImage, "baseMember", ret); - return ret; - } - - public static Curator createValidCurator() { - MemberImage memberImage = ImageData.createValidMemberImage(validCuratorId); - Curator ret = Curator.curatorBuilder() - .accountType(AccountType.google) - .email("curator1@example.com") - .nickname("nick_curator_12") - .phone("010-4545-6767") - .profileImage(memberImage) - .area(Area.광주) - .gender("Women") - .socialId("200200200200200") - .build(); - ReflectionTestUtils.setField(ret, "Id", validCuratorId); - ReflectionTestUtils.setField(ret, "intro", "큐레이터 자기소개입니다."); - ReflectionTestUtils.setField(ret, "hashtags", List.of("#큐레이터", "#해시태그", "#입니다")); - ReflectionTestUtils.setField(memberImage, "baseMember", ret); - return ret; - } - - public static Host createValidHost() { - MemberImage memberImage = ImageData.createValidMemberImage(validHostId); - Host ret = Host.builder() - .email("curator1@example.com") - .nickname("nick_curator_12") - .phone("010-4545-6767") - .hashedPassword("hashed_password") - .profileImage(memberImage) - .build(); - ReflectionTestUtils.setField(ret, "Id", validHostId); - ReflectionTestUtils.setField(ret, "intro", "주최자 자기소개입니다."); - ReflectionTestUtils.setField(ret, "hashtags", List.of("#주최자", "#해시태그", "#입니다")); - ReflectionTestUtils.setField(memberImage, "baseMember", ret); - return ret; - } - - public static Member createValidWelcomeMember() { - MemberImage memberImage = ImageData.createValidMemberImage(validWelcomeMemberId); - Member ret = Member.builder() - .accountType(AccountType.kakao) - .email("member2@example.com") - .nickname("nick_normal_333") - .phone("010-4414-1144") - .profileImage(memberImage) - .area(Area.서울) - .gender("Men") - .socialId("456456456456") - .build(); - ReflectionTestUtils.setField(ret, "Id", validWelcomeMemberId); - ReflectionTestUtils.setField(memberImage, "baseMember", ret); - return ret; - } - - public static ReservationMemberResponse createValidReservationMemberResponse() { - return ReservationMemberResponse.builder() - .id(validMemberId) - .email("member1@example.com") - .nickname("nick_normal_123") - .phone("010-1112-2233") - .gender("Men") - .build(); - } - } - public static class EventDomain { public static Event createValidEvent() { @@ -219,7 +44,8 @@ public static Event createValidEvent() { .totalSeat(100) .category(Category.밴드) .build(); - validEvent.setHost(MemberDomain.createValidHost()); + validEvent.setHost(createValidHost()); + ReflectionTestUtils.setField(validEvent, "id", 201L); return validEvent; } @@ -228,7 +54,7 @@ public static EventCreateRequest createValidEventCreateRequest() { .title("title") .content("content 조홍식씨 최소글자 20자라고 해놓고 안 지켰어요.") .area(Area.전체) - .location(new Location(132.0, 43.0, "광주 북구 용봉로 77")) + .location(new Location(55.0, 43.0, "광주 북구 용봉로 77")) .startTime(LocalDateTime.of(2025, 10, 1, 9, 0)) .endTime(LocalDateTime.of(2025, 10, 1, 10, 0)) .price(10000) @@ -284,7 +110,7 @@ public static ReservationRequest createValidReservationRequest() { public static Reservation createValidReservation() { Reservation reservation = Reservation.builder() - .member(MemberDomain.createValidMember()) + .member(createValidMember()) .event(EventDomain.createValidEvent()) .price(15000) .quantity(2) @@ -296,7 +122,7 @@ public static Reservation createValidReservation() { public static ReservationResponse createValidReservationResponse() { return ReservationResponse.builder() .id(1L) - .member(MemberDomain.createValidReservationMemberResponse()) + .member(createValidReservationMemberResponse()) .event(EventDomain.createValidReservationEventResponse()) .price(15000) .quantity(2) @@ -311,105 +137,4 @@ public static ReservationSliceResponse createValidReservationSliceResponse() { .build(); } } - - public static class CurationDomain { - - public static Curation createValidCuration() { - return Curation.builder() - .curator(MemberDomain.createValidCurator()) - .title("title") - .content("content") - .curationCards(List.of()) - .area(Area.전체) - .hashTags(List.of("#해시태그1", "#해시태그2")) - .events(List.of(EventDomain.createValidEvent())) - .build(); - } - - public static CurationCardRequest createValidCurationCardRequest1() { - return CurationCardRequest.builder() - .subtitle("부제목1") - .content("큐레이션 카드 내용1 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") - .imageIds(List.of(1L, 2L)) - .build(); - } - - public static CurationCardRequest createValidCurationCardRequest2() { - return CurationCardRequest.builder() - .subtitle("부제목2") - .content("큐레이션 카드 내용2 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") - .imageIds(List.of(3L, 4L)) - .build(); - } - - public static CurationCardResponse createCurationCardResponse1() { - return CurationCardResponse.builder() - .subtitle("부제목1") - .content("큐레이션 카드 내용1 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") - .imageUrls(List.of("image1.com", "image2.com")) - .build(); - } - - public static CurationCardResponse createCurationCardResponse2() { - return CurationCardResponse.builder() - .subtitle("부제목2") - .content("큐레이션 카드 내용2 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") - .imageUrls(List.of("image3.com", "image4.com")) - .build(); - } - - public static CurationCreateRequest createValidCurationCreateRequest() { - return CurationCreateRequest.builder() - .title("큐레이션 제목1") - .content("큐레이션 카드 내용1 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") - .curationCards(List.of(createValidCurationCardRequest1())) - .area(Area.광주) - .hashTags(List.of("#광주밴드", "#전남대")) - .eventIds(List.of(1L, 2L)) - .build(); - } - - public static CurationEditRequest createValidCurationEditRequest() { - return CurationEditRequest.builder() - .title("큐레이션 제목2") - .content("큐레이션 내용2 입니다.") - .curationCards(List.of(createValidCurationCardRequest2())) - .area(Area.서울) - .hashTags(List.of("#서울밴드", "#서울대")) - .eventIds(List.of(3L, 4L)) - .build(); - } - - public static CurationCuratorResponse createCurationCuratorResponse() { - Curator curator = MemberDomain.createValidCurator(); - return CurationCuratorResponse.builder() - .nickname(curator.getNickname()) - .email(curator.getEmail()) - .phone(curator.getPhone()) - .profileImageUrl(curator.getProfileImageUrl()) - .intro(curator.getIntro()) - .likes(curator.getLikes()) - .hashtags(curator.getHashtags()) - .build(); - } - - public static CurationResponse createValidCurationResponse() { - return CurationResponse.builder() - .curator(createCurationCuratorResponse()) - .title("title") - .content("content") - .curationCards(List.of(createCurationCardResponse1())) - .area(Area.전체) - .hashTag(List.of("#해시태그1", "#해시태그2")) - .eventsInfo(List.of(EventDomain.createValidCurationEventResponse())) - .build(); - } - - public static CurationSliceResponse createValidCurationSliceResponse() { - return CurationSliceResponse.builder() - .curations(List.of(createValidCurationResponse())) - .sliceInfo(TestData.createSliceInfo()) - .build(); - } - } -} +} \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java index 50cbb345..f6f35839 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java @@ -9,5 +9,4 @@ public static T getLeftOrRight(T left, T right) { public static T getOrNull(T object) { return getLeftOrRight(object, null); } - } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java index 58488b23..e07aa771 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java @@ -2,11 +2,11 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; +import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = MemberDomain.validCuratorId, memberType = MemberType.curator) +@WithMockCustomUser(memberId = MemberData.validCuratorId, memberType = MemberType.curator) public @interface WithMockCurator { } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java index 1805cbf8..2a84fd47 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java @@ -2,11 +2,11 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; +import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = MemberDomain.validHostId, memberType = MemberType.host) +@WithMockCustomUser(memberId = MemberData.validHostId, memberType = MemberType.host) public @interface WithMockHost { } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java index 0bbaf38c..6b71ff00 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java @@ -2,11 +2,11 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; +import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = MemberDomain.validMemberId, memberType = MemberType.normal) +@WithMockCustomUser(memberId = MemberData.validMemberId, memberType = MemberType.normal) public @interface WithMockMember { } \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java index 1f8e877b..4dc60ff8 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java @@ -2,11 +2,11 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; +import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = MemberDomain.validWelcomeMemberId, memberType = MemberType.welcome) +@WithMockCustomUser(memberId = MemberData.validWelcomeMemberId, memberType = MemberType.welcome) public @interface WithMockWelcomeMember { } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/CurationData.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/CurationData.java new file mode 100644 index 00000000..a32b9cb7 --- /dev/null +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/CurationData.java @@ -0,0 +1,108 @@ +package org.ktc2.cokaen.wouldyouin._global.testdata; + +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidCurator; + +import java.time.LocalDateTime; +import java.util.List; +import org.ktc2.cokaen.wouldyouin._common.vo.Area; +import org.ktc2.cokaen.wouldyouin._global.TestData; +import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; +import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardRequest; +import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardResponse; +import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; +import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationEditRequest; +import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationResponse; +import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationSliceResponse; +import org.ktc2.cokaen.wouldyouin.curation.persist.Curation; +import org.ktc2.cokaen.wouldyouin.curation.persist.CurationCard; +import org.ktc2.cokaen.wouldyouin.member.api.dto.relationResponse.CurationCuratorResponse; +import org.springframework.test.util.ReflectionTestUtils; + +public class CurationData { + + public static CurationCardRequest createValidCurationCardRequest1() { + return CurationCardRequest.builder() + .subtitle("큐레이션 카드 부제목1") + .content("큐레이션 카드 내용1 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") + .imageIds(List.of(1301L)) + .build(); + } + + public static CurationCardRequest createValidCurationCardRequest2() { + return CurationCardRequest.builder() + .subtitle("큐레이션 카드 부제목2") + .content("큐레이션 카드 내용2 입니다. 큐레이션 카드의 내용은 최소 20자 최대 1000자 입니다.") + .imageIds(List.of(1302L)) + .build(); + } + + public static CurationCard createValidCurationCard1() { + CurationCard validCurationCard1 = + createValidCurationCardRequest1().toEntity(List.of(ImageData.createValidCurationImage1())); + ReflectionTestUtils.setField(validCurationCard1, "id", 351L); +// ReflectionTestUtils.setField(validCurationCard1, "curation", createValidCuration()); + return validCurationCard1; + } + + public static CurationCard createValidCurationCard2() { + CurationCard validCurationCard2 = + createValidCurationCardRequest2().toEntity(List.of(ImageData.createValidCurationImage2())); + ReflectionTestUtils.setField(validCurationCard2, "id", 352L); +// ReflectionTestUtils.setField(validCurationCard1, "curation", createValidCuration()); + return validCurationCard2; + } + + public static CurationCardResponse createValidCurationCardResponse1() { + return CurationCardResponse.from(createValidCurationCard1()); + } + + public static CurationCardResponse createValidCurationCardResponse2() { + return CurationCardResponse.from(createValidCurationCard2()); + } + + public static CurationCreateRequest createValidCurationCreateRequest() { + return CurationCreateRequest.builder() + .title("큐레이션 제목") + .content("큐레이션 본문") + .curationCards(List.of(createValidCurationCardRequest1())) + .area(Area.전체) + .hashtags(List.of("#큐레이션", "#해시태그")) + .eventIds(List.of(201L)) + .build(); + } + + public static CurationEditRequest createValidCurationEditRequest() { + return CurationEditRequest.builder() + .title("큐레이션 제목 수정") + .content("큐레이션 본문 수정") + .curationCards(List.of(createValidCurationCardRequest2())) + .area(Area.광주) + .hashtags(List.of("수정 해시태그")) + .eventIds(List.of(202L)) + .build(); + } + + public static Curation createValidCuration() { + Curation validCuration = createValidCurationCreateRequest().toEntity( + createValidCurator(), List.of(createValidCurationCard1()), List.of(EventDomain.createValidEvent())); + ReflectionTestUtils.setField(validCuration, "id", 301L); + ReflectionTestUtils.setField(validCuration, "createdDate", LocalDateTime.of(2023, 3, 23, 0, 0)); + ReflectionTestUtils.setField(validCuration, "modifiedDate", LocalDateTime.of(2024, 3, 23, 0, 0)); + return validCuration; + } + + public static CurationCuratorResponse createValidCurationCuratorResponse() { + return CurationCuratorResponse.from(createValidCurator()); + } + + public static CurationResponse createValidCurationResponse() { + return CurationResponse.from(createValidCuration()); + } + + public static CurationSliceResponse createValidCurationSliceResponse() { + return CurationSliceResponse.builder() + .curations(List.of(createValidCurationResponse())) + .sliceInfo(TestData.createSliceInfo()) + .build(); + } +} \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/ImageData.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/ImageData.java new file mode 100644 index 00000000..11e047d6 --- /dev/null +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/ImageData.java @@ -0,0 +1,135 @@ +package org.ktc2.cokaen.wouldyouin._global.testdata; + +import java.time.LocalDateTime; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageRequest; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse; +import org.ktc2.cokaen.wouldyouin.Image.persist.AdvertisementImage; +import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage; +import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage; +import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.util.ReflectionTestUtils; + +public class ImageData { + + public static final long validMemberImageId = 1101L; + public static final long validEventImageId = 1201L; + public static final long validCurationImageId = 1301L; + public static final long validAdImageId = 1401L; + + public static MemberImage createValidMemberImage() { + MemberImage validMemberImage = MemberImage.builder() + .url("member/memberImage.jpg") + .size(10L) + .extension(".jpg") + .build(); + ReflectionTestUtils.setField(validMemberImage, "id", 1101L); +// ReflectionTestUtils.setField(validMemberImage, "baseMember", MemberDomain.createValidMember()); + return validMemberImage; + } + + public static EventImage createValidEventImage() { + EventImage validEventImage = EventImage.builder() + .url("event/eventImage.jpg") + .size(20L) + .extension(".jpg") + .build(); + ReflectionTestUtils.setField(validEventImage, "id", 1201L); +// ReflectionTestUtils.setField(validMemberImage, "baseMember", MemberDomain.createValidMember()); + return validEventImage; + } + + public static CurationImage createValidCurationImage1() { + CurationImage validCurationImage = CurationImage.builder() + .url("curation/curationImage1.jpg") + .size(30L) + .extension(".jpg") + .build(); + ReflectionTestUtils.setField(validCurationImage, "id", 1301L); +// ReflectionTestUtils.setField(validMemberImage, "baseMember", MemberDomain.createValidMember()); + return validCurationImage; + } + + public static CurationImage createValidCurationImage2() { + CurationImage validCurationImage = CurationImage.builder() + .url("curation/curationImage2.jpg") + .size(30L) + .extension(".jpg") + .build(); + ReflectionTestUtils.setField(validCurationImage, "id", 1302L); +// ReflectionTestUtils.setField(validMemberImage, "baseMember", MemberDomain.createValidMember()); + return validCurationImage; + } + + public static AdvertisementImage createValidAdImage() { + AdvertisementImage validAdImage = AdvertisementImage.builder() + .url("curation/curationImage.jpg") + .size(40L) + .extension(".jpg") + .build(); + ReflectionTestUtils.setField(validAdImage, "id", 1401L); +// ReflectionTestUtils.setField(validMemberImage, "baseMember", MemberDomain.createValidMember()); + return validAdImage; + } + + public static ImageRequest createValidMemberImageRequest() { + return ImageRequest.builder() + .url("member/memberImage.jpg") + .size(10L) + .extension(".jpg") + .build(); + } + + public static ImageRequest createValidEventImageRequest() { + return ImageRequest.builder() + .url("event/eventImage.jpg") + .size(20L) + .extension(".jpg") + .build(); + } + + public static ImageRequest createValidCurationImageRequest() { + return ImageRequest.builder() + .url("curation/curationImage.jpg") + .size(30L) + .extension(".jpg") + .build(); + } + + public static ImageRequest createValidAdImageRequest() { + return ImageRequest.builder() + .url("ad/adImage.jpg") + .size(40L) + .extension(".jpg") + .build(); + } + + public static ImageResponse createValidImageResponse1() { + return ImageResponse.builder() + .id(1L) + .url("curationImageUrl.jpg") + .size(10L) + .extension(".jpg") + .createdDate(LocalDateTime.of(2024, 3, 23, 0, 0)) + .build(); + } + + public static ImageResponse createValidImageResponse2() { + return ImageResponse.builder() + .id(2L) + .url("memberImage.png") + .size(20L) + .extension(".png") + .createdDate(LocalDateTime.of(2024, 3, 23, 0, 0)) + .build(); + } + + public static MockMultipartFile createValidMultipartFile1() { + return new MockMultipartFile("images", "image1.jpg", MediaType.IMAGE_JPEG_VALUE, "imageData1".getBytes()); + } + + public static MockMultipartFile createValidMultipartFile2() { + return new MockMultipartFile("images", "image2.png", MediaType.IMAGE_JPEG_VALUE, "imageData2".getBytes()); + } +} diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/MemberData.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/MemberData.java new file mode 100644 index 00000000..1633ce5c --- /dev/null +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/testdata/MemberData.java @@ -0,0 +1,101 @@ +package org.ktc2.cokaen.wouldyouin._global.testdata; + +import java.util.List; +import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; +import org.ktc2.cokaen.wouldyouin._common.vo.Area; +import org.ktc2.cokaen.wouldyouin.member.api.dto.relation.ReservationMemberResponse; +import org.ktc2.cokaen.wouldyouin.member.persist.AccountType; +import org.ktc2.cokaen.wouldyouin.member.persist.Curator; +import org.ktc2.cokaen.wouldyouin.member.persist.Host; +import org.ktc2.cokaen.wouldyouin.member.persist.Member; +import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; +import org.springframework.test.util.ReflectionTestUtils; + +public class MemberData { + + public static final long validMemberId = 101L; + public static final long validCuratorId = 102L; + public static final long validHostId = 103L; + public static final long validWelcomeMemberId = 104L; + public static final long validAdminId = 105L; + + public static Member createValidMember() { + MemberImage memberImage = ImageData.createValidMemberImage(); + Member ret = Member.builder() + .accountType(AccountType.kakao) + .email("member1@example.com") + .nickname("nick_normal_123") + .phone("010-1112-2233") + .profileImage(memberImage) + .area(Area.광주) + .gender("Men") + .socialId("100100100100100") + .build(); + ReflectionTestUtils.setField(ret, "Id", validMemberId); + ReflectionTestUtils.setField(ret, "memberType", MemberType.normal); + ReflectionTestUtils.setField(memberImage, "baseMember", ret); + return ret; + } + + public static Curator createValidCurator() { + MemberImage memberImage = ImageData.createValidMemberImage(); + Curator ret = Curator.curatorBuilder() + .accountType(AccountType.google) + .email("curator1@example.com") + .nickname("nick_curator_12") + .phone("010-4545-6767") + .profileImage(memberImage) + .area(Area.광주) + .gender("Women") + .socialId("200200200200200") + .build(); + ReflectionTestUtils.setField(ret, "Id", validCuratorId); + ReflectionTestUtils.setField(ret, "intro", "큐레이터 자기소개입니다."); + ReflectionTestUtils.setField(ret, "hashtags", List.of("#큐레이터", "#해시태그", "#입니다")); + ReflectionTestUtils.setField(memberImage, "baseMember", ret); + return ret; + } + + public static Host createValidHost() { + MemberImage memberImage = ImageData.createValidMemberImage(); + Host ret = Host.builder() + .email("curator1@example.com") + .nickname("nick_curator_12") + .phone("010-4545-6767") + .hashedPassword("hashed_password") + .profileImage(memberImage) + .build(); + ReflectionTestUtils.setField(ret, "Id", validHostId); + ReflectionTestUtils.setField(ret, "intro", "주최자 자기소개입니다."); + ReflectionTestUtils.setField(ret, "hashtags", List.of("#주최자", "#해시태그", "#입니다")); + ReflectionTestUtils.setField(memberImage, "baseMember", ret); + return ret; + } + + public static Member createValidWelcomeMember() { + MemberImage memberImage = ImageData.createValidMemberImage(); + Member ret = Member.builder() + .accountType(AccountType.kakao) + .email("member2@example.com") + .nickname("nick_normal_333") + .phone("010-4414-1144") + .profileImage(memberImage) + .area(Area.서울) + .gender("Men") + .socialId("456456456456") + .build(); + ReflectionTestUtils.setField(ret, "Id", validWelcomeMemberId); + ReflectionTestUtils.setField(memberImage, "baseMember", ret); + return ret; + } + + public static ReservationMemberResponse createValidReservationMemberResponse() { + return ReservationMemberResponse.builder() + .id(validMemberId) + .email("member1@example.com") + .nickname("nick_normal_123") + .phone("010-1112-2233") + .gender("Men") + .build(); + } +} \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index fad5703e..fd437c1b 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -2,6 +2,10 @@ import static java.lang.Math.abs; import static org.assertj.core.api.Assertions.assertThat; +import static org.ktc2.cokaen.wouldyouin._global.testdata.CurationData.createValidCurationCardRequest1; +import static org.ktc2.cokaen.wouldyouin._global.testdata.CurationData.createValidCurationCreateRequest; +import static org.ktc2.cokaen.wouldyouin._global.testdata.CurationData.createValidCurationEditRequest; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validCuratorId; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.times; @@ -22,11 +26,10 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.ktc2.cokaen.wouldyouin._common.vo.Area; -import org.ktc2.cokaen.wouldyouin._global.TestData.CurationDomain; -import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockCurator; import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockHost; import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockMember; +import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData; import org.ktc2.cokaen.wouldyouin.auth.application.JwtAuthFilter; import org.ktc2.cokaen.wouldyouin.curation.api.CurationController; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; @@ -54,14 +57,14 @@ class CurationControllerUnitTest { @Autowired private WebApplicationContext context; - private static final long randomId = abs(new Random().nextLong()); - @MockBean private CurationService curationService; @MockBean private JwtAuthFilter jwtAuthFilter; + private final long randomId = abs(new Random().nextLong()); + @BeforeEach public void setup() throws Exception { mockMvc = MockMvcBuilders @@ -147,7 +150,7 @@ void getCurationByCurationId() throws Exception { void createCuration1() throws Exception { // given ArgumentCaptor captor = ArgumentCaptor.forClass(CurationCreateRequest.class); - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest(); + CurationCreateRequest request = createValidCurationCreateRequest(); // when mockMvc.perform(post("/api/curations") @@ -158,7 +161,7 @@ void createCuration1() throws Exception { .andExpect(status().isCreated()); // then - then(curationService).should(times(1)).create(eq(MemberDomain.validCuratorId), captor.capture()); + then(curationService).should(times(1)).create(eq(validCuratorId), captor.capture()); assertThat(captor.getValue()).isEqualTo(request); } @@ -170,7 +173,7 @@ void createCuration2() throws Exception { mockMvc.perform(post("/api/curations") .with(csrf()) .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(CurationDomain.createValidCurationCreateRequest()))) + .content(objectMapper.writeValueAsString(createValidCurationCreateRequest()))) .andDo(print()) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.message").value("요구된 멤버 형식과 실제 형식이 다릅니다.")); @@ -188,7 +191,7 @@ void createCuration3() throws Exception { mockMvc.perform(post("/api/curations") .with(csrf()) .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(CurationDomain.createValidCurationCreateRequest()))) + .content(objectMapper.writeValueAsString(createValidCurationCreateRequest()))) .andDo(print()) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.message").value("요구된 멤버 형식과 실제 형식이 다릅니다.")); @@ -203,7 +206,7 @@ void createCuration3() throws Exception { @WithMockCurator void createCuration4() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() .title("").build(); // when @@ -224,8 +227,8 @@ void createCuration4() throws Exception { @WithMockCurator void createCuration5() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder().subtitle("").build())) + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder().subtitle("").build())) .build(); // when @@ -246,8 +249,8 @@ void createCuration5() throws Exception { @WithMockCurator void createCuration6() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder().content(null).build())) + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder().content(null).build())) .build(); // when @@ -268,8 +271,8 @@ void createCuration6() throws Exception { @WithMockCurator void createCuration7() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder().content(null).build())) + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder().content(null).build())) .build(); // when @@ -290,8 +293,8 @@ void createCuration7() throws Exception { @WithMockCurator void createCuration8() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder().content("짧은 내용").build())) + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder().content("짧은 내용").build())) .build(); // when @@ -312,8 +315,8 @@ void createCuration8() throws Exception { @WithMockCurator void createCuration9() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder() + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder() .imageIds(List.of(1L, 2L, 3L, 4L, 5L, 6L)).build())) .build(); @@ -335,7 +338,7 @@ void createCuration9() throws Exception { @WithMockCurator void createCuration10() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder() + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder() .area(null).build(); // when @@ -356,7 +359,7 @@ void createCuration10() throws Exception { @WithMockCurator void createCuration11() throws Exception { // given - CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest().toBuilder(). + CurationCreateRequest request = createValidCurationCreateRequest().toBuilder(). curationCards(List.of()).build(); // when @@ -378,7 +381,7 @@ void createCuration11() throws Exception { void updateCuration1() throws Exception { // given ArgumentCaptor captor = ArgumentCaptor.forClass(CurationEditRequest.class); - CurationEditRequest request = CurationDomain.createValidCurationEditRequest(); + CurationEditRequest request = createValidCurationEditRequest(); // when mockMvc.perform(put("/api/curations/" + randomId) @@ -389,7 +392,7 @@ void updateCuration1() throws Exception { .andExpect(status().isOk()); // then - then(curationService).should(times(1)).update(eq(MemberDomain.validCuratorId), eq(randomId), captor.capture()); + then(curationService).should(times(1)).update(eq(validCuratorId), eq(randomId), captor.capture()); assertThat(captor.getValue()).isEqualTo(request); } @@ -401,7 +404,7 @@ void updateCuration2() throws Exception { mockMvc.perform(put("/api/curations/" + randomId) .with(csrf()) .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(CurationDomain.createValidCurationEditRequest()))) + .content(objectMapper.writeValueAsString(createValidCurationEditRequest()))) .andDo(print()) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.message").value("요구된 멤버 형식과 실제 형식이 다릅니다.")); @@ -419,7 +422,7 @@ void updateCuration3() throws Exception { mockMvc.perform(put("/api/curations/" + randomId) .with(csrf()) .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(CurationDomain.createValidCurationEditRequest()))) + .content(objectMapper.writeValueAsString(createValidCurationEditRequest()))) .andDo(print()) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.message").value("요구된 멤버 형식과 실제 형식이 다릅니다.")); @@ -434,7 +437,7 @@ void updateCuration3() throws Exception { @WithMockCurator void updateCuration4() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() .title(null).build(); // when @@ -455,8 +458,8 @@ void updateCuration4() throws Exception { @WithMockCurator void updateCuration5() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder() .subtitle("").build())) .build(); @@ -478,8 +481,8 @@ void updateCuration5() throws Exception { @WithMockCurator void updateCuration6() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder() .content(null).build())) .build(); @@ -501,8 +504,8 @@ void updateCuration6() throws Exception { @WithMockCurator void updateCuration7() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder() .content("짧은 내용").build())) .build(); @@ -524,8 +527,8 @@ void updateCuration7() throws Exception { @WithMockCurator void updateCuration8() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() - .curationCards(List.of(CurationDomain.createValidCurationCardRequest1().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() + .curationCards(List.of(createValidCurationCardRequest1().toBuilder() .imageIds(List.of(1L, 2L, 3L, 4L, 5L, 6L)).build())) .build(); @@ -547,7 +550,7 @@ void updateCuration8() throws Exception { @WithMockCurator void updateCuration9() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() .area(null).build(); // when @@ -568,7 +571,7 @@ void updateCuration9() throws Exception { @WithMockCurator void updateCuration10() throws Exception { // given - CurationEditRequest request = CurationDomain.createValidCurationEditRequest().toBuilder() + CurationEditRequest request = createValidCurationEditRequest().toBuilder() .curationCards(List.of()).build(); // when @@ -595,7 +598,7 @@ void deleteCuration1() throws Exception { .andExpect(status().isNoContent()); // then - then(curationService).should(times(1)).delete(eq(MemberDomain.validCuratorId), eq(randomId)); + then(curationService).should(times(1)).delete(eq(validCuratorId), eq(randomId)); } @Test diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationServiceTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationServiceTest.java new file mode 100644 index 00000000..d14d31bd --- /dev/null +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationServiceTest.java @@ -0,0 +1,111 @@ +package org.ktc2.cokaen.wouldyouin.curation; + +import static java.lang.Math.abs; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.ktc2.cokaen.wouldyouin._global.testdata.CurationData.createValidCuration; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.times; + +import java.util.Optional; +import java.util.Random; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; +import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException; +import org.ktc2.cokaen.wouldyouin.curation.application.CurationCardService; +import org.ktc2.cokaen.wouldyouin.curation.application.CurationService; +import org.ktc2.cokaen.wouldyouin.curation.persist.Curation; +import org.ktc2.cokaen.wouldyouin.curation.persist.CurationRepository; +import org.ktc2.cokaen.wouldyouin.event.application.EventService; +import org.ktc2.cokaen.wouldyouin.member.application.CuratorService; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CurationServiceTest { + + private CurationService curationService; + + @Mock + private CurationRepository curationRepository; + + @Mock + private CuratorService curatorService; + + @Mock + private EventService eventService; + + @Mock + private CurationCardService curationCardService; + + @Mock + private MemberImage memberImage; + + private Curation validCuration; + + private final long randomId = abs(new Random().nextLong()); + + @BeforeEach + void setUp() { + curationService = new CurationService(curationRepository, curatorService, eventService, curationCardService); + validCuration = createValidCuration(); + } + + @Test + @DisplayName("큐레이션 ID를 통해 큐레이션 조회한다.") + void getByIdOrThrow1() { + // given + given(curationRepository.findById(randomId)).willReturn(Optional.of(validCuration)); + + // when + curationService.getByIdOrThrow(randomId); + + // then + then(curationRepository).should(times(1)).findById(randomId); + assertThat(curationService.getByIdOrThrow(randomId)).isEqualTo(validCuration); + } + + @Test + @DisplayName("큐레이션 ID를 통해 큐레이션을 찾지 못한 경우, 예외를 던진다.") + void getByIdOrThrow2() { + // given + given(curationRepository.findById(randomId)).willReturn(Optional.empty()); + + // when, then + EntityNotFoundException thrown = assertThrows( + EntityNotFoundException.class, () -> curationService.getByIdOrThrow(randomId)); + assertThat(thrown.getMessage()).isEqualTo("해당하는 큐레이션을 찾을 수 없습니다."); + } + + @Test + void getById() { + } + + @Test + void getAllByAreaOrderByCreatedDateDesc() { + } + + @Test + void getAllByCuratorIdOrderByCreatedDateDesc() { + } + + @Test + void create() { + } + + @Test + void update() { + } + + @Test + void delete() { + } + + @Test + void validateCuratorId() { + } +} \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java index b19b6299..a840608b 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java @@ -1,7 +1,7 @@ package org.ktc2.cokaen.wouldyouin.event; - -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validHostId; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; @@ -22,20 +22,19 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; - import org.ktc2.cokaen.wouldyouin._common.vo.Area; import org.ktc2.cokaen.wouldyouin._common.vo.Category; +import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; +import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockHost; import org.ktc2.cokaen.wouldyouin.auth.application.JwtAuthFilter; import org.ktc2.cokaen.wouldyouin.auth.application.JwtService; -import org.ktc2.cokaen.wouldyouin.event.api.dto.LocationFilter; import org.ktc2.cokaen.wouldyouin.event.api.EventController; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventCreateRequest; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventEditRequest; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventSliceResponse; +import org.ktc2.cokaen.wouldyouin.event.api.dto.LocationFilter; import org.ktc2.cokaen.wouldyouin.event.api.dto.LocationRequest; import org.ktc2.cokaen.wouldyouin.event.application.EventService; -import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; -import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockHost; import org.ktc2.cokaen.wouldyouin.member.application.MemberService; import org.ktc2.cokaen.wouldyouin.member.persist.Host; import org.springframework.beans.factory.annotation.Autowired; @@ -192,7 +191,7 @@ void createEvent() throws Exception { .andExpect(status().isCreated()); //then - then(eventService).should(times(1)).create(eq(id), any(EventCreateRequest.class)); + then(eventService).should(times(1)).create(eq(validHostId), any(EventCreateRequest.class)); } @Test @@ -208,7 +207,7 @@ void updateEvent() throws Exception { .andExpect(status().isOk()); //then - then(eventService).should(times(1)).update(eq(id), eq(id), any(EventEditRequest.class)); + then(eventService).should(times(1)).update(eq(validHostId), eq(id), any(EventEditRequest.class)); } @Test @@ -222,6 +221,6 @@ void deleteEvent() throws Exception { ).andExpect(status().isNoContent()); //then - then(eventService).should(times(1)).delete(eq(id), eq(id)); + then(eventService).should(times(1)).delete(eq(validHostId), eq(id)); } } \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/image/ImageControllerTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/image/ImageControllerTest.java index c41c2a58..52e6f2ff 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/image/ImageControllerTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/image/ImageControllerTest.java @@ -1,19 +1,26 @@ package org.ktc2.cokaen.wouldyouin.image; +import static java.lang.Math.abs; +import static org.ktc2.cokaen.wouldyouin._global.testdata.ImageData.createValidImageResponse1; +import static org.ktc2.cokaen.wouldyouin._global.testdata.ImageData.createValidImageResponse2; +import static org.ktc2.cokaen.wouldyouin._global.testdata.ImageData.createValidMultipartFile1; +import static org.ktc2.cokaen.wouldyouin._global.testdata.ImageData.createValidMultipartFile2; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.times; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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 java.nio.file.Paths; import java.util.List; -import java.util.UUID; +import java.util.Random; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -22,8 +29,9 @@ import org.ktc2.cokaen.wouldyouin.Image.application.CurationImageService; import org.ktc2.cokaen.wouldyouin.Image.application.ImageServiceFactory; import org.ktc2.cokaen.wouldyouin.Image.application.ImageStorageService; -import org.ktc2.cokaen.wouldyouin._global.TestData.ImageData; +import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockCurator; import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockMember; +import org.ktc2.cokaen.wouldyouin._global.testdata.ImageData; import org.ktc2.cokaen.wouldyouin.auth.application.JwtAuthFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -55,6 +63,8 @@ class ImageControllerTest { @MockBean private JwtAuthFilter jwtAuthFilter; + private final long randomId = abs(new Random().nextLong()); + @BeforeEach public void setup() throws Exception { @@ -70,61 +80,103 @@ public void setup() throws Exception { void getImage() throws Exception { // given String directory = "member"; - String file = UUID.randomUUID().toString() + ".png"; + String file = "image.jpg"; // when - mockMvc.perform(get("/api/images/" + directory + "/" + file)) + mockMvc.perform(get("/api/images/{directory}/{file}", directory, file)) .andDo(print()) .andExpect(status().isOk()); -// then + // then then(imageStorageService).should(times(1)).readFromDirectory(eq(Paths.get(directory, file))); } -// @PostMapping -// public ResponseEntity>> uploadImages( -// @RequestParam List images, -// @RequestParam(value = "type") ImageDomain imageDomain) { -// return ApiResponse.ok(imageServiceFactory.getImageService(imageDomain).saveImages(images)); -// } - @Test - @WithMockMember - void uploadImages() throws Exception { -// // given - MockMultipartFile image1 = ImageData.createValidMultipartFile1(); - MockMultipartFile image2 = ImageData.createValidMultipartFile2(); + @DisplayName("RequestParam으로 이미지 도메인을 받아 첨부된 이미지를 업로드한다.") + @WithMockCurator + void uploadImages1() throws Exception { + // given + MockMultipartFile image1 = createValidMultipartFile1(); + MockMultipartFile image2 = createValidMultipartFile2(); given((CurationImageService) imageServiceFactory.getImageService(ImageDomain.CURATION)).willReturn(curationImageService); given(curationImageService.saveImages(List.of(image1, image2))) - .willReturn(List.of(ImageData.createValidImageResponse1(), ImageData.createValidImageResponse2())); + .willReturn(List.of(createValidImageResponse1(), createValidImageResponse2())); -// // when - mockMvc.perform(multipart("/api/images?type=CURATION") - .file(ImageData.createValidMultipartFile1()) - .file(ImageData.createValidMultipartFile2()) + // when + mockMvc.perform(multipart("/api/images") + .file(image1) + .file(image2) .contentType(MediaType.MULTIPART_FORM_DATA) .param("type", ImageDomain.CURATION.name()) .with(csrf())) .andDo(print()) .andExpect(status().isOk()); -// ArgumentCaptor captor = ArgumentCaptor.forClass(CurationCreateRequest.class); -// CurationCreateRequest request = CurationDomain.createValidCurationCreateRequest(); + // then + then(imageServiceFactory).should(times(1)).getImageService(eq(ImageDomain.CURATION)); + then(curationImageService).should(times(1)).saveImages(List.of(image1, image2)); + } + + @Test + @DisplayName("RequestParam의 이미지 도메인의 값으로는 MEMBER, CURATION, ADVERTISEMENT, EVENT만 사용할 수 있다.") + @WithMockCurator + void uploadImages2() throws Exception { + // given + MockMultipartFile image1 = createValidMultipartFile1(); + MockMultipartFile image2 = createValidMultipartFile2(); // when -// mockMvc.perform(post("/api/curations") -// .with(csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(objectMapper.writeValueAsString(request))) -// .andDo(print()) -// .andExpect(status().isCreated()); -// -// // then -// then(curationService).should(times(1)).create(eq(MemberDomain.validCuratorId), captor.capture()); -// assertThat(captor.getValue()).isEqualTo(request); + mockMvc.perform(multipart("/api/images") + .file(image1) + .file(image2) + .contentType(MediaType.MULTIPART_FORM_DATA) + .param("type", "INVALID") + .with(csrf())) + .andDo(print()) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.message").value("해당 이미지 도메인에 대한 서비스가 존재하지 않습니다.")); + + // then + then(imageServiceFactory).shouldHaveNoInteractions(); + then(curationImageService).shouldHaveNoInteractions(); } @Test - void deleteImage() { + @DisplayName("PathVariable로 이미지의 ID를 받아 이미지를 삭제한다.") + @WithMockCurator + void deleteImage1() throws Exception { + // given + given((CurationImageService) imageServiceFactory.getImageService(ImageDomain.CURATION)).willReturn(curationImageService); + + // when + mockMvc.perform(delete("/api/images/" + randomId) + .param("type", ImageDomain.CURATION.name()) + .with(csrf())) + .andDo(print()) + .andExpect(status().isNoContent()); + + // then + then(imageServiceFactory).should(times(1)).getImageService(eq(ImageDomain.CURATION)); + then(curationImageService).should(times(1)).deleteImage(randomId); + } + + @Test + @DisplayName("RequestParam의 이미지 도메인의 값으로는 MEMBER, CURATION, ADVERTISEMENT, EVENT만 사용할 수 있다.") + @WithMockCurator + void deleteImage2() throws Exception { + // given + given((CurationImageService) imageServiceFactory.getImageService(ImageDomain.CURATION)).willReturn(curationImageService); + + // when + mockMvc.perform(delete("/api/images/" + randomId) + .param("type", "INVALID") + .with(csrf())) + .andDo(print()) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.message").value("해당 이미지 도메인에 대한 서비스가 존재하지 않습니다.")); + + // then + then(imageServiceFactory).shouldHaveNoInteractions(); + then(curationImageService).shouldHaveNoInteractions(); } } \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java index 23f7fd54..28968e9e 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java @@ -2,14 +2,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidCurator; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidHost; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidMember; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidWelcomeMember; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validCuratorId; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validHostId; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validMemberId; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validWelcomeMemberId; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidCurator; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidWelcomeMember; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validCuratorId; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validHostId; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validMemberId; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validWelcomeMemberId; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java index e0fe3e54..31a5b87f 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java @@ -1,9 +1,8 @@ package org.ktc2.cokaen.wouldyouin.member.application; import static org.assertj.core.api.Assertions.assertThat; -import static org.ktc2.cokaen.wouldyouin._global.TestData.ImageData.createValidMemberImage; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidCurator; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidCurator; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidMember; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -15,10 +14,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.ktc2.cokaen.wouldyouin.Image.application.MemberImageService; -import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; -import org.ktc2.cokaen.wouldyouin._common.vo.Area; -import org.ktc2.cokaen.wouldyouin._global.TestUtil; -import org.ktc2.cokaen.wouldyouin.member.api.dto.request.edit.CuratorEditRequest; import org.ktc2.cokaen.wouldyouin.member.persist.BaseMemberRepository; import org.ktc2.cokaen.wouldyouin.member.persist.Curator; import org.ktc2.cokaen.wouldyouin.member.persist.CuratorRepository; diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java index 25c13d4c..0c518e8e 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java @@ -1,8 +1,8 @@ package org.ktc2.cokaen.wouldyouin.member.application; import static org.assertj.core.api.Assertions.assertThat; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidHost; -import static org.ktc2.cokaen.wouldyouin._global.TestData.ImageData.createValidMemberImage; +import static org.ktc2.cokaen.wouldyouin._global.testdata.ImageData.createValidMemberImage; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidHost; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.times; @@ -88,7 +88,7 @@ void createHost() { void updateHost() { // given Long newProfileImageId = 5L; - MemberImage newProfileImage = createValidMemberImage(newProfileImageId); + MemberImage newProfileImage = createValidMemberImage(); newProfileImage.setBaseMember(validHost); HostEditRequest editRequest = HostEditRequest.builder() .nickname(TestUtil.getOrNull("newNickname")) diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java index 1fbcb3eb..e26283f6 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java @@ -2,9 +2,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.ktc2.cokaen.wouldyouin._global.TestData.ImageData.createValidMemberImage; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidMember; -import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidWelcomeMember; +import static org.ktc2.cokaen.wouldyouin._global.testdata.ImageData.createValidMemberImage; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.createValidWelcomeMember; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.never; @@ -76,7 +76,7 @@ void createMember() { @DisplayName("사용자 업데이트 테스트") void updateMember() { // given - MemberImage validMemberImage = createValidMemberImage(5L); + MemberImage validMemberImage = createValidMemberImage(); Long givenMemberId = validMember.getId(); given(memberRepository.findById(givenMemberId)).willReturn(Optional.of(validMember)); given(memberImageService.getById(validMember.getProfileImage().getId())).willReturn(validMemberImage); diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/reservation/ReservationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/reservation/ReservationControllerUnitTest.java index 8c9711ee..1e9c2195 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/reservation/ReservationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/reservation/ReservationControllerUnitTest.java @@ -2,6 +2,9 @@ import static java.lang.Math.abs; import static org.assertj.core.api.Assertions.assertThat; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validCuratorId; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validHostId; +import static org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.validMemberId; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.times; @@ -19,7 +22,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; import org.ktc2.cokaen.wouldyouin._global.TestData.ReservationDomain; import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockCurator; import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockHost; @@ -78,7 +80,7 @@ void getReservationsByMemberId1() throws Exception { // then then(reservationService).should(times(1)).getAllByMemberId( - eq(MemberDomain.validMemberId), eq(PageRequest.of(5, 20)), eq(100L)); + eq(validMemberId), eq(PageRequest.of(5, 20)), eq(100L)); } @Test @@ -91,7 +93,7 @@ void getReservationsByMemberId2() throws Exception { // then then(reservationService).should(times(1)).getAllByMemberId( - eq(MemberDomain.validMemberId), eq(PageRequest.of(0, 10)), eq(Long.MAX_VALUE)); + eq(validMemberId), eq(PageRequest.of(0, 10)), eq(Long.MAX_VALUE)); } @Test @@ -107,7 +109,7 @@ void getReservationsByMemberId3() throws Exception { // then then(reservationService).should(times(1)).getAllByMemberId( - eq(MemberDomain.validCuratorId), eq(PageRequest.of(5, 20)), eq(100L)); + eq(validCuratorId), eq(PageRequest.of(5, 20)), eq(100L)); } @Test @@ -139,7 +141,7 @@ void getReservationByEventId1() throws Exception { // then then(reservationService).should(times(1)).getAllByEventId( - eq(MemberDomain.validHostId), eq(randomId), eq(PageRequest.of(5, 20)), eq(100L)); + eq(validHostId), eq(randomId), eq(PageRequest.of(5, 20)), eq(100L)); } @Test @@ -153,7 +155,7 @@ void getReservationByEventId2() throws Exception { // then then(reservationService).should(times(1)).getAllByEventId( - eq(MemberDomain.validHostId), eq(randomId), eq(PageRequest.of(0, 10)), eq(Long.MAX_VALUE)); + eq(validHostId), eq(randomId), eq(PageRequest.of(0, 10)), eq(Long.MAX_VALUE)); } @Test @@ -214,7 +216,7 @@ void createReservation1() throws Exception { .andExpect(status().isCreated()); // then - then(reservationService).should(times(1)).create(eq(MemberDomain.validMemberId), captor.capture()); + then(reservationService).should(times(1)).create(eq(validMemberId), captor.capture()); assertThat(captor.getValue()).isEqualTo(request); } @@ -235,7 +237,7 @@ void createReservation2() throws Exception { .andExpect(status().isCreated()); // then - then(reservationService).should(times(1)).create(eq(MemberDomain.validCuratorId), captor.capture()); + then(reservationService).should(times(1)).create(eq(validCuratorId), captor.capture()); assertThat(captor.getValue()).isEqualTo(request); } @@ -330,7 +332,7 @@ void deleteReservation1() throws Exception { .andExpect(status().isNoContent()); // then - then(reservationService).should(times(1)).delete(MemberDomain.validMemberId, randomId); + then(reservationService).should(times(1)).delete(validMemberId, randomId); } @Test @@ -344,7 +346,7 @@ void deleteReservation2() throws Exception { .andExpect(status().isNoContent()); // then - then(reservationService).should(times(1)).delete(MemberDomain.validCuratorId, randomId); + then(reservationService).should(times(1)).delete(validCuratorId, randomId); } @Test