22
33import konkuk .thip .book .adapter .out .jpa .BookJpaEntity ;
44import konkuk .thip .book .adapter .out .persistence .repository .BookJpaRepository ;
5+ import konkuk .thip .common .entity .StatusType ;
56import konkuk .thip .common .util .TestEntityFactory ;
67import konkuk .thip .room .adapter .out .jpa .RoomJpaEntity ;
78import konkuk .thip .room .adapter .out .jpa .RoomParticipantJpaEntity ;
1819import org .springframework .beans .factory .annotation .Autowired ;
1920import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
2021import org .springframework .boot .test .context .SpringBootTest ;
22+ import org .springframework .jdbc .core .JdbcTemplate ;
2123import org .springframework .test .context .ActiveProfiles ;
2224import org .springframework .test .web .servlet .MockMvc ;
2325import org .springframework .test .web .servlet .ResultActions ;
2426
27+ import java .sql .Timestamp ;
2528import java .time .LocalDate ;
2629
2730import static konkuk .thip .common .exception .code .ErrorCode .INVALID_MY_ROOM_TYPE ;
@@ -41,6 +44,7 @@ class RoomShowMineApiTest {
4144 @ Autowired private BookJpaRepository bookJpaRepository ;
4245 @ Autowired private RoomJpaRepository roomJpaRepository ;
4346 @ Autowired private RoomParticipantJpaRepository roomParticipantJpaRepository ;
47+ @ Autowired private JdbcTemplate jdbcTemplate ;
4448
4549 @ AfterEach
4650 void tearDown () {
@@ -84,16 +88,18 @@ private void changeRoomMemberCount(RoomJpaEntity roomJpaEntity, int count) {
8488 roomJpaRepository .save (roomJpaEntity );
8589 }
8690
87- private void saveSingleUserToRoom (RoomJpaEntity roomJpaEntity , UserJpaEntity userJpaEntity ) {
91+ private RoomParticipantJpaEntity saveSingleUserToRoom (RoomJpaEntity roomJpaEntity , UserJpaEntity userJpaEntity ) {
8892 RoomParticipantJpaEntity roomParticipantJpaEntity = RoomParticipantJpaEntity .builder ()
8993 .userJpaEntity (userJpaEntity )
9094 .roomJpaEntity (roomJpaEntity )
9195 .roomParticipantRole (RoomParticipantRole .MEMBER )
9296 .build ();
93- roomParticipantJpaRepository .save (roomParticipantJpaEntity );
97+ RoomParticipantJpaEntity saved = roomParticipantJpaRepository .save (roomParticipantJpaEntity );
9498
9599 roomJpaEntity .updateMemberCount (roomJpaEntity .getMemberCount () + 1 );
96100 roomJpaRepository .save (roomJpaEntity ); // room의 memberCount 값도 업데이트 해줘야 한다
101+
102+ return saved ;
97103 }
98104
99105 @ Test
@@ -440,4 +446,50 @@ void get_my_rooms_page_2() throws Exception {
440446 .andExpect (jsonPath ("$.data.roomList[0].roomName" , is ("과학-방-11일뒤-활동시작" )))
441447 .andExpect (jsonPath ("$.data.roomList[1].roomName" , is ("과학-방-12일뒤-활동시작" )));
442448 }
449+
450+ @ Test
451+ @ DisplayName ("유저가 나간 방(= 모집기간 중 참여 취소한 경우, 진행기간 중 방 나간 경우) 은 조회되지 않는다." )
452+ void get_my_rooms_about_exit_rooms () throws Exception {
453+ //given
454+ RoomJpaEntity recruitingRoom1 = saveScienceRoom ("모집중인방-책-1" , "isbn1" , "과학-방-1일뒤-활동시작" , LocalDate .now ().plusDays (1 ), LocalDate .now ().plusDays (30 ), 10 );
455+ changeRoomMemberCount (recruitingRoom1 , 5 );
456+
457+ RoomJpaEntity exitRecruitingRoom = saveScienceRoom ("모집중인방-책-2" , "isbn2" , "참여신청하고-나간-모집중인-과학-방" , LocalDate .now ().plusDays (5 ), LocalDate .now ().plusDays (30 ), 10 );
458+ changeRoomMemberCount (exitRecruitingRoom , 8 );
459+
460+ RoomJpaEntity playingRoom1 = saveScienceRoom ("진행중인방-책-1" , "isbn3" , "과학-방-5일뒤-활동마감" , LocalDate .now ().minusDays (5 ), LocalDate .now ().plusDays (5 ), 10 );
461+ changeRoomMemberCount (playingRoom1 , 6 );
462+
463+ RoomJpaEntity exitPlayingRoom = saveScienceRoom ("진행중인방-책-2" , "isbn4" , "모임진행중-나간-진행중인-과학-방" , LocalDate .now ().minusDays (5 ), LocalDate .now ().plusDays (5 ), 10 );
464+ changeRoomMemberCount (exitPlayingRoom , 6 );
465+
466+ Alias scienceAlias = TestEntityFactory .createScienceAlias ();
467+ UserJpaEntity user = userJpaRepository .save (TestEntityFactory .createUser (scienceAlias ));
468+
469+ // user가 생성한 방에 참여한 상황 가정
470+ saveSingleUserToRoom (recruitingRoom1 , user );
471+ RoomParticipantJpaEntity exit1 = saveSingleUserToRoom (exitRecruitingRoom , user );
472+ saveSingleUserToRoom (playingRoom1 , user );
473+ RoomParticipantJpaEntity exit2 = saveSingleUserToRoom (exitPlayingRoom , user );
474+
475+ // user가 exitRecruitingRoom, exitPlayingRoom 방에서 나간 상황 가정
476+ jdbcTemplate .update (
477+ "UPDATE room_participants SET status = ? WHERE room_participant_id = ?" ,
478+ StatusType .INACTIVE .name (), exit1 .getRoomParticipantId ());
479+ jdbcTemplate .update (
480+ "UPDATE room_participants SET status = ? WHERE room_participant_id = ?" ,
481+ StatusType .INACTIVE .name (), exit2 .getRoomParticipantId ());
482+
483+ //when
484+ ResultActions result = mockMvc .perform (get ("/rooms/my" )
485+ .requestAttr ("userId" , user .getUserId ())); // type request param 없는 경우
486+
487+ //then
488+ result .andExpect (status ().isOk ())
489+ .andExpect (jsonPath ("$.data.roomList" , hasSize (2 ))) // 나간방 제외하고 2개만 보임
490+ .andExpect (jsonPath ("$.data.roomList[0].roomName" , is ("과학-방-5일뒤-활동마감" )))
491+ .andExpect (jsonPath ("$.data.roomList[0].memberCount" , is (7 ))) // 기존 6명 + user
492+ .andExpect (jsonPath ("$.data.roomList[1].roomName" , is ("과학-방-1일뒤-활동시작" )))
493+ .andExpect (jsonPath ("$.data.roomList[1].memberCount" , is (6 ))); // 기존 5명 + user
494+ }
443495}
0 commit comments