Skip to content

Commit 7cdb4a9

Browse files
committed
Migrate MessageAttributesTests and supporting tests to new test structure
1 parent 38ea13f commit 7cdb4a9

File tree

8 files changed

+178
-149
lines changed

8 files changed

+178
-149
lines changed

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

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

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

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.elasticmq.rest.sqs.aws
22

3-
import org.elasticmq.rest.sqs.AwsConfig
43
import org.elasticmq.rest.sqs.client._
54
import org.elasticmq.MessageAttribute
65
import org.scalatest.concurrent.Eventually

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ abstract class AmazonJavaSdkNewTestSuite
1212
with MessageMoveTaskTests
1313
with CreateQueueRaceConditionTests
1414
with FifoDeduplicationTests
15+
with MessageAttributesTests
1516

1617
class AmazonJavaSdkV1TestSuite extends AmazonJavaSdkNewTestSuite with SqsClientServerCommunication
1718
class AmazonJavaSdkV2TestSuite extends AmazonJavaSdkNewTestSuite with SqsClientServerWithSdkV2Communication

rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AwsConfig.scala renamed to rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/aws/AwsConfig.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.elasticmq.rest.sqs
1+
package org.elasticmq.rest.sqs.aws
22

33
trait AwsConfig {
44

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.elasticmq.rest.sqs.aws
2+
3+
import org.elasticmq.StringMessageAttribute
4+
import org.elasticmq.rest.sqs.client._
5+
6+
trait MessageAttributesTests extends AmazonJavaSdkNewTestBase {
7+
8+
test("Sending message with empty attribute value and String data type should result in error") {
9+
// given
10+
val queueUrl = testClient.createQueue("testQueue1").toOption.get
11+
12+
// expect
13+
assertError(
14+
testClient.sendMessage(
15+
queueUrl,
16+
"Message 1",
17+
messageAttributes = Map("attribute1" -> StringMessageAttribute("", Some("String")))
18+
),
19+
InvalidParameterValue,
20+
"Attribute 'attribute1' must contain a non-empty value of type 'String"
21+
)
22+
}
23+
24+
test("Sending message with empty attribute value and Number data type should result in error") {
25+
// given
26+
val queueUrl = testClient.createQueue("testQueue1").toOption.get
27+
28+
// expect
29+
assertError(
30+
testClient.sendMessage(
31+
queueUrl,
32+
"Message 1",
33+
messageAttributes = Map("attribute1" -> StringMessageAttribute("", Some("Number")))
34+
),
35+
InvalidParameterValue,
36+
"must contain a non-empty value"
37+
)
38+
}
39+
40+
test("Sending message with empty attribute type should result in error") {
41+
// given
42+
val queueUrl = testClient.createQueue("testQueue1").toOption.get
43+
44+
// expect
45+
assertError(
46+
testClient.sendMessage(
47+
queueUrl,
48+
"Message 1",
49+
messageAttributes = Map("attribute1" -> StringMessageAttribute("value", Some("")))
50+
),
51+
InvalidParameterValue,
52+
"" // Message varies between SDK versions and based on whether it's a client or server error
53+
)
54+
}
55+
56+
test(
57+
"Sending message in batch should result in accepting only those messages that do not have empty message attributes"
58+
) {
59+
// given
60+
val queueUrl = testClient.createQueue("testQueue1").toOption.get
61+
62+
// when
63+
val result = testClient.sendMessageBatch(
64+
queueUrl,
65+
List(
66+
SendMessageBatchEntry(
67+
"1",
68+
"Message 1",
69+
messageAttributes = Map("attribute1" -> StringMessageAttribute("value1"))
70+
),
71+
SendMessageBatchEntry(
72+
"2",
73+
"Message 2",
74+
messageAttributes = Map("attribute1" -> StringMessageAttribute("", Some("String")))
75+
),
76+
SendMessageBatchEntry(
77+
"3",
78+
"Message 3",
79+
messageAttributes = Map("attribute1" -> StringMessageAttribute("value1"))
80+
)
81+
)
82+
).toOption.get
83+
84+
// then
85+
result.successful should have size 2
86+
result.failed should have size 1
87+
88+
val messages = testClient.receiveMessage(queueUrl, maxNumberOfMessages = Some(3))
89+
messages should have size 2
90+
messages.map(_.body).toSet should be(Set("Message 1", "Message 3"))
91+
}
92+
93+
test("For normal queues, MessageDeduplicationId and MessageGroupId should not be sent with ReceiveMessage response") {
94+
// given
95+
val queueUrl = testClient.createQueue("testQueue1").toOption.get
96+
testClient.sendMessage(queueUrl, "Message 1")
97+
98+
// when
99+
val messages = testClient.receiveMessage(queueUrl, systemAttributes = List("All"))
100+
101+
// then
102+
messages should have size 1
103+
val messageAttributes = messages.head.attributes
104+
messageAttributes.get(MessageDeduplicationId) should not be defined
105+
messageAttributes.get(MessageGroupId) should not be defined
106+
}
107+
108+
test("For FIFO queues, MessageDeduplicationId and MessageGroupId should be sent with ReceiveMessage response") {
109+
// given
110+
val queueUrl = testClient
111+
.createQueue(
112+
"testQueue1.fifo",
113+
Map(FifoQueueAttributeName -> "true", ContentBasedDeduplicationAttributeName -> "true")
114+
)
115+
.toOption
116+
.get
117+
testClient.sendMessage(
118+
queueUrl,
119+
"Message 1",
120+
messageDeduplicationId = Some("123"),
121+
messageGroupId = Some("456")
122+
)
123+
124+
// when
125+
val messages = testClient.receiveMessage(queueUrl, systemAttributes = List("All"))
126+
127+
// then
128+
messages should have size 1
129+
val messageAttributes = messages.head.attributes
130+
messageAttributes.get(MessageDeduplicationId) shouldBe Some("123")
131+
messageAttributes.get(MessageGroupId) shouldBe Some("456")
132+
}
133+
134+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,15 @@ class AwsSdkV1SqsClient(client: AmazonSQS) extends SqsClient {
430430
Left(SqsClientError(ResourceNotFound, e.getErrorMessage))
431431
case e: QueueDoesNotExistException =>
432432
Left(SqsClientError(QueueDoesNotExist, e.getErrorMessage))
433-
case e: com.amazonaws.services.sqs.model.AmazonSQSException if e.getErrorCode == "InvalidParameterValue" || e.getErrorCode == "InvalidAttributeValue" =>
433+
case e: com.amazonaws.services.sqs.model.AmazonSQSException
434+
if e.getErrorCode == "InvalidParameterValue" || e.getErrorCode == "InvalidAttributeValue" || e.getErrorCode == "InvalidAttributeName" =>
434435
Left(SqsClientError(InvalidParameterValue, e.getErrorMessage))
435436
case e: com.amazonaws.services.sqs.model.AmazonSQSException if e.getErrorCode == "MissingParameter" =>
436437
Left(SqsClientError(MissingParameter, e.getErrorMessage))
437-
case e: Exception => Left(SqsClientError(UnknownSqsClientErrorType, e.getMessage))
438+
case e: com.amazonaws.AmazonClientException if e.getMessage.contains("MD5 returned by SQS does not match") =>
439+
Left(SqsClientError(InvalidParameterValue, e.getMessage))
440+
case e: Exception =>
441+
Left(SqsClientError(UnknownSqsClientErrorType, e.getMessage))
438442
}
439443
}
440444
}

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

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,31 @@ class AwsSdkV2SqsClient(client: software.amazon.awssdk.services.sqs.SqsClient) e
132132
}
133133

