Skip to content

Commit

Permalink
#1002 [Payment-Paypal] Test for module Payment-Paypal (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
nashtech-tuannguyenhuu1 authored Sep 11, 2024
1 parent bff735f commit 56fda46
Show file tree
Hide file tree
Showing 12 changed files with 514 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.yas.paymentpaypal.config;

import dasniko.testcontainers.keycloak.KeycloakContainer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.containers.PostgreSQLContainer;

@TestConfiguration
public class IntegrationTestConfiguration {

@Bean(destroyMethod = "stop")
public PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>("postgres:16")
.withReuse(true);
}

@Bean(destroyMethod = "stop")
public KeycloakContainer keycloakContainer(DynamicPropertyRegistry registry) {
KeycloakContainer keycloak = new KeycloakContainer()
.withRealmImportFiles("/test-realm.json")
.withReuse(true);

registry.add("spring.security.oauth2.resourceserver.jwt.issuer-uri",
() -> keycloak.getAuthServerUrl() + "/realms/quarkus");
registry.add("spring.security.oauth2.resourceserver.jwt.jwk-set-uri",
() -> keycloak.getAuthServerUrl() + "/realms/quarkus/protocol/openid-connect/certs");
return keycloak;
}
}
20 changes: 20 additions & 0 deletions payment-paypal/src/it/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Setting Spring context path & port
server.servlet.context-path=/v1
server.port=8093

spring.jpa.hibernate.ddl-auto=update

# Setting Spring profile
spring.profiles.active=test
springdoc.swagger-ui.enabled=true
springdoc.api-docs.enabled=true

# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.packagesToScan=com.yas.paymentpaypal


spring.security.oauth2.resourceserver.jwt.issuer-uri=http://identity/realms/Yas
springdoc.oauthflow.authorization-url=http://identity/realms/Yas/protocol/openid-connect/auth
springdoc.oauthflow.token-url=http://identity/realms/Yas/protocol/openid-connect/token
spring.jpa.open-in-view=false
15 changes: 15 additions & 0 deletions payment-paypal/src/it/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<springProperty scope="context" name="appName" source="spring.application.name"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.yas.paymentpaypal.controller;


import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.yas.paymentpaypal.PaymentPaypalApplication;
import com.yas.paymentpaypal.service.PaypalService;
import com.yas.paymentpaypal.viewmodel.CapturedPaymentVm;
import com.yas.paymentpaypal.viewmodel.PaypalRequestPayment;
import com.yas.paymentpaypal.viewmodel.RequestPayment;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
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.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = PaypalController.class)
@ContextConfiguration(classes = PaymentPaypalApplication.class)
@AutoConfigureMockMvc(addFilters = false)
class PaypalControllerTest {

@MockBean
private PaypalService paypalService;

@Autowired
private MockMvc mockMvc;

private ObjectWriter objectWriter;

@BeforeEach
void setUp() {
objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
}

@Test
void testCreatePayment_whenNormalCase_responsePaypalRequestPayment() throws Exception {

RequestPayment requestPayment = new RequestPayment(new BigDecimal(10), "1");
PaypalRequestPayment paypalRequestPayment = new PaypalRequestPayment(
"success",
"PAYID-LJ4Z8A8P2R48",
"https://www.example.com/redirect"
);
when(paypalService.createPayment(requestPayment)).thenReturn(paypalRequestPayment);

mockMvc.perform(MockMvcRequestBuilders.post("/init")
.contentType("application/json")
.content(objectWriter.writeValueAsString(requestPayment)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json(objectWriter.writeValueAsString(paypalRequestPayment)));

}

@Test
void testCapturePayment_whenNormalCase_responseCapturedPaymentVm() throws Exception {

String token = "testToken";
CapturedPaymentVm payment = new CapturedPaymentVm(
12345L,
"chk_7890",
new BigDecimal("250.75"),
new BigDecimal("5.00"),
"txn_0011223344",
"credit_card",
"completed",
null
);
when(paypalService.capturePayment(token)).thenReturn(payment);
mockMvc.perform(MockMvcRequestBuilders.get("/capture")
.param("token", token)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json(objectWriter.writeValueAsString(payment)));

}

@Test
void testCancelPayment_whenNormalCase_responseString() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/cancel")
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Payment cancelled"));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.yas.paymentpaypal.service;

import static com.yas.paymentpaypal.utils.SecurityContextUtils.setUpSecurityContext;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.yas.paymentpaypal.config.ServiceUrlConfig;
import com.yas.paymentpaypal.viewmodel.CapturedPaymentVm;
import java.math.BigDecimal;
import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;

class PaymentServiceTest {

private RestClient restClient;

private ServiceUrlConfig serviceUrlConfig;

private PaymentService paymentService;

private RestClient.ResponseSpec responseSpec;

private static final String PAYMENT_URL = "http://api.yas.local/payment";

@BeforeEach
void setUp() {
restClient = mock(RestClient.class);
serviceUrlConfig = mock(ServiceUrlConfig.class);
paymentService = new PaymentService(restClient, serviceUrlConfig);
responseSpec = Mockito.mock(RestClient.ResponseSpec.class);
setUpSecurityContext("test");
when(serviceUrlConfig.payment()).thenReturn(PAYMENT_URL);
}

@Test
void testCapturePayment_ifNormalCase_returnAddressDetailVm() {

CapturedPaymentVm payment = new CapturedPaymentVm(
12345L,
"chk_7890",
new BigDecimal("250.75"),
new BigDecimal("5.00"),
"txn_0011223344",
"credit_card",
"completed",
null
);

final URI url = UriComponentsBuilder
.fromHttpUrl(serviceUrlConfig.payment())
.path("/storefront/payments/capture")
.buildAndExpand()
.toUri();

RestClient.RequestBodyUriSpec requestBodyUriSpec = mock(RestClient.RequestBodyUriSpec.class);
when(restClient.post()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(url)).thenReturn(requestBodyUriSpec);

when(requestBodyUriSpec.body(payment)).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.retrieve()).thenReturn(responseSpec);

paymentService.capturePayment(payment);

verify(restClient, times(1)).post();
}

}
Loading

0 comments on commit 56fda46

Please sign in to comment.