From 101c480e01822ce2e9cdfc5cc4007e80c4f19cd0 Mon Sep 17 00:00:00 2001 From: Dong Seok Lee Date: Fri, 22 Dec 2023 17:37:01 +0900 Subject: [PATCH] [refactor] add testCode(PostController half~getPostsV2_fail_due_to_parsing_error) --- .../bside_311/controller/PostController.java | 3 +- .../controller/PostControllerTest.java | 157 ++++++++++++++++++ 2 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 server/src/test/java/com/bside/bside_311/controller/PostControllerTest.java diff --git a/server/src/main/java/com/bside/bside_311/controller/PostController.java b/server/src/main/java/com/bside/bside_311/controller/PostController.java index 1ea513e..0f682d5 100644 --- a/server/src/main/java/com/bside/bside_311/controller/PostController.java +++ b/server/src/main/java/com/bside/bside_311/controller/PostController.java @@ -13,7 +13,6 @@ import com.bside.bside_311.dto.GetQuotesByPostResponseDto; import com.bside.bside_311.dto.PostResponseDto; import com.bside.bside_311.entity.Post; -import com.bside.bside_311.service.AttachService; import com.bside.bside_311.service.PostService; import com.bside.bside_311.util.AuthUtil; import io.swagger.v3.oas.annotations.Operation; @@ -50,11 +49,11 @@ @Tag(name = "게시글", description = "게시글 API") public class PostController { private final PostService postService; - private final AttachService attachService; @Operation(summary = "[o]게시글 등록 ", description = "게시글 등록 API") @UserRequired @PostMapping + @ResponseStatus(HttpStatus.CREATED) public AddPostResponseDto addPost(@RequestBody @Valid AddPostRequestDto addPostRequestDto) { log.info(">>> PostController.addPost"); return postService.addPost(Post.of(addPostRequestDto), addPostRequestDto.getAlcoholNo(), diff --git a/server/src/test/java/com/bside/bside_311/controller/PostControllerTest.java b/server/src/test/java/com/bside/bside_311/controller/PostControllerTest.java new file mode 100644 index 0000000..39d000c --- /dev/null +++ b/server/src/test/java/com/bside/bside_311/controller/PostControllerTest.java @@ -0,0 +1,157 @@ +package com.bside.bside_311.controller; + +import com.bside.bside_311.dto.AddPostRequestDto; +import com.bside.bside_311.dto.AddPostResponseDto; +import com.bside.bside_311.entity.Post; +import com.bside.bside_311.service.PostService; +import com.bside.bside_311.util.JwtUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(PostController.class) +class PostControllerTest extends ControllerTest { + @Autowired + private MockMvc mockMvc; + + @MockBean + private PostService postService; + + @Autowired + private ObjectMapper objectMapper; + + @Test + void addPost_success() throws Exception { + //given + AddPostRequestDto addPostRequestDto = + AddPostRequestDto.builder().alcoholNo(1L).alcoholFeature("산뜻함. 달콤함.").postContent("게시글 내용") + .postType("BASIC").positionInfo("위치 정보").build(); + given(postService.addPost(any(), any(), any(), any())).willReturn( + AddPostResponseDto.of(Post.builder().id(1L).build())); + //when + //then + mockMvc.perform(post("/posts") + .header(HttpHeaders.AUTHORIZATION, JwtUtil.BEARER_PREFIX + userAccessToken) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(addPostRequestDto))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.postNo").exists()); + } + + @Test + void editPost_success() throws Exception { + //given + AddPostRequestDto addPostRequestDto = + AddPostRequestDto.builder().alcoholNo(1L).alcoholFeature("산뜻함. 달콤함.").postContent("게시글 내용") + .postType("BASIC").positionInfo("위치 정보").build(); + //when + //then + mockMvc.perform(patch("/posts/1") + .header(HttpHeaders.AUTHORIZATION, JwtUtil.BEARER_PREFIX + userAccessToken) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(addPostRequestDto))) + .andExpect(status().isOk()); + } + + @Test + void deletePost_success() throws Exception { + //given + AddPostRequestDto addPostRequestDto = + AddPostRequestDto.builder().alcoholNo(1L).alcoholFeature("산뜻함. 달콤함.").postContent("게시글 내용") + .postType("BASIC").positionInfo("위치 정보").build(); + //when + //then + mockMvc.perform(delete("/posts/1") + .header(HttpHeaders.AUTHORIZATION, JwtUtil.BEARER_PREFIX + userAccessToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + void getPostsV2_success() throws Exception { + //given + AddPostRequestDto addPostRequestDto = + AddPostRequestDto.builder().alcoholNo(1L).alcoholFeature("산뜻함. 달콤함.").postContent("게시글 내용") + .postType("BASIC").positionInfo("위치 정보").build(); + //when + //then + String queryParameter = "?page=1&size=10&sort=createdAt,desc&searchKeyword=test"; + String queryParameterSearchUserNos = "&searchUserNos=1,2,3"; + String queryParameterSearchAlcoholNos = "&searchAlcoholNos=1,2,3"; + mockMvc.perform( + get(String.format("/posts/v2%s%s%s", queryParameter, queryParameterSearchUserNos, + queryParameterSearchAlcoholNos))) + .andExpect(status().isOk()); + } + + @Test + void getPostsV2_fail_due_to_parsing_error() throws Exception { + //given + AddPostRequestDto addPostRequestDto = + AddPostRequestDto.builder().alcoholNo(1L).alcoholFeature("산뜻함. 달콤함.").postContent("게시글 내용") + .postType("BASIC").positionInfo("위치 정보").build(); + //when + //then + String queryParameter = "?page=1&size=10&sort=createdAt,desc&searchKeyword=test"; + String queryParameterSearchUserNos = "&searchUserNos=1,2,3_"; + String queryParameterSearchAlcoholNos = "&searchAlcoholNos=1,2,3_"; + mockMvc.perform( + get(String.format("/posts/v2%s%s%s", queryParameter, queryParameterSearchUserNos, + queryParameterSearchAlcoholNos))) + .andExpect(status().is4xxClientError()); + } + + @Test + void getPostDetail_success() { + } + + @Test + void addComment_success() { + } + + @Test + void getPostComments_success() { + } + + @Test + void editComment_success() { + } + + @Test + void deleteComment() { + } + + @Test + void addQuote() { + } + + @Test + void deleteQuote() { + } + + @Test + void getQuotesByPost() { + } + + @Test + void likePost() { + } + + @Test + void likeCancelPost() { + } +} \ No newline at end of file