PaymentGatewayControllerTest.java
Bridget/payment_solutions/src/test/java/com/checkout/payment/gateway/controller/PaymentGatewayControllerTest.java
package com.checkout.payment.gateway.controller;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.checkout.payment.gateway.enums.PaymentStatus;
import com.checkout.payment.gateway.exception.BankServiceException;
import com.checkout.payment.gateway.exception.EventProcessingException;
import com.checkout.payment.gateway.model.GetPaymentResponse;
import com.checkout.payment.gateway.model.PostPaymentRequest;
import com.checkout.payment.gateway.model.PostPaymentResponse;
import com.checkout.payment.gateway.model.ProcessPaymentResult;
import com.checkout.payment.gateway.service.PaymentGatewayService;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@WebMvcTest(
controllers = PaymentGatewayController.class,
excludeAutoConfiguration = SecurityAutoConfiguration.class)
@AutoConfigureMockMvc(addFilters = false)
class PaymentGatewayControllerTest {
private static final String API_BASE = "/api/v1";
@Autowired
private MockMvc mvc;
@MockBean
private PaymentGatewayService paymentGatewayService;
@Autowired
private ObjectMapper objectMapper;
@Test
void getPayment_whenExists_returnsPayment() throws Exception {
UUID paymentId = UUID.randomUUID();
GetPaymentResponse payment = new GetPaymentResponse();
payment.setId(paymentId);
payment.setAmount(100);
payment.setCurrency("USD");
payment.setStatus(PaymentStatus.AUTHORIZED);
payment.setExpiryMonth(12);
payment.setExpiryYear(2030);
payment.setCardNumberLastFour(8877);
when(paymentGatewayService.getPaymentById(paymentId)).thenReturn(payment);
mvc.perform(MockMvcRequestBuilders.get(API_BASE + "/payments/" + paymentId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(paymentId.toString()))
.andExpect(jsonPath("$.status").value("Authorized"))
.andExpect(jsonPath("$.cardNumberLastFour").value(8877))
.andExpect(jsonPath("$.expiryMonth").value(12))
.andExpect(jsonPath("$.expiryYear").value(2030))
.andExpect(jsonPath("$.currency").value("USD"))
.andExpect(jsonPath("$.amount").value(100));
}
@Test
void getPayment_whenNotFound_returns404() throws Exception {
UUID paymentId = UUID.randomUUID();
when(paymentGatewayService.getPaymentById(paymentId))
.thenThrow(new EventProcessingException("Payment not found"));
mvc.perform(MockMvcRequestBuilders.get(API_BASE + "/payments/" + paymentId))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("Payment not found"));
}
@Test
void processPayment_whenAuthorized_returns200() throws Exception {
PostPaymentResponse response = new PostPaymentResponse();
response.setId(UUID.randomUUID());
response.setStatus(PaymentStatus.AUTHORIZED);
response.setCardNumberLastFour(8877);
response.setExpiryMonth(4);
response.setExpiryYear(2030);
response.setCurrency("GBP");
response.setAmount(100);
when(paymentGatewayService.processPayment(isNull(), any(PostPaymentRequest.class)))
.thenReturn(new ProcessPaymentResult(response, 200));
PostPaymentRequest request = createValidRequest();
mvc.perform(MockMvcRequestBuilders.post(API_BASE + "/payments")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("Authorized"))
.andExpect(jsonPath("$.cardNumberLastFour").value(8877))
.andExpect(jsonPath("$.currency").value("GBP"))
.andExpect(jsonPath("$.amount").value(100));
}
@Test
void processPayment_whenDeclined_returns200() throws Exception {
PostPaymentResponse response = new PostPaymentResponse();
response.setId(UUID.randomUUID());
response.setStatus(PaymentStatus.DECLINED);
response.setCardNumberLastFour(8878);
response.setExpiryMonth(4);
response.setExpiryYear(2030);
response.setCurrency("USD");
response.setAmount(500);
when(paymentGatewayService.processPayment(isNull(), any(PostPaymentRequest.class)))
.thenReturn(new ProcessPaymentResult(response, 200));
PostPaymentRequest request = createValidRequest();
request.setCardNumber("2222405343248878");
mvc.perform(MockMvcRequestBuilders.post(API_BASE + "/payments")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("Declined"));
}
@Test
void processPayment_whenRejected_returns400() throws Exception {
PostPaymentResponse response = new PostPaymentResponse();
response.setStatus(PaymentStatus.REJECTED);
response.setErrors(List.of("Card number is required"));
when(paymentGatewayService.processPayment(isNull(), any(PostPaymentRequest.class)))
.thenReturn(new ProcessPaymentResult(response, 400));
PostPaymentRequest request = new PostPaymentRequest();
mvc.perform(MockMvcRequestBuilders.post(API_BASE + "/payments")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.status").value("Rejected"))
.andExpect(jsonPath("$.errors").isArray());
}
@Test
void processPayment_whenBankUnavailable_returns502() throws Exception {
when(paymentGatewayService.processPayment(isNull(), any(PostPaymentRequest.class)))
.thenThrow(new BankServiceException("Bank unavailable"));
PostPaymentRequest request = createValidRequest();
mvc.perform(MockMvcRequestBuilders.post(API_BASE + "/payments")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadGateway());
}
private PostPaymentRequest createValidRequest() {
PostPaymentRequest request = new PostPaymentRequest();
request.setCardNumber("2222405343248877");
request.setExpiryMonth(4);
request.setExpiryYear(2030);
request.setCurrency("GBP");
request.setAmount(100);
request.setCvv("123");
return request;
}
}
相关文章
PaymentGatewayApplication.java
PaymentGatewayApplication.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/PaymentGatewayApplication.java).
阅读文章 →ApplicationConfiguration.java
ApplicationConfiguration.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/ApplicationConfiguration.java).
阅读文章 →RequestResponseLogger.java
RequestResponseLogger.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/RequestResponseLogger.java).
阅读文章 →PaymentGatewayController.java
PaymentGatewayController.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/controller/PaymentGatewayController.java).
阅读文章 →PaymentStatus.java
PaymentStatus.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/enums/PaymentStatus.java).
阅读文章 →BankErrorException.java
BankErrorException.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/exception/BankErrorException.java).
阅读文章 →