use of software.amazon.awssdk.services.sns.model.MessageAttributeValue in project spring-cloud-aws by awspring.
the class TopicMessageChannelTest method sendMessage_withNumericMessageHeaders_shouldBeSentAsTopicMessageAttributes.
@Test
void sendMessage_withNumericMessageHeaders_shouldBeSentAsTopicMessageAttributes() {
// Arrange
double doubleValue = 1234.56;
long longValue = 1234L;
int integerValue = 1234;
byte byteValue = 2;
short shortValue = 12;
float floatValue = 1234.56f;
BigInteger bigIntegerValue = new BigInteger("616416546156");
BigDecimal bigDecimalValue = new BigDecimal("7834938");
Message<String> message = MessageBuilder.withPayload("Hello").setHeader("double", doubleValue).setHeader("long", longValue).setHeader("integer", integerValue).setHeader("byte", byteValue).setHeader("short", shortValue).setHeader("float", floatValue).setHeader("bigInteger", bigIntegerValue).setHeader("bigDecimal", bigDecimalValue).build();
// Act
boolean sent = messageChannel.send(message);
// Assert
verify(snsClient).publish(requestMatches(it -> {
Map<String, MessageAttributeValue> attrs = it.messageAttributes();
assertThat(attrs.get("double").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.lang.Double");
assertThat(attrs.get("double").stringValue()).isEqualTo(String.valueOf(doubleValue));
assertThat(attrs.get("long").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.lang.Long");
assertThat(attrs.get("long").stringValue()).isEqualTo(String.valueOf(longValue));
assertThat(attrs.get("integer").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.lang.Integer");
assertThat(attrs.get("integer").stringValue()).isEqualTo(String.valueOf(integerValue));
assertThat(attrs.get("byte").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.lang.Byte");
assertThat(attrs.get("byte").stringValue()).isEqualTo(String.valueOf(byteValue));
assertThat(attrs.get("short").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.lang.Short");
assertThat(attrs.get("short").stringValue()).isEqualTo(String.valueOf(shortValue));
assertThat(attrs.get("float").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.lang.Float");
assertThat(attrs.get("float").stringValue()).isEqualTo(String.valueOf(floatValue));
assertThat(attrs.get("bigInteger").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.math.BigInteger");
assertThat(attrs.get("bigInteger").stringValue()).isEqualTo(String.valueOf(bigIntegerValue));
assertThat(attrs.get("bigDecimal").dataType()).isEqualTo(MessageAttributeDataTypes.NUMBER + ".java.math.BigDecimal");
assertThat(attrs.get("bigDecimal").stringValue()).isEqualTo(String.valueOf(bigDecimalValue));
}));
assertThat(sent).isTrue();
}
use of software.amazon.awssdk.services.sns.model.MessageAttributeValue in project formkiq-core by formkiq.
the class DocumentsS3Update method sendSnsMessage.
/**
* Either sends the Create Message to SNS.
*
* @param logger {@link LambdaLogger}
* @param eventType {@link String}
* @param siteId {@link String}
* @param doc {@link DynamicDocumentItem}
* @param s3Bucket {@link String}
* @param s3Key {@link String}
* @param content {@link String}
*/
private void sendSnsMessage(final LambdaLogger logger, final String eventType, final String siteId, final DynamicDocumentItem doc, final String s3Bucket, final String s3Key, final String content) {
String site = siteId != null ? siteId : SiteIdKeyGenerator.DEFAULT_SITE_ID;
DocumentEvent event = new DocumentEvent().siteId(site).documentId(resetDatabaseKey(siteId, doc.getDocumentId())).s3bucket(s3Bucket).s3key(s3Key).type(eventType).userId(doc.getUserId()).contentType(doc.getContentType()).path(doc.getPath());
if ("application/json".equals(doc.getContentType())) {
event.content(content);
}
String eventJson = this.gson.toJson(event);
if (eventJson.length() > MAX_SNS_MESSAGE_SIZE) {
event.content(null);
eventJson = this.gson.toJson(event);
}
boolean debug = "true".equals(System.getenv("DEBUG"));
if (debug) {
logger.log("event: " + eventJson);
}
logger.log("publishing " + event.type() + " document message to " + this.snsDocumentEvent);
MessageAttributeValue typeAttr = MessageAttributeValue.builder().dataType("String").stringValue(event.type()).build();
MessageAttributeValue siteIdAttr = MessageAttributeValue.builder().dataType("String").stringValue(convertToPrintableCharacters(event.siteId())).build();
Map<String, MessageAttributeValue> tags = Map.of("type", typeAttr, "siteId", siteIdAttr);
if (doc.getUserId() != null) {
MessageAttributeValue userIdAttr = MessageAttributeValue.builder().dataType("String").stringValue(convertToPrintableCharacters(event.userId())).build();
tags = Map.of("type", typeAttr, "siteId", siteIdAttr, "userId", userIdAttr);
}
for (Map.Entry<String, MessageAttributeValue> e : tags.entrySet()) {
logger.log("Adding Message Attribute: '" + e.getKey() + "', attributes: " + e.getValue().dataType() + " " + e.getValue().stringValue());
}
this.snsService.publish(this.snsDocumentEvent, eventJson, tags);
}
Aggregations