Search in sources :

Example 1 with MessageAttributeValue

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();
}
Also used : Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UUID(java.util.UUID) NOTIFICATION_SUBJECT_HEADER(io.awspring.cloud.sns.core.SnsHeaders.NOTIFICATION_SUBJECT_HEADER) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) ByteBuffer(java.nio.ByteBuffer) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Arn(software.amazon.awssdk.arns.Arn) BigDecimal(java.math.BigDecimal) List(java.util.List) MESSAGE_GROUP_ID_HEADER(io.awspring.cloud.sns.core.SnsHeaders.MESSAGE_GROUP_ID_HEADER) Map(java.util.Map) SnsClient(software.amazon.awssdk.services.sns.SnsClient) Matchers.requestMatches(io.awspring.cloud.sns.core.Matchers.requestMatches) BigInteger(java.math.BigInteger) Message(org.springframework.messaging.Message) MessageAttributeValue(software.amazon.awssdk.services.sns.model.MessageAttributeValue) Mockito.only(org.mockito.Mockito.only) MessageBuilder(org.springframework.messaging.support.MessageBuilder) MESSAGE_DEDUPLICATION_ID_HEADER(io.awspring.cloud.sns.core.SnsHeaders.MESSAGE_DEDUPLICATION_ID_HEADER) Mockito.mock(org.mockito.Mockito.mock) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with MessageAttributeValue

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);
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) MessageAttributeValue(software.amazon.awssdk.services.sns.model.MessageAttributeValue)

Aggregations

Map (java.util.Map)2 MessageAttributeValue (software.amazon.awssdk.services.sns.model.MessageAttributeValue)2 Matchers.requestMatches (io.awspring.cloud.sns.core.Matchers.requestMatches)1 MESSAGE_DEDUPLICATION_ID_HEADER (io.awspring.cloud.sns.core.SnsHeaders.MESSAGE_DEDUPLICATION_ID_HEADER)1 MESSAGE_GROUP_ID_HEADER (io.awspring.cloud.sns.core.SnsHeaders.MESSAGE_GROUP_ID_HEADER)1 NOTIFICATION_SUBJECT_HEADER (io.awspring.cloud.sns.core.SnsHeaders.NOTIFICATION_SUBJECT_HEADER)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 UUID (java.util.UUID)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.only (org.mockito.Mockito.only)1 Mockito.verify (org.mockito.Mockito.verify)1 Message (org.springframework.messaging.Message)1 MessageChannel (org.springframework.messaging.MessageChannel)1