134134
private def mapMessageAttributes(
135-
messageAttributes: Map[
136-
String,
137-
MessageAttribute
138-
]
139-
) = {
140-
messageAttributes.map {
141-
case (k, v: StringMessageAttribute) =>
142-
k -> MessageAttributeValue.builder().dataType(v.getDataType()).stringValue(v.stringValue).build()
143-
case (k, v: NumberMessageAttribute) =>
144-
k -> MessageAttributeValue.builder().dataType(v.getDataType()).stringValue(v.stringValue).build()
145-
case (k, v: BinaryMessageAttribute) =>
146-
k -> MessageAttributeValue
147-
.builder()
148-
.dataType(v.getDataType())
135+
messageAttributes: Map[String, MessageAttribute]
136+
): java.util.Map[String, MessageAttributeValue] = {
137+
messageAttributes.map { case (key, attribute) =>
138+
key -> buildMessageAttributeValue(attribute)
139+
}.asJava
140+
}
141+
142+
private def buildMessageAttributeValue(attribute: MessageAttribute): MessageAttributeValue = {
143+
val builder = MessageAttributeValue.builder()
144+
val dataType = attribute.getDataType()
145+
146+
if (dataType.nonEmpty) {
147+
builder.dataType(dataType)
148+
}
149+
150+
attribute match {
151+
case v: StringMessageAttribute =>
152+
builder.stringValue(v.stringValue).build()
153+
case v: NumberMessageAttribute =>
154+
builder.stringValue(v.stringValue).build()
155+
case v: BinaryMessageAttribute =>
156+
builder
149157
.binaryValue(SdkBytes.fromByteArray(v.binaryValue.toArray))
150158
.build()
151-
}.asJava
159+
}
152160
}
153161

