Skip to content

Commit b4635b2

Browse files
committed
Migrate CustomServerContextTest and SqlQueuePersistenceServer to new test structure, updating to AWS SDK v2 clients
1 parent 526eb61 commit b4635b2

File tree

4 files changed

+146
-148
lines changed

4 files changed

+146
-148
lines changed

rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/CustomServerContextTest.scala

Lines changed: 0 additions & 58 deletions
This file was deleted.

rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/SqlQueuePersistenceServer.scala

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.elasticmq.rest.sqs.aws
2+
3+
import org.elasticmq.NodeAddress
4+
import org.elasticmq.rest.sqs.client.{AwsSdkV2SqsClient, SqsClient}
5+
import org.elasticmq.rest.sqs.{SQSRestServer, SQSRestServerBuilder}
6+
import org.scalatest.BeforeAndAfter
7+
import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
8+
import software.amazon.awssdk.regions.Region
9+
import software.amazon.awssdk.services.sqs.{SqsClient => AwsSqsClient}
10+
11+
import java.net.URI
12+
import scala.util.Try
13+
14+
class CustomServerContextTests extends AmazonJavaSdkNewTestBase with BeforeAndAfter {
15+
16+
var testClient: SqsClient = _
17+
var clientV2: AwsSqsClient = _
18+
var server: SQSRestServer = _
19+
20+
val awsAccountId = "123456789012"
21+
val awsRegion = "elasticmq"
22+
val port = 9324
23+
val contextPath = "xyz"
24+
25+
before {
26+
server = SQSRestServerBuilder
27+
.withPort(port)
28+
.withServerAddress(NodeAddress(port = port, contextPath = contextPath))
29+
.withAWSAccountId(awsAccountId)
30+
.withAWSRegion(awsRegion)
31+
.start()
32+
33+
server.waitUntilStarted()
34+
35+
clientV2 = AwsSqsClient
36+
.builder()
37+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
38+
.region(Region.US_EAST_1)
39+
.endpointOverride(new URI(s"http://localhost:$port/$contextPath"))
40+
.build()
41+
42+
testClient = new AwsSdkV2SqsClient(clientV2)
43+
}
44+
45+
after {
46+
clientV2.close()
47+
Try(server.stopAndWait())
48+
}
49+
50+
test("should create queue when context path is set") {
51+
val queueUrl = testClient.createQueue("testQueue").toOption.get
52+
queueUrl should be("http://localhost:9324/xyz/123456789012/testQueue")
53+
}
54+
55+
test("should send and receive messages when context path is set") {
56+
val queueUrl = testClient.createQueue("testQueue").toOption.get
57+
58+
testClient.sendMessage("http://localhost:9324/xyz/123456789012/testQueue", "test msg1").isRight shouldBe true
59+
testClient.sendMessage("http://localhost:9324/xyz/queue/testQueue", "test msg2").isRight shouldBe true
60+
testClient.sendMessage("http://localhost:9324/xyz/abc/testQueue", "test msg3").isRight shouldBe true
61+
testClient.sendMessage("http://localhost:9324/xyz/testQueue", "test msg4").isRight shouldBe true
62+
63+
val messages = testClient.receiveMessage(queueUrl, maxNumberOfMessages = Some(10))
64+
65+
messages.map(_.body) should contain allOf ("test msg1", "test msg2", "test msg3", "test msg4")
66+
}
67+
}

rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/aws/SqlQueuePersistenceTests.scala

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,102 @@
11
package org.elasticmq.rest.sqs.aws
22

