Skip to content

Commit 930ed97

Browse files
committed
feat: added a few tests
1 parent 9a14d1c commit 930ed97

File tree

7 files changed

+140
-25
lines changed

7 files changed

+140
-25
lines changed

pom.xml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,30 @@
9393
<version>0.12.3</version>
9494
</dependency>
9595
<dependency>
96-
<groupId>org.springframework.boot</groupId>
97-
<artifactId>spring-boot-starter-test</artifactId>
98-
<scope>test</scope>
96+
<groupId>io.jsonwebtoken</groupId>
97+
<artifactId>jjwt-impl</artifactId>
98+
<version>0.12.3</version>
9999
</dependency>
100100
<dependency>
101-
<groupId>com.h2database</groupId>
102-
<artifactId>h2</artifactId>
103-
<scope>test</scope>
101+
<groupId>io.jsonwebtoken</groupId>
102+
<artifactId>jjwt-jackson</artifactId>
103+
<version>0.12.3</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>io.jsonwebtoken</groupId>
107+
<artifactId>jjwt-api</artifactId>
108+
<version>0.12.3</version>
104109
</dependency>
105110
<dependency>
106111
<groupId>org.springframework.cloud</groupId>
107112
<artifactId>spring-cloud-starter-openfeign</artifactId>
108113
<version>4.0.4</version>
109114
</dependency>
115+
<dependency>
116+
<groupId>org.springframework.boot</groupId>
117+
<artifactId>spring-boot-starter-test</artifactId>
118+
<scope>test</scope>
119+
</dependency>
110120
</dependencies>
111121

112122
<build>
@@ -123,6 +133,12 @@
123133
</excludes>
124134
</configuration>
125135
</plugin>
136+
137+
<plugin>
138+
<groupId>org.apache.maven.plugins</groupId>
139+
<artifactId>maven-surefire-plugin</artifactId>
140+
<version>3.0.0-M5</version>
141+
</plugin>
126142
</plugins>
127143
<resources>
128144
<resource>

src/main/java/br/com/grupo63/techchallenge/payment/api/controller/PaymentAPIController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.swagger.v3.oas.annotations.tags.Tag;
1313
import lombok.RequiredArgsConstructor;
1414
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
1516
import org.springframework.web.bind.annotation.*;
1617

