-
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.
- Loading branch information
Showing
17 changed files
with
268 additions
and
39 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
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
5 changes: 0 additions & 5 deletions
5
src/main/resources/META-INF/additional-spring-configuration-metadata.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
security: | ||
enable: true | ||
|
||
spring: | ||
security: | ||
oauth2: | ||
resourceserver: | ||
jwt: | ||
issuer-uri: ${COGNITO_ISSUER_URI} | ||
jwk-set-uri: ${COGNITO_JWK_SET_URI} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
admin: | ||
access-token: token | ||
local: true | ||
|
||
payment-provider: | ||
mock: true | ||
mock: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
package com.fiap.payments | ||
|
||
import com.fiap.payments.it.LocalStackContainerInitializer | ||
import org.junit.jupiter.api.Tag | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.springframework.test.context.ContextConfiguration | ||
|
||
@Tag("IntegrationTest") | ||
@ActiveProfiles("test") | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE) | ||
annotation class IntegrationTest | ||
|
||
@ContextConfiguration(initializers = [LocalStackContainerInitializer::class]) | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE) | ||
annotation class WithLocalstack |
15 changes: 15 additions & 0 deletions
15
src/test/kotlin/com/fiap/payments/it/BasicIntegrationTest.kt
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.fiap.payments.it | ||
|
||
import com.fiap.payments.IntegrationTest | ||
import com.fiap.payments.WithLocalstack | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@IntegrationTest | ||
@WithLocalstack | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class BasicIntegrationTest { | ||
|
||
@Test | ||
fun contextLoads() { } | ||
} |
145 changes: 145 additions & 0 deletions
145
src/test/kotlin/com/fiap/payments/it/LocalstackContainerInitializer.kt
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,145 @@ | ||
package com.fiap.payments.it | ||
|
||
import org.springframework.boot.test.util.TestPropertyValues | ||
import org.springframework.context.ApplicationContextInitializer | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.testcontainers.containers.localstack.LocalStackContainer | ||
import org.testcontainers.containers.wait.strategy.Wait.forListeningPort | ||
import org.testcontainers.utility.DockerImageName | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.services.sns.SnsClient | ||
import software.amazon.awssdk.services.sns.model.CreateTopicRequest | ||
import software.amazon.awssdk.services.sns.model.SubscribeRequest | ||
import software.amazon.awssdk.services.sqs.SqsClient | ||
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest | ||
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest | ||
import software.amazon.awssdk.services.sqs.model.QueueAttributeName | ||
|
||
class LocalStackContainerInitializer : | ||
ApplicationContextInitializer<ConfigurableApplicationContext>, | ||
LocalStackContainer( | ||
DockerImageName.parse("localstack/localstack") | ||
) { | ||
|
||
companion object { | ||
private val instance: LocalStackContainer = LocalStackContainerInitializer() | ||
.withServices( | ||
Service.DYNAMODB, | ||
Service.SQS, | ||
Service.SNS, | ||
) | ||
.withEnv( | ||
mapOf( | ||
"DEBUG" to "1", | ||
"DEFAULT_REGION" to "us-east-1", | ||
) | ||
) | ||
.waitingFor(forListeningPort()) | ||
} | ||
|
||
override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) { | ||
instance.start() | ||
|
||
val awsCredentials = AwsBasicCredentials.create( | ||
instance.accessKey, | ||
instance.secretKey | ||
) | ||
|
||
val region = Region.of(instance.region) | ||
|
||
val sqsClient = SqsClient.builder() | ||
.endpointOverride(instance.getEndpointOverride(Service.SQS)) | ||
.region(region) | ||
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials)) | ||
.build() | ||
|
||
val snsClient = SnsClient.builder() | ||
.endpointOverride(instance.getEndpointOverride(Service.SNS)) | ||
.region(region) | ||
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials)) | ||
.build() | ||
|
||
createQueues(sqsClient) | ||
createTopics(snsClient) | ||
subscribeToTopics(snsClient, sqsClient) | ||
|
||
TestPropertyValues.of( | ||
"spring.cloud.aws.endpoint=${instance.endpoint}", | ||
"spring.cloud.aws.credentials.access-key=${instance.accessKey}", | ||
"spring.cloud.aws.credentials.secret-key=${instance.secretKey}", | ||
"spring.cloud.aws.region.static=${instance.region}", | ||
"sns.topics.response-payment=${getTopicArn(snsClient, "payment-response_topic")}", | ||
).applyTo(configurableApplicationContext) | ||
} | ||
|
||
private fun createQueues(sqsClient: SqsClient) { | ||
val queues = listOf("request-payment_queue", "payment-response_queue") | ||
|
||
queues.forEach { queue -> | ||
val dlqName = "${queue}_dlq" | ||
sqsClient.createQueue(CreateQueueRequest.builder().queueName(dlqName).build()) | ||
println("DLQ queue [$dlqName] created") | ||
|
||
val dlqArn = getQueueArn(sqsClient, dlqName) | ||
|
||
val redrivePolicy = """{"deadLetterTargetArn":"$dlqArn","maxReceiveCount":"3"}""" | ||
|
||
sqsClient.createQueue( | ||
CreateQueueRequest.builder() | ||
.queueName(queue) | ||
.attributes( | ||
mapOf( | ||
QueueAttributeName.DELAY_SECONDS to "5", | ||
QueueAttributeName.REDRIVE_POLICY to redrivePolicy | ||
) | ||
) | ||
.build() | ||
) | ||
println("Queue [$queue] created") | ||
} | ||
} | ||
|
||
private fun createTopics(snsClient: SnsClient) { | ||
val topics = listOf("request-payment_topic", "payment-response_topic") | ||
|
||
topics.forEach { topic -> | ||
snsClient.createTopic(CreateTopicRequest.builder().name(topic).build()) | ||
println("Topic [$topic] created") | ||
} | ||
} | ||
|
||
private fun getQueueArn(sqsClient: SqsClient, queueName: String) = | ||
sqsClient.getQueueAttributes( | ||
GetQueueAttributesRequest.builder() | ||
.queueUrl(sqsClient.getQueueUrl { it.queueName(queueName) }.queueUrl()) | ||
.attributeNames(QueueAttributeName.QUEUE_ARN) | ||
.build() | ||
).attributes()[QueueAttributeName.QUEUE_ARN] | ||
|
||
private fun getTopicArn(snsClient: SnsClient, topicName: String) = | ||
snsClient.createTopic { it.name(topicName) }.topicArn() | ||
|
||
private fun subscribeToTopics(snsClient: SnsClient, sqsClient: SqsClient) { | ||
val subscriptions = listOf( | ||
Pair("request-payment_topic", "request-payment_queue"), | ||
Pair("payment-response_topic", "payment-response_queue") | ||
) | ||
|
||
subscriptions.forEach { (topicName, queueName) -> | ||
val topicArn = getTopicArn(snsClient, topicName) | ||
val queueArn = getQueueArn(sqsClient, queueName) | ||
|
||
snsClient.subscribe( | ||
SubscribeRequest.builder() | ||
.topicArn(topicArn) | ||
.protocol("sqs") | ||
.endpoint(queueArn) | ||
.attributes(mapOf("RawMessageDelivery" to "true")) | ||
.build() | ||
) | ||
println("Queue [$queueName] subscribed to Topic [$topicName]") | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
resource "aws_sns_topic" "request_payment_topic" { | ||
name = "request-payment_topic" | ||
} | ||
|
||
resource "aws_sns_topic" "payment_response_topic" { | ||
name = "payment-response_topic" | ||
} | ||
|
||
resource "aws_sns_topic_subscription" "request_payment_subscription" { | ||
topic_arn = aws_sns_topic.request_payment_topic.arn | ||
protocol = "sqs" | ||
endpoint = aws_sqs_queue.request_payment_queue.arn | ||
|
||
raw_message_delivery = true | ||
} | ||
|
||
resource "aws_sns_topic_subscription" "payment_response_subscription" { | ||
topic_arn = aws_sns_topic.payment_response_topic.arn | ||
protocol = "sqs" | ||
endpoint = aws_sqs_queue.payment_response_queue.arn | ||
|
||
raw_message_delivery = true | ||
} |
Oops, something went wrong.