Skip to content

Commit

Permalink
merge : #130-행사 목록 조회에서 Status 필터링 시 발생하는 500 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
amaran-th authored Jul 31, 2023
2 parents 1c0de2a + f9e033e commit 6c4b410
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -75,10 +75,9 @@ public List<EventResponse> findEvents(final LocalDate nowDate, final int year, f
validateYearAndMonth(year, month);
List<Event> events = filterEventsByTag(tagName);

final EnumMap<EventStatus, List<Event>> sortAndGroupByStatus
final EnumMap<EventStatus, List<Event>> eventsForEventStatus
= groupByEventStatus(nowDate, events, year, month);

return filterEventResponsesByStatus(statusName, sortAndGroupByStatus);
return filterEventResponsesByStatus(statusName, eventsForEventStatus);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -145,13 +144,20 @@ private boolean isBeforeOrEquals(LocalDate criteria, LocalDate comparison) {
}

private List<EventResponse> filterEventResponsesByStatus(final String statusName,
final EnumMap<EventStatus, List<Event>> sortAndGroupByEventStatus) {
final EnumMap<EventStatus, List<Event>> eventsForEventStatus) {
if (isExistStatusName(statusName)) {
EventStatus status = EventStatus.from(statusName);
return EventResponse.makeEventResponsesByStatus(status,
sortAndGroupByEventStatus.get(status));
List<Event> 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<Event> filteredEvents) {
return filteredEvents == null;
}

private boolean isExistStatusName(final String statusName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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<ParticipantResponse> actual = eventService.findParticipants(인프콘.getId());

final List<ParticipantResponse> 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 {
Expand Down Expand Up @@ -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<EventResponse> actualEvents = eventService.findEvents(TODAY, 2023, 12, null,
statusName);

// then
assertThat(actualEvents).isEmpty();
}

@Test
@DisplayName("아무 행사도 없는 2023년 12월 행사를 조회하면, 빈 목록을 반환한다.")
void findEvents_2023_12() {
// given, when
final List<EventResponse> 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<EventResponse> 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<EventResponse> actualEvents = eventService.findEvents(TODAY, 2023, 12, null,
"진행 중");

// then
assertThat(actualEvents).isEmpty();
}

@ParameterizedTest
@ValueSource(ints = {2014, 0, -1})
@DisplayName("유효하지 않은 값이 연도 값으로 들어오면 예외를 반환한다.")
Expand Down Expand Up @@ -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<ParticipantResponse> actual = eventService.findParticipants(인프콘.getId());

final List<ParticipantResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 6c4b410

Please sign in to comment.