|
| 1 | +package com.somemore.note.controller; |
| 2 | + |
| 3 | +import com.somemore.ControllerTestSupport; |
| 4 | +import com.somemore.WithMockCustomUser; |
| 5 | +import com.somemore.note.dto.SendNoteToCenterRequestDto; |
| 6 | +import com.somemore.note.usecase.SendNoteToCenterUseCase; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | + |
| 12 | +import java.util.UUID; |
| 13 | + |
| 14 | +import static org.mockito.ArgumentMatchers.any; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 18 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 19 | + |
| 20 | +class NoteCommandApiControllerTest extends ControllerTestSupport{ |
| 21 | + |
| 22 | + @MockBean |
| 23 | + private SendNoteToCenterUseCase sendNoteToCenterUseCase; |
| 24 | + |
| 25 | + @DisplayName("봉사자는 기관에게 쪽지를 보낼수 있다 (controller)") |
| 26 | + @Test |
| 27 | + @WithMockCustomUser |
| 28 | + void sendNoteToCenter_Success() throws Exception { |
| 29 | + // Given |
| 30 | + UUID receiverId = UUID.randomUUID(); |
| 31 | + SendNoteToCenterRequestDto requestDto = new SendNoteToCenterRequestDto( |
| 32 | + receiverId, |
| 33 | + "쪽지 제목 문의 드릴게 있습니다.", |
| 34 | + "쪽지 내용" |
| 35 | + ); |
| 36 | + |
| 37 | + when(sendNoteToCenterUseCase.sendNoteToCenter(any(UUID.class), any(SendNoteToCenterRequestDto.class))) |
| 38 | + .thenReturn(1L); |
| 39 | + |
| 40 | + // When & Then |
| 41 | + mockMvc.perform(post("/api/note/volunteer-to-center") |
| 42 | + .contentType(MediaType.APPLICATION_JSON) |
| 43 | + .content(objectMapper.writeValueAsString(requestDto))) |
| 44 | + .andExpect(status().isOk()) |
| 45 | + .andExpect(jsonPath("$.code").value(201)) |
| 46 | + .andExpect(jsonPath("$.data").value(1L)) |
| 47 | + .andExpect(jsonPath("$.message").value("쪽지 송신 성공")); |
| 48 | + } |
| 49 | + |
| 50 | + @DisplayName("쪽지 송신 시 누락된 정보가 있다면 송신을 할 수 없다") |
| 51 | + @Test |
| 52 | + void sendNoteToCenter_ValidationFail() throws Exception { |
| 53 | + // Given |
| 54 | + UUID userId = UUID.randomUUID(); |
| 55 | + SendNoteToCenterRequestDto invalidRequestDto = new SendNoteToCenterRequestDto( |
| 56 | + null, |
| 57 | + null, |
| 58 | + null |
| 59 | + ); |
| 60 | + |
| 61 | + // When & Then |
| 62 | + mockMvc.perform(post("/api/note/volunteer-to-center") |
| 63 | + .contentType(MediaType.APPLICATION_JSON) |
| 64 | + .content(objectMapper.writeValueAsString(invalidRequestDto)) |
| 65 | + .requestAttr("userId", userId)) |
| 66 | + .andExpect(status().isBadRequest()); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments