From 76c3f4d29529d1f7383b119be587cc6e7cd9f9eb Mon Sep 17 00:00:00 2001 From: hmizumoto Date: Fri, 25 Feb 2022 16:04:20 +0900 Subject: [PATCH] add unit test for SNS message attributes --- app/gosns/gosns_create_message_test.go | 59 ++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/app/gosns/gosns_create_message_test.go b/app/gosns/gosns_create_message_test.go index 7898e44b..f2080522 100644 --- a/app/gosns/gosns_create_message_test.go +++ b/app/gosns/gosns_create_message_test.go @@ -12,6 +12,7 @@ const ( subjectKey = "Subject" messageStructureJSON = "json" messageStructureEmpty = "" + messageAttributesKey = "MessageAttributes" ) // When simple message string is passed, @@ -202,3 +203,61 @@ func TestCreateMessageBody_NonJsonContainingJson(t *testing.T) { t.Errorf(`expected subject "%s" but received "%s"`, subject, receivedSubject) } } + +// When message attributes are passed, +// the requested Name, DataType, StringValue (or NumberValue) are correctly mapped to Name, Type, Value +func TestCreateMessageBody_WithMessageAttributes(t *testing.T) { + message := "message text" + subject := "subject" + subs := &app.Subscription{ + Protocol: "sqs", + TopicArn: "topic-arn", + SubscriptionArn: "subs-arn", + Raw: false, + } + stringMessageAttributeValue := app.MessageAttributeValue{ValueKey: "StringValue", Value: "test", DataType: "String"} + attributes := map[string]app.MessageAttributeValue{ + stringMessageAttributeValue.DataType: stringMessageAttributeValue, + } + snsMessage, err := CreateMessageBody(subs, message, subject, messageStructureEmpty, attributes) + if err != nil { + t.Fatalf(`error creating SNS message: %s`, err) + } + + var unmarshalled map[string]interface{} + err = json.Unmarshal(snsMessage, &unmarshalled) + + if err != nil { + t.Fatalf(`error unmarshalling SNS message "%s": %s`, snsMessage, err) + } + + receivedMessageAttributes, ok := unmarshalled[messageAttributesKey] + if !ok { + t.Fatalf(`SNS message "%s" does not contain key "%s"`, snsMessage, messageAttributesKey) + } + + attributesMap, ok := receivedMessageAttributes.(map[string]interface{}) + if !ok { + t.Fatalf(`SNS messageAttributes is invalid interface`) + } + + attribute, ok := attributesMap[stringMessageAttributeValue.DataType] + if !ok { + t.Fatalf(`SNS messageAttributes does not contain key "%s"`, stringMessageAttributeValue.DataType) + } + + attributeMap, ok := attribute.(map[string]interface{}) + if !ok { + t.Fatalf(`SNS messageAttribute is invalid interface`) + } + + attributeType, _ := attributeMap["Type"] + if attributeType != stringMessageAttributeValue.DataType { + t.Fatalf(`expected Type "%s" but received %s`, stringMessageAttributeValue.DataType, attributeType) + } + + attributeValue, _ := attributeMap["Value"] + if attributeValue != stringMessageAttributeValue.Value { + t.Fatalf(`expected Value "%s" but received %s`, stringMessageAttributeValue.Value, attributeValue) + } +}