22
33import com .somemore .ControllerTestSupport ;
44import com .somemore .WithMockCustomUser ;
5+ import com .somemore .global .exception .NoSuchElementException ;
6+ import com .somemore .note .repository .mapper .NoteDetailViewForCenter ;
7+ import com .somemore .note .repository .mapper .NoteDetailViewForVolunteer ;
58import com .somemore .note .repository .mapper .NoteReceiverViewForCenter ;
69import com .somemore .note .repository .mapper .NoteReceiverViewForVolunteer ;
710import com .somemore .note .usecase .NoteQueryUseCase ;
1417import org .springframework .data .domain .Pageable ;
1518import org .springframework .http .MediaType ;
1619
20+ import java .time .LocalDateTime ;
1721import java .util .List ;
1822import java .util .UUID ;
1923
24+ import static com .somemore .global .exception .ExceptionMessage .NOT_EXISTS_NOTE ;
2025import static org .mockito .ArgumentMatchers .any ;
2126import static org .mockito .ArgumentMatchers .eq ;
2227import static org .mockito .Mockito .when ;
@@ -122,4 +127,106 @@ void getNotesByVolunteerId() throws Exception {
122127 .andExpect (jsonPath ("$.data.pageable.pageSize" ).value (6 ))
123128 .andExpect (jsonPath ("$.data.totalElements" ).value (2 ));
124129 }
130+
131+ @ DisplayName ("기관은 특정 쪽지의 상세 내용을 조회할 수 있다. (Controller)" )
132+ @ Test
133+ @ WithMockCustomUser (role = "CENTER" )
134+ void getNoteDetailForCenter () throws Exception {
135+ // given
136+ Long noteId = 1L ;
137+ NoteDetailViewForCenter mockResponse = new NoteDetailViewForCenter (
138+ noteId ,
139+ "문의 드립니다." ,
140+ "상세 내용입니다." ,
141+ UUID .randomUUID (),
142+ "봉사왕" ,
143+ "profileImgLinkHere" ,
144+ LocalDateTime .now ()
145+ );
146+
147+ when (noteQueryUseCase .getNoteDetailForCenter (noteId ))
148+ .thenReturn (mockResponse );
149+
150+ // When & Then
151+ mockMvc .perform (get ("/api/note/center/{noteId}" , noteId )
152+ .accept (MediaType .APPLICATION_JSON ))
153+ .andExpect (status ().isOk ())
154+ .andExpect (jsonPath ("$.code" ).value (200 ))
155+ .andExpect (jsonPath ("$.message" ).value ("쪽지 상세 조회 성공" ))
156+ .andExpect (jsonPath ("$.data.note_id" ).value (noteId ))
157+ .andExpect (jsonPath ("$.data.title" ).value ("문의 드립니다." ))
158+ .andExpect (jsonPath ("$.data.content" ).value ("상세 내용입니다." ))
159+ .andExpect (jsonPath ("$.data.sender_name" ).value ("봉사왕" ))
160+ .andExpect (jsonPath ("$.data.sender_profile_img_link" ).value ("profileImgLinkHere" ));
161+ }
162+
163+ @ DisplayName ("봉사자는 특정 쪽지의 상세 내용을 조회할 수 있다. (Controller)" )
164+ @ Test
165+ @ WithMockCustomUser
166+ void getNoteDetailForVolunteer () throws Exception {
167+ // given
168+ Long noteId = 1L ;
169+ NoteDetailViewForVolunteer mockResponse = new NoteDetailViewForVolunteer (
170+ noteId ,
171+ "답변 드립니다." ,
172+ "상세 내용입니다." ,
173+ UUID .randomUUID (),
174+ "서울 도서관" ,
175+ "profileImgLinkHere" ,
176+ LocalDateTime .now ()
177+ );
178+
179+ when (noteQueryUseCase .getNoteDetailForVolunteer (noteId ))
180+ .thenReturn (mockResponse );
181+
182+ // When & Then
183+ mockMvc .perform (get ("/api/note/volunteer/{noteId}" , noteId )
184+ .accept (MediaType .APPLICATION_JSON ))
185+ .andExpect (status ().isOk ())
186+ .andExpect (jsonPath ("$.code" ).value (200 ))
187+ .andExpect (jsonPath ("$.message" ).value ("쪽지 상세 조회 성공" ))
188+ .andExpect (jsonPath ("$.data.note_id" ).value (noteId ))
189+ .andExpect (jsonPath ("$.data.title" ).value ("답변 드립니다." ))
190+ .andExpect (jsonPath ("$.data.content" ).value ("상세 내용입니다." ))
191+ .andExpect (jsonPath ("$.data.sender_name" ).value ("서울 도서관" ))
192+ .andExpect (jsonPath ("$.data.sender_profile_img_link" ).value ("profileImgLinkHere" ));
193+ }
194+
195+ @ DisplayName ("존재하지 않는 쪽지를 기관이 조회할 경우 예외를 반환한다." )
196+ @ Test
197+ @ WithMockCustomUser (role = "CENTER" )
198+ void getNoteDetailForCenter_NotFound () throws Exception {
199+ // given
200+ Long nonExistentNoteId = 9999L ;
201+
202+ when (noteQueryUseCase .getNoteDetailForCenter (nonExistentNoteId ))
203+ .thenThrow (new NoSuchElementException (NOT_EXISTS_NOTE ));
204+
205+ // When & Then
206+ mockMvc .perform (get ("/api/note/center/{noteId}" , nonExistentNoteId )
207+ .accept (MediaType .APPLICATION_JSON ))
208+ .andExpect (status ().isBadRequest ())
209+ .andExpect (jsonPath ("$.status" ).value (400 ))
210+ .andExpect (jsonPath ("$.title" ).value ("데이터가 존재하지 않음" ))
211+ .andExpect (jsonPath ("$.detail" ).value ("존재하지 않는 쪽지입니다." ));
212+ }
213+
214+ @ DisplayName ("존재하지 않는 쪽지를 봉사자가 조회할 경우 예외를 반환한다." )
215+ @ Test
216+ @ WithMockCustomUser
217+ void getNoteDetailForVolunteer_NotFound () throws Exception {
218+ // given
219+ Long nonExistentNoteId = 9999L ;
220+
221+ when (noteQueryUseCase .getNoteDetailForVolunteer (nonExistentNoteId ))
222+ .thenThrow (new NoSuchElementException (NOT_EXISTS_NOTE ));
223+
224+ // When & Then
225+ mockMvc .perform (get ("/api/note/volunteer/{noteId}" , nonExistentNoteId )
226+ .accept (MediaType .APPLICATION_JSON ))
227+ .andExpect (jsonPath ("$.status" ).value (400 ))
228+ .andExpect (jsonPath ("$.title" ).value ("데이터가 존재하지 않음" ))
229+ .andExpect (jsonPath ("$.detail" ).value ("존재하지 않는 쪽지입니다." ));
230+ }
231+
125232}
0 commit comments