use of org.springframework.integration.aws.support.SnsBodyBuilder in project spring-integration-aws by spring-projects.
the class SnsMessageHandlerTests method testSnsMessageHandler.
@Test
@SuppressWarnings("unchecked")
public void testSnsMessageHandler() {
SnsBodyBuilder payload = SnsBodyBuilder.withDefault("foo").forProtocols("{\"foo\" : \"bar\"}", "sms");
Message<?> message = MessageBuilder.withPayload(payload).setHeader("topic", "topic").setHeader("subject", "subject").setHeader("foo", "bar").build();
this.sendToSnsChannel.send(message);
Message<?> reply = this.resultChannel.receive(10000);
assertThat(reply).isNotNull();
ArgumentCaptor<PublishRequest> captor = ArgumentCaptor.forClass(PublishRequest.class);
verify(this.amazonSNS).publishAsync(captor.capture(), any(AsyncHandler.class));
PublishRequest publishRequest = captor.getValue();
assertThat(publishRequest.getMessageStructure()).isEqualTo("json");
assertThat(publishRequest.getTopicArn()).isEqualTo("topic");
assertThat(publishRequest.getSubject()).isEqualTo("subject");
assertThat(publishRequest.getMessage()).isEqualTo("{\"default\":\"foo\",\"sms\":\"{\\\"foo\\\" : \\\"bar\\\"}\"}");
Map<String, MessageAttributeValue> messageAttributes = publishRequest.getMessageAttributes();
assertThat(messageAttributes).doesNotContainKey(MessageHeaders.ID);
assertThat(messageAttributes).doesNotContainKey(MessageHeaders.TIMESTAMP);
assertThat(messageAttributes).containsKey("foo");
assertThat(messageAttributes.get("foo").getStringValue()).isEqualTo("bar");
assertThat(reply.getHeaders().get(AwsHeaders.MESSAGE_ID)).isEqualTo("111");
assertThat(reply.getHeaders().get(AwsHeaders.TOPIC)).isEqualTo("topic");
assertThat(reply.getPayload()).isSameAs(payload);
}
use of org.springframework.integration.aws.support.SnsBodyBuilder in project spring-integration-aws by spring-projects.
the class SnsMessageHandler method handleMessageToAws.
@Override
protected Future<?> handleMessageToAws(Message<?> message) {
Object payload = message.getPayload();
PublishRequest publishRequest = null;
if (payload instanceof PublishRequest) {
publishRequest = (PublishRequest) payload;
} else {
Assert.state(this.topicArnExpression != null, "'topicArn' or 'topicArnExpression' must be specified.");
publishRequest = new PublishRequest();
String topicArn = this.topicArnExpression.getValue(getEvaluationContext(), message, String.class);
if (this.resourceIdResolver != null) {
topicArn = this.resourceIdResolver.resolveToPhysicalResourceId(topicArn);
}
publishRequest.setTopicArn(topicArn);
if (this.subjectExpression != null) {
String subject = this.subjectExpression.getValue(getEvaluationContext(), message, String.class);
publishRequest.setSubject(subject);
}
Object snsMessage = message.getPayload();
if (this.bodyExpression != null) {
snsMessage = this.bodyExpression.getValue(getEvaluationContext(), message);
}
if (snsMessage instanceof SnsBodyBuilder) {
publishRequest.withMessageStructure("json").setMessage(((SnsBodyBuilder) snsMessage).build());
} else {
publishRequest.setMessage(getConversionService().convert(snsMessage, String.class));
}
HeaderMapper<Map<String, MessageAttributeValue>> headerMapper = getHeaderMapper();
if (headerMapper != null) {
mapHeaders(message, publishRequest, headerMapper);
}
}
AsyncHandler<PublishRequest, PublishResult> asyncHandler = obtainAsyncHandler(message, publishRequest);
return this.amazonSns.publishAsync(publishRequest, asyncHandler);
}
Aggregations