|
| 1 | +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +package events |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/aws/aws-lambda-go/events/test" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestKafkaEventMarshaling(t *testing.T) { |
| 13 | + |
| 14 | + // 1. read JSON from file |
| 15 | + inputJson := test.ReadJSONFromFile(t, "./testdata/kafka-event.json") |
| 16 | + |
| 17 | + // 2. de-serialize into Go object |
| 18 | + var inputEvent KafkaEvent |
| 19 | + if err := json.Unmarshal(inputJson, &inputEvent); err != nil { |
| 20 | + t.Errorf("could not unmarshal event. details: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + for _, records := range inputEvent.Records { |
| 24 | + for _, record := range records { |
| 25 | + utc := record.Timestamp.UTC() |
| 26 | + assert.Equal(t, 2020, utc.Year()) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // 3. serialize to JSON |
| 31 | + outputJson, err := json.Marshal(inputEvent) |
| 32 | + if err != nil { |
| 33 | + t.Errorf("could not marshal event. details: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // 4. check result |
| 37 | + assert.JSONEq(t, string(inputJson), string(outputJson)) |
| 38 | +} |
| 39 | + |
| 40 | +func TestKafkaMarshalingMalformedJson(t *testing.T) { |
| 41 | + test.TestMalformedJson(t, KafkaEvent{}) |
| 42 | +} |
0 commit comments