Skip to content

Commit

Permalink
Restore JSON tests deleted accidentally (elastic#2093)
Browse files Browse the repository at this point in the history
I realized that when rebasing elastic#2091 on top of elastic#2088 a JSON test was
lost. This resurrects it.
  • Loading branch information
tsg authored and ruflin committed Jul 26, 2016
1 parent 1ce32d2 commit ebbbb4c
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions filebeat/harvester/reader/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reader
import (
"testing"

"github.com/elastic/beats/libbeat/common"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -78,3 +79,83 @@ func TestUnmarshal(t *testing.T) {
assert.Equal(t, test.Output, output)
}
}

func TestDecodeJSON(t *testing.T) {
type io struct {
Text string
Config JSONConfig
ExpectedText string
ExpectedMap common.MapStr
}

var tests = []io{
{
Text: `{"message": "test", "value": 1}`,
Config: JSONConfig{MessageKey: "message"},
ExpectedText: "test",
ExpectedMap: common.MapStr{"message": "test", "value": int64(1)},
},
{
Text: `{"message": "test", "value": 1}`,
Config: JSONConfig{MessageKey: "message1"},
ExpectedText: "",
ExpectedMap: common.MapStr{"message": "test", "value": int64(1)},
},
{
Text: `{"message": "test", "value": 1}`,
Config: JSONConfig{MessageKey: "value"},
ExpectedText: "",
ExpectedMap: common.MapStr{"message": "test", "value": int64(1)},
},
{
Text: `{"message": "test", "value": "1"}`,
Config: JSONConfig{MessageKey: "value"},
ExpectedText: "1",
ExpectedMap: common.MapStr{"message": "test", "value": "1"},
},
{
// in case of JSON decoding errors, the text is passed as is
Text: `{"message": "test", "value": "`,
Config: JSONConfig{MessageKey: "value"},
ExpectedText: `{"message": "test", "value": "`,
ExpectedMap: nil,
},
{
// Add key error helps debugging this
Text: `{"message": "test", "value": "`,
Config: JSONConfig{MessageKey: "value", AddErrorKey: true},
ExpectedText: `{"message": "test", "value": "`,
ExpectedMap: common.MapStr{"json_error": "Error decoding JSON: unexpected EOF"},
},
{
// If the text key is not found, put an error
Text: `{"message": "test", "value": "1"}`,
Config: JSONConfig{MessageKey: "hello", AddErrorKey: true},
ExpectedText: ``,
ExpectedMap: common.MapStr{"message": "test", "value": "1", "json_error": "Key 'hello' not found"},
},
{
// If the text key is found, but not a string, put an error
Text: `{"message": "test", "value": 1}`,
Config: JSONConfig{MessageKey: "value", AddErrorKey: true},
ExpectedText: ``,
ExpectedMap: common.MapStr{"message": "test", "value": int64(1), "json_error": "Value of key 'value' is not a string"},
},
{
// Without a text key, simple return the json and an empty text
Text: `{"message": "test", "value": 1}`,
Config: JSONConfig{AddErrorKey: true},
ExpectedText: ``,
ExpectedMap: common.MapStr{"message": "test", "value": int64(1)},
},
}

for _, test := range tests {

var p JSON
p.cfg = &test.Config
text, map_ := p.decodeJSON([]byte(test.Text))
assert.Equal(t, test.ExpectedText, string(text))
assert.Equal(t, test.ExpectedMap, map_)
}
}

0 comments on commit ebbbb4c

Please sign in to comment.