-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from code-review-platform-flow/dev
Dev
- Loading branch information
Showing
14 changed files
with
365 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
7 changes: 7 additions & 0 deletions
7
src/main/java/com/flow/payment/common/exception/CustomNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.flow.payment.common.exception; | ||
|
||
public class CustomNotFoundException extends RuntimeException { | ||
public CustomNotFoundException() { | ||
super(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/flow/payment/common/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.flow.payment.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
@ExceptionHandler(CustomNotFoundException.class) | ||
public ResponseEntity<String> handleOrderNotFoundException(CustomNotFoundException ex) { | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
src/test/java/com/flow/payment/FlowPaymentApplicationTests.java
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
src/test/java/com/flow/payment/config/ResourceSnippetIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.flow.payment.config; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.restdocs.RestDocumentationContextProvider; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.filter.CharacterEncodingFilter; | ||
|
||
import com.flow.payment.common.client.TossPaymentsApiAdapter; | ||
import com.flow.payment.controller.OrderController; | ||
import com.flow.payment.controller.PaymentsController; | ||
import com.flow.payment.service.orders.OrdersCreateService; | ||
import com.flow.payment.service.payment.PaymentsConfirmService; | ||
import com.flow.payment.service.payment.TossPaymentsService; | ||
|
||
@ExtendWith({RestDocumentationExtension.class}) | ||
@WebMvcTest( | ||
controllers = { | ||
PaymentsController.class, | ||
OrderController.class | ||
} | ||
) | ||
@AutoConfigureRestDocs | ||
public abstract class ResourceSnippetIntegrationTest { | ||
|
||
protected static final String DEFAULT_RESTDOC_PATH = "{class_name}/{method_name}"; | ||
|
||
@Autowired | ||
protected MockMvc mockMvc; | ||
|
||
@MockBean | ||
protected OrdersCreateService ordersCreateService; | ||
|
||
@MockBean | ||
protected PaymentsConfirmService paymentsConfirmService; | ||
|
||
@MockBean | ||
protected TossPaymentsService tossPaymentsService; | ||
|
||
@BeforeEach | ||
void setUp(final WebApplicationContext context, final RestDocumentationContextProvider provider) { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(context) | ||
.apply(MockMvcRestDocumentation.documentationConfiguration(provider) | ||
//요청 body 의 payload 를 보기 좋게 출력 | ||
.operationPreprocessors().withRequestDefaults(Preprocessors.prettyPrint()) | ||
.and() | ||
//응답 body 의 payload 를 보기 좋게 출력 | ||
.operationPreprocessors().withResponseDefaults(Preprocessors.prettyPrint())) | ||
//테스트 결과를 항상 print | ||
.alwaysDo(MockMvcResultHandlers.print()) | ||
//한글 깨짐 방지 | ||
.addFilter(new CharacterEncodingFilter("UTF-8", true)) | ||
.build(); | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/test/java/com/flow/payment/order/OrderControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.flow.payment.order; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import com.epages.restdocs.apispec.ResourceSnippetParameters; | ||
import com.epages.restdocs.apispec.Schema; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.flow.payment.common.exception.CustomNotFoundException; | ||
import com.flow.payment.config.ResourceSnippetIntegrationTest; | ||
import com.flow.payment.dto.order.request.OrdersRequestDto; | ||
import com.flow.payment.dto.order.response.OrdersResponseDto; | ||
|
||
import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.*; | ||
import static com.epages.restdocs.apispec.ResourceDocumentation.*; | ||
import static org.mockito.BDDMockito.*; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.UUID; | ||
|
||
public class OrderControllerTest extends ResourceSnippetIntegrationTest { | ||
|
||
@Test | ||
@DisplayName("주문 생성 성공") | ||
void createOrderSuccess() throws Exception { | ||
OrdersRequestDto request = OrdersRequestDto.builder().email("[email protected]").totalAmount( | ||
BigDecimal.valueOf(1000)).build(); | ||
OrdersResponseDto response = OrdersResponseDto.builder() | ||
.customerKey(UUID.randomUUID()) | ||
.tossOrderId(UUID.randomUUID().toString()) | ||
.build(); | ||
|
||
given(ordersCreateService.create(request)).willReturn(response); | ||
|
||
// when | ||
ResultActions resultActions = mockMvc.perform( | ||
RestDocumentationRequestBuilders.post("/order") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(new ObjectMapper().writeValueAsString(request))); | ||
|
||
resultActions | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(new ObjectMapper().writeValueAsString(response))) | ||
.andDo(document(DEFAULT_RESTDOC_PATH, | ||
resource(ResourceSnippetParameters.builder() | ||
.tag("주문") | ||
.summary("주문 생성") | ||
.description("주문 생성 요청 API") | ||
.requestSchema(Schema.schema("주문 생성 요청")) | ||
.responseSchema(Schema.schema("주문 생성 응답")) | ||
.requestFields( | ||
fieldWithPath("email").description("이메일"), | ||
fieldWithPath("totalAmount").description("총 금액") | ||
) | ||
.responseFields( | ||
fieldWithPath("customerKey").description("고객 번호 (UUID)"), | ||
fieldWithPath("tossOrderId").description("토스 주문 번호 (UUID)") | ||
) | ||
.build() | ||
))); | ||
} | ||
|
||
@Test | ||
@DisplayName("주문 생성 실패") | ||
void createOrderFailByEmail() throws Exception { | ||
OrdersRequestDto request = OrdersRequestDto.builder().email("[email protected]").totalAmount( | ||
BigDecimal.valueOf(1000)).build(); | ||
|
||
given(ordersCreateService.create(request)).willThrow(new CustomNotFoundException()); | ||
|
||
// when | ||
ResultActions resultActions = mockMvc.perform( | ||
RestDocumentationRequestBuilders.post("/order") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(new ObjectMapper().writeValueAsString(request))); | ||
|
||
resultActions | ||
.andExpect(status().isNotFound()) | ||
.andDo(document(DEFAULT_RESTDOC_PATH, | ||
resource(ResourceSnippetParameters.builder() | ||
.tag("주문") | ||
.summary("주문 생성") | ||
.description("주문 생성 요청 API") | ||
.requestSchema(Schema.schema("주문 생성 요청")) | ||
.responseSchema(Schema.schema("주문 생성 응답")) | ||
.requestFields( | ||
fieldWithPath("email").description("이메일"), | ||
fieldWithPath("totalAmount").description("총 금액") | ||
) | ||
.responseFields() | ||
.build() | ||
))); | ||
} | ||
} |
Oops, something went wrong.