diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java index 47b2e8477..ae09a3980 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java @@ -63,7 +63,7 @@ public Long participate(final Long eventId, final Long memberId, final Member me return participant.getId(); } - private static void validateMemberNotAllowed(final Long memberId, final Member member) { + private void validateMemberNotAllowed(final Long memberId, final Member member) { if (member.isNotMe(memberId)) { throw new EventException(EventExceptionType.FORBIDDEN_PARTICIPATE_EVENT); } @@ -75,10 +75,9 @@ public List findEvents(final LocalDate nowDate, final int year, f validateYearAndMonth(year, month); List events = filterEventsByTag(tagName); - final EnumMap> sortAndGroupByStatus + final EnumMap> eventsForEventStatus = groupByEventStatus(nowDate, events, year, month); - - return filterEventResponsesByStatus(statusName, sortAndGroupByStatus); + return filterEventResponsesByStatus(statusName, eventsForEventStatus); } @Transactional(readOnly = true) @@ -145,13 +144,20 @@ private boolean isBeforeOrEquals(LocalDate criteria, LocalDate comparison) { } private List filterEventResponsesByStatus(final String statusName, - final EnumMap> sortAndGroupByEventStatus) { + final EnumMap> eventsForEventStatus) { if (isExistStatusName(statusName)) { EventStatus status = EventStatus.from(statusName); - return EventResponse.makeEventResponsesByStatus(status, - sortAndGroupByEventStatus.get(status)); + List filteredEvents = eventsForEventStatus.get(status); + if (cannotFoundKeyStatus(filteredEvents)) { + return List.of(); + } + return EventResponse.makeEventResponsesByStatus(status, filteredEvents); } - return EventResponse.mergeEventResponses(sortAndGroupByEventStatus); + return EventResponse.mergeEventResponses(eventsForEventStatus); + } + + private boolean cannotFoundKeyStatus(final List filteredEvents) { + return filteredEvents == null; } private boolean isExistStatusName(final String statusName) { diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/domain/EventStatus.java b/backend/emm-sale/src/main/java/com/emmsale/event/domain/EventStatus.java index 40e2dbb73..b0d743426 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event/domain/EventStatus.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event/domain/EventStatus.java @@ -26,7 +26,7 @@ public static EventStatus from(final String value) { .orElseThrow(() -> new EventException(INVALID_STATUS)); } - public boolean isSameValue(String value) { + private boolean isSameValue(String value) { return this.value.equals(value); } } diff --git a/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceTest.java index 8ce947774..01068c997 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceTest.java @@ -38,13 +38,12 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; class EventServiceTest extends ServiceIntegrationTestHelper { - @Autowired - private MemberRepository memberRepository; private static final EventResponse 인프콘_2023 = new EventResponse(null, "인프콘 2023", null, null, List.of(), "진행 중"); private static final EventResponse 웹_컨퍼런스 = new EventResponse(null, "웹 컨퍼런스", null, null, @@ -57,7 +56,8 @@ class EventServiceTest extends ServiceIntegrationTestHelper { private static final EventResponse 모바일_컨퍼런스 = new EventResponse(null, "모바일 컨퍼런스", null, null, List.of(), "진행 예정"); private static final LocalDate TODAY = LocalDate.of(2023, 7, 21); - + @Autowired + private MemberRepository memberRepository; @Autowired private EventService eventService; @Autowired @@ -89,6 +89,32 @@ void init() { ); } + @Test + @DisplayName("event의 id로 참여자 목록을 조회할 수 있다.") + void findParticipants() { + // given + final Event 인프콘 = eventRepository.save(eventFixture()); + final Member 멤버1 = memberRepository.save(new Member(123L, "image1.com", "멤버1")); + final Member 멤버2 = memberRepository.save(new Member(124L, "image2.com", "멤버2")); + + final Long 멤버1_참가자_ID = eventService.participate(인프콘.getId(), 멤버1.getId(), 멤버1); + final Long 멤버2_참가자_ID = eventService.participate(인프콘.getId(), 멤버2.getId(), 멤버2); + + //when + final List actual = eventService.findParticipants(인프콘.getId()); + + final List expected = List.of( + new ParticipantResponse(멤버1_참가자_ID, 멤버1.getId(), 멤버1.getName(), 멤버1.getImageUrl(), + 멤버1.getDescription()), + new ParticipantResponse(멤버2_참가자_ID, 멤버2.getId(), 멤버2.getName(), 멤버2.getImageUrl(), + 멤버2.getDescription()) + ); + //then + assertThat(actual) + .usingRecursiveFieldByFieldElementComparator() + .containsExactlyInAnyOrderElementsOf(expected); + } + @Nested @DisplayName("id로 이벤트를 조회할 수 있다.") class findEventTest { @@ -212,6 +238,55 @@ void findEvents_2023_6() { .isEqualTo(expectedEvents); } + @ParameterizedTest + @NullSource + @ValueSource(strings = "진행 중") + @DisplayName("등록된 행사가 없을 경우, status 옵션이 있든 없든 빈 목록을 반환한다.") + void findEvents_empty(String statusName) { + // given + eventRepository.deleteAll(); + + // when + final List actualEvents = eventService.findEvents(TODAY, 2023, 12, null, + statusName); + + // then + assertThat(actualEvents).isEmpty(); + } + + @Test + @DisplayName("아무 행사도 없는 2023년 12월 행사를 조회하면, 빈 목록을 반환한다.") + void findEvents_2023_12() { + // given, when + final List actualEvents = eventService.findEvents(TODAY, 2023, 12, null, null); + + // then + assertThat(actualEvents).isEmpty(); + } + + + @Test + @DisplayName("아무 행사도 없는 2023년 12월의 행사를 tag로 필터링하면, 빈 목록을 반환한다.") + void findEvents_empty_tag_filter() { + // given, when + final List actualEvents = eventService.findEvents(TODAY, 2023, 12, "안드로이드", + null); + + // then + assertThat(actualEvents).isEmpty(); + } + + @Test + @DisplayName("아무 행사도 없는 2023년 12월의 행사를 status로 필터링하면, 빈 목록을 반환한다.") + void findEvents_empty_status_filter() { + // given, when + final List actualEvents = eventService.findEvents(TODAY, 2023, 12, null, + "진행 중"); + + // then + assertThat(actualEvents).isEmpty(); + } + @ParameterizedTest @ValueSource(ints = {2014, 0, -1}) @DisplayName("유효하지 않은 값이 연도 값으로 들어오면 예외를 반환한다.") @@ -297,30 +372,4 @@ void findEvents_status_filter_fail() { .hasMessage(EventExceptionType.INVALID_STATUS.errorMessage()); } } - - @Test - @DisplayName("event의 id로 참여자 목록을 조회할 수 있다.") - void findParticipants() { - // given - final Event 인프콘 = eventRepository.save(eventFixture()); - final Member 멤버1 = memberRepository.save(new Member(123L, "image1.com", "멤버1")); - final Member 멤버2 = memberRepository.save(new Member(124L, "image2.com", "멤버2")); - - final Long 멤버1_참가자_ID = eventService.participate(인프콘.getId(), 멤버1.getId(), 멤버1); - final Long 멤버2_참가자_ID = eventService.participate(인프콘.getId(), 멤버2.getId(), 멤버2); - - //when - final List actual = eventService.findParticipants(인프콘.getId()); - - final List expected = List.of( - new ParticipantResponse(멤버1_참가자_ID, 멤버1.getId(), 멤버1.getName(), 멤버1.getImageUrl(), - 멤버1.getDescription()), - new ParticipantResponse(멤버2_참가자_ID, 멤버2.getId(), 멤버2.getName(), 멤버2.getImageUrl(), - 멤버2.getDescription()) - ); - //then - assertThat(actual) - .usingRecursiveFieldByFieldElementComparator() - .containsExactlyInAnyOrderElementsOf(expected); - } } diff --git a/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventStatusTest.java b/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventStatusTest.java new file mode 100644 index 000000000..52de332f6 --- /dev/null +++ b/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventStatusTest.java @@ -0,0 +1,40 @@ +package com.emmsale.event.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.emmsale.event.exception.EventException; +import com.emmsale.event.exception.EventExceptionType; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +class EventStatusTest { + + @ParameterizedTest + @CsvSource(value = {"진행 중,IN_PROGRESS", "진행 예정,UPCOMING", "종료된 행사,ENDED"}, delimiter = ',') + @DisplayName("동일한 문자 값을 갖는 EventStatus가 존재하면 해당 enum 객체를 반환한다.") + void from_success(String input, EventStatus expected) { + // given, when + final EventStatus actual = EventStatus.from(input); + + // then + assertThat(actual).isEqualTo(expected); + + } + + @ParameterizedTest + @ValueSource(strings = {"진행중", "진행예정", "아마란스", "진행 중 "}) + @DisplayName("동일한 문자 값을 갖는 EventStatus가 존재하지 않으면 예외를 반환한다.") + void from_fail(String input) { + // given, when + final ThrowingCallable actual = () -> EventStatus.from(input); + + // then + assertThatThrownBy(actual) + .isInstanceOf(EventException.class) + .hasMessage(EventExceptionType.INVALID_STATUS.errorMessage()); + } +} diff --git a/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventTest.java b/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventTest.java index 082d562c7..089112417 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/event/domain/EventTest.java @@ -7,14 +7,30 @@ import com.emmsale.event.exception.EventException; import com.emmsale.event.exception.EventExceptionType; import com.emmsale.member.domain.Member; +import java.time.LocalDate; import java.util.List; import java.util.stream.Collectors; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class EventTest { + @ParameterizedTest + @CsvSource(value = {"2023-03-01,UPCOMING", "2023-07-25,IN_PROGRESS", + "2023-08-01,ENDED"}, delimiter = ',') + @DisplayName("현재 날짜가 주어지면 행사의 진행 상태를 계산한다.") + void calculateEventStatus(LocalDate input, EventStatus expected) { + // given, when + EventStatus actual = EventFixture.AI_컨퍼런스().calculateEventStatus(input); + + // then + assertThat(actual).isEqualTo(expected); + + } + @Nested class addParticipant {