Skip to content

Commit 4f901c2

Browse files
authored
fix empty attributes being present in output json (#913)
1 parent 52424d5 commit 4f901c2

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,14 @@ class AmazonCliTestSuite
348348
ReceiptHandle.nonEmpty shouldBe true
349349
MD5OfBody shouldBe firstMessage.MD5OfMessageBody
350350
Body shouldBe firstMessageBody
351-
Attributes.size shouldBe 4
352-
Attributes.get("SentTimestamp").map(v => v.nonEmpty shouldBe true)
353-
Attributes.get("ApproximateReceiveCount").map(_ shouldBe "1")
354-
Attributes.get("ApproximateFirstReceiveTimestamp").map(v => v.nonEmpty shouldBe true)
355-
Attributes.get("SenderId").map(_ shouldBe "127.0.0.1")
351+
Attributes.get.size shouldBe 4
352+
Attributes.get.get("SentTimestamp").map(v => v.nonEmpty shouldBe true)
353+
Attributes.get.get("ApproximateReceiveCount").map(_ shouldBe "1")
354+
Attributes.get.get("ApproximateFirstReceiveTimestamp").map(v => v.nonEmpty shouldBe true)
355+
Attributes.get.get("SenderId").map(_ shouldBe "127.0.0.1")
356356
MD5OfMessageAttributes.nonEmpty shouldBe true
357-
MessageAttributes.nonEmpty shouldBe true
358-
MessageAttributes.get("firstAttribute").map { msgAtt =>
357+
MessageAttributes.get.nonEmpty shouldBe true
358+
MessageAttributes.get.get("firstAttribute").map { msgAtt =>
359359
msgAtt.getDataType() shouldBe "String"
360360
msgAtt shouldBe a[StringMessageAttribute]
361361
inside(msgAtt) { case stringMessageAttribute: StringMessageAttribute =>
@@ -369,14 +369,14 @@ class AmazonCliTestSuite
369369
ReceiptHandle.nonEmpty shouldBe true
370370
MD5OfBody shouldBe secondMessage.MD5OfMessageBody
371371
Body shouldBe secondMessageBody
372-
Attributes.size shouldBe 4
373-
Attributes.get("SentTimestamp").map(v => v.nonEmpty shouldBe true)
374-
Attributes.get("ApproximateReceiveCount").map(_ shouldBe "1")
375-
Attributes.get("ApproximateFirstReceiveTimestamp").map(v => v.nonEmpty shouldBe true)
376-
Attributes.get("SenderId").map(_ shouldBe "127.0.0.1")
372+
Attributes.get.size shouldBe 4
373+
Attributes.get.get("SentTimestamp").map(v => v.nonEmpty shouldBe true)
374+
Attributes.get.get("ApproximateReceiveCount").map(_ shouldBe "1")
375+
Attributes.get.get("ApproximateFirstReceiveTimestamp").map(v => v.nonEmpty shouldBe true)
376+
Attributes.get.get("SenderId").map(_ shouldBe "127.0.0.1")
377377
MD5OfMessageAttributes.nonEmpty shouldBe true
378-
MessageAttributes.nonEmpty shouldBe true
379-
MessageAttributes.get("secondAttribute").map { msgAtt =>
378+
MessageAttributes.get.nonEmpty shouldBe true
379+
MessageAttributes.get.get("secondAttribute").map { msgAtt =>
380380
msgAtt.getDataType() shouldBe "String"
381381
msgAtt shouldBe a[StringMessageAttribute]
382382
inside(msgAtt) { case stringMessageAttribute: StringMessageAttribute =>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ class AmazonJavaSdkV2TestSuite extends SqsClientServerWithSdkV2Communication wit
3131
thrown.awsErrorDetails().errorCode() shouldBe "QueueDoesNotExist"
3232
thrown.awsErrorDetails().errorMessage() shouldBe "The specified queue does not exist."
3333
}
34+
35+
test("should send and receive message") {
36+
val queue = clientV2.createQueue(CreateQueueRequest.builder().queueName("testQueue1").build())
37+
38+
clientV2.sendMessage(SendMessageRequest.builder().queueUrl(queue.queueUrl()).messageBody("test msg 123").build())
39+
40+
val messages = clientV2.receiveMessage(ReceiveMessageRequest.builder().queueUrl(queue.queueUrl()).build())
41+
42+
System.err.println(messages)
43+
44+
messages.messages().size() shouldBe 1
45+
messages.messages().get(0).body() shouldBe "test msg 123"
46+
}
3447
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ trait SqsClientServerWithSdkV2Communication extends AnyFunSuite with BeforeAndAf
2626
val ServiceEndpoint = "http://localhost:9321"
2727

2828
before {
29-
logger.info(s"\n---\nRunning test: $currentTestName\n---\n")
30-
3129
strictServer = SQSRestServerBuilder
3230
.withPort(9321)
3331
.withServerAddress(NodeAddress(port = 9321))
@@ -65,7 +63,5 @@ trait SqsClientServerWithSdkV2Communication extends AnyFunSuite with BeforeAndAf
6563

6664
Try(strictServer.stopAndWait())
6765
Try(relaxedServer.stopAndWait())
68-
69-
logger.info(s"\n---\nTest done: $currentTestName\n---\n")
7066
}
7167
}

rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/ReceiveMessageDirectives.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ trait ReceiveMessageDirectives {
135135
.map(_.receipt)
136136
.getOrElse(throw new RuntimeException("No receipt for a received msg."))
137137
val filteredMessageAttributes = getFilteredAttributeNames(messageAttributeNames, message)
138+
val calculatedAttributes = calculateAttributeValues(message).toMap
138139

139140
ReceivedMessage(
140-
Attributes = calculateAttributeValues(message).toMap,
141+
Attributes = if (calculatedAttributes.nonEmpty) Some(calculatedAttributes) else None,
141142
Body = message.content,
142143
MD5OfBody = md5Digest(message.content),
143144
MD5OfMessageAttributes =
144145
if (filteredMessageAttributes.nonEmpty) Some(md5AttributeDigest(filteredMessageAttributes)) else None,
145-
MessageAttributes = filteredMessageAttributes,
146+
MessageAttributes = if (filteredMessageAttributes.nonEmpty) Some(filteredMessageAttributes) else None,
146147
MessageId = message.id.id,
147148
ReceiptHandle = receipt
148149
)
@@ -247,11 +248,11 @@ object ReceiveMessageResponse {
247248
}
248249

249250
case class ReceivedMessage(
250-
Attributes: Map[String, String],
251+
Attributes: Option[Map[String, String]],
251252
Body: String,
252253
MD5OfBody: String,
253254
MD5OfMessageAttributes: Option[String],
254-
MessageAttributes: Map[String, MessageAttribute],
255+
MessageAttributes: Option[Map[String, MessageAttribute]],
255256
MessageId: String,
256257
ReceiptHandle: String
257258
)
@@ -266,9 +267,9 @@ object ReceivedMessage extends MessageAttributesSupport {
266267
<ReceiptHandle>{msg.ReceiptHandle}</ReceiptHandle>
267268
<MD5OfBody>{msg.MD5OfBody}</MD5OfBody>
268269
<Body>{XmlUtil.convertTexWithCRToNodeSeq(msg.Body)}</Body>
269-
{attributesToXmlConverter.convert(msg.Attributes.toList)}
270+
{attributesToXmlConverter.convert(msg.Attributes.getOrElse(Map.empty).toList)}
270271
{msg.MD5OfMessageAttributes.map(md5 => <MD5OfMessageAttributes>{md5}</MD5OfMessageAttributes>).getOrElse("")}
271-
{messageAttributesToXmlConverter.convert(msg.MessageAttributes.toList)}
272+
{messageAttributesToXmlConverter.convert(msg.MessageAttributes.getOrElse(Map.empty).toList)}
272273
</Message>
273274
}
274275
}

0 commit comments

Comments
 (0)