Skip to content

Commit 9f6b39c

Browse files
authored
Merge pull request #268 from Link-MIND/Fix/#267
2 parents 17c9bd4 + 0d14bf9 commit 9f6b39c

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

linkmind/src/main/java/com/app/toaster/exception/Error.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ public enum Error {
2222
NOT_FOUND_TIMER(HttpStatus.NOT_FOUND, "찾을 수 없는 타이머입니다."),
2323
NOT_FOUND_POPUP_EXCEPTION(HttpStatus.NOT_FOUND, "유효하지 않은 팝업입니다."),
2424

25+
2526
/**
2627
* 400 BAD REQUEST EXCEPTION
2728
*/
2829
BAD_REQUEST_ISREAD(HttpStatus.BAD_REQUEST, "isRead 값이 잘못요청 되었습니다."),
2930
BAD_REQUEST_ID(HttpStatus.BAD_REQUEST, "잘못된 id값입니다."),
31+
BAD_REQUEST_EMPTY_URL(HttpStatus.BAD_REQUEST, "빈 url로는 저장할 수 없습니다."),
32+
3033
BAD_REQUEST_VALIDATION(HttpStatus.BAD_REQUEST, "유효한 값으로 요청을 다시 보내주세요."),
3134
BAD_REQUEST_FILE_EXTENSION(HttpStatus.BAD_REQUEST, "파일형식이 잘못된 것 같습니다."),
3235
BAD_REQUEST_FILE_SIZE(HttpStatus.BAD_REQUEST, "파일크기가 잘못된 것 같습니다. 최대 5MB"),

linkmind/src/main/java/com/app/toaster/toast/service/ToastService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public class ToastService {
5151
public void createToast(Long userId, SaveToastDto saveToastDto){
5252
//해당 유저 탐색
5353
User presentUser = findUser(userId);
54+
if (saveToastDto.linkUrl() ==null || saveToastDto.linkUrl().isBlank()){
55+
throw new CustomException(Error.BAD_REQUEST_EMPTY_URL, Error.BAD_REQUEST_EMPTY_URL.getMessage());
56+
}
5457
//토스트 생성
5558
try {
56-
System.out.println(saveToastDto.linkUrl());
5759
OgResponse res = parsingService.getOg(saveToastDto.linkUrl());
5860
//byte 배열로 읽어들임.
5961
log.info(res.titleAdvanced());
@@ -63,6 +65,7 @@ public void createToast(Long userId, SaveToastDto saveToastDto){
6365
// log.info(realRes.fileName());
6466
// log.info(realRes.preSignedUrl());
6567

68+
6669
//presigned url
6770
Toast toast = Toast.builder()
6871
.user(presentUser)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.app.toaster.toast.service;
2+
3+
import com.app.toaster.category.domain.Category;
4+
import com.app.toaster.category.infrastructure.CategoryRepository;
5+
import com.app.toaster.exception.Error;
6+
import com.app.toaster.exception.model.CustomException;
7+
import com.app.toaster.parse.controller.response.OgResponse;
8+
import com.app.toaster.parse.service.ParsingService;
9+
import com.app.toaster.toast.controller.request.SaveToastDto;
10+
import com.app.toaster.toast.domain.Toast;
11+
import com.app.toaster.toast.infrastructure.ToastRepository;
12+
import com.app.toaster.user.domain.User;
13+
import com.app.toaster.user.infrastructure.UserRepository;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.DisplayName;
16+
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.extension.ExtendWith;
18+
import org.mockito.ArgumentCaptor;
19+
import org.mockito.InjectMocks;
20+
import org.mockito.Mock;
21+
import org.mockito.junit.jupiter.MockitoExtension;
22+
23+
import java.io.IOException;
24+
import java.util.Optional;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.mockito.Mockito.*;
29+
30+
@ExtendWith(MockitoExtension.class)
31+
class ToastServiceTest {
32+
33+
@Mock
34+
private ToastRepository toastRepository;
35+
36+
@Mock
37+
private ParsingService parsingService;
38+
39+
@Mock
40+
private CategoryRepository categoryRepository;
41+
42+
@Mock
43+
private UserRepository userRepository; // findUser 메소드를 위해 필요할 수 있음
44+
45+
@InjectMocks
46+
private ToastService toastService;
47+
48+
private User testUser;
49+
50+
private Category testCategory;
51+
private SaveToastDto validSaveToastDto;
52+
private OgResponse mockOgResponse;
53+
54+
@BeforeEach
55+
void setUp() {
56+
testUser = User.builder()
57+
.socialId("testUser")
58+
.nickname("test")
59+
.build();
60+
61+
validSaveToastDto = new SaveToastDto("https://example.com", 1L);
62+
63+
mockOgResponse = OgResponse.of("Test Title", "https://example.com/image.jpg");
64+
65+
testCategory = Category.builder()
66+
.priority(0)
67+
.title("hi")
68+
.user(testUser)
69+
.build();
70+
}
71+
72+
@Test
73+
@DisplayName("Toast 생성 성공 테스트")
74+
void createToast_Success() throws IOException {
75+
// given
76+
Long userId = 1L;
77+
78+
79+
when(userRepository.findByUserId(userId)).thenReturn(Optional.of(testUser));
80+
when(parsingService.getOg(validSaveToastDto.linkUrl())).thenReturn(mockOgResponse);
81+
when(categoryRepository.findById(userId)).thenReturn(Optional.of(testCategory));
82+
83+
// when
84+
toastService.createToast(userId, validSaveToastDto);
85+
86+
// then
87+
verify(parsingService, times(1)).getOg(validSaveToastDto.linkUrl());
88+
verify(toastRepository, times(1)).save(any(Toast.class));
89+
90+
// Toast 객체가 올바르게 생성되는지 확인
91+
ArgumentCaptor<Toast> toastCaptor = ArgumentCaptor.forClass(Toast.class);
92+
verify(toastRepository).save(toastCaptor.capture());
93+
94+
Toast savedToast = toastCaptor.getValue();
95+
assertThat(savedToast.getUser()).isEqualTo(testUser);
96+
assertThat(savedToast.getLinkUrl()).isEqualTo(validSaveToastDto.linkUrl());
97+
assertThat(savedToast.getTitle()).isEqualTo(mockOgResponse.titleAdvanced());
98+
}
99+
100+
@Test
101+
@DisplayName("URL이 null인 경우 EMPTY_URL 에러를 반환한다.")
102+
void createToast_Fail_NullUrl() {
103+
// given
104+
Long userId = 1L;
105+
106+
SaveToastDto invalidDto = new SaveToastDto(null, 1L);
107+
108+
when(userRepository.findByUserId(userId)).thenReturn(Optional.of(testUser));
109+
110+
// when & then
111+
CustomException exception = assertThrows(CustomException.class,
112+
() -> toastService.createToast(userId, invalidDto));
113+
114+
assertThat(exception.getError()).isEqualTo(Error.BAD_REQUEST_EMPTY_URL);
115+
}
116+
117+
@Test
118+
@DisplayName("URL이 빈 문자열인 경우 EMPTY_URL에러를 반환한다.")
119+
void createToast_Fail_EmptyUrl() {
120+
// given
121+
Long userId = 1L;
122+
123+
SaveToastDto invalidDto = new SaveToastDto("", 1L);
124+
SaveToastDto invalidDto2 = new SaveToastDto(" ", 1L);
125+
126+
127+
when(userRepository.findByUserId(userId)).thenReturn(Optional.of(testUser));
128+
129+
// when & then
130+
CustomException exception1 = assertThrows(CustomException.class,
131+
() -> toastService.createToast(userId, invalidDto));
132+
CustomException exception2 = assertThrows(CustomException.class,
133+
() -> toastService.createToast(userId, invalidDto2));
134+
135+
assertThat(exception1.getError()).isEqualTo(Error.BAD_REQUEST_EMPTY_URL);
136+
assertThat(exception2.getError()).isEqualTo(Error.BAD_REQUEST_EMPTY_URL);
137+
138+
}
139+
140+
141+
142+
}

0 commit comments

Comments
 (0)