From c469e568f12d26c7765d6a3d2e852ef08f27c635 Mon Sep 17 00:00:00 2001 From: Tiago Queiroz Date: Thu, 25 Nov 2021 11:47:30 +0100 Subject: [PATCH] Add test --- .../actions/decode_json_fields_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/libbeat/processors/actions/decode_json_fields_test.go b/libbeat/processors/actions/decode_json_fields_test.go index 73d4f3a5fccf..f58efd29b465 100644 --- a/libbeat/processors/actions/decode_json_fields_test.go +++ b/libbeat/processors/actions/decode_json_fields_test.go @@ -491,6 +491,27 @@ func TestOverwriteMetadata(t *testing.T) { assert.Equal(t, expected, actual) } +func TestAddErrorToEventOnUnmarshalError(t *testing.T) { + testConfig := common.MustNewConfigFrom(map[string]interface{}{ + "fields": "message", + "add_error_key": true, + }) + + input := common.MapStr{ + "message": "Broken JSON [[", + } + + actual := getActualValue(t, testConfig, input) + + errObj, ok := actual["error"].(common.MapStr) + require.True(t, ok, "'error' field not present or of invalid type") + require.NotNil(t, actual["error"]) + + assert.Equal(t, "message", errObj["field"]) + assert.NotNil(t, errObj["data"]) + assert.NotNil(t, errObj["message"]) +} + func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr { log := logp.NewLogger("decode_json_fields_test")