Skip to content

Commit 15813d7

Browse files
committed
feat: 봉사자 to 기관 쪽지 송신 기능 엔드포인트 구현
- 컨트롤러 구현 - 테스트 코드 작성및 검증 완료
1 parent 7d5e6f9 commit 15813d7

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.somemore.note.controller;
2+
3+
import com.somemore.auth.annotation.CurrentUser;
4+
import com.somemore.global.common.response.ApiResponse;
5+
import com.somemore.note.dto.SendNoteToCenterRequestDto;
6+
import com.somemore.note.usecase.SendNoteToCenterUseCase;
7+
import io.swagger.v3.oas.annotations.Operation;
8+
import io.swagger.v3.oas.annotations.tags.Tag;
9+
import jakarta.validation.Valid;
10+
import lombok.RequiredArgsConstructor;
11+
import org.springframework.security.access.annotation.Secured;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import java.util.UUID;
18+
19+
@Tag(name = "Note Command API", description = "쪽지 송신 삭제 API")
20+
@RequiredArgsConstructor
21+
@RequestMapping("/api/note")
22+
@RestController
23+
public class NoteCommandApiController {
24+
25+
private final SendNoteToCenterUseCase sendNoteToCenterUseCase;
26+
27+
@Secured("ROLE_VOLUNTEER")
28+
@Operation(summary = "봉사자 to 기관 쪽지 송신")
29+
@PostMapping(value = "/volunteer-to-center")
30+
public ApiResponse<Long> sendNoteToCenter(@CurrentUser UUID userId, @Valid @RequestBody SendNoteToCenterRequestDto requestDto) {
31+
32+
Long noteId = sendNoteToCenterUseCase.sendNoteToCenter(userId, requestDto);
33+
34+
return ApiResponse.ok(201, noteId, "쪽지 송신 성공");
35+
}
36+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)