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