Skip to content

Commit

Permalink
[BE] 약속 생성 결과 조회 API 구현 (#52)
Browse files Browse the repository at this point in the history
* test(AttendeeFixture): 참가자용 테스트 fixture 추가

* feat(MeetingService): 약속 생성 결과 조회 로직 추가

* feat(MeetingController): 약속 생성 결과 조회 컨트롤러 추가

* refactor(MeetingController): 불분명한 메서드명 변경

* refactor(MeetingController): API Path 수정 및 불분명한 명명 수정

* test(MeetingController): API Path 수정 및 UUID 오류 발생시 응답 코드 테스트 추가

* refactor(MeetingService): 사용하지 않는 어노테이션 및 Enum 값 제거
  • Loading branch information
ikjo39 authored Jul 24, 2024
1 parent 079213a commit bd766f0
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.woowacourse.momo.controller.MomoApiResponse;
import com.woowacourse.momo.service.meeting.MeetingService;
import com.woowacourse.momo.service.meeting.dto.MeetingResponse;
import com.woowacourse.momo.service.meeting.dto.MeetingSharingResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -19,4 +20,10 @@ public MomoApiResponse<MeetingResponse> find(@PathVariable String uuid) {
MeetingResponse meetingResponse = meetingService.findByUUID(uuid);
return new MomoApiResponse<>(meetingResponse);
}

@GetMapping("/api/v1/meeting/{uuid}/sharing")
public MomoApiResponse<MeetingSharingResponse> findMeetingSharing(@PathVariable String uuid) {
MeetingSharingResponse response = meetingService.findMeetingSharing(uuid);
return new MomoApiResponse<>(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.woowacourse.momo.exception.code;

import org.springframework.http.HttpStatus;

public enum MeetingErrorCode implements ErrorCodeType {

INVALID_UUID(HttpStatus.BAD_REQUEST, "유효하지 않은 UUID 입니다.");

private final HttpStatus httpStatus;
private final String message;

MeetingErrorCode(HttpStatus httpStatus, String message) {
this.httpStatus = httpStatus;
this.message = message;
}

@Override
public HttpStatus httpStatus() {
return httpStatus;
}

@Override
public String message() {
return message;
}

@Override
public String errorCode() {
return name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.woowacourse.momo.domain.meeting.MeetingRepository;
import com.woowacourse.momo.domain.schedule.Schedule;
import com.woowacourse.momo.domain.schedule.ScheduleRepository;
import com.woowacourse.momo.exception.MomoException;
import com.woowacourse.momo.exception.code.MeetingErrorCode;
import com.woowacourse.momo.service.meeting.dto.MeetingResponse;
import com.woowacourse.momo.service.meeting.dto.MeetingSharingResponse;
import com.woowacourse.momo.service.schedule.dto.ScheduleTimeResponse;
import java.time.LocalDate;
import java.util.Comparator;
Expand Down Expand Up @@ -47,4 +50,10 @@ public MeetingResponse findByUUID(String uuid) {

return MeetingResponse.from(meeting, dates, list);
}

public MeetingSharingResponse findMeetingSharing(String uuid) {
Meeting meeting = meetingRepository.findByUuid(uuid)
.orElseThrow(() -> new MomoException(MeetingErrorCode.INVALID_UUID));
return MeetingSharingResponse.from(meeting);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.momo.service.meeting.dto;

import com.woowacourse.momo.domain.meeting.Meeting;

public record MeetingSharingResponse(String uuid) {

public static MeetingSharingResponse from(Meeting meeting) {
return new MeetingSharingResponse(meeting.getUuid());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.woowacourse.momo.controller.meeting;

import com.woowacourse.momo.domain.attendee.AttendeeRepository;
import com.woowacourse.momo.domain.meeting.Meeting;
import com.woowacourse.momo.domain.meeting.MeetingRepository;
import com.woowacourse.momo.fixture.AttendeeFixture;
import com.woowacourse.momo.fixture.MeetingFixture;
import com.woowacourse.momo.support.IsolateDatabase;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;

@IsolateDatabase
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class MeetingControllerTest {

@LocalServerPort
private int port;

@Autowired
private AttendeeRepository attendeeRepository;

@Autowired
private MeetingRepository meetingRepository;

private Meeting meeting;

@BeforeEach
void setUp() {
RestAssured.port = port;

meeting = meetingRepository.save(MeetingFixture.COFFEE.create());
attendeeRepository.save(AttendeeFixture.HOST_JAZZ.create(meeting));
}

@DisplayName("약속 공유 정보를 조회하면 200OK와 응답을 반환한다.")
@Test
void findMeetingSharing() {
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.when().get("/api/v1/meeting/{uuid}/sharing", meeting.getUuid())
.then().log().all()
.statusCode(HttpStatus.OK.value());
}

@DisplayName("약속 공유 정보 조회시 UUID가 유효하지 않으면 400 Bad Request를 반환한다.")
@Test
void findMeetingSharingFailedWithInvalidUUID() {
RestAssured.given().log().all()
.contentType(ContentType.JSON)
.when().get("/api/v1/meeting/{uuid}/sharing", "1234")
.then().log().all()
.statusCode(HttpStatus.BAD_REQUEST.value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.woowacourse.momo.fixture;

import com.woowacourse.momo.domain.attendee.Attendee;
import com.woowacourse.momo.domain.attendee.AttendeeName;
import com.woowacourse.momo.domain.attendee.AttendeePassword;
import com.woowacourse.momo.domain.attendee.Role;
import com.woowacourse.momo.domain.meeting.Meeting;

public enum AttendeeFixture {

HOST_JAZZ("jazz", "hostPw!123", Role.HOST),
GUEST_DAON("daon", "daonPw!123", Role.GUEST),
GUEST_BAKEY("bakey", "bakeyPw!123", Role.GUEST),
GUEST_PEDRO("pedro", "pedroPw!123", Role.GUEST);

private final String name;
private final String password;
private final Role role;

AttendeeFixture(String name, String password, Role role) {
this.name = name;
this.password = password;
this.role = role;
}

public Attendee create(Meeting meeting) {
return new Attendee(meeting, new AttendeeName(name), new AttendeePassword(password), role);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.woowacourse.momo.service.meeting;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.woowacourse.momo.domain.attendee.Attendee;
import com.woowacourse.momo.domain.attendee.AttendeeRepository;
import com.woowacourse.momo.domain.attendee.Role;
Expand All @@ -10,17 +13,23 @@
import com.woowacourse.momo.domain.schedule.Schedule;
import com.woowacourse.momo.domain.schedule.ScheduleRepository;
import com.woowacourse.momo.domain.timeslot.Timeslot;
import com.woowacourse.momo.exception.MomoException;
import com.woowacourse.momo.exception.code.MeetingErrorCode;
import com.woowacourse.momo.fixture.AttendeeFixture;
import com.woowacourse.momo.fixture.MeetingFixture;
import com.woowacourse.momo.service.meeting.dto.MeetingResponse;
import com.woowacourse.momo.service.meeting.dto.MeetingSharingResponse;
import com.woowacourse.momo.support.IsolateDatabase;
import java.time.LocalDate;
import java.util.UUID;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

@IsolateDatabase
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
class MeetingServiceTest {

Expand All @@ -39,14 +48,6 @@ class MeetingServiceTest {
@Autowired
private AttendeeRepository attendeeRepository;

@BeforeEach
void setup() {
scheduleRepository.deleteAllInBatch();
availableDateRepository.deleteAllInBatch();
meetingRepository.deleteAllInBatch();
attendeeRepository.deleteAllInBatch();
}

@DisplayName("UUID로 약속 정보를 조회한다.")
@Test
void findByUUID() {
Expand Down Expand Up @@ -87,4 +88,26 @@ void findByUUID() {
softAssertions.assertThat(result.availableDates().get(0)).isEqualTo(LocalDate.now().minusDays(1));
softAssertions.assertAll();
}

@DisplayName("생성 완료된 약속의 정보를 조회한다.")
@Test
void findMeetingSharing() {
Meeting meeting = meetingRepository.save(MeetingFixture.GAME.create());

MeetingSharingResponse result = meetingService.findMeetingSharing(meeting.getUuid());

assertThat(result.uuid()).isEqualTo(meeting.getUuid());
}

@DisplayName("생성 완료된 약속의 정보를 조회시 uuid가 일치하지 않으면 예외가 발생한다.")
@Test
void doesNotFindMeetingSharingMeetingIfUUIDNotExist() {
String invalidUUID = "1234";
Meeting meeting = meetingRepository.save(MeetingFixture.GAME.create());
attendeeRepository.save(AttendeeFixture.HOST_JAZZ.create(meeting));

assertThatThrownBy(() -> meetingService.findMeetingSharing(invalidUUID))
.isInstanceOf(MomoException.class)
.hasMessage(MeetingErrorCode.INVALID_UUID.message());
}
}

0 comments on commit bd766f0

Please sign in to comment.