|
1 | 1 | package org.elasticmq.rest.sqs |
2 | 2 |
|
| 3 | +import org.elasticmq.{BinaryMessageAttribute, MessageAttribute, NumberMessageAttribute, StringMessageAttribute} |
3 | 4 | import org.scalatest.matchers.should.Matchers |
| 5 | + |
| 6 | +import scala.collection.JavaConverters._ |
| 7 | +import software.amazon.awssdk.core.SdkBytes |
4 | 8 | import software.amazon.awssdk.services.sqs.model._ |
5 | 9 | import software.amazon.awssdk.services.sqs.model.{GetQueueUrlRequest => AwsSdkGetQueueUrlRequest} |
6 | 10 |
|
@@ -32,16 +36,159 @@ class AmazonJavaSdkV2TestSuite extends SqsClientServerWithSdkV2Communication wit |
32 | 36 | thrown.awsErrorDetails().errorMessage() shouldBe "The specified queue does not exist." |
33 | 37 | } |
34 | 38 |
|
35 | | - test("should send and receive message") { |
| 39 | + test("should send and receive a simple message") { |
| 40 | + doTestSendAndReceiveMessage("test msg 123") |
| 41 | + } |
| 42 | + |
| 43 | + test("should send and receive a simple message with message attributes") { |
| 44 | + doTestSendAndReceiveMessageWithAttributes( |
| 45 | + "Message 1", |
| 46 | + Map( |
| 47 | + "red" -> StringMessageAttribute("fish"), |
| 48 | + "blue" -> StringMessageAttribute("cat"), |
| 49 | + // affected by https://github.com/softwaremill/elasticmq/issues/946 |
| 50 | + // "green" -> BinaryMessageAttribute("dog".getBytes("UTF-8")), |
| 51 | + "yellow" -> NumberMessageAttribute("1234567890"), |
| 52 | + "orange" -> NumberMessageAttribute("0987654321", Some("custom")) |
| 53 | + ) |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + test("should send a simple message with message attributes and only receive requested attributes") { |
| 58 | + doTestSendAndReceiveMessageWithAttributes( |
| 59 | + "Message 1", |
| 60 | + Map( |
| 61 | + "red" -> StringMessageAttribute("fish"), |
| 62 | + "blue" -> StringMessageAttribute("cat"), |
| 63 | + // affected by https://github.com/softwaremill/elasticmq/issues/946 |
| 64 | + // "green" -> BinaryMessageAttribute("dog".getBytes("UTF-8")), |
| 65 | + "yellow" -> NumberMessageAttribute("1234567890"), |
| 66 | + "orange" -> NumberMessageAttribute("0987654321", Some("custom")) |
| 67 | + ), |
| 68 | + List("red", "green", "orange") |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + test("should send a simple message with message attributes and only receive no requested attributes by default") { |
| 73 | + doTestSendAndReceiveMessageWithAttributes( |
| 74 | + "Message 1", |
| 75 | + Map( |
| 76 | + "red" -> StringMessageAttribute("fish"), |
| 77 | + "blue" -> StringMessageAttribute("cat"), |
| 78 | + // affected by https://github.com/softwaremill/elasticmq/issues/946 |
| 79 | + // "green" -> BinaryMessageAttribute("dog".getBytes("UTF-8")), |
| 80 | + "yellow" -> NumberMessageAttribute("1234567890"), |
| 81 | + "orange" -> NumberMessageAttribute("0987654321", Some("custom")) |
| 82 | + ), |
| 83 | + List() |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + private def doTestSendAndReceiveMessage(content: String): Unit = { |
| 88 | + doTestSendAndReceiveMessageWithAttributes(content, Map(), List()) |
| 89 | + } |
| 90 | + |
| 91 | + private def doTestSendAndReceiveMessageWithAttributes( |
| 92 | + content: String, |
| 93 | + messageAttributes: Map[String, MessageAttribute] |
| 94 | + ): Unit = { |
| 95 | + doTestSendAndReceiveMessageWithAttributes(content, messageAttributes, List("All")) |
| 96 | + } |
| 97 | + |
| 98 | + private def doTestSendAndReceiveMessageWithAttributes( |
| 99 | + content: String, |
| 100 | + messageAttributes: Map[String, MessageAttribute], |
| 101 | + requestedAttributes: List[String] |
| 102 | + ): Unit = { |
| 103 | + // Given |
36 | 104 | val queue = clientV2.createQueue(CreateQueueRequest.builder().queueName("testQueue1").build()) |
37 | 105 |
|
38 | | - clientV2.sendMessage(SendMessageRequest.builder().queueUrl(queue.queueUrl()).messageBody("test msg 123").build()) |
| 106 | + // When |
| 107 | + val attributes = messageAttributes.map { |
| 108 | + case (k, v: StringMessageAttribute) => |
| 109 | + k -> MessageAttributeValue.builder().dataType(v.getDataType()).stringValue(v.stringValue).build() |
| 110 | + case (k, v: NumberMessageAttribute) => |
| 111 | + k -> MessageAttributeValue.builder().dataType(v.getDataType()).stringValue(v.stringValue).build() |
| 112 | + case (k, v: BinaryMessageAttribute) => |
| 113 | + k -> MessageAttributeValue |
| 114 | + .builder() |
| 115 | + .dataType(v.getDataType()) |
| 116 | + .binaryValue(SdkBytes.fromByteArray(v.binaryValue)) |
| 117 | + .build() |
| 118 | + } |
| 119 | + |
| 120 | + val sendMessageRequest = SendMessageRequest |
| 121 | + .builder() |
| 122 | + .queueUrl(queue.queueUrl()) |
| 123 | + .messageBody(content) |
| 124 | + .messageAttributes(attributes.asJava) |
| 125 | + .build() |
39 | 126 |
|
40 | | - val messages = clientV2.receiveMessage(ReceiveMessageRequest.builder().queueUrl(queue.queueUrl()).build()) |
| 127 | + clientV2.sendMessage(sendMessageRequest) |
41 | 128 |
|
42 | | - System.err.println(messages) |
| 129 | + val message = receiveSingleMessageObject(queue.queueUrl(), requestedAttributes).orNull |
| 130 | + |
| 131 | + // Then |
| 132 | + message.body() shouldBe content |
| 133 | + checkMessageAttributesMatchRequestedAttributes(messageAttributes, requestedAttributes, sendMessageRequest, message) |
| 134 | + } |
43 | 135 |
|
44 | | - messages.messages().size() shouldBe 1 |
45 | | - messages.messages().get(0).body() shouldBe "test msg 123" |
| 136 | + private def receiveSingleMessageObject(queueUrl: String, requestedAttributes: List[String]): Option[Message] = { |
| 137 | + clientV2 |
| 138 | + .receiveMessage( |
| 139 | + ReceiveMessageRequest |
| 140 | + .builder() |
| 141 | + .queueUrl(queueUrl) |
| 142 | + .messageAttributeNames(requestedAttributes.asJava) |
| 143 | + .build() |
| 144 | + ) |
| 145 | + .messages |
| 146 | + .asScala |
| 147 | + .headOption |
| 148 | + } |
| 149 | + |
| 150 | + private def checkMessageAttributesMatchRequestedAttributes( |
| 151 | + messageAttributes: Map[String, MessageAttribute], |
| 152 | + requestedAttributes: List[String], |
| 153 | + sendMessageRequest: SendMessageRequest, |
| 154 | + message: Message |
| 155 | + ) = { |
| 156 | + val filteredSendMessageAttr = |
| 157 | + filterBasedOnRequestedAttributes(requestedAttributes, sendMessageRequest.messageAttributes.asScala.toMap).asJava |
| 158 | + val filteredMessageAttributes = filterBasedOnRequestedAttributes(requestedAttributes, messageAttributes) |
| 159 | + |
| 160 | + message.messageAttributes should be(filteredSendMessageAttr) |
| 161 | + message.messageAttributes.asScala.map { case (k, attr) => |
| 162 | + ( |
| 163 | + k, |
| 164 | + if (attr.dataType.startsWith("String") && attr.stringValue != null) { |
| 165 | + StringMessageAttribute(attr.stringValue).stringValue |
| 166 | + } else if (attr.dataType.startsWith("Number") && attr.stringValue != null) { |
| 167 | + NumberMessageAttribute(attr.stringValue).stringValue |
| 168 | + } else { |
| 169 | + BinaryMessageAttribute.fromByteBuffer(attr.binaryValue.asByteBuffer()).asBase64 |
| 170 | + } |
| 171 | + ) |
| 172 | + } should be(filteredMessageAttributes.map { case (k, attr) => |
| 173 | + ( |
| 174 | + k, |
| 175 | + attr match { |
| 176 | + case s: StringMessageAttribute => s.stringValue |
| 177 | + case n: NumberMessageAttribute => n.stringValue |
| 178 | + case b: BinaryMessageAttribute => b.asBase64 |
| 179 | + } |
| 180 | + ) |
| 181 | + }) |
| 182 | + } |
| 183 | + |
| 184 | + private def filterBasedOnRequestedAttributes[T]( |
| 185 | + requestedAttributes: List[String], |
| 186 | + messageAttributes: Map[String, T] |
| 187 | + ): Map[String, T] = { |
| 188 | + if (requestedAttributes.contains("All")) { |
| 189 | + messageAttributes |
| 190 | + } else { |
| 191 | + messageAttributes.filterKeys(k => requestedAttributes.contains(k)).toMap |
| 192 | + } |
46 | 193 | } |
47 | 194 | } |
0 commit comments