Skip to content

Commit

Permalink
adding integration test for NotificationMessageArgumentResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
msosa committed Oct 16, 2023
1 parent 2fd78a4 commit 401d7d0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.sqs.annotation.SnsNotificationMessage;
import io.awspring.cloud.sqs.annotation.SqsListener;
import io.awspring.cloud.sqs.config.SqsBootstrapConfiguration;
import io.awspring.cloud.sqs.config.SqsMessageListenerContainerFactory;
Expand Down Expand Up @@ -45,6 +46,7 @@
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import software.amazon.awssdk.services.sqs.SqsAsyncClient;
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;

Expand All @@ -65,6 +67,7 @@ class SqsMessageConversionIntegrationTests extends BaseSqsIntegrationTest {
static final String RESOLVES_POJO_MESSAGE_LIST_QUEUE_NAME = "resolves_pojo_message_list_test_queue";
static final String RESOLVES_POJO_FROM_HEADER_QUEUE_NAME = "resolves_pojo_from_mapping_test_queue";
static final String RESOLVES_MY_OTHER_POJO_FROM_HEADER_QUEUE_NAME = "resolves_my_other_pojo_from_mapping_test_queue";
static final String RESOLVES_POJO_FROM_NOTIFICATION_MESSAGE_QUEUE_NAME = "resolves_pojo_from_notification_message_queue";

@Autowired
LatchContainer latchContainer;
Expand All @@ -80,7 +83,9 @@ static void beforeTests() {
createQueue(client, RESOLVES_POJO_LIST_QUEUE_NAME),
createQueue(client, RESOLVES_POJO_MESSAGE_LIST_QUEUE_NAME),
createQueue(client, RESOLVES_POJO_FROM_HEADER_QUEUE_NAME),
createQueue(client, RESOLVES_MY_OTHER_POJO_FROM_HEADER_QUEUE_NAME)).join();
createQueue(client, RESOLVES_MY_OTHER_POJO_FROM_HEADER_QUEUE_NAME),
createQueue(client, RESOLVES_POJO_FROM_NOTIFICATION_MESSAGE_QUEUE_NAME)
).join();
}

@Test
Expand Down Expand Up @@ -135,6 +140,17 @@ void resolvesMyOtherPojoFromHeader() throws Exception {
assertThat(latchContainer.resolvesMyOtherPojoFromMappingLatch.await(10, TimeUnit.SECONDS)).isTrue();
}

@Test
void resolvesMyPojoFromNotificationMessage() throws Exception {
byte[] notificationJsonContent = FileCopyUtils
.copyToByteArray(getClass().getClassLoader().getResourceAsStream("notificationMessage.json"));
String payload = new String(notificationJsonContent);
sqsTemplate.send(RESOLVES_POJO_FROM_NOTIFICATION_MESSAGE_QUEUE_NAME, payload);
logger.debug("Sent message to queue {} with messageBody {}", RESOLVES_POJO_FROM_NOTIFICATION_MESSAGE_QUEUE_NAME,
payload);
assertThat(latchContainer.resolvesPojoNotificationMessageLatch.await(10, TimeUnit.SECONDS)).isTrue();
}

private Map<String, Object> getHeaderMapping(Class<?> clazz) {
return Collections.singletonMap(SqsHeaders.SQS_DEFAULT_TYPE_HEADER, clazz.getName());
}
Expand Down Expand Up @@ -218,6 +234,18 @@ void listen(MyInterface pojo, @Header(SqsHeaders.SQS_QUEUE_NAME_HEADER) String q
latchContainer.resolvesPojoMessageLatch.countDown();
}
}
static class ResolvesPojoWithNotificationAnnotationListener {

@Autowired
LatchContainer latchContainer;

@SqsListener(queueNames = RESOLVES_POJO_FROM_NOTIFICATION_MESSAGE_QUEUE_NAME, id = "resolves-pojo-with-notification-message", factory = "defaultSqsListenerContainerFactory")
void listen(@SnsNotificationMessage MyPojo myPojo) {
Assert.notNull((myPojo).firstField, "Received null message");
logger.debug("Received message {} from queue {}", myPojo, RESOLVES_POJO_FROM_NOTIFICATION_MESSAGE_QUEUE_NAME);
latchContainer.resolvesPojoNotificationMessageLatch.countDown();
}
}

static class LatchContainer {

Expand All @@ -227,6 +255,7 @@ static class LatchContainer {
CountDownLatch resolvesPojoMessageListLatch = new CountDownLatch(1);
CountDownLatch resolvesPojoFromMappingLatch = new CountDownLatch(1);
CountDownLatch resolvesMyOtherPojoFromMappingLatch = new CountDownLatch(1);
CountDownLatch resolvesPojoNotificationMessageLatch = new CountDownLatch(1);

}

Expand Down Expand Up @@ -304,6 +333,10 @@ ResolvesMyPojoWithMappingListener resolvesMyPojoWithMappingListener() {
ResolvesMyOtherPojoWithMappingListener resolvesMyOtherPojoWithMappingListener() {
return new ResolvesMyOtherPojoWithMappingListener();
}
@Bean
ResolvesPojoWithNotificationAnnotationListener resolvesPojoWithNotificationAnnotationListener() {
return new ResolvesPojoWithNotificationAnnotationListener();
}

@Bean
LatchContainer latchContainer() {
Expand Down
11 changes: 11 additions & 0 deletions spring-cloud-aws-sqs/src/test/resources/notificationMessage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Type": "Notification",
"MessageId": "f2c15fec-c617-5b08-b54d-13c4099fec60",
"TopicArn": "arn:aws:sns:eu-west-1:111111111111:mySampleTopic",
"Message": "{\"firstField\":\"pojoNotificationMessage\",\"secondField\":\"secondValue\"}",
"Timestamp": "2014-06-28T14:12:24.418Z",
"SignatureVersion": "1",
"Signature": "XDvKSAnhxECrAmyIrs0Dsfbp/tnKD1IvoOOYTU28FtbUoxr/CgziuW87yZwTuSNNbHJbdD3BEjHS0vKewm0xBeQ0PToDkgtoORXo5RWnmShDQ2nhkthFhZnNulKtmFtRogjBtCwbz8sPnbOCSk21ruyXNdV2RUbdDalndAW002CWEQmYMxFSN6OXUtMueuT610aX+tqeYP4Z6+8WTWLWjAuVyy7rOI6KHYBcVDhKtskvTOPZ4tiVohtQdQbO2Gjuh1vblRzzwMkfaoFTSWImd4pFXxEsv/fq9aGIlqq9xEryJ0w2huFwI5gxyhvGt0RnTd9YvmAEC+WzdJDOqaDNxg==",
"SigningCertURL": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-e372f8ca30337fdb084e8ac449342c77.pem",
"UnsubscribeURL": "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:721324560415:mySampleTopic:9859a6c9-6083-4690-ab02-d1aead3442df"
}

0 comments on commit 401d7d0

Please sign in to comment.