Skip to content

Commit

Permalink
Handling custom type labels in headers from Binary/String message att…
Browse files Browse the repository at this point in the history
…ributes. Fix spring-atticgh-410 [v2].

This is another approach to solve spring-atticgh-410.
First approach is here: spring-attic#633.
Original issue and discussion is here: spring-attic#410
  • Loading branch information
voytech committed Jul 25, 2020
1 parent 223b336 commit 00ebe87
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ else if (MessageHeaders.ID.equals(messageAttribute.getKey())) {
messageHeaders.put(MessageHeaders.ID,
UUID.fromString(messageAttribute.getValue().getStringValue()));
}
else if (MessageAttributeDataTypes.STRING
.equals(messageAttribute.getValue().getDataType())) {
else if (messageAttribute.getValue().getDataType()
.startsWith(MessageAttributeDataTypes.STRING)) {
messageHeaders.put(messageAttribute.getKey(),
messageAttribute.getValue().getStringValue());
}
Expand All @@ -122,8 +122,8 @@ else if (messageAttribute.getValue().getDataType()
messageHeaders.put(messageAttribute.getKey(),
getNumberValue(messageAttribute.getValue()));
}
else if (MessageAttributeDataTypes.BINARY
.equals(messageAttribute.getValue().getDataType())) {
else if (messageAttribute.getValue().getDataType()
.startsWith(MessageAttributeDataTypes.BINARY)) {
messageHeaders.put(messageAttribute.getKey(),
messageAttribute.getValue().getBinaryValue());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,12 @@
package org.springframework.cloud.aws.messaging.core;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import com.amazonaws.services.sqs.model.Message;
Expand All @@ -31,9 +37,14 @@
* Tests for {@link QueueMessageUtils}.
*
* @author Maciej Walkowiak
* @author Wojciech Mąka
*/
class QueueMessageUtilsTest {

public static Charset charset = StandardCharsets.UTF_8;

public static CharsetEncoder encoder = charset.newEncoder();

@ParameterizedTest
@MethodSource("validArguments")
void createsMessageWithNumberHeader(String value, String type, Number expected) {
Expand Down Expand Up @@ -69,4 +80,52 @@ private static Stream<Arguments> validArguments() {
Arguments.of("3.4", "Number.java.lang.Double", 3.4d));
}

@ParameterizedTest
@MethodSource("binaryMessageAttributes")
void createsMessageWithBinaryMessageAttributes(String extendedType, String value,
ByteBuffer expected) throws CharacterCodingException {
final MessageAttributeValue messageAttributeValue = new MessageAttributeValue()
.withBinaryValue(encoder.encode(CharBuffer.wrap(value)))
.withDataType(extendedType);

Message message = new Message().withBody("some body")
.addMessageAttributesEntry("binary-attribute", messageAttributeValue);

org.springframework.messaging.Message<String> result = QueueMessageUtils
.createMessage(message);

assertThat(result.getHeaders().get("binary-attribute")).isEqualTo(expected);
}

private static Stream<Arguments> binaryMessageAttributes()
throws CharacterCodingException {
return Stream.of(
Arguments.of("Binary.png", "cmFuZG9t",
encoder.encode(CharBuffer.wrap("cmFuZG9t"))),
Arguments.of("Binary.png.800x600", "cmFuZG9t",
encoder.encode(CharBuffer.wrap("cmFuZG9t"))));
}

@ParameterizedTest
@MethodSource("stringMessageAttributes")
void createsMessageWithStringMessageAttributes(String extendedType, String value,
String expected) {
final MessageAttributeValue messageAttributeValue = new MessageAttributeValue()
.withStringValue(value).withDataType(extendedType);

Message message = new Message().withBody("some body").addMessageAttributesEntry(
"string-type-attribute", messageAttributeValue);

org.springframework.messaging.Message<String> result = QueueMessageUtils
.createMessage(message);

assertThat(result.getHeaders().get("string-type-attribute")).isEqualTo(expected);
}

private static Stream<Arguments> stringMessageAttributes() {
return Stream.of(Arguments.of("String.description", "A message", "A message"),
Arguments.of("String.description.moreInfo", "In the bottle",
"In the bottle"));
}

}

0 comments on commit 00ebe87

Please sign in to comment.