|
| 1 | +package com.back.domain.studyroom.controller; |
| 2 | + |
| 3 | +import com.back.domain.studyroom.dto.AvatarResponse; |
| 4 | +import com.back.domain.studyroom.dto.UpdateAvatarRequest; |
| 5 | +import com.back.domain.studyroom.entity.Avatar; |
| 6 | +import com.back.domain.studyroom.service.AvatarService; |
| 7 | +import com.back.global.common.dto.RsData; |
| 8 | +import com.back.global.security.user.CurrentUser; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.InjectMocks; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.http.ResponseEntity; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.mockito.BDDMockito.given; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | + |
| 25 | +@ExtendWith(MockitoExtension.class) |
| 26 | +@DisplayName("RoomAvatarController 테스트") |
| 27 | +class RoomAvatarControllerTest { |
| 28 | + |
| 29 | + @Mock |
| 30 | + private AvatarService avatarService; |
| 31 | + |
| 32 | + @Mock |
| 33 | + private CurrentUser currentUser; |
| 34 | + |
| 35 | + @InjectMocks |
| 36 | + private RoomAvatarController roomAvatarController; |
| 37 | + |
| 38 | + private List<AvatarResponse> avatarResponses; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setUp() { |
| 42 | + avatarResponses = List.of( |
| 43 | + new AvatarResponse(1L, "검은 고양이", "/images/avatars/cat-black.png", |
| 44 | + "귀여운 검은 고양이", true, "CAT"), |
| 45 | + new AvatarResponse(2L, "하얀 고양이", "/images/avatars/cat-white.png", |
| 46 | + "우아한 하얀 고양이", true, "CAT"), |
| 47 | + new AvatarResponse(3L, "노란 고양이", "/images/avatars/cat-orange.png", |
| 48 | + "발랄한 노란 고양이", true, "CAT") |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @DisplayName("아바타 목록 조회 API - 성공") |
| 54 | + void getAvatars_Success() { |
| 55 | + // given |
| 56 | + given(avatarService.getAvailableAvatars()).willReturn(avatarResponses); |
| 57 | + |
| 58 | + // when |
| 59 | + ResponseEntity<RsData<List<AvatarResponse>>> response = |
| 60 | + roomAvatarController.getAvatars(1L); |
| 61 | + |
| 62 | + // then |
| 63 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 64 | + assertThat(response.getBody()).isNotNull(); |
| 65 | + assertThat(response.getBody().isSuccess()).isTrue(); |
| 66 | + assertThat(response.getBody().getMessage()).isEqualTo("아바타 목록 조회 완료"); |
| 67 | + assertThat(response.getBody().getData()).hasSize(3); |
| 68 | + assertThat(response.getBody().getData()) |
| 69 | + .extracting(AvatarResponse::getName) |
| 70 | + .containsExactly("검은 고양이", "하얀 고양이", "노란 고양이"); |
| 71 | + |
| 72 | + verify(avatarService, times(1)).getAvailableAvatars(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisplayName("아바타 목록 조회 API - 빈 리스트 반환") |
| 77 | + void getAvatars_EmptyList() { |
| 78 | + // given |
| 79 | + given(avatarService.getAvailableAvatars()).willReturn(List.of()); |
| 80 | + |
| 81 | + // when |
| 82 | + ResponseEntity<RsData<List<AvatarResponse>>> response = |
| 83 | + roomAvatarController.getAvatars(1L); |
| 84 | + |
| 85 | + // then |
| 86 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 87 | + assertThat(response.getBody()).isNotNull(); |
| 88 | + assertThat(response.getBody().getData()).isEmpty(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @DisplayName("아바타 변경 API - 성공 (VISITOR)") |
| 93 | + void updateMyAvatar_Visitor_Success() { |
| 94 | + // given |
| 95 | + given(currentUser.getUserId()).willReturn(100L); |
| 96 | + |
| 97 | + UpdateAvatarRequest request = new UpdateAvatarRequest(2L); |
| 98 | + |
| 99 | + doNothing().when(avatarService).updateRoomAvatar(1L, 100L, 2L); |
| 100 | + |
| 101 | + // when |
| 102 | + ResponseEntity<RsData<Void>> response = |
| 103 | + roomAvatarController.updateMyAvatar(1L, request); |
| 104 | + |
| 105 | + // then |
| 106 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 107 | + assertThat(response.getBody()).isNotNull(); |
| 108 | + assertThat(response.getBody().isSuccess()).isTrue(); |
| 109 | + assertThat(response.getBody().getMessage()).isEqualTo("아바타가 변경되었습니다"); |
| 110 | + |
| 111 | + verify(currentUser, times(1)).getUserId(); |
| 112 | + verify(avatarService, times(1)).updateRoomAvatar(1L, 100L, 2L); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + @DisplayName("아바타 변경 API - 성공 (MEMBER)") |
| 117 | + void updateMyAvatar_Member_Success() { |
| 118 | + // given |
| 119 | + given(currentUser.getUserId()).willReturn(100L); |
| 120 | + |
| 121 | + UpdateAvatarRequest request = new UpdateAvatarRequest(3L); |
| 122 | + |
| 123 | + doNothing().when(avatarService).updateRoomAvatar(1L, 100L, 3L); |
| 124 | + |
| 125 | + // when |
| 126 | + ResponseEntity<RsData<Void>> response = |
| 127 | + roomAvatarController.updateMyAvatar(1L, request); |
| 128 | + |
| 129 | + // then |
| 130 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 131 | + assertThat(response.getBody()).isNotNull(); |
| 132 | + assertThat(response.getBody().isSuccess()).isTrue(); |
| 133 | + |
| 134 | + verify(avatarService, times(1)).updateRoomAvatar(1L, 100L, 3L); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + @DisplayName("아바타 변경 API - 다른 방에서는 다른 아바타 설정 가능") |
| 139 | + void updateMyAvatar_DifferentRooms() { |
| 140 | + // given |
| 141 | + given(currentUser.getUserId()).willReturn(100L); |
| 142 | + |
| 143 | + UpdateAvatarRequest request1 = new UpdateAvatarRequest(1L); |
| 144 | + UpdateAvatarRequest request2 = new UpdateAvatarRequest(3L); |
| 145 | + |
| 146 | + // when |
| 147 | + roomAvatarController.updateMyAvatar(1L, request1); // 방1에서 아바타 1 |
| 148 | + roomAvatarController.updateMyAvatar(2L, request2); // 방2에서 아바타 3 |
| 149 | + |
| 150 | + // then |
| 151 | + verify(avatarService, times(1)).updateRoomAvatar(1L, 100L, 1L); |
| 152 | + verify(avatarService, times(1)).updateRoomAvatar(2L, 100L, 3L); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + @DisplayName("아바타 변경 API - 같은 아바타로 여러 번 변경 가능") |
| 157 | + void updateMyAvatar_SameAvatarMultipleTimes() { |
| 158 | + // given |
| 159 | + given(currentUser.getUserId()).willReturn(100L); |
| 160 | + |
| 161 | + UpdateAvatarRequest request = new UpdateAvatarRequest(2L); |
| 162 | + |
| 163 | + // when |
| 164 | + roomAvatarController.updateMyAvatar(1L, request); |
| 165 | + roomAvatarController.updateMyAvatar(1L, request); |
| 166 | + |
| 167 | + // then |
| 168 | + verify(avatarService, times(2)).updateRoomAvatar(1L, 100L, 2L); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + @DisplayName("아바타 변경 API - 여러 사용자가 동시에 변경 가능") |
| 173 | + void updateMyAvatar_MultipleUsers() { |
| 174 | + // given |
| 175 | + UpdateAvatarRequest request1 = new UpdateAvatarRequest(1L); |
| 176 | + UpdateAvatarRequest request2 = new UpdateAvatarRequest(2L); |
| 177 | + |
| 178 | + // when |
| 179 | + given(currentUser.getUserId()).willReturn(100L); |
| 180 | + roomAvatarController.updateMyAvatar(1L, request1); |
| 181 | + |
| 182 | + given(currentUser.getUserId()).willReturn(200L); |
| 183 | + roomAvatarController.updateMyAvatar(1L, request2); |
| 184 | + |
| 185 | + // then |
| 186 | + verify(avatarService, times(1)).updateRoomAvatar(1L, 100L, 1L); |
| 187 | + verify(avatarService, times(1)).updateRoomAvatar(1L, 200L, 2L); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + @DisplayName("아바타 목록 조회 - 카테고리 정보 포함") |
| 192 | + void getAvatars_IncludesCategory() { |
| 193 | + // given |
| 194 | + given(avatarService.getAvailableAvatars()).willReturn(avatarResponses); |
| 195 | + |
| 196 | + // when |
| 197 | + ResponseEntity<RsData<List<AvatarResponse>>> response = |
| 198 | + roomAvatarController.getAvatars(1L); |
| 199 | + |
| 200 | + // then |
| 201 | + assertThat(response.getBody().getData()) |
| 202 | + .allMatch(avatar -> avatar.getCategory() != null); |
| 203 | + assertThat(response.getBody().getData()) |
| 204 | + .allMatch(avatar -> avatar.getCategory().equals("CAT")); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + @DisplayName("아바타 목록 조회 - isDefault 정보 포함") |
| 209 | + void getAvatars_IncludesIsDefault() { |
| 210 | + // given |
| 211 | + given(avatarService.getAvailableAvatars()).willReturn(avatarResponses); |
| 212 | + |
| 213 | + // when |
| 214 | + ResponseEntity<RsData<List<AvatarResponse>>> response = |
| 215 | + roomAvatarController.getAvatars(1L); |
| 216 | + |
| 217 | + // then |
| 218 | + assertThat(response.getBody().getData()) |
| 219 | + .allMatch(AvatarResponse::isDefault); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + @DisplayName("아바타 변경 API - 요청 검증 (avatarId 필수)") |
| 224 | + void updateMyAvatar_RequestValidation() { |
| 225 | + // given |
| 226 | + given(currentUser.getUserId()).willReturn(100L); |
| 227 | + |
| 228 | + // avatarId가 있는 정상 요청 |
| 229 | + UpdateAvatarRequest validRequest = new UpdateAvatarRequest(1L); |
| 230 | + |
| 231 | + // when |
| 232 | + ResponseEntity<RsData<Void>> response = |
| 233 | + roomAvatarController.updateMyAvatar(1L, validRequest); |
| 234 | + |
| 235 | + // then |
| 236 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 237 | + verify(avatarService, times(1)).updateRoomAvatar(1L, 100L, 1L); |
| 238 | + } |
| 239 | +} |
0 commit comments