154162
override def sendMessageBatch(
@@ -498,17 +506,28 @@ class AwsSdkV2SqsClient(client: software.amazon.awssdk.services.sqs.SqsClient) e
498506
try {
499507
Right(f)
500508
} catch {
501-
case e: software.amazon.awssdk.services.sqs.model.SqsException if e.awsErrorDetails().errorCode() == "InvalidParameterValue" || e.awsErrorDetails().errorCode() == "InvalidAttributeValue" =>
509+
case e: software.amazon.awssdk.services.sqs.model.SqsException
510+
if e.awsErrorDetails().errorCode() == "InvalidParameterValue" || e.awsErrorDetails().errorCode() == "InvalidAttributeValue" || e.awsErrorDetails()
511+
.errorCode() == "InvalidAttributeName" =>
502512
Left(SqsClientError(InvalidParameterValue, e.awsErrorDetails().errorMessage()))
503513
case e: software.amazon.awssdk.services.sqs.model.SqsException if e.awsErrorDetails().errorCode() == "MissingParameter" =>
504514
Left(SqsClientError(MissingParameter, e.awsErrorDetails().errorMessage()))
515+
case e: software.amazon.awssdk.core.exception.SdkClientException if e.getMessage.contains("MD5 returned by SQS does not match") =>
516+
Left(SqsClientError(InvalidParameterValue, e.getMessage))
505517
case e: UnsupportedOperationException =>
506518
Left(SqsClientError(UnsupportedOperation, e.awsErrorDetails().errorMessage()))
507519
case e: ResourceNotFoundException =>
508520
Left(SqsClientError(ResourceNotFound, e.awsErrorDetails().errorMessage()))
509521
case e: QueueDoesNotExistException =>
510522
Left(SqsClientError(QueueDoesNotExist, e.awsErrorDetails().errorMessage()))
511-
case e: Exception => Left(SqsClientError(UnknownSqsClientErrorType, e.getMessage))
523+
case e: Exception =>
524+
println(s"[DEBUG_LOG] Unknown exception in AwsSdkV2SqsClient: ${e.getClass.getName} - ${e.getMessage}")
525+
if (e.isInstanceOf[software.amazon.awssdk.services.sqs.model.SqsException]) {
526+
println(
527+
s"[DEBUG_LOG] Error code: ${e.asInstanceOf[software.amazon.awssdk.services.sqs.model.SqsException].awsErrorDetails().errorCode()}"
528+
)
529+
}
530+
Left(SqsClientError(UnknownSqsClientErrorType, e.getMessage))
512531
}
513532
}
514533
}

0 commit comments

Comments
 (0)