1718
@Tag(name = "Pagamentos", description = "Gerencia o processo de pagamento de um pedido")
@@ -28,10 +29,10 @@ public class PaymentAPIController extends AbstractAPIController {
2829
description = "Registra um pedido e o associa a um pedido. Retorna o QRCode gerado via Mercado Pago para exibição ao cliente.",
2930
security = @SecurityRequirement(name = "bearerAuth"))
3031
@PostMapping("/initialize")
31-
public QRCodeResponseDTO startPayment(
32+
public ResponseEntity<QRCodeResponseDTO> startPayment(
3233
@Parameter(description = "Id do pedido") @RequestParam Long orderId
3334
) throws NotFoundException {
34-
return controller.startPayment(orderId);
35+
return ResponseEntity.ok(controller.startPayment(orderId));
3536
}
3637

3738
@Operation(
@@ -50,9 +51,9 @@ public void confirmPaymentFromOrderId(@Parameter(description = "Id do pedido ass
5051
summary = "Recuperar status do pagamento",
5152
description = "Recupera o status atual do pagamento. Seria utilizado na tela de pagamento do cliente para verificar se o pagamento foi realizado.")
5253
@GetMapping("/status")
53-
public PaymentStatusResponseDTO getStatusByOrderId(@Parameter(description = "Id do pedido associado ao pagamento")
54+
public ResponseEntity<PaymentStatusResponseDTO> getStatusByOrderId(@Parameter(description = "Id do pedido associado ao pagamento")
5455
@RequestParam Long orderId) throws NotFoundException, ValidationException {
55-
return controller.getPaymentStatus(orderId);
56+
return ResponseEntity.ok(controller.getPaymentStatus(orderId));
5657
}
5758

5859
}

src/main/java/br/com/grupo63/techchallenge/payment/gateway/order/IOrderGateway.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
import java.util.Optional;
1010

11-
@FeignClient(name = "order", url = "/")
11+
@FeignClient(name = "order", url = "${soat-services.base-url}/orders}")
1212
public interface IOrderGateway {
1313

14-
@RequestMapping(method = RequestMethod.GET, value = "/public/orders/{orderId}")
14+
@RequestMapping(method = RequestMethod.GET, value = "/{orderId}")
1515
Optional<OrderDTO> getOrderById(@PathVariable("orderId") Long orderId);
1616

1717
}

src/main/resources/application-dev.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ jwt:
2222
token:
2323
key:
2424
public: "${JWT_PUBLIC_KEY:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqStd8n4SGNM0eZhV/hzU+urHA5/IMZPoP9YQ9ZcLKWiX33nI6bSuZMCrLZcJExf63xS+uxDpGxM8Mnk2zOdl+lPwANXLzP1us5P1PyA3YPycW9J7C5YTQW0GiEL3M93ZX7vMJiVoBYblP3JPlYnoYlBORuc0JPk33KtfEZP+78qXpPHM8imYrJLe8ceiDLLFDU/nh5KC2dWAy3ci1ahoJ1Q9ELhp3IZLvOTX57H/T2VKOYOya5+ST41h+JjzI+qGTVnLcKaW+k25YLlVnkSspvdx98+yQDi7kbOTS6yRZHUPD6wPk/nUozpD0nZKccoH4W+zMwmQVtsAA6JCA9gfGwIDAQAB}"
25+
26+
soat-services:
27+
base-url: http://localhost:8585/public
28+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package br.com.grupo63.techchallenge.payment;
2+
3+
import br.com.grupo63.techchallenge.payment.api.controller.PaymentAPIController;
4+
import br.com.grupo63.techchallenge.payment.api.controller.dto.PaymentStatusResponseDTO;
5+
import br.com.grupo63.techchallenge.payment.api.controller.dto.QRCodeResponseDTO;
6+
import br.com.grupo63.techchallenge.payment.controller.PaymentController;
7+
import br.com.grupo63.techchallenge.payment.domain.Payment;
8+
import br.com.grupo63.techchallenge.payment.domain.PaymentMethod;
9+
import br.com.grupo63.techchallenge.payment.domain.PaymentStatus;
10+
import br.com.grupo63.techchallenge.payment.gateway.order.IOrderGateway;
11+
import br.com.grupo63.techchallenge.payment.gateway.order.dto.OrderDTO;
12+
import br.com.grupo63.techchallenge.payment.gateway.payment.*;
13+
import br.com.grupo63.techchallenge.payment.usecase.PaymentUseCase;
14+
import lombok.SneakyThrows;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.mockito.InjectMocks;
18+
import org.mockito.Mock;
19+
import org.mockito.MockitoAnnotations;
20+
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.ResponseEntity;
22+
import static org.junit.jupiter.api.Assertions.*;
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.Mockito.*;
25+
26+
import java.util.Optional;
27+
import java.util.UUID;
28+
29+
class PaymentIntegrationTest {
30+
31+
@Mock
32+
private PaymentJpaRepository paymentJpaRepository;
33+
34+
@InjectMocks
35+
private PaymentJpaAdapter paymentJpaAdapter;
36+
@Mock
37+
private PaymentUseCase paymentUseCase;
38+
@Mock
39+
private IOrderGateway orderGateway;
40+
@Mock
41+
private IMercadoPagoGateway mercadoPagoGateway;
42+
@Mock
43+
private IPaymentGateway paymentGateway;
44+
private PaymentController paymentController;
45+
private PaymentAPIController paymentAPIController;
46+
47+
private final Long defaultOrderId = 1L;
48+
private final String qrData = UUID.randomUUID().toString();
49+
private final Double totalPrice = 100.00;
50+
private final OrderDTO orderDTO = new OrderDTO(defaultOrderId, totalPrice);
51+
private final Payment payment = new Payment(1L, false, PaymentStatus.PENDING, PaymentMethod.MERCADO_PAGO_QR_CODE, qrData, defaultOrderId);
52+
private final PaymentPersistenceEntity defaultPaymentPersistenceEntity =
53+
new PaymentPersistenceEntity(payment);
54+
55+
@BeforeEach
56+
void setUp() {
57+
MockitoAnnotations.openMocks(this);
58+
paymentUseCase = new PaymentUseCase(mercadoPagoGateway, orderGateway, paymentGateway);
59+
paymentController = new PaymentController(paymentUseCase);
60+
paymentAPIController = new PaymentAPIController(paymentController);
61+
}
62+
63+
@SneakyThrows
64+
@Test
65+
public void testGetPaymentStatus_EndToEnd() {
66+
when(paymentGateway.findByOrderId(defaultOrderId)).thenReturn(Optional.of(payment));
67+
when(orderGateway.getOrderById(defaultOrderId)).thenReturn(Optional.of(orderDTO));
68+
69+
ResponseEntity<PaymentStatusResponseDTO> response = paymentAPIController.getStatusByOrderId(defaultOrderId);
70+
assertEquals(HttpStatus.OK, response.getStatusCode());
71+
assertEquals(response.getBody().getStatus(), PaymentStatus.PENDING);
72+
}
73+
74+
@SneakyThrows
75+
@Test
76+
public void testStartPayment_EndToEnd() {
77+
when(paymentJpaRepository.save(any())).thenReturn(defaultPaymentPersistenceEntity);
78+
when(orderGateway.getOrderById(defaultOrderId)).thenReturn(Optional.of(orderDTO));
79+
when(mercadoPagoGateway.generateQRCode(defaultOrderId, totalPrice)).thenReturn(qrData);
80+
81+
ResponseEntity<QRCodeResponseDTO> response = paymentAPIController.startPayment(defaultOrderId);
82+
assertEquals(HttpStatus.OK, response.getStatusCode());
83+
assertEquals(response.getBody().getQrData(), qrData);
84+
}
85+
}

src/test/java/br/com/grupo63/techchallenge/payment/SoatTechChallengeBackendPaymentApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
app:
2+
version: '@project.version@'
3+
springdoc:
4+
api-docs:
5+
enabled: false
6+
spring:
7+
jpa:
8+
hibernate:
9+
ddl-auto: create
10+
show-sql: true
11+
main:
12+
banner-mode: off
13+
output:
14+
ansi:
15+
enabled: always
16+
jwt:
17+
token:
18+
key:
19+
public: "${JWT_PUBLIC_KEY:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoNgzDuoQFLRFb3ivIA5CEHFRuqltT7xR6hFu+6xPD5q0sq8Exjue5s5MIQwkJMldk8/Dhp85OT5ULmAA5x4hPZfFlQEMlMPT5QItQImQ7UFCxBbMwksZlUQEP37Aa5OJ+KhEPBDTe0AaXqXFKxIOLEjHYWqhFHq5whxNotbuPrqLWEopCAgY4lqCe0pSoT3V+7Jszpq+czLBOl6eCbD933pK98/aVD9yA2+7LNOwmkYVyR4kvXX25K5Q/DDUZgMWyQTm3TCnfCrbdH6IH/Z12/DkePNuGFygUKDCEYPkPYnHbRXee4CXbeDkEdgZwzGJPM8gPhSUFrrzFVeihvN5OQIDAQAB}"
20+
management:
21+
endpoints:
22+
enabled-by-default: false

0 commit comments

Comments
 (0)