|
| 1 | +package com.back.domain.studyroom.integration; |
| 2 | + |
| 3 | +import com.back.domain.studyroom.dto.CreateRoomRequest; |
| 4 | +import com.back.domain.studyroom.entity.Room; |
| 5 | +import com.back.domain.studyroom.repository.RoomMemberRepository; |
| 6 | +import com.back.domain.studyroom.repository.RoomRepository; |
| 7 | +import com.back.domain.studyroom.service.RoomService; |
| 8 | +import com.back.domain.user.entity.Role; |
| 9 | +import com.back.domain.user.entity.User; |
| 10 | +import com.back.domain.user.entity.UserProfile; |
| 11 | +import com.back.domain.user.entity.UserStatus; |
| 12 | +import com.back.domain.user.repository.UserRepository; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.boot.test.context.SpringBootTest; |
| 18 | +import org.springframework.test.context.ActiveProfiles; |
| 19 | +import org.springframework.transaction.annotation.Transactional; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +@SpringBootTest |
| 24 | +@ActiveProfiles("test") |
| 25 | +@Transactional |
| 26 | +@DisplayName("방 생성 통합 테스트 - 실제 DB 저장") |
| 27 | +public class RoomCreateIntegrationTest { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private RoomService roomService; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private UserRepository userRepository; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private RoomRepository roomRepository; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private RoomMemberRepository roomMemberRepository; |
| 40 | + |
| 41 | + private User testUser; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + void setUp() { |
| 45 | + // 실제 사용자 생성 및 저장 |
| 46 | + UserProfile userProfile = new UserProfile(); |
| 47 | + userProfile.setNickname("테스트유저"); |
| 48 | + userProfile.setProfileImageUrl("https://example.com/profile.jpg"); |
| 49 | + |
| 50 | + testUser = User.builder() |
| 51 | + |
| 52 | + |
| 53 | + .password("password123") |
| 54 | + .role(Role.USER) |
| 55 | + .userStatus(UserStatus.ACTIVE) |
| 56 | + .build(); |
| 57 | + |
| 58 | + testUser.setUserProfile(userProfile); |
| 59 | + testUser = userRepository.save(testUser); |
| 60 | + |
| 61 | + System.out.println("=== 테스트 사용자 생성 완료: ID = " + testUser.getId()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("방 생성 실제 저장 테스트 - 프론트엔드 요청과 동일한 조건") |
| 66 | + void createRoom_RealSave() { |
| 67 | + // given - 프론트엔드가 보낸 것과 동일한 요청 |
| 68 | + String title = "테스트1"; |
| 69 | + String description = "테스트 제발 제발 제"; |
| 70 | + boolean isPrivate = true; |
| 71 | + String password = "123123123"; |
| 72 | + int maxParticipants = 9; |
| 73 | + boolean useWebRTC = false; |
| 74 | + |
| 75 | + System.out.println("=== 방 생성 시작 ==="); |
| 76 | + System.out.println("Title: " + title); |
| 77 | + System.out.println("Description: " + description); |
| 78 | + System.out.println("IsPrivate: " + isPrivate); |
| 79 | + System.out.println("Password: " + password); |
| 80 | + System.out.println("MaxParticipants: " + maxParticipants); |
| 81 | + System.out.println("UseWebRTC: " + useWebRTC); |
| 82 | + System.out.println("CreatorId: " + testUser.getId()); |
| 83 | + |
| 84 | + // when - 실제 서비스 호출 |
| 85 | + Room createdRoom = roomService.createRoom( |
| 86 | + title, |
| 87 | + description, |
| 88 | + isPrivate, |
| 89 | + password, |
| 90 | + maxParticipants, |
| 91 | + testUser.getId(), |
| 92 | + useWebRTC |
| 93 | + ); |
| 94 | + |
| 95 | + // then |
| 96 | + assertThat(createdRoom).isNotNull(); |
| 97 | + assertThat(createdRoom.getId()).isNotNull(); |
| 98 | + assertThat(createdRoom.getTitle()).isEqualTo(title); |
| 99 | + assertThat(createdRoom.getDescription()).isEqualTo(description); |
| 100 | + assertThat(createdRoom.isPrivate()).isEqualTo(isPrivate); |
| 101 | + assertThat(createdRoom.getPassword()).isEqualTo(password); |
| 102 | + assertThat(createdRoom.getMaxParticipants()).isEqualTo(maxParticipants); |
| 103 | + assertThat(createdRoom.isAllowCamera()).isEqualTo(useWebRTC); |
| 104 | + assertThat(createdRoom.isAllowAudio()).isEqualTo(useWebRTC); |
| 105 | + assertThat(createdRoom.isAllowScreenShare()).isEqualTo(useWebRTC); |
| 106 | + |
| 107 | + System.out.println("=== 방 생성 성공! ==="); |
| 108 | + System.out.println("Room ID: " + createdRoom.getId()); |
| 109 | + System.out.println("Room Members: " + createdRoom.getRoomMembers()); |
| 110 | + System.out.println("Room Chat Messages: " + createdRoom.getRoomChatMessages()); |
| 111 | + |
| 112 | + // DB에 실제로 저장되었는지 확인 |
| 113 | + Room savedRoom = roomRepository.findById(createdRoom.getId()).orElse(null); |
| 114 | + assertThat(savedRoom).isNotNull(); |
| 115 | + assertThat(savedRoom.getTitle()).isEqualTo(title); |
| 116 | + |
| 117 | + // 방장 멤버도 저장되었는지 확인 |
| 118 | + boolean hostExists = roomMemberRepository.existsByRoomIdAndUserId( |
| 119 | + createdRoom.getId(), |
| 120 | + testUser.getId() |
| 121 | + ); |
| 122 | + assertThat(hostExists).isTrue(); |
| 123 | + |
| 124 | + System.out.println("=== 방 생성 통합 테스트 완료 ==="); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @DisplayName("방 생성 - 컬렉션 필드 null 체크") |
| 129 | + void createRoom_CheckCollections() { |
| 130 | + // given |
| 131 | + String title = "컬렉션 테스트"; |
| 132 | + |
| 133 | + // when |
| 134 | + Room createdRoom = roomService.createRoom( |
| 135 | + title, |
| 136 | + "설명", |
| 137 | + false, |
| 138 | + null, |
| 139 | + 10, |
| 140 | + testUser.getId(), |
| 141 | + true |
| 142 | + ); |
| 143 | + |
| 144 | + // then - 컬렉션 필드들이 null이 아니어야 함 |
| 145 | + assertThat(createdRoom.getRoomMembers()).isNotNull(); |
| 146 | + assertThat(createdRoom.getRoomChatMessages()).isNotNull(); |
| 147 | + assertThat(createdRoom.getRoomParticipantHistories()).isNotNull(); |
| 148 | + assertThat(createdRoom.getStudyRecords()).isNotNull(); |
| 149 | + |
| 150 | + System.out.println("=== 컬렉션 필드 확인 ==="); |
| 151 | + System.out.println("RoomMembers: " + createdRoom.getRoomMembers()); |
| 152 | + System.out.println("RoomChatMessages: " + createdRoom.getRoomChatMessages()); |
| 153 | + System.out.println("RoomParticipantHistories: " + createdRoom.getRoomParticipantHistories()); |
| 154 | + System.out.println("StudyRecords: " + createdRoom.getStudyRecords()); |
| 155 | + } |
| 156 | +} |
0 commit comments