From 27cabca298a62674265134a1ac1ee89abc4b480a Mon Sep 17 00:00:00 2001 From: scmacdon Date: Tue, 18 Jun 2024 12:52:05 -0400 Subject: [PATCH] updated file with 12.1.1 --- .../topics_and_queues/build.gradle.kts | 4 +- .../kotlin/com/example/sns/SNSWorkflow.kt | 311 +++++++++------- .../src/test/kotlin/AWSSNSTest.kt | 342 +++++++++--------- 3 files changed, 355 insertions(+), 302 deletions(-) diff --git a/kotlin/usecases/topics_and_queues/build.gradle.kts b/kotlin/usecases/topics_and_queues/build.gradle.kts index 407cd8f1a22..c45cfa70fb5 100644 --- a/kotlin/usecases/topics_and_queues/build.gradle.kts +++ b/kotlin/usecases/topics_and_queues/build.gradle.kts @@ -19,7 +19,7 @@ buildscript { } dependencies { - classpath("org.jlleitschuh.gradle:ktlint-gradle:10.3.0") + classpath("org.jlleitschuh.gradle:ktlint-gradle:12.1.1") } } @@ -39,7 +39,7 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1") testImplementation(kotlin("test")) } -tasks.withType() { +tasks.withType { kotlinOptions.jvmTarget = "17" } diff --git a/kotlin/usecases/topics_and_queues/src/main/kotlin/com/example/sns/SNSWorkflow.kt b/kotlin/usecases/topics_and_queues/src/main/kotlin/com/example/sns/SNSWorkflow.kt index fdf39a90420..34bf5dc8c65 100644 --- a/kotlin/usecases/topics_and_queues/src/main/kotlin/com/example/sns/SNSWorkflow.kt +++ b/kotlin/usecases/topics_and_queues/src/main/kotlin/com/example/sns/SNSWorkflow.kt @@ -50,6 +50,7 @@ This Kotlin example performs the following tasks: */ val DASHES: String = String(CharArray(80)).replace("\u0000", "-") + suspend fun main() { val input = Scanner(System.`in`) val useFIFO: String @@ -72,20 +73,20 @@ suspend fun main() { println("Welcome to the AWS SDK for Kotlin messaging with topics and queues.") println( """ - In this workflow, you will create an SNS topic and subscribe an SQS queue to the topic. - You can select from several options for configuring the topic and the subscriptions for the queue. - You can then post to the topic and see the results in the queue. - """.trimIndent() + In this workflow, you will create an SNS topic and subscribe an SQS queue to the topic. + You can select from several options for configuring the topic and the subscriptions for the queue. + You can then post to the topic and see the results in the queue. + """.trimIndent(), ) println(DASHES) println(DASHES) println( """ - SNS topics can be configured as FIFO (First-In-First-Out). - FIFO topics deliver messages in order and support deduplication and message filtering. - Would you like to work with FIFO topics? (y/n) - """.trimIndent() + SNS topics can be configured as FIFO (First-In-First-Out). + FIFO topics deliver messages in order and support deduplication and message filtering. + Would you like to work with FIFO topics? (y/n) + """.trimIndent(), ) useFIFO = input.nextLine() if (useFIFO.compareTo("y") == 0) { @@ -96,7 +97,7 @@ suspend fun main() { Deduplication IDs are either set in the message or automatically generated from content using a hash function. If a message is successfully published to an SNS FIFO topic, any message published and determined to have the same deduplication ID, within the five-minute deduplication interval, is accepted but not delivered. - For more information about deduplication, see https://docs.aws.amazon.com/sns/latest/dg/fifo-message-dedup.html.""" + For more information about deduplication, see https://docs.aws.amazon.com/sns/latest/dg/fifo-message-dedup.html.""", ) println("Would you like to use content-based deduplication instead of entering a deduplication ID? (y/n)") @@ -176,7 +177,7 @@ suspend fun main() { println( """If you add a filter to this subscription, then only the filtered messages will be received in the queue. For information about message filtering, see https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html -For this example, you can filter messages by a "tone" attribute.""" +For this example, you can filter messages by a "tone" attribute.""", ) println("Would you like to filter messages for $sqsQueueName's subscription to the topic $topicName? (y/n)") val filterAns: String = input.nextLine() @@ -216,12 +217,13 @@ For this example, you can filter messages by a "tone" attribute.""" println("4. sincere") println("Select a number or choose 0 to end.") val ans: String = input.nextLine() - msgAttValue = when (ans) { - "1" -> "cheerful" - "2" -> "funny" - "3" -> "serious" - else -> "sincere" - } + msgAttValue = + when (ans) { + "1" -> "cheerful" + "2" -> "funny" + "3" -> "serious" + else -> "sincere" + } println("Selected value is $msgAttValue") } println("Enter a message.") @@ -273,9 +275,10 @@ For this example, you can filter messages by a "tone" attribute.""" } suspend fun deleteSNSTopic(topicArnVal: String?) { - val request = DeleteTopicRequest { - topicArn = topicArnVal - } + val request = + DeleteTopicRequest { + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) @@ -284,15 +287,17 @@ suspend fun deleteSNSTopic(topicArnVal: String?) { } suspend fun deleteSQSQueue(queueNameVal: String) { - val getQueueRequest = GetQueueUrlRequest { - queueName = queueNameVal - } + val getQueueRequest = + GetQueueUrlRequest { + queueName = queueNameVal + } SqsClient { region = "us-east-1" }.use { sqsClient -> val queueUrlVal = sqsClient.getQueueUrl(getQueueRequest).queueUrl - val deleteQueueRequest = DeleteQueueRequest { - queueUrl = queueUrlVal - } + val deleteQueueRequest = + DeleteQueueRequest { + queueUrl = queueUrlVal + } sqsClient.deleteQueue(deleteQueueRequest) println("$queueNameVal was successfully deleted.") @@ -300,28 +305,34 @@ suspend fun deleteSQSQueue(queueNameVal: String) { } suspend fun unSub(subscripArn: String?) { - val request = UnsubscribeRequest { - subscriptionArn = subscripArn - } + val request = + UnsubscribeRequest { + subscriptionArn = subscripArn + } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.unsubscribe(request) println("Subscription was removed for $subscripArn") } } -suspend fun deleteMessages(queueUrlVal: String?, messages: List) { +suspend fun deleteMessages( + queueUrlVal: String?, + messages: List, +) { val entriesVal: MutableList = mutableListOf() for (msg in messages) { - val entry = DeleteMessageBatchRequestEntry { - id = msg.messageId - } + val entry = + DeleteMessageBatchRequestEntry { + id = msg.messageId + } entriesVal.add(entry) } - val deleteMessageBatchRequest = DeleteMessageBatchRequest { - queueUrl = queueUrlVal - entries = entriesVal - } + val deleteMessageBatchRequest = + DeleteMessageBatchRequest { + queueUrl = queueUrlVal + entries = entriesVal + } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.deleteMessageBatch(deleteMessageBatchRequest) @@ -329,32 +340,41 @@ suspend fun deleteMessages(queueUrlVal: String?, messages: List) { } } -suspend fun receiveMessages(queueUrlVal: String?, msgAttValue: String): List? { +suspend fun receiveMessages( + queueUrlVal: String?, + msgAttValue: String, +): List? { if (msgAttValue.isEmpty()) { - val request = ReceiveMessageRequest { - queueUrl = queueUrlVal - maxNumberOfMessages = 5 - } + val request = + ReceiveMessageRequest { + queueUrl = queueUrlVal + maxNumberOfMessages = 5 + } SqsClient { region = "us-east-1" }.use { sqsClient -> return sqsClient.receiveMessage(request).messages } } else { - val receiveRequest = ReceiveMessageRequest { - queueUrl = queueUrlVal - waitTimeSeconds = 1 - maxNumberOfMessages = 5 - } + val receiveRequest = + ReceiveMessageRequest { + queueUrl = queueUrlVal + waitTimeSeconds = 1 + maxNumberOfMessages = 5 + } SqsClient { region = "us-east-1" }.use { sqsClient -> return sqsClient.receiveMessage(receiveRequest).messages } } } -suspend fun pubMessage(messageVal: String?, topicArnVal: String?) { - val request = PublishRequest { - message = messageVal - topicArn = topicArnVal - } +suspend fun pubMessage( + messageVal: String?, + topicArnVal: String?, +) { + val request = + PublishRequest { + message = messageVal + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) @@ -368,28 +388,30 @@ suspend fun pubMessageFIFO( msgAttValue: String, duplication: String, groupIdVal: String?, - deduplicationID: String? + deduplicationID: String?, ) { // Means the user did not choose to use a message attribute. if (msgAttValue.isEmpty()) { if (duplication.compareTo("y") == 0) { - val request = PublishRequest { - message = messageVal - messageGroupId = groupIdVal - topicArn = topicArnVal - } + val request = + PublishRequest { + message = messageVal + messageGroupId = groupIdVal + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println(result.messageId.toString() + " Message sent.") } } else { - val request = PublishRequest { - message = messageVal - messageDeduplicationId = deduplicationID - messageGroupId = groupIdVal - topicArn = topicArnVal - } + val request = + PublishRequest { + message = messageVal + messageDeduplicationId = deduplicationID + messageGroupId = groupIdVal + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) @@ -397,19 +419,21 @@ suspend fun pubMessageFIFO( } } } else { - val messAttr = aws.sdk.kotlin.services.sns.model.MessageAttributeValue { - dataType = "String" - stringValue = "true" - } + val messAttr = + aws.sdk.kotlin.services.sns.model.MessageAttributeValue { + dataType = "String" + stringValue = "true" + } val mapAtt: Map = mapOf(msgAttValue to messAttr) if (duplication.compareTo("y") == 0) { - val request = PublishRequest { - message = messageVal - messageGroupId = groupIdVal - topicArn = topicArnVal - } + val request = + PublishRequest { + message = messageVal + messageGroupId = groupIdVal + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) @@ -417,13 +441,14 @@ suspend fun pubMessageFIFO( } } else { // Create a publish request with the message and attributes. - val request = PublishRequest { - topicArn = topicArnVal - message = messageVal - messageDeduplicationId = deduplicationID - messageGroupId = groupIdVal - messageAttributes = mapAtt - } + val request = + PublishRequest { + topicArn = topicArnVal + message = messageVal + messageDeduplicationId = deduplicationID + messageGroupId = groupIdVal + messageAttributes = mapAtt + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) @@ -434,36 +459,44 @@ suspend fun pubMessageFIFO( } // Subscribe to the SQS queue. -suspend fun subQueue(topicArnVal: String?, queueArnVal: String, filterList: List): String? { +suspend fun subQueue( + topicArnVal: String?, + queueArnVal: String, + filterList: List, +): String? { val request: SubscribeRequest if (filterList.isEmpty()) { // No filter subscription is added. - request = SubscribeRequest { - protocol = "sqs" - endpoint = queueArnVal - returnSubscriptionArn = true - topicArn = topicArnVal - } + request = + SubscribeRequest { + protocol = "sqs" + endpoint = queueArnVal + returnSubscriptionArn = true + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) println( "The queue " + queueArnVal + " has been subscribed to the topic " + topicArnVal + "\n" + - "with the subscription ARN " + result.subscriptionArn + "with the subscription ARN " + result.subscriptionArn, ) return result.subscriptionArn } } else { - request = SubscribeRequest { - protocol = "sqs" - endpoint = queueArnVal - returnSubscriptionArn = true - topicArn = topicArnVal - } + request = + SubscribeRequest { + protocol = "sqs" + endpoint = queueArnVal + returnSubscriptionArn = true + topicArn = topicArnVal + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) - println("The queue $queueArnVal has been subscribed to the topic $topicArnVal with the subscription ARN ${result.subscriptionArn}") + println( + "The queue $queueArnVal has been subscribed to the topic $topicArnVal with the subscription ARN ${result.subscriptionArn}", + ) val attributeNameVal = "FilterPolicy" val gson = Gson() @@ -476,11 +509,12 @@ suspend fun subQueue(topicArnVal: String?, queueArnVal: String, filterList: List val updatedJsonString: String = gson.toJson(jsonObject) println(updatedJsonString) - val attRequest = SetSubscriptionAttributesRequest { - subscriptionArn = result.subscriptionArn - attributeName = attributeNameVal - attributeValue = updatedJsonString - } + val attRequest = + SetSubscriptionAttributesRequest { + subscriptionArn = result.subscriptionArn + attributeName = attributeNameVal + attributeValue = updatedJsonString + } snsClient.setSubscriptionAttributes(attRequest) return result.subscriptionArn @@ -488,14 +522,18 @@ suspend fun subQueue(topicArnVal: String?, queueArnVal: String, filterList: List } } -suspend fun setQueueAttr(queueUrlVal: String?, policy: String) { +suspend fun setQueueAttr( + queueUrlVal: String?, + policy: String, +) { val attrMap: MutableMap = HashMap() attrMap[QueueAttributeName.Policy.toString()] = policy - val attributesRequest = SetQueueAttributesRequest { - queueUrl = queueUrlVal - attributes = attrMap - } + val attributesRequest = + SetQueueAttributesRequest { + queueUrl = queueUrlVal + attributes = attrMap + } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.setQueueAttributes(attributesRequest) @@ -507,10 +545,11 @@ suspend fun getSQSQueueAttrs(queueUrlVal: String?): String { val atts: MutableList = ArrayList() atts.add(QueueAttributeName.QueueArn) - val attributesRequest = GetQueueAttributesRequest { - queueUrl = queueUrlVal - attributeNames = atts - } + val attributesRequest = + GetQueueAttributesRequest { + queueUrl = queueUrlVal + attributeNames = atts + } SqsClient { region = "us-east-1" }.use { sqsClient -> val response = sqsClient.getQueueAttributes(attributesRequest) val mapAtts = response.attributes @@ -524,40 +563,47 @@ suspend fun getSQSQueueAttrs(queueUrlVal: String?): String { return "" } -suspend fun createQueue(queueNameVal: String?, selectFIFO: Boolean): String? { +suspend fun createQueue( + queueNameVal: String?, + selectFIFO: Boolean, +): String? { println("\nCreate Queue") if (selectFIFO) { val attrs = mutableMapOf() attrs[QueueAttributeName.FifoQueue.toString()] = "true" - val createQueueRequest = CreateQueueRequest { - queueName = queueNameVal - attributes = attrs - } + val createQueueRequest = + CreateQueueRequest { + queueName = queueNameVal + attributes = attrs + } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.createQueue(createQueueRequest) println("\nGet queue url") - val urlRequest = GetQueueUrlRequest { - queueName = queueNameVal - } + val urlRequest = + GetQueueUrlRequest { + queueName = queueNameVal + } val getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest) return getQueueUrlResponse.queueUrl } } else { - val createQueueRequest = CreateQueueRequest { - queueName = queueNameVal - } + val createQueueRequest = + CreateQueueRequest { + queueName = queueNameVal + } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.createQueue(createQueueRequest) println("Get queue url") - val urlRequest = GetQueueUrlRequest { - queueName = queueNameVal - } + val urlRequest = + GetQueueUrlRequest { + queueName = queueNameVal + } val getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest) return getQueueUrlResponse.queueUrl @@ -566,9 +612,10 @@ suspend fun createQueue(queueNameVal: String?, selectFIFO: Boolean): String? { } suspend fun createSNSTopic(topicName: String?): String? { - val request = CreateTopicRequest { - name = topicName - } + val request = + CreateTopicRequest { + name = topicName + } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.createTopic(request) @@ -576,7 +623,10 @@ suspend fun createSNSTopic(topicName: String?): String? { } } -suspend fun createFIFO(topicName: String?, duplication: String): String? { +suspend fun createFIFO( + topicName: String?, + duplication: String, +): String? { val topicAttributes: MutableMap = HashMap() if (duplication.compareTo("n") == 0) { topicAttributes["FifoTopic"] = "true" @@ -586,10 +636,11 @@ suspend fun createFIFO(topicName: String?, duplication: String): String? { topicAttributes["ContentBasedDeduplication"] = "true" } - val topicRequest = CreateTopicRequest { - name = topicName - attributes = topicAttributes - } + val topicRequest = + CreateTopicRequest { + name = topicName + attributes = topicAttributes + } SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.createTopic(topicRequest) return response.topicArn diff --git a/kotlin/usecases/topics_and_queues/src/test/kotlin/AWSSNSTest.kt b/kotlin/usecases/topics_and_queues/src/test/kotlin/AWSSNSTest.kt index 26d91d5f37b..e6ed9943451 100644 --- a/kotlin/usecases/topics_and_queues/src/test/kotlin/AWSSNSTest.kt +++ b/kotlin/usecases/topics_and_queues/src/test/kotlin/AWSSNSTest.kt @@ -29,53 +29,54 @@ import org.junit.jupiter.api.TestMethodOrder class AWSSNSTest { @Test @Order(1) - fun testWorkflowFIFO() = runBlocking { - val duplication = "n" - val topicName: String - val deduplicationID: String? - val groupId: String? - val topicArnVal: String? - val sqsQueueName: String - val sqsQueueUrl: String? - val sqsQueueArn: String - val subscriptionArn: String? - val selectFIFO = true - val randomNum = (1..10000).random() - - val messageVal: String - val messageList: List? - val filterList = ArrayList() - val msgAttValue = "" - deduplicationID = "dup100" - groupId = "group100" - println(DASHES) - - println(DASHES) - println("2. Create a topic.") - println("Enter a name for your SNS topic.") - topicName = "topic$randomNum.fifo" - println("The name of the topic is $topicName") - topicArnVal = createFIFO(topicName, duplication) - println("The ARN of the FIFO topic is $topicArnVal") - println(DASHES) - - println(DASHES) - println("3. Create an SQS queue.") - sqsQueueName = "queue$randomNum.fifo" - sqsQueueUrl = createQueue(sqsQueueName, selectFIFO) - println("The queue URL is $sqsQueueUrl") - println(DASHES) - - println(DASHES) - println("4. Get the SQS queue ARN attribute.") - sqsQueueArn = getSQSQueueAttrs(sqsQueueUrl) - println("The ARN of the new queue is $sqsQueueArn") - println(DASHES) - - println(DASHES) - println("5. Attach an IAM policy to the queue.") - // Define the policy to use. - val policy = """{ + fun testWorkflowFIFO() = + runBlocking { + val duplication = "n" + val topicName: String + val deduplicationID: String? + val groupId: String? + val topicArnVal: String? + val sqsQueueName: String + val sqsQueueUrl: String? + val sqsQueueArn: String + val subscriptionArn: String? + val selectFIFO = true + val randomNum = (1..10000).random() + + val messageVal: String + val messageList: List? + val filterList = ArrayList() + val msgAttValue = "" + deduplicationID = "dup100" + groupId = "group100" + println(DASHES) + + println(DASHES) + println("2. Create a topic.") + println("Enter a name for your SNS topic.") + topicName = "topic$randomNum.fifo" + println("The name of the topic is $topicName") + topicArnVal = createFIFO(topicName, duplication) + println("The ARN of the FIFO topic is $topicArnVal") + println(DASHES) + + println(DASHES) + println("3. Create an SQS queue.") + sqsQueueName = "queue$randomNum.fifo" + sqsQueueUrl = createQueue(sqsQueueName, selectFIFO) + println("The queue URL is $sqsQueueUrl") + println(DASHES) + + println(DASHES) + println("4. Get the SQS queue ARN attribute.") + sqsQueueArn = getSQSQueueAttrs(sqsQueueUrl) + println("The ARN of the new queue is $sqsQueueArn") + println(DASHES) + + println(DASHES) + println("5. Attach an IAM policy to the queue.") + // Define the policy to use. + val policy = """{ "Statement": [ { "Effect": "Allow", @@ -92,93 +93,94 @@ class AWSSNSTest { } ] }""" - setQueueAttr(sqsQueueUrl, policy) - println(DASHES) - - println(DASHES) - println("6. Subscribe to the SQS queue.") - subscriptionArn = subQueue(topicArnVal, sqsQueueArn, filterList) - println(DASHES) - - println(DASHES) - println("7. Publish a message to the topic.") - delay(1000) - messageVal = "A message sent from a test." - pubMessageFIFO(messageVal, topicArnVal, msgAttValue, duplication, groupId, deduplicationID) - println(DASHES) - - println(DASHES) - println("8. Display the message.") - messageList = receiveMessages(sqsQueueUrl, msgAttValue) - if (messageList != null) { - for (mes in messageList) { - println("Message Id: ${mes.messageId}") - println("Full Message: ${mes.body}") + setQueueAttr(sqsQueueUrl, policy) + println(DASHES) + + println(DASHES) + println("6. Subscribe to the SQS queue.") + subscriptionArn = subQueue(topicArnVal, sqsQueueArn, filterList) + println(DASHES) + + println(DASHES) + println("7. Publish a message to the topic.") + delay(1000) + messageVal = "A message sent from a test." + pubMessageFIFO(messageVal, topicArnVal, msgAttValue, duplication, groupId, deduplicationID) + println(DASHES) + + println(DASHES) + println("8. Display the message.") + messageList = receiveMessages(sqsQueueUrl, msgAttValue) + if (messageList != null) { + for (mes in messageList) { + println("Message Id: ${mes.messageId}") + println("Full Message: ${mes.body}") + } } - } - println(DASHES) + println(DASHES) - println(DASHES) - println("9. Delete the received message.") - if (messageList != null) { - deleteMessages(sqsQueueUrl, messageList) + println(DASHES) + println("9. Delete the received message.") + if (messageList != null) { + deleteMessages(sqsQueueUrl, messageList) + } + println(DASHES) + + println(DASHES) + println("10. Unsubscribe from the topic and delete the queue") + unSub(subscriptionArn) + deleteSQSQueue(sqsQueueName) + println(DASHES) + + println(DASHES) + println("11. Delete the topic") + deleteSNSTopic(topicArnVal) + println(DASHES) + println("Test 1 passed") } - println(DASHES) - - println(DASHES) - println("10. Unsubscribe from the topic and delete the queue") - unSub(subscriptionArn) - deleteSQSQueue(sqsQueueName) - println(DASHES) - - println(DASHES) - println("11. Delete the topic") - deleteSNSTopic(topicArnVal) - println(DASHES) - println("Test 1 passed") - } @Test @Order(2) - fun testWorkflowNonFIFO() = runBlocking { - val accountId = "814548047983" - val topicName: String - val topicArnVal: String? - val sqsQueueName: String - val sqsQueueUrl: String? - val sqsQueueArn: String - val subscriptionArn: String? - val selectFIFO = false - val randomNum = (1..10000).random() - val messageList: List? - val filterList = ArrayList() - val msgAttValue = "" - println(DASHES) - - println("2. Create a topic.") - topicName = "topic$randomNum" - println("The name of the topic is $topicName") - topicArnVal = createSNSTopic(topicName) - println("The ARN of the non-FIFO topic is $topicArnVal") - println(DASHES) - - println(DASHES) - println("3. Create an SQS queue.") - sqsQueueName = "queue$randomNum" - sqsQueueUrl = createQueue(sqsQueueName, selectFIFO) - println("The queue URL is $sqsQueueUrl") - println(DASHES) - - println(DASHES) - println("4. Get the SQS queue ARN attribute.") - sqsQueueArn = getSQSQueueAttrs(sqsQueueUrl) - println("The ARN of the new queue is $sqsQueueArn") - println(DASHES) - - println(DASHES) - println("5. Attach an IAM policy to the queue.") - // Define the policy to use. - val policy = """{ + fun testWorkflowNonFIFO() = + runBlocking { + val accountId = "814548047983" + val topicName: String + val topicArnVal: String? + val sqsQueueName: String + val sqsQueueUrl: String? + val sqsQueueArn: String + val subscriptionArn: String? + val selectFIFO = false + val randomNum = (1..10000).random() + val messageList: List? + val filterList = ArrayList() + val msgAttValue = "" + println(DASHES) + + println("2. Create a topic.") + topicName = "topic$randomNum" + println("The name of the topic is $topicName") + topicArnVal = createSNSTopic(topicName) + println("The ARN of the non-FIFO topic is $topicArnVal") + println(DASHES) + + println(DASHES) + println("3. Create an SQS queue.") + sqsQueueName = "queue$randomNum" + sqsQueueUrl = createQueue(sqsQueueName, selectFIFO) + println("The queue URL is $sqsQueueUrl") + println(DASHES) + + println(DASHES) + println("4. Get the SQS queue ARN attribute.") + sqsQueueArn = getSQSQueueAttrs(sqsQueueUrl) + println("The ARN of the new queue is $sqsQueueArn") + println(DASHES) + + println(DASHES) + println("5. Attach an IAM policy to the queue.") + // Define the policy to use. + val policy = """{ "Statement": [ { "Effect": "Allow", @@ -195,49 +197,49 @@ class AWSSNSTest { } ] }""" - setQueueAttr(sqsQueueUrl, policy) - println(DASHES) - - println(DASHES) - println("6. Subscribe to the SQS queue.") - subscriptionArn = subQueue(topicArnVal, sqsQueueArn, filterList) - println(DASHES) - - println(DASHES) - println("7. Publish a message to the topic.") - val message = "Hello this is a test." - pubMessage(message, topicArnVal) - println(DASHES) - - println(DASHES) - println("8. Display the message.") - delay(1000) - messageList = receiveMessages(sqsQueueUrl, msgAttValue) - if (messageList != null) { - for (mes in messageList) { - println("Message Id: ${mes.messageId}") - println("Full Message: ${mes.body}") + setQueueAttr(sqsQueueUrl, policy) + println(DASHES) + + println(DASHES) + println("6. Subscribe to the SQS queue.") + subscriptionArn = subQueue(topicArnVal, sqsQueueArn, filterList) + println(DASHES) + + println(DASHES) + println("7. Publish a message to the topic.") + val message = "Hello this is a test." + pubMessage(message, topicArnVal) + println(DASHES) + + println(DASHES) + println("8. Display the message.") + delay(1000) + messageList = receiveMessages(sqsQueueUrl, msgAttValue) + if (messageList != null) { + for (mes in messageList) { + println("Message Id: ${mes.messageId}") + println("Full Message: ${mes.body}") + } } - } - println(DASHES) + println(DASHES) - println(DASHES) - println("9. Delete the received message.") - if (messageList != null) { - deleteMessages(sqsQueueUrl, messageList) + println(DASHES) + println("9. Delete the received message.") + if (messageList != null) { + deleteMessages(sqsQueueUrl, messageList) + } + println(DASHES) + + println(DASHES) + println("10. Unsubscribe from the topic and delete the queue") + unSub(subscriptionArn) + deleteSQSQueue(sqsQueueName) + println(DASHES) + + println(DASHES) + println("11. Delete the topic") + deleteSNSTopic(topicArnVal) + println(DASHES) + println("Test 2 passed") } - println(DASHES) - - println(DASHES) - println("10. Unsubscribe from the topic and delete the queue") - unSub(subscriptionArn) - deleteSQSQueue(sqsQueueName) - println(DASHES) - - println(DASHES) - println("11. Delete the topic") - deleteSNSTopic(topicArnVal) - println(DASHES) - println("Test 2 passed") - } }