Skip to content

Commit 083139b

Browse files
committed
Use AWS SQS to update the initial order status
1 parent 64eae8a commit 083139b

File tree

6 files changed

+100
-5
lines changed

6 files changed

+100
-5
lines changed

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
</repository>
3030
</repositories>
3131

32+
<dependencyManagement>
33+
<dependencies>
34+
<dependency>
35+
<groupId>io.awspring.cloud</groupId>
36+
<artifactId>spring-cloud-aws-dependencies</artifactId>
37+
<version>3.0.4</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
3244
<dependencies>
3345
<dependency>
3446
<groupId>com.github.soat-tech-challenge</groupId>
@@ -51,6 +63,19 @@
5163
<groupId>org.springframework.boot</groupId>
5264
<artifactId>spring-boot-starter-validation</artifactId>
5365
</dependency>
66+
<dependency>
67+
<groupId>io.awspring.cloud</groupId>
68+
<artifactId>spring-cloud-aws-starter</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>io.awspring.cloud</groupId>
72+
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.amazonaws</groupId>
76+
<artifactId>aws-java-sdk-sqs</artifactId>
77+
<version>1.12.674</version>
78+
</dependency>
5479
<dependency>
5580
<groupId>org.springframework.boot</groupId>
5681
<artifactId>spring-boot-devtools</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package br.com.grupo63.techchallenge.payment.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
7+
import software.amazon.awssdk.auth.credentials.AwsCredentials;
8+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
9+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
10+
import software.amazon.awssdk.regions.Region;
11+
import software.amazon.awssdk.services.sqs.SqsAsyncClient;
12+
13+
@Configuration
14+
class SQSClientConfig {
15+
16+
@Value("${spring.cloud.aws.credentials.access-key}")
17+
private String accessKey;
18+
19+
@Value("${spring.cloud.aws.credentials.secret-key}")
20+
private String secretKey;
21+
22+
@Value("${app.aws.session-token}")
23+
private String sessionToken;
24+
25+
@Value("${spring.cloud.aws.region.static}")
26+
private String region;
27+
28+
@Bean
29+
SqsAsyncClient sqsAsyncClient(AwsCredentials awsCredentials) {
30+
return SqsAsyncClient
31+
.builder()
32+
.region(Region.of(region))
33+
.credentialsProvider(StaticCredentialsProvider
34+
.create(awsCredentials))
35+
.build();
36+
}
37+
38+
@Bean
39+
public AwsCredentials awsCredentials() {
40+
if (sessionToken == null || sessionToken.isBlank()) {
41+
return AwsBasicCredentials.create(accessKey, secretKey);
42+
}
43+
return AwsSessionCredentials.create(accessKey, secretKey, sessionToken);
44+
}
45+
}

src/main/java/br/com/grupo63/techchallenge/payment/usecase/PaymentUseCase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import br.com.grupo63.techchallenge.payment.gateway.order.dto.OrderDTO;
99
import br.com.grupo63.techchallenge.payment.gateway.payment.IMercadoPagoGateway;
1010
import br.com.grupo63.techchallenge.payment.gateway.payment.IPaymentGateway;
11-
import br.com.grupo63.techchallenge.payment.gateway.status.IStatusGateway;
11+
import io.awspring.cloud.sqs.operations.SqsTemplate;
1212
import lombok.RequiredArgsConstructor;
1313
import org.springframework.stereotype.Service;
1414

@@ -19,7 +19,7 @@ public class PaymentUseCase implements IPaymentUseCase {
1919
private final IMercadoPagoGateway mercadoPagoGateway;
2020
private final IOrderGateway orderGateway;
2121
private final IPaymentGateway paymentGateway;
22-
private final IStatusGateway statusGateway;
22+
private final SqsTemplate sqsTemplate;
2323

2424
@Override
2525
public String startPayment(Long orderId) throws NotFoundException {
@@ -41,8 +41,12 @@ public void finishPayment(Long orderId) throws NotFoundException {
4141
payment.setStatus(PaymentStatus.PAID);
4242
paymentGateway.saveAndFlush(payment);
4343

44-
statusGateway.advanceStatus(orderId);
45-
44+
sqsTemplate.send(sqsSendOptions ->
45+
sqsSendOptions
46+
.queue("approvedPayments.fifo")
47+
.payload(orderId)
48+
.messageDeduplicationId(orderId.toString())
49+
.messageGroupId(orderId.toString()));
4650
}
4751

4852
@Override

src/main/resources/application-dev.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ spring:
1818
flyway:
1919
locations: classpath:db/migrations/{vendor}/
2020
enabled: true
21+
cloud:
22+
aws:
23+
credentials:
24+
access-key: "${AWS_ACCESS_KEY:accesskey}"
25+
secret-key: "${AWS_SECRET_KEY:secretkey}"
26+
region:
27+
static: "${AWS_REGION:us-east-2}"
28+
sqs:
29+
endpoint: "${AWS_SQS_ENDPOINT:endpoint}"
2130

2231
# --- Custom keys ---
2332

src/main/resources/application.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ spring:
1414
flyway:
1515
locations: classpath:db/migrations/{vendor}/
1616
enabled: true
17+
cloud:
18+
aws:
19+
credentials:
20+
access-key: "${AWS_ACCESS_KEY:accesskey}"
21+
secret-key: "${AWS_SECRET_KEY:secretkey}"
22+
region:
23+
static: "${AWS_REGION:us-east-2}"
24+
sqs:
25+
endpoint: "${AWS_SQS_ENDPOINT:endpoint}"
1726

1827
springdoc:
1928
swagger-ui:

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import br.com.grupo63.techchallenge.payment.gateway.payment.*;
1313
import br.com.grupo63.techchallenge.payment.gateway.status.IStatusGateway;
1414
import br.com.grupo63.techchallenge.payment.usecase.PaymentUseCase;
15+
import io.awspring.cloud.sqs.operations.SqsTemplate;
1516
import lombok.SneakyThrows;
1617
import org.junit.jupiter.api.BeforeEach;
1718
import org.junit.jupiter.api.Test;
@@ -42,6 +43,8 @@ class PaymentIntegrationTest {
4243
private PaymentController paymentController;
4344
private PaymentAPIController paymentAPIController;
4445
private MercadoPagoGateway mercadoPagoGateway;
46+
@Mock
47+
private SqsTemplate sqsTemplate;
4548

4649

4750

@@ -57,7 +60,7 @@ class PaymentIntegrationTest {
5760
void setUp() {
5861
MockitoAnnotations.openMocks(this);
5962
mercadoPagoGateway = new MercadoPagoGateway();
60-
paymentUseCase = new PaymentUseCase(mercadoPagoGateway, orderGateway, paymentJpaAdapter, statusGateway);
63+
paymentUseCase = new PaymentUseCase(mercadoPagoGateway, orderGateway, paymentJpaAdapter, sqsTemplate);
6164
paymentController = new PaymentController(paymentUseCase);
6265
paymentAPIController = new PaymentAPIController(paymentController);
6366
}

0 commit comments

Comments
 (0)