From aafce03b7117c5c1edada8d59877c85213191351 Mon Sep 17 00:00:00 2001 From: "jungkh405@naver.com" Date: Tue, 4 May 2021 16:09:50 +0900 Subject: [PATCH] =?UTF-8?q?test=20:=20Point=20=EC=B6=A9=EC=A0=84/=EC=B6=9C?= =?UTF-8?q?=EA=B8=88(=EC=B0=A8=EA=B0=90=EB=82=B4=EC=97=AD,=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=82=B4=EC=97=AD)=20=EA=B4=80=EB=A0=A8=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Controller Layer test (SpringRestDocs) 2. Service Layer test issue Number : #95 --- .../controller/PointApiControllerTest.java | 195 ++++++++++++++++++ .../shoeauction/service/PointServiceTest.java | 13 +- 2 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/flab/shoeauction/controller/PointApiControllerTest.java diff --git a/src/test/java/com/flab/shoeauction/controller/PointApiControllerTest.java b/src/test/java/com/flab/shoeauction/controller/PointApiControllerTest.java new file mode 100644 index 00000000..505bad74 --- /dev/null +++ b/src/test/java/com/flab/shoeauction/controller/PointApiControllerTest.java @@ -0,0 +1,195 @@ +package com.flab.shoeauction.controller; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doNothing; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; +import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields; +import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flab.shoeauction.controller.dto.PointDto.ChargeRequest; +import com.flab.shoeauction.controller.dto.PointDto.PointHistoryDto; +import com.flab.shoeauction.controller.dto.PointDto.WithdrawalRequest; +import com.flab.shoeauction.domain.point.PointDivision; +import com.flab.shoeauction.service.PointService; +import com.flab.shoeauction.service.SessionLoginService; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +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.data.jpa.mapping.JpaMetamodelMappingContext; +import org.springframework.http.MediaType; +import org.springframework.restdocs.RestDocumentationContextProvider; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.restdocs.payload.JsonFieldType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@ExtendWith(RestDocumentationExtension.class) +@WebMvcTest(PointApiController.class) +@ActiveProfiles("test") +@MockBean(JpaMetamodelMappingContext.class) +class PointApiControllerTest { + + @MockBean + private PointService pointService; + @MockBean + private SessionLoginService sessionLoginService; + + private MockMvc mockMvc; + + @Autowired + ObjectMapper objectMapper; + + @BeforeEach + public void setup(WebApplicationContext webApplicationContext, + RestDocumentationContextProvider restDocumentation) { + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .apply(documentationConfiguration(restDocumentation)) + .apply(sharedHttpSession()) + .build(); + } + + private List createPointChargeHistoryList() { + + List pointHistoryDtoList = new ArrayList<>(); + + PointHistoryDto chargeMockData = PointHistoryDto.builder() + .time(LocalDateTime.now()) + .division(PointDivision.CHARGE) + .amount(30000L) + .build(); + + pointHistoryDtoList.add(chargeMockData); + + PointHistoryDto saleMockData = PointHistoryDto.builder() + .time(LocalDateTime.now()) + .division(PointDivision.SALES_REVENUE) + .amount(40000L) + .build(); + + pointHistoryDtoList.add(saleMockData); + + return pointHistoryDtoList; + } + + private List createPointDeductionHistoryList() { + List pointHistoryDtoList = new ArrayList<>(); + + PointHistoryDto withdrawMockData = PointHistoryDto.builder() + .time(LocalDateTime.now()) + .division(PointDivision.WITHDRAW) + .amount(10000L) + .build(); + + pointHistoryDtoList.add(withdrawMockData); + + PointHistoryDto purchaseMockData = PointHistoryDto.builder() + .time(LocalDateTime.now()) + .division(PointDivision.PURCHASE_DEDUCTION) + .amount(20000L) + .build(); + + pointHistoryDtoList.add(purchaseMockData); + + return pointHistoryDtoList; + } + + @DisplayName("포인트를 충전한다.") + @Test + public void payment() throws Exception { + ChargeRequest chargeRequest = ChargeRequest.builder() + .chargeAmount(100000L) + .build(); + + String email = "test123@test.com"; + + doNothing().when(pointService).charging(email, chargeRequest); + + mockMvc.perform(post("/points/charging") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(chargeRequest))) + .andExpect(status().isOk()) + .andDo(print()) + .andDo(document("points/charging", requestFields( + fieldWithPath("chargeAmount").type(JsonFieldType.NUMBER).description("충전할 포인트") + ))); + } + + @DisplayName("포인트를 출금한다.") + @Test + public void withdrawal() throws Exception { + WithdrawalRequest withdrawalRequest = WithdrawalRequest.builder() + .password("test1234") + .withdrawalAmount(30000L) + .build(); + + String email = "test123@test.com"; + + doNothing().when(pointService).withdrawal(email, withdrawalRequest); + + mockMvc.perform(post("/points/withdrawal") + .content(objectMapper.writeValueAsString(withdrawalRequest)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()) + .andDo(document("points/withdrawal", requestFields( + fieldWithPath("withdrawalAmount").type(JsonFieldType.NUMBER).description("출금할 포인트"), + fieldWithPath("password").type(JsonFieldType.STRING).description("비밀번호") + ))); + } + + @DisplayName("포인트 추가 내역(판매 대금 / 충전)을 리턴한다.") + @Test + public void chargingHistory() throws Exception { + List chargingHistory = createPointChargeHistoryList(); + given(pointService.getChargingHistory(any())).willReturn(chargingHistory); + + mockMvc.perform(get("/points/charging-details") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()) + .andDo(document("points/history/charging", + responseFields( + fieldWithPath("[].time").type(JsonFieldType.STRING).description("생성 날짜"), + fieldWithPath("[].amount").type(JsonFieldType.NUMBER).description("충전 금액"), + fieldWithPath("[].division").type(JsonFieldType.STRING).description("종류") + ))); + } + + @DisplayName("포인트 차감 내역(구매 대금 / 출금)을 리턴한다.") + @Test + public void deductionHistory() throws Exception { + List deductionHistory = createPointDeductionHistoryList(); + given(pointService.getDeductionHistory(any())).willReturn(deductionHistory); + + mockMvc.perform(get("/points/deduction-details") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()) + .andDo(document("points/history/deduction", + responseFields( + fieldWithPath("[].time").type(JsonFieldType.STRING).description("생성 날짜"), + fieldWithPath("[].amount").type(JsonFieldType.NUMBER).description("차감 금액"), + fieldWithPath("[].division").type(JsonFieldType.STRING).description("종류") + ))); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/flab/shoeauction/service/PointServiceTest.java b/src/test/java/com/flab/shoeauction/service/PointServiceTest.java index a3b298a0..b67e955e 100644 --- a/src/test/java/com/flab/shoeauction/service/PointServiceTest.java +++ b/src/test/java/com/flab/shoeauction/service/PointServiceTest.java @@ -59,7 +59,18 @@ private User createUser() { } private List createPointBreakDownMockData() { - User tempUser = null; + User tempUser = User.builder() + .id(10L) + .email("tempUser@test.com") + .password("test1234") + .phone("01033335555") + .nickname("임시유저") + .nicknameModifiedDate(LocalDateTime.now()) + .userLevel(UserLevel.AUTH) + .userStatus(UserStatus.NORMAL) + .addressBook(new AddressBook()) + .point(50000L) + .build(); List pointBreakDownMockDataList = new ArrayList<>();