Repository: softwaremill/elasticmq
Pull request #905 (2023-11-15 09:22:08)

Fix QueueDoesNotExist error in json protocol
#903


Patch:
diff --git a/build.sbt b/build.sbt
index 691c588b..38e6880b 100644
--- a/build.sbt
+++ b/build.sbt
@@ -37,6 +37,7 @@ val scalatest = "org.scalatest" %% "scalatest" % "3.2.17"
 val awaitility = "org.awaitility" % "awaitility-scala" % "4.2.0"
 
 val amazonJavaSdkSqs = "com.amazonaws" % "aws-java-sdk-sqs" % "1.12.472" exclude ("commons-logging", "commons-logging")
+val amazonJavaV2SdkSqs = "software.amazon.awssdk" % "sqs" % "2.21.22"
 
 val pekkoVersion = "1.0.1"
 val pekkoHttpVersion = "1.0.0"
@@ -191,7 +192,7 @@ lazy val restSqsTestingAmazonJavaSdk: Project =
     .settings(
       Seq(
         name := "elasticmq-rest-sqs-testing-amazon-java-sdk",
-        libraryDependencies ++= Seq(amazonJavaSdkSqs, awsSpringMessaging, jclOverSlf4j, springWeb) ++ common,
+        libraryDependencies ++= Seq(amazonJavaSdkSqs, amazonJavaV2SdkSqs, awsSpringMessaging, jclOverSlf4j, springWeb) ++ common,
         publish / skip := true
       )
     )
diff --git a/rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/SQSException.scala b/rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/SQSException.scala
index cd4c49bc..b904aa13 100644
--- a/rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/SQSException.scala
+++ b/rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/SQSException.scala
@@ -71,5 +71,5 @@ object SQSException {
 
   /** Indicates a queue does not exist */
   def nonExistentQueue: SQSException =
-    new SQSException("AWS.SimpleQueueService.NonExistentQueue")
+    new SQSException("AWS.SimpleQueueService.NonExistentQueue", errorType = "com.amazonaws.sqs#QueueDoesNotExist", errorMessage = Some("The specified queue does not exist."))
 }


Test patch:
diff --git a/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonCliTestSuite.scala b/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonCliTestSuite.scala
index aae13b05..116b3986 100644
--- a/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonCliTestSuite.scala
+++ b/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonCliTestSuite.scala
@@ -264,7 +264,7 @@ class AmazonCliTestSuite
 
       // then
       outLines.mkString("\n") shouldBe empty
-      errLines.mkString("\n") should include("AWS.SimpleQueueService.NonExistentQueue; see the SQS docs.")
+      errLines.mkString("\n") should include("The specified queue does not exist.")
     }
 
     test(s"should tag, untag and list queue tags ${version.name}", Only213) {
diff --git a/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonJavaSdkV2TestSuite.scala b/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonJavaSdkV2TestSuite.scala
new file mode 100644
index 00000000..e2fb482c
--- /dev/null
+++ b/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonJavaSdkV2TestSuite.scala
@@ -0,0 +1,34 @@
+package org.elasticmq.rest.sqs
+
+import org.scalatest.matchers.should.Matchers
+import software.amazon.awssdk.services.sqs.model._
+import software.amazon.awssdk.services.sqs.model.{GetQueueUrlRequest => AwsSdkGetQueueUrlRequest}
+
+class AmazonJavaSdkV2TestSuite extends SqsClientServerWithSdkV2Communication with Matchers {
+
+  test("should create a queue") {
+    clientV2.createQueue(CreateQueueRequest.builder().queueName("testQueue1").build())
+  }
+
+  test("should get queue url") {
+    // Given
+    clientV2.createQueue(CreateQueueRequest.builder().queueName("testQueue1").build())
+
+    // When
+    val queueUrl = clientV2.getQueueUrl(AwsSdkGetQueueUrlRequest.builder().queueName("testQueue1").build()).queueUrl()
+
+    // Then
+    queueUrl shouldEqual "http://localhost:9321/123456789012/testQueue1"
+  }
+
+  test("should fail to get queue url if queue doesn't exist") {
+    // When
+    val thrown = intercept[QueueDoesNotExistException] {
+      clientV2.getQueueUrl(AwsSdkGetQueueUrlRequest.builder().queueName("testQueue1").build()).queueUrl()
+    }
+
+    // Then
+    thrown.awsErrorDetails().errorCode() shouldBe "QueueDoesNotExist"
+    thrown.awsErrorDetails().errorMessage() shouldBe "The specified queue does not exist."
+  }
+}
diff --git a/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/SqsClientServerWithSdkV2Communication.scala b/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/SqsClientServerWithSdkV2Communication.scala
new file mode 100644
index 00000000..433e8393
--- /dev/null
+++ b/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/SqsClientServerWithSdkV2Communication.scala
@@ -0,0 +1,69 @@
+package org.elasticmq.rest.sqs
+
+import org.elasticmq.util.Logging
+import org.elasticmq.{NodeAddress, RelaxedSQSLimits}
+import org.scalatest.funsuite.AnyFunSuite
+import org.scalatest.{Args, BeforeAndAfter, Status}
+import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
+import software.amazon.awssdk.regions.Region
+import software.amazon.awssdk.services.sqs.{SqsClient, SqsClientBuilder}
+
+import java.net.URI
+import scala.util.Try
+
+trait SqsClientServerWithSdkV2Communication extends AnyFunSuite with BeforeAndAfter with Logging {
+
+  var clientV2: SqsClient = _ // strict server
+  var relaxedClientV2: SqsClient = _
+
+  var currentTestName: String = _
+
+  var strictServer: SQSRestServer = _
+  var relaxedServer: SQSRestServer = _
+  val awsAccountId = "123456789012"
+  val awsRegion = "elasticmq"
+
+  val ServiceEndpoint = "http://localhost:9321"
+
+  before {
+    logger.info(s"\n---\nRunning test: $currentTestName\n---\n")
+
+    strictServer = SQSRestServerBuilder
+      .withPort(9321)
+      .withServerAddress(NodeAddress(port = 9321))
+      .withAWSAccountId(awsAccountId)
+      .withAWSRegion(awsRegion)
+      .start()
+
+    relaxedServer = SQSRestServerBuilder
+      .withPort(9322)
+      .withServerAddress(NodeAddress(port = 9322))
+      .withSQSLimits(RelaxedSQSLimits)
+      .start()
+
+    strictServer.waitUntilStarted()
+    relaxedServer.waitUntilStarted()
+
+    clientV2 = SqsClient.builder()
+      .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
+      .region(Region.EU_CENTRAL_1)
+      .endpointOverride(new URI("http://localhost:9321"))
+      .build()
+
+    relaxedClientV2 = SqsClient.builder()
+      .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
+      .region(Region.EU_CENTRAL_1)
+      .endpointOverride(new URI("http://localhost:9322"))
+      .build()
+  }
+
+  after {
+    clientV2.close()
+    relaxedClientV2.close()
+
+    Try(strictServer.stopAndWait())
+    Try(relaxedServer.stopAndWait())
+
+    logger.info(s"\n---\nTest done: $currentTestName\n---\n")
+  }
+}