From 73085a1b884773ca7ff278598eea696ec35cd218 Mon Sep 17 00:00:00 2001 From: Krishnaswamy Date: Wed, 16 Dec 2020 21:46:09 -0800 Subject: [PATCH 1/3] Add Amazon MQ event structure --- events/activemq.go | 30 +++++++++++++++++++ events/activemq_test.go | 46 +++++++++++++++++++++++++++++ events/testdata/activemq-event.json | 24 +++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 events/activemq.go create mode 100644 events/activemq_test.go create mode 100644 events/testdata/activemq-event.json diff --git a/events/activemq.go b/events/activemq.go new file mode 100644 index 00000000..ac1969ae --- /dev/null +++ b/events/activemq.go @@ -0,0 +1,30 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +package events + +type ActiveMQEvent struct { + EventSource string `json:"eventSource"` + EventSourceArn string `json:"eventSourceArn"` + Messages []ActiveMQMessage `json:"messages"` +} + +type ActiveMQMessage struct { + MessageID string `json:"messageID"` + MessageType string `json:"messageType"` + Timestamp int64 `json:"timestamp"` + DeliveryMode int `json:"deliveryMode"` + CorrelationID string `json:"correlationID"` + ReplyTo string `json:"replyTo"` + Destination Destination `json:"destination"` + Redelivered bool `json:"redelivered"` + Type string `json:"type"` + Expiration int64 `json:"expiration"` + Priority int `json:"priority"` + Data string `json:"data"` + BrokerInTime int64 `json:"brokerInTime"` + BrokerOutTime int64 `json:"brokerOutTime"` +} + +type Destination struct { + PhysicalName string `json:"physicalName"` +} \ No newline at end of file diff --git a/events/activemq_test.go b/events/activemq_test.go new file mode 100644 index 00000000..2a1a196e --- /dev/null +++ b/events/activemq_test.go @@ -0,0 +1,46 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +package events + +import ( + "encoding/json" + "testing" + + "github.com/aws/aws-lambda-go/events/test" + "github.com/stretchr/testify/assert" +) + +func TestActiveMQEventMarshaling(t *testing.T) { + // 1. read JSON from file + inputJson := test.ReadJSONFromFile(t, "./testdata/activemq-event.json") + + // 2. de-serialize into Go object + var inputEvent ActiveMQEvent + if err := json.Unmarshal(inputJson, &inputEvent); err != nil { + t.Errorf("could not unmarshal event. details: %v", err) + } + + // 3. Verify values populated into Go Object, at least one validation per data type + assert.Equal(t, "aws:mq", inputEvent.EventSource) + assert.Equal(t, "arn:aws:mq:us-west-2:533019413397:broker:shask-test:b-0f5b7522-2b41-4f85-a615-735a4e6d96b5", inputEvent.EventSourceArn) + assert.Equal(t, 1, len(inputEvent.Messages)) + + var message = inputEvent.Messages[0] + assert.Equal(t, "jms/text-message", message.MessageType) + assert.Equal(t, int64(1599863938941), message.Timestamp) + assert.Equal(t, 1, message.DeliveryMode) + assert.Equal(t, "testQueue", message.Destination.PhysicalName) + assert.Equal(t, false, message.Redelivered) + + // 4. serialize to JSON + outputJson, err := json.Marshal(inputEvent) + if err != nil { + t.Errorf("could not marshal event. details: %v", err) + } + + // 5. check result + assert.JSONEq(t, string(inputJson), string(outputJson)) +} + +func TestActiveMQMarshalingMalformedJson(t *testing.T) { + test.TestMalformedJson(t, ActiveMQEvent{}) +} diff --git a/events/testdata/activemq-event.json b/events/testdata/activemq-event.json new file mode 100644 index 00000000..6ae6d625 --- /dev/null +++ b/events/testdata/activemq-event.json @@ -0,0 +1,24 @@ +{ + "eventSource": "aws:mq", + "eventSourceArn": "arn:aws:mq:us-west-2:533019413397:broker:shask-test:b-0f5b7522-2b41-4f85-a615-735a4e6d96b5", + "messages": [ + { + "messageID": "ID:b-0f5b7522-2b41-4f85-a615-735a4e6d96b5-2.mq.us-west-2.amazonaws.com-34859-1598944546501-4:12:1:1:3", + "messageType": "jms/text-message", + "timestamp": 1599863938941, + "deliveryMode": 1, + "correlationID": "", + "replyTo": "null", + "destination": { + "physicalName": "testQueue" + }, + "redelivered": false, + "type": "", + "expiration": 0, + "priority": 0, + "data": "RW50ZXIgc29tZSB0ZXh0IGhlcmUgZm9yIHRoZSBtZXNzYWdlIGJvZHkuLi4=", + "brokerInTime": 1599863938943, + "brokerOutTime": 1599863938944 + } + ] +} \ No newline at end of file From cabf7e69927a853ccb5be8269c5ef51cdc35fb5b Mon Sep 17 00:00:00 2001 From: Krishnaswamy Date: Thu, 17 Dec 2020 14:24:16 -0800 Subject: [PATCH 2/3] Formatted --- events/activemq.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/events/activemq.go b/events/activemq.go index ac1969ae..8efe2f0c 100644 --- a/events/activemq.go +++ b/events/activemq.go @@ -3,28 +3,28 @@ package events type ActiveMQEvent struct { - EventSource string `json:"eventSource"` - EventSourceArn string `json:"eventSourceArn"` - Messages []ActiveMQMessage `json:"messages"` + EventSource string `json:"eventSource"` + EventSourceArn string `json:"eventSourceArn"` + Messages []ActiveMQMessage `json:"messages"` } type ActiveMQMessage struct { - MessageID string `json:"messageID"` - MessageType string `json:"messageType"` - Timestamp int64 `json:"timestamp"` - DeliveryMode int `json:"deliveryMode"` - CorrelationID string `json:"correlationID"` - ReplyTo string `json:"replyTo"` - Destination Destination `json:"destination"` - Redelivered bool `json:"redelivered"` - Type string `json:"type"` - Expiration int64 `json:"expiration"` - Priority int `json:"priority"` - Data string `json:"data"` - BrokerInTime int64 `json:"brokerInTime"` - BrokerOutTime int64 `json:"brokerOutTime"` + MessageID string `json:"messageID"` + MessageType string `json:"messageType"` + Timestamp int64 `json:"timestamp"` + DeliveryMode int `json:"deliveryMode"` + CorrelationID string `json:"correlationID"` + ReplyTo string `json:"replyTo"` + Destination Destination `json:"destination"` + Redelivered bool `json:"redelivered"` + Type string `json:"type"` + Expiration int64 `json:"expiration"` + Priority int `json:"priority"` + Data string `json:"data"` + BrokerInTime int64 `json:"brokerInTime"` + BrokerOutTime int64 `json:"brokerOutTime"` } type Destination struct { - PhysicalName string `json:"physicalName"` -} \ No newline at end of file + PhysicalName string `json:"physicalName"` +} From 946815a6be651273d68a4378e494c933a48ea855 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Tue, 22 Dec 2020 00:14:16 -0800 Subject: [PATCH 3/3] apply not-enforced naming conventions * EventSourceArn -> EventSourceARN * Destination -> ActiveMQDestination --- events/activemq.go | 32 ++++++++++++++++---------------- events/activemq_test.go | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/events/activemq.go b/events/activemq.go index 8efe2f0c..3e9ffb87 100644 --- a/events/activemq.go +++ b/events/activemq.go @@ -4,27 +4,27 @@ package events type ActiveMQEvent struct { EventSource string `json:"eventSource"` - EventSourceArn string `json:"eventSourceArn"` + EventSourceARN string `json:"eventSourceArn"` Messages []ActiveMQMessage `json:"messages"` } type ActiveMQMessage struct { - MessageID string `json:"messageID"` - MessageType string `json:"messageType"` - Timestamp int64 `json:"timestamp"` - DeliveryMode int `json:"deliveryMode"` - CorrelationID string `json:"correlationID"` - ReplyTo string `json:"replyTo"` - Destination Destination `json:"destination"` - Redelivered bool `json:"redelivered"` - Type string `json:"type"` - Expiration int64 `json:"expiration"` - Priority int `json:"priority"` - Data string `json:"data"` - BrokerInTime int64 `json:"brokerInTime"` - BrokerOutTime int64 `json:"brokerOutTime"` + MessageID string `json:"messageID"` + MessageType string `json:"messageType"` + Timestamp int64 `json:"timestamp"` + DeliveryMode int `json:"deliveryMode"` + CorrelationID string `json:"correlationID"` + ReplyTo string `json:"replyTo"` + Destination ActiveMQDestination `json:"destination"` + Redelivered bool `json:"redelivered"` + Type string `json:"type"` + Expiration int64 `json:"expiration"` + Priority int `json:"priority"` + Data string `json:"data"` + BrokerInTime int64 `json:"brokerInTime"` + BrokerOutTime int64 `json:"brokerOutTime"` } -type Destination struct { +type ActiveMQDestination struct { PhysicalName string `json:"physicalName"` } diff --git a/events/activemq_test.go b/events/activemq_test.go index 2a1a196e..4eb57f5f 100644 --- a/events/activemq_test.go +++ b/events/activemq_test.go @@ -21,7 +21,7 @@ func TestActiveMQEventMarshaling(t *testing.T) { // 3. Verify values populated into Go Object, at least one validation per data type assert.Equal(t, "aws:mq", inputEvent.EventSource) - assert.Equal(t, "arn:aws:mq:us-west-2:533019413397:broker:shask-test:b-0f5b7522-2b41-4f85-a615-735a4e6d96b5", inputEvent.EventSourceArn) + assert.Equal(t, "arn:aws:mq:us-west-2:533019413397:broker:shask-test:b-0f5b7522-2b41-4f85-a615-735a4e6d96b5", inputEvent.EventSourceARN) assert.Equal(t, 1, len(inputEvent.Messages)) var message = inputEvent.Messages[0]