Skip to content

Commit

Permalink
test: 참여중인 방 목록 조회 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
ymkim97 committed Nov 15, 2023
1 parent 4138363 commit 97e1eca
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.moabam.api.application.room;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.time.LocalDate;
import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.moabam.api.application.member.MemberService;
import com.moabam.api.domain.room.Participant;
import com.moabam.api.domain.room.Room;
import com.moabam.api.domain.room.RoomType;
import com.moabam.api.domain.room.repository.CertificationsSearchRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.domain.room.repository.RoutineSearchRepository;
import com.moabam.api.dto.room.MyRoomsResponse;
import com.moabam.support.fixture.RoomFixture;

@ExtendWith(MockitoExtension.class)
class RoomSearchServiceTest {

@InjectMocks
private RoomSearchService roomSearchService;

@Mock
private CertificationsSearchRepository certificationsSearchRepository;

@Mock
private ParticipantSearchRepository participantSearchRepository;

@Mock
private RoutineSearchRepository routineSearchRepository;

@Mock
private MemberService memberService;

@Mock
private RoomCertificationService certificationService;

@DisplayName("유저가 참여중인 방 목록 조회 성공")
@Test
void get_my_rooms_success() {
// given
LocalDate today = LocalDate.now();
Long memberId = 1L;
Room room1 = spy(RoomFixture.room("아침 - 첫 번째 방", RoomType.MORNING, 10));
Room room2 = spy(RoomFixture.room("아침 - 두 번째 방", RoomType.MORNING, 9));
Room room3 = spy(RoomFixture.room("밤 - 첫 번째 방", RoomType.NIGHT, 22));

lenient().when(room1.getId()).thenReturn(1L);
lenient().when(room2.getId()).thenReturn(2L);
lenient().when(room3.getId()).thenReturn(3L);

Participant participant1 = RoomFixture.participant(room1, memberId);
Participant participant2 = RoomFixture.participant(room2, memberId);
Participant participant3 = RoomFixture.participant(room3, memberId);
List<Participant> participants = List.of(participant1, participant2, participant3);

given(participantSearchRepository.findParticipantsByMemberId(memberId)).willReturn(participants);
given(certificationService.existsMemberCertification(memberId, room1.getId(), today)).willReturn(true);
given(certificationService.existsMemberCertification(memberId, room2.getId(), today)).willReturn(false);
given(certificationService.existsMemberCertification(memberId, room3.getId(), today)).willReturn(true);

given(certificationService.existsRoomCertification(room1.getId(), today)).willReturn(true);
given(certificationService.existsRoomCertification(room2.getId(), today)).willReturn(false);
given(certificationService.existsRoomCertification(room3.getId(), today)).willReturn(false);

// when
MyRoomsResponse myRooms = roomSearchService.getMyRooms(memberId);

// then
assertThat(myRooms.participatingRooms()).hasSize(3);

assertThat(myRooms.participatingRooms().get(0).isMemberCertifiedToday()).isTrue();
assertThat(myRooms.participatingRooms().get(0).isRoomCertifiedToday()).isTrue();

assertThat(myRooms.participatingRooms().get(1).isMemberCertifiedToday()).isFalse();
assertThat(myRooms.participatingRooms().get(1).isRoomCertifiedToday()).isFalse();

assertThat(myRooms.participatingRooms().get(2).isMemberCertifiedToday()).isTrue();
assertThat(myRooms.participatingRooms().get(2).isRoomCertifiedToday()).isFalse();
}
}
27 changes: 27 additions & 0 deletions src/test/java/com/moabam/api/presentation/RoomControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -858,4 +858,31 @@ void deport_member_success() throws Exception {
assertThat(getMemberParticipant.getDeletedAt()).isNotNull();
assertThat(participantSearchRepository.findOne(member.getId(), room.getId())).isEmpty();
}

@DisplayName("현재 참여중인 모든 방 조회 성공 - 첫번째 방은 개인과 방 모두 인증 성공")
@WithMember(id = 1L)
@Test
void get_all_my_rooms_success() throws Exception {
// given
Room room1 = RoomFixture.room("아침 - 첫 번째 방", MORNING, 10);
Room room2 = RoomFixture.room("아침 - 두 번째 방", MORNING, 8);
Room room3 = RoomFixture.room("밤 - 세 번째 방", NIGHT, 22);

Participant participant1 = RoomFixture.participant(room1, 1L);
Participant participant2 = RoomFixture.participant(room2, 1L);
Participant participant3 = RoomFixture.participant(room3, 1L);

DailyMemberCertification dailyMemberCertification = RoomFixture.dailyMemberCertification(1L, 1L, participant1);
DailyRoomCertification dailyRoomCertification = RoomFixture.dailyRoomCertification(1L, LocalDate.now());

roomRepository.saveAll(List.of(room1, room2, room3));
participantRepository.saveAll(List.of(participant1, participant2, participant3));
dailyMemberCertificationRepository.save(dailyMemberCertification);
dailyRoomCertificationRepository.save(dailyRoomCertification);

// expected
mockMvc.perform(get("/rooms/my-join"))
.andExpect(status().isOk())
.andDo(print());
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/moabam/support/fixture/RoomFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ public static Room room(int certifyTime) {
.build();
}

public static Room room(String title, RoomType roomType, int certifyTime) {
return Room.builder()
.title(title)
.roomType(roomType)
.certifyTime(certifyTime)
.maxUserCount(8)
.build();
}

public static List<Room> rooms() {
return List.of(
room("아침 - 첫 번째 방", RoomType.MORNING, 10),
room("아침 - 두 번째 방", RoomType.MORNING, 9),
room("밤 - 첫 번째 방", RoomType.NIGHT, 22)
);
}

public static Participant participant(Room room, Long memberId) {
return Participant.builder()
.room(room)
Expand Down

0 comments on commit 97e1eca

Please sign in to comment.