3+
import org.apache.pekko.actor.{ActorRef, ActorSystem, Props}
4+
import org.apache.pekko.util.Timeout
5+
import org.elasticmq.actor.QueueManagerActor
6+
import org.elasticmq.actor.queue.QueueEvent
37
import org.elasticmq.actor.reply._
4-
import org.elasticmq.persistence.sql.GetAllMessages
5-
import org.elasticmq.rest.sqs.SqlQueuePersistenceServer
8+
import org.elasticmq.persistence.sql.{GetAllMessages, SqlQueuePersistenceActor, SqlQueuePersistenceConfig}
69
import org.elasticmq.rest.sqs.client._
710
import org.elasticmq.rest.sqs.model.RedrivePolicy
811
import org.elasticmq.rest.sqs.model.RedrivePolicyJson.format
9-
import org.elasticmq.util.Logging
12+
import org.elasticmq.rest.sqs.{SQSRestServer, SQSRestServerBuilder}
13+
import org.elasticmq.util.{Logging, NowProvider}
14+
import org.elasticmq.{NodeAddress, StrictSQSLimits}
1015
import org.scalatest.BeforeAndAfter
16+
import org.scalatest.concurrent.ScalaFutures
1117
import org.scalatest.funsuite.AnyFunSuite
1218
import org.scalatest.matchers.should.Matchers
19+
import org.scalatest.time.{Seconds, Span}
20+
import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
21+
import software.amazon.awssdk.regions.Region
22+
import software.amazon.awssdk.services.sqs.{SqsClient => AwsSqsClient}
1323
import spray.json.enrichAny
1424

25+
import java.net.URI
26+
import scala.util.Try
27+
1528
class SqlQueuePersistenceTests
1629
extends AnyFunSuite
17-
with SqlQueuePersistenceServer
30+
with ScalaFutures
1831
with BeforeAndAfter
1932
with Matchers
2033
with Logging {
2134

35+
private val awsAccountId = "123456789012"
36+
private val awsRegion = "elasticmq"
37+
38+
private val actorSystem: ActorSystem = ActorSystem("elasticmq-test-v2")
39+
private var strictServer: SQSRestServer = _
40+
41+
var clientV2: AwsSqsClient = _
42+
var testClient: AwsSdkV2SqsClient = _
43+
var store: ActorRef = _
44+
45+
implicit val timeout: Timeout = {
46+
import scala.concurrent.duration._
47+
Timeout(5.seconds)
48+
}
49+
50+
implicit val defaultPatience: PatienceConfig = PatienceConfig(timeout = Span(5, Seconds))
51+
2252
val testQueueName = "testQueue1"
2353
val deadLetterQueueName = "testDLQ1"
2454

25-
val awsAccountId = "123456789012"
26-
val awsRegion = "elasticmq"
55+
def startServerAndRun(pruneDataOnInit: Boolean)(body: => Unit): Unit = {
56+
startServerAndSetupClient(pruneDataOnInit)
57+
try {
58+
body
59+
} finally {
60+
stopServerAndClient()
61+
}
62+
}
63+
64+
private def startServerAndSetupClient(pruneDataOnInit: Boolean): Unit = {
65+
val persistenceConfig = SqlQueuePersistenceConfig(
66+
enabled = true,
67+
driverClass = "org.h2.Driver",
68+
uri = "jdbc:h2:./elasticmq-h2-v2",
69+
pruneDataOnInit = pruneDataOnInit
70+
)
71+
72+
store = actorSystem.actorOf(Props(new SqlQueuePersistenceActor(persistenceConfig, List.empty)))
73+
val manager = actorSystem.actorOf(Props(new QueueManagerActor(new NowProvider(), StrictSQSLimits, Some(store))))
74+
75+
strictServer = SQSRestServerBuilder
76+
.withActorSystem(actorSystem)
77+
.withQueueManagerActor(manager)
78+
.withPort(9323) // different port to avoid conflicts
79+
.withServerAddress(NodeAddress(port = 9323))
80+
.withAWSAccountId(awsAccountId)
81+
.withAWSRegion(awsRegion)
82+
.start()
83+
84+
(store ? QueueEvent.Restore(manager)).futureValue
85+
86+
clientV2 = AwsSqsClient
87+
.builder()
88+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
89+
.region(Region.EU_CENTRAL_1)
90+
.endpointOverride(new URI("http://localhost:9323"))
91+
.build()
92+
93+
testClient = new AwsSdkV2SqsClient(clientV2)
94+
}
95+
96+
private def stopServerAndClient(): Unit = {
97+
if (clientV2 != null) clientV2.close()
98+
if (strictServer != null) Try(strictServer.stopAndWait())
99+
}
27100

28101
test("should persist the messages and after restart read the messages") {
29102
startServerAndRun(pruneDataOnInit = true) {

0 commit comments

Comments
 (0)