11package org .dfbf .soundlink .domain .emotionRecord ;
22
3- import org .dfbf .soundlink .domain .chat .entity .ChatRoom ;
43import org .dfbf .soundlink .domain .chat .repository .ChatRoomRepository ;
54import org .dfbf .soundlink .domain .emotionRecord .dto .request .EmotionRecordRequestDTO ;
65import org .dfbf .soundlink .domain .emotionRecord .dto .request .EmotionRecordUpdateRequestDTO ;
76import org .dfbf .soundlink .domain .emotionRecord .entity .EmotionRecord ;
87import org .dfbf .soundlink .domain .emotionRecord .entity .SpotifyMusic ;
98import org .dfbf .soundlink .domain .emotionRecord .repository .EmotionRecordRepository ;
109import org .dfbf .soundlink .domain .emotionRecord .repository .SpotifyMusicRepository ;
11- import org .dfbf .soundlink .domain .emotionRecord .service .EmotionRecordCacheService ;
1210import org .dfbf .soundlink .domain .emotionRecord .service .EmotionRecordService ;
1311import org .dfbf .soundlink .domain .user .entity .User ;
1412import org .dfbf .soundlink .domain .user .repository .UserRepository ;
2321import org .springframework .http .HttpStatus ;
2422
2523import java .util .Collections ;
26- import java .util .List ;
2724import java .util .Optional ;
2825
2926import static org .dfbf .soundlink .global .comm .enums .Emotions .HAPPY ;
@@ -35,132 +32,92 @@ class EmotionRecordServiceTest {
3532 @ InjectMocks
3633 private EmotionRecordService emotionRecordService ;
3734 @ Mock
38- private EmotionRecordCacheService emotionRecordCacheService ;
39- @ Mock
40- private ChatRoomRepository chatRoomRepository ;
41- @ Mock
4235 private EmotionRecordRepository emotionRecordRepository ;
4336 @ Mock
4437 private SpotifyMusicRepository spotifyMusicRepository ;
4538 @ Mock
4639 private UserRepository userRepository ;
40+ @ Mock
41+ private ChatRoomRepository chatRoomRepository ;
4742
4843
49- @ DisplayName ("감정기록 작성(제목,가수,앨범,스포티파이아이디, 감정,멘트):성공" )
44+ @ DisplayName ("감정기록 작성(제목, 가수, 앨범, 스포티파이 아이디, 감정, 멘트): 성공" )
5045 @ Test
5146 void saveEmotionRecordWithMusic_SUCCESS () {
52-
53- // 각 테스트 전에 캐시 제거 호출은 아무 동작도 하지 않도록 설정
54- doNothing ().when (emotionRecordCacheService )
55- .evictEmotionRecordCache (any (), any (), any ());
56-
5747 // given
5848 Long userId = 1L ;
5949 EmotionRecordRequestDTO requestDTO = new EmotionRecordRequestDTO (
6050 "spotify1233" , "New videoId" , "New Title" , "New Artist" , "New Image" , HAPPY , "Test comment"
6151 );
6252
6353 User mockUser = mock (User .class );
64- SpotifyMusic newSpotifyMusic = new SpotifyMusic (requestDTO .spotifyId (), requestDTO .title (), requestDTO .artist (), requestDTO .albumImage (),requestDTO .videoId ());
65- EmotionRecord newEmotionRecord = mock (EmotionRecord .class );
54+ when (userRepository .findById (userId )).thenReturn (Optional .of (mockUser ));
55+ when (spotifyMusicRepository .findBySpotifyId (requestDTO .spotifyId ())).thenReturn (Optional .empty ());
56+
57+ SpotifyMusic newSpotifyMusic = SpotifyMusic .builder ()
58+ .spotifyId (requestDTO .spotifyId ())
59+ .videoId (requestDTO .videoId ())
60+ .title (requestDTO .title ())
61+ .artist (requestDTO .artist ())
62+ .albumImage (requestDTO .albumImage ())
63+ .build ();
64+ when (spotifyMusicRepository .save (any (SpotifyMusic .class ))).thenReturn (newSpotifyMusic );
6665
67- when (userRepository .findById (userId )).thenReturn (Optional .of (mockUser )); // 사용자 조회 성공
68- when (spotifyMusicRepository .save (any (SpotifyMusic .class ))).thenReturn (newSpotifyMusic ); // 음악 저장
69- when (emotionRecordRepository .save (any (EmotionRecord .class ))).thenReturn (newEmotionRecord ); // 감정 기록 저장
66+ EmotionRecord newEmotionRecord = mock (EmotionRecord .class );
67+ when (emotionRecordRepository .save (any (EmotionRecord .class ))).thenReturn (newEmotionRecord );
7068
7169 // when
7270 ResponseResult result = emotionRecordService .saveEmotionRecordWithMusic (userId , requestDTO );
7371
7472 // then
7573 assertEquals (HttpStatus .OK .value (), result .getCode ());
76- verify (spotifyMusicRepository ).save (any (SpotifyMusic .class )); // 음악이 저장되었는지 검증
77- verify (emotionRecordRepository ).save (any (EmotionRecord .class )); // 감정 기록이 저장되었는지 검증
74+ verify (spotifyMusicRepository ).save (any (SpotifyMusic .class ));
75+ verify (emotionRecordRepository ).save (any (EmotionRecord .class ));
7876 }
7977
80- //감정기록 삭제 : 성공
81- @ DisplayName ("감정기록 삭제:성공" )
78+ @ DisplayName ("감정기록 삭제: 성공" )
8279 @ Test
8380 void deleteEmotionRecord_SUCCESS () {
8481 // given
8582 Long recordId = 1L ;
86-
87- // 모의 User, SpotifyMusic, Emotions 생성 및 스텁 처리
88- User mockUser = mock (User .class );
89- when (mockUser .getUserId ()).thenReturn (100L ); // 예시 값
90-
91- SpotifyMusic mockMusic = mock (SpotifyMusic .class );
92- when (mockMusic .getSpotifyId ()).thenReturn ("spotify123" );
93-
83+ // delete 호출 전, 해당 record가 존재함을 확인하기 위한 stub 추가
9484 EmotionRecord mockRecord = mock (EmotionRecord .class );
95-
96- // 실제 존재하는 EmotionRecord 모의 객체 (캐싱제거 처리로 추가)
97- when (mockRecord .getUser ()).thenReturn (mockUser );
98- when (mockRecord .getSpotifyMusic ()).thenReturn (mockMusic );
99- when (mockRecord .getEmotion ()).thenReturn (Emotions .HAPPY );
100-
101- // 채팅방 모의 객체 생성 (해당 EmotionRecord를 참조하는 채팅방 존재)
102- ChatRoom mockChatRoom = mock (ChatRoom .class );
103- List <ChatRoom > mockChatRooms = Collections .singletonList (mockChatRoom );
104-
105- // 스텁 설정
106- when (chatRoomRepository .findByRecordId (mockRecord ))
107- .thenReturn (mockChatRooms );
10885 when (emotionRecordRepository .findByRecordId (recordId )).thenReturn (Optional .of (mockRecord ));
109- when (emotionRecordRepository .deleteByRecordId (recordId )).thenReturn (1 ); // 삭제된 레코드 수 1개
110-
111- // 캐시 제거 호출은 아무 동작도 하지 않도록 설정
112- doNothing ().when (emotionRecordCacheService )
113- .evictEmotionRecordCache (any (), any (), any ());
86+ // 채팅방 조회 시 빈 리스트 반환 (삭제할 채팅방 없음)
87+ when (chatRoomRepository .findByRecordId (mockRecord )).thenReturn (Collections .emptyList ());
88+ when (emotionRecordRepository .deleteByRecordId (recordId )).thenReturn (1 );
11489
11590 // when
11691 ResponseResult result = emotionRecordService .deleteEmotionRecord (recordId );
11792
11893 // then
119- assertEquals (200 , result .getCode ());
120- verify (emotionRecordRepository ).findByRecordId (recordId );
121- verify (chatRoomRepository ).findByRecordId (mockRecord );
122- verify (chatRoomRepository ).deleteAll (mockChatRooms );
123- verify (chatRoomRepository ).flush ();
94+ assertEquals (HttpStatus .OK .value (), result .getCode ());
12495 verify (emotionRecordRepository ).deleteByRecordId (recordId );
125- verify (emotionRecordRepository ).flush ();
126- verify (emotionRecordCacheService ).evictEmotionRecordCache (any (), any (), any ());
12796 }
12897
129-
13098 @ Test
131- @ DisplayName ("감정 기록 수정 성공 테스트 " )
99+ @ DisplayName ("감정 기록 수정: 성공" )
132100 void updateEmotionRecord_Success () {
133-
134- // 각 테스트 전에 캐시 제거 호출은 아무 동작도 하지 않도록 설정
135- doNothing ().when (emotionRecordCacheService )
136- .evictEmotionRecordCache (any (), any (), any ());
137-
138101 // given
139102 Long recordId = 1L ;
140103 EmotionRecordUpdateRequestDTO updateDTO = new EmotionRecordUpdateRequestDTO (
141- "spotify1233" , "New videoId" , "New Title" , "New Artist" , "New Image" ,"HAPPY" , "Test comment"
104+ "spotify1233" , "New videoId" , "New Title" , "New Artist" , "New Image" , "HAPPY" , "Test comment"
142105 );
143106
144- // 기존 감정 기록 및 음악 정보
145- SpotifyMusic existingMusic = new SpotifyMusic ("spotify123" , "1234" ,"Old Title" , "Old Artist" , "Old Image" );
146- EmotionRecord existingRecord = new EmotionRecord (
147- mock (User .class ), Emotions .SAD , "Old Comment" , existingMusic
148- );
107+ SpotifyMusic existingMusic = new SpotifyMusic ("spotify1233" , "Old videoId" , "Old Title" , "Old Artist" , "Old Image" );
108+ EmotionRecord existingRecord = new EmotionRecord (mock (User .class ), Emotions .SAD , "Old Comment" , existingMusic );
149109
150- // 기존 감정 기록 조회
110+ // 기존 감정 기록 조회 stub
151111 when (emotionRecordRepository .findByRecordId (recordId )).thenReturn (Optional .of (existingRecord ));
152112 when (spotifyMusicRepository .findBySpotifyId (updateDTO .spotifyId ())).thenReturn (Optional .of (existingMusic ));
153113
154114 // when
155115 ResponseResult result = emotionRecordService .updateEmotionRecord (recordId , updateDTO );
156116
157117 // then
158- assertEquals (200 , result .getCode ());
118+ assertEquals (HttpStatus . OK . value () , result .getCode ());
159119 verify (emotionRecordRepository ).findByRecordId (recordId );
160120 verify (spotifyMusicRepository ).findBySpotifyId (updateDTO .spotifyId ());
161121 verify (emotionRecordRepository , never ()).save (any ()); // update는 save 호출 안 함
162122 }
163-
164-
165-
166123}
0 commit comments