From 8d32db4a1ff7147097cee9fa0e53b20b0500f2a0 Mon Sep 17 00:00:00 2001 From: 0BVer Date: Wed, 11 May 2022 00:46:47 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20post=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20(=EB=AF=B8=EC=99=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostControllerTest.java | 178 +++++++++++++++++- 1 file changed, 175 insertions(+), 3 deletions(-) diff --git a/backend/src/test/java/com/hjjang/backend/domain/post/controller/PostControllerTest.java b/backend/src/test/java/com/hjjang/backend/domain/post/controller/PostControllerTest.java index 6f6b69d..5ed3ef6 100644 --- a/backend/src/test/java/com/hjjang/backend/domain/post/controller/PostControllerTest.java +++ b/backend/src/test/java/com/hjjang/backend/domain/post/controller/PostControllerTest.java @@ -1,18 +1,190 @@ package com.hjjang.backend.domain.post.controller; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.hjjang.backend.domain.post.domain.entity.Post; +import com.hjjang.backend.domain.post.domain.entity.PostDefaultValue; +import com.hjjang.backend.domain.post.dto.PostMapper; +import com.hjjang.backend.domain.post.dto.PostRequestDto; +import com.hjjang.backend.domain.post.dto.PostResponseDto; +import com.hjjang.backend.domain.post.service.PostServiceImpl; +import com.hjjang.backend.domain.user.entity.Agreement; +import com.hjjang.backend.domain.user.entity.RoleType; +import com.hjjang.backend.domain.user.entity.User; +import com.hjjang.backend.domain.user.repository.UserRefreshTokenRepository; +import com.hjjang.backend.global.config.properties.AuthProperties; +import com.hjjang.backend.global.config.security.handler.TokenAccessDeniedHandler; +import com.hjjang.backend.global.config.security.service.CustomOAuth2UserService; +import com.hjjang.backend.global.config.security.token.AuthTokenProvider; +import com.hjjang.backend.global.util.UserUtil; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; -import static org.junit.jupiter.api.Assertions.*; +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import static com.hjjang.backend.domain.post.domain.entity.PostDefaultValue.*; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@ExtendWith(SpringExtension.class) +@WebMvcTest(controllers = PostController.class) +//@ExtendWith(RestDocumentationExtension.class) +//@AutoConfigureRestDocs class PostControllerTest { + //TODO security 관련 종속성을 어떻게 해결해야 할지 모르겠음 아마도 MockBean 으로 만들고 모두 when then 을 설정하면 되지 않을까 싶은데 너무 비효율 적 + @MockBean private PostServiceImpl postService; + @MockBean private PostMapper postMapper; + @MockBean private UserUtil userUtil; +// @MockBean private AuthProperties authProperties; +// @MockBean private AuthTokenProvider authTokenProvider; +// @MockBean private CustomOAuth2UserService customOAuth2UserService; +// @MockBean private TokenAccessDeniedHandler tokenAccessDeniedHandler; +// @MockBean private UserRefreshTokenRepository userRefreshTokenRepository; + + private MockMvc mockMvc; + + private ObjectMapper objectMapper; + + private final long givenId = 1L; + + private final String givenTitle = "test title"; + private final String givenContent = "test content"; + private final int givenItemPrice = 10000; + + private final String changedTitle = "changed Title"; + private final String changedContent = "changed Content"; + private final int changedItemPrice = 999999; + + private final String providerId = "provider Id"; + private final String nickName = "NN"; + private final String email = "user@email.ac.kr"; + private final long mannerTemperature = 36L; + private final String imagePath = "image path"; + private final Agreement pushAgree = Agreement.AGREE; + private final long univId = 1L; + private final RoleType role = RoleType.USER; + + private final Map givenPostRequestJson = new HashMap<>(); + + private PostResponseDto expectPostResponseDto; + + private final Map expectApiResponseJson = new HashMap<>(); + @BeforeEach - void setUp() { + void setUp(WebApplicationContext context) { + objectMapper = new ObjectMapper(); + + mockMvc = MockMvcBuilders + .webAppContextSetup(context) + .addFilter(new CharacterEncodingFilter(StandardCharsets.UTF_8.name(), true)) + .build(); + + givenPostRequestJson.put("title", givenTitle); + givenPostRequestJson.put("content", givenContent); + givenPostRequestJson.put("price", String.valueOf(givenItemPrice)); + + User givenUser = User.builder() + .providerId(providerId) + .email(email) + .imageUrl(imagePath) + .isPushAgree(pushAgree) + .mannerTemperature(mannerTemperature) + .nickName(nickName) + .role(role) + .univId(univId) + .build(); + + Post expectRawPost = Post.builder() + .user(givenUser) + .title(givenTitle) + .content(givenContent) + .itemPrice(givenItemPrice) + .build(); + + User expectSavedUser = new User(givenId, providerId, nickName, email, + mannerTemperature, imagePath, LocalDateTime.now(), pushAgree, univId, role); + + Post expectSavedPost = new Post(givenId, givenUser, givenTitle, givenContent, givenItemPrice, + DEFAULT_VIEWS, DEFAULT_INTEREST_NUMBER, DEFAULT_CHAT_NUMBER, + DEFAULT_IS_SALE_COMPLETION, DEFAULT_REMOVED, LocalDateTime.now()); + + Post expectUpdatedPost = new Post(givenId, givenUser, changedTitle, changedContent, + changedItemPrice, DEFAULT_VIEWS, DEFAULT_INTEREST_NUMBER, DEFAULT_CHAT_NUMBER, + DEFAULT_IS_SALE_COMPLETION, DEFAULT_REMOVED, LocalDateTime.now()); + + expectPostResponseDto = PostResponseDto.builder() + .id(expectSavedPost.getId()) + .user_id(expectSavedPost.getUser().getId()) + .title(expectSavedPost.getTitle()) + .content(expectSavedPost.getContent()) + .item_price(expectSavedPost.getItemPrice()) + .views(expectSavedPost.getViews()) + .interest_number(expectSavedPost.getInterestNumber()) + .chat_number(expectSavedPost.getChatNumber()) + .is_sale_completion(expectSavedPost.getIsSaleCompletion().getState()) + .removed(expectSavedPost.isRemoved()) + .time(expectSavedPost.getTime().toString()) + .build(); + + expectApiResponseJson.put("id", String.valueOf(givenId)); + expectApiResponseJson.put("user_id", String.valueOf(givenId)); + expectApiResponseJson.put("title", givenTitle); + expectApiResponseJson.put("content", givenContent); + expectApiResponseJson.put("item_price", String.valueOf(givenItemPrice)); + expectApiResponseJson.put("views", String.valueOf(DEFAULT_VIEWS)); + expectApiResponseJson.put("interest_number", String.valueOf(DEFAULT_INTEREST_NUMBER)); + expectApiResponseJson.put("chat_number", String.valueOf(DEFAULT_CHAT_NUMBER)); + expectApiResponseJson.put("removed", String.valueOf(DEFAULT_REMOVED)); + expectApiResponseJson.put("time", String.valueOf(LocalDateTime.now())); + + List expectPostList = List.of(expectSavedPost); + + when(postMapper.fromEntity(any())).thenReturn(expectPostResponseDto); + when(postMapper.toEntity(any(), any())).thenReturn(expectRawPost); + when(postService.save(expectRawPost)).thenReturn(expectSavedPost); + when(postService.findAll()).thenReturn(expectPostList); + when(postService.findOneById(anyLong())).thenReturn(expectSavedPost); + when(postService.updateOneById(anyLong(), any(PostRequestDto.class), any(User.class))) + .thenReturn(expectUpdatedPost); + doNothing().when(postService).deleteOneById(anyLong()); + + when(userUtil.getLoginUserByToken()).thenReturn(expectSavedUser); } @Test - void createItem() { + void createItemTest() throws Exception { + //given + Map expectResponseEntityJson = new HashMap<>(); + expectResponseEntityJson.put("name", "createItem"); + expectResponseEntityJson.put("body", expectApiResponseJson); + //when + + //then + mockMvc.perform(RestDocumentationRequestBuilders.post("/api/posts") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(givenPostRequestJson))) + .andExpect(status().isCreated()) + .andExpect(content().json(objectMapper.writeValueAsString(expectResponseEntityJson))); } @Test