Skip to content

Commit 77d4563

Browse files
authored
Merge #90 from fix/emotionRecord/main
카카오 랜덤 닉네임 형식 변경 및 메인 서비스에 캐싱 처리로 인한 문제가 발생하여 우선적으로 해당 로직을 비활성화 시킴
2 parents 1ddfc81 + c373ce0 commit 77d4563

File tree

4 files changed

+225
-90
lines changed

4 files changed

+225
-90
lines changed

src/main/java/org/dfbf/soundlink/domain/emotionRecord/service/EmotionRecordService.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.dfbf.soundlink.domain.emotionRecord.repository.SpotifyMusicRepository;
1717
import org.dfbf.soundlink.domain.user.entity.User;
1818
import org.dfbf.soundlink.domain.user.repository.UserRepository;
19+
import org.dfbf.soundlink.global.comm.enums.Emotions;
1920
import org.dfbf.soundlink.global.exception.ErrorCode;
2021
import org.dfbf.soundlink.global.exception.ResponseResult;
2122
import org.springframework.dao.DataAccessException;
@@ -37,7 +38,7 @@ public class EmotionRecordService {
3738
private final EmotionRecordRepository emotionRecordRepository;
3839
private final UserRepository userRepository;
3940

40-
private final EmotionRecordCacheService emotionRecordCacheService;
41+
// private final EmotionRecordCacheService emotionRecordCacheService;
4142
private final ChatRoomRepository chatRoomRepository;
4243

4344
@Transactional
@@ -67,12 +68,12 @@ public ResponseResult saveEmotionRecordWithMusic(Long userId, EmotionRecordReque
6768
.build();
6869
emotionRecordRepository.save(emotionRecord);
6970

70-
// 게시글 생성 시, 해당 조건에 맞는 캐시 키 삭제
71+
/* // 게시글 생성 시, 해당 조건에 맞는 캐시 키 삭제
7172
emotionRecordCacheService.evictEmotionRecordCache(
7273
userId,
7374
request.spotifyId(),
7475
request.emotion().name()
75-
);
76+
);*/
7677

7778
return new ResponseResult(ErrorCode.SUCCESS);
7879
} catch (UserNotFoundException e) {
@@ -110,22 +111,33 @@ public ResponseResult getEmotionRecordsByLoginId(String userTag, int page, int s
110111
}
111112
}
112113

113-
// 동적 검색 (로그인 사용자를 제외한 EmotionRecord 조회) - 캐시 적용
114-
@Transactional(readOnly = true)
115114
public ResponseResult getEmotionRecordsExcludingUserIdByFilters(Long userId, List<String> emotionList, String spotifyId, int page, int size) {
115+
List<Emotions> emotionEnums = null;
116116
ResponseResult pageValidationResult = validateAndCreatePageable(page, size);
117117
if (pageValidationResult.getCode() != 200 /*SUCCESS*/) {
118118
return pageValidationResult;
119119
}
120120

121+
Pageable pageable = (Pageable) pageValidationResult.getData();
122+
123+
if (emotionList != null && !emotionList.isEmpty()) {
124+
try {
125+
emotionEnums = emotionList.stream()
126+
.map(e -> Emotions.valueOf(e.toUpperCase()))
127+
.toList();
128+
} catch (IllegalArgumentException e) {
129+
return new ResponseResult(ErrorCode.FAIL_TO_FIND_EMOTION, "잘못된 감정 값이 포함되어 있습니다.");
130+
}
131+
}
132+
121133
try {
122-
// EmotionRecordCacheService의 결합 캐시 조회 및 Fallback 처리
123-
EmotionRecordPageResponseDTO<EmotionRecordResponseMainDTO> result =
124-
emotionRecordCacheService.getEmotionRecords(userId, emotionList, spotifyId, page, size);
125-
return new ResponseResult(ErrorCode.SUCCESS, result);
126-
} catch (IllegalArgumentException e) {
127-
// 잘못된 감정 값이 포함된 경우
128-
return new ResponseResult(ErrorCode.FAIL_TO_FIND_EMOTION, e.getMessage());
134+
Page<EmotionRecord> recordsPage = emotionRecordRepository.findByFilters(userId, emotionEnums, spotifyId, pageable);
135+
List<EmotionRecordResponseMainDTO> dtoList = recordsPage.getContent()
136+
.stream()
137+
.map(EmotionRecordResponseMainDTO::fromEntity)
138+
.toList();
139+
140+
return new ResponseResult(ErrorCode.SUCCESS, EmotionRecordPageResponseDTO.fromPage(recordsPage, dtoList));
129141
} catch (DataAccessException e) {
130142
return new ResponseResult(ErrorCode.DB_ERROR, e.getMessage());
131143
} catch (Exception e) {
@@ -195,12 +207,12 @@ public ResponseResult updateEmotionRecord(Long recordId, EmotionRecordUpdateRequ
195207
// 수정된 정보를 Response DTO로 변환
196208
EmotionRecordUpdateResponseDTO responseDTO = EmotionRecordUpdateResponseDTO.fromEntity(emotionRecord);
197209

198-
// 게시글 수정 시, 해당 조건에 맞는 캐시 키 삭제
210+
/* // 게시글 수정 시, 해당 조건에 맞는 캐시 키 삭제
199211
emotionRecordCacheService.evictEmotionRecordCache(
200212
emotionRecord.getUser().getUserId(),
201213
updateDTO.spotifyId(),
202214
updateDTO.emotion()
203-
);
215+
);*/
204216
return new ResponseResult(ErrorCode.SUCCESS, responseDTO);
205217
} catch (EmotionRecordNotFoundException e) {
206218
return new ResponseResult(ErrorCode.FAIL_TO_FIND_EMOTION_RECORD, e.getMessage());
@@ -229,12 +241,12 @@ public ResponseResult deleteEmotionRecord(Long recordId) {
229241
int deletedCount = emotionRecordRepository.deleteByRecordId(recordId);
230242
emotionRecordRepository.flush(); // 즉시 DB에 반영
231243

232-
// 게시글 삭제 시, 해당 조건에 맞는 캐시 키 삭제
244+
/* // 게시글 삭제 시, 해당 조건에 맞는 캐시 키 삭제
233245
emotionRecordCacheService.evictEmotionRecordCache(
234246
emotionRecord.getUser().getUserId(),
235247
emotionRecord.getSpotifyMusic().getSpotifyId(),
236248
emotionRecord.getEmotion().name()
237-
);
249+
);*/
238250

239251
// 삭제할 데이터가 없는 경우
240252
if (deletedCount == 0) {

src/main/java/org/dfbf/soundlink/domain/user/service/KakaoAuthService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private String generateUniqueNickname(String baseNickname) {
128128

129129
// 닉네임이 중복되지 않을 때까지 반복
130130
while (userRepository.existsByNickname(newNickname)) {
131-
newNickname = baseNickname + "_" + suffix;
131+
newNickname = "k" + suffix;
132132
suffix++;
133133
}
134134
return newNickname;
Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.dfbf.soundlink.domain.emotionRecord;
22

3-
import org.dfbf.soundlink.domain.chat.entity.ChatRoom;
43
import org.dfbf.soundlink.domain.chat.repository.ChatRoomRepository;
54
import org.dfbf.soundlink.domain.emotionRecord.dto.request.EmotionRecordRequestDTO;
65
import org.dfbf.soundlink.domain.emotionRecord.dto.request.EmotionRecordUpdateRequestDTO;
76
import org.dfbf.soundlink.domain.emotionRecord.entity.EmotionRecord;
87
import org.dfbf.soundlink.domain.emotionRecord.entity.SpotifyMusic;
98
import org.dfbf.soundlink.domain.emotionRecord.repository.EmotionRecordRepository;
109
import org.dfbf.soundlink.domain.emotionRecord.repository.SpotifyMusicRepository;
11-
import org.dfbf.soundlink.domain.emotionRecord.service.EmotionRecordCacheService;
1210
import org.dfbf.soundlink.domain.emotionRecord.service.EmotionRecordService;
1311
import org.dfbf.soundlink.domain.user.entity.User;
1412
import org.dfbf.soundlink.domain.user.repository.UserRepository;
@@ -23,7 +21,6 @@
2321
import org.springframework.http.HttpStatus;
2422

2523
import java.util.Collections;
26-
import java.util.List;
2724
import java.util.Optional;
2825

2926
import 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

Comments
 (0)