Skip to content

Commit

Permalink
add and refactor tests for DeDotJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
walktall committed Jan 29, 2018
1 parent 9abaf64 commit 1c4e218
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 78 deletions.
104 changes: 56 additions & 48 deletions libbeat/common/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (

"github.com/stretchr/testify/assert"

"io/ioutil"
"path/filepath"

"github.com/elastic/beats/libbeat/logp"
)

Expand Down Expand Up @@ -399,49 +396,60 @@ func BenchmarkConvertToGenericEventStringPointer(b *testing.B) {
ConvertToGenericEvent(MapStr{"key": &val})
}
}

func TestDeDotJsonMap(t *testing.T) {
var actualJSONBody map[string]interface{}
var expectedJSONBody map[string]interface{}

absPath, err := filepath.Abs("./testdata")
assert.NotNil(t, absPath)
assert.Nil(t, err)

actualJSONResponse, err := ioutil.ReadFile(absPath + "/json_map_with_dots.json")
assert.Nil(t, err)
err = json.Unmarshal(actualJSONResponse, &actualJSONBody)
assert.Nil(t, err)

dedottedJSONResponse, err := ioutil.ReadFile(absPath + "/json_map_dedot.json")
assert.Nil(t, err)
err = json.Unmarshal(dedottedJSONResponse, &expectedJSONBody)
assert.Nil(t, err)

actualJSONBody = DeDotJSON(actualJSONBody).(map[string]interface{})

assert.Equal(t, expectedJSONBody, actualJSONBody)
}

func TestDeDotJsonArray(t *testing.T) {
var actualJSONBody []interface{}
var expectedJSONBody []interface{}

absPath, err := filepath.Abs("./testdata")
assert.NotNil(t, absPath)
assert.Nil(t, err)

actualJSONResponse, err := ioutil.ReadFile(absPath + "/json_array_with_dots.json")
assert.Nil(t, err)
err = json.Unmarshal(actualJSONResponse, &actualJSONBody)
assert.Nil(t, err)

dedottedJSONResponse, err := ioutil.ReadFile(absPath + "/json_array_dedot.json")
assert.Nil(t, err)
err = json.Unmarshal(dedottedJSONResponse, &expectedJSONBody)
assert.Nil(t, err)

actualJSONBody = DeDotJSON(actualJSONBody).([]interface{})

assert.Equal(t, expectedJSONBody, actualJSONBody)
func TestDeDotJSON(t *testing.T) {
var tests = []struct {
input []byte
output []byte
valuer func() interface{}
}{
{
input: []byte(`[
{"key_with_dot.1":"value1_1"},
{"key_without_dot_2":"value1_2"},
{"key_with_multiple.dots.3": {"key_with_dot.2":"value2_1"}}
]
`),
output: []byte(`[
{"key_with_dot_1":"value1_1"},
{"key_without_dot_2":"value1_2"},
{"key_with_multiple_dots_3": {"key_with_dot_2":"value2_1"}}
]
`),
valuer: func() interface{} { return []interface{}{} },
},
{
input: []byte(`{
"key_without_dot_l1": {
"key_with_dot.l2": 1,
"key.with.multiple.dots_l2": 2,
"key_without_dot_l2": {
"key_with_dot.l3": 3,
"key.with.multiple.dots_l3": 4
}
}
}
`),
output: []byte(`{
"key_without_dot_l1": {
"key_with_dot_l2": 1,
"key_with_multiple_dots_l2": 2,
"key_without_dot_l2": {
"key_with_dot_l3": 3,
"key_with_multiple_dots_l3": 4
}
}
}
`),
valuer: func() interface{} { return map[string]interface{}{} },
},
}
for _, test := range tests {
input, output := test.valuer(), test.valuer()
assert.Nil(t, json.Unmarshal(test.input, &input))
assert.Nil(t, json.Unmarshal(test.output, &output))
assert.Equal(t, output, DeDotJSON(input))
if _, ok := test.valuer().(map[string]interface{}); ok {
assert.Equal(t, MapStr(output.(map[string]interface{})), DeDotJSON(MapStr(input.(map[string]interface{}))))
}
}
}
5 changes: 0 additions & 5 deletions libbeat/common/testdata/json_array_dedot.json

This file was deleted.

5 changes: 0 additions & 5 deletions libbeat/common/testdata/json_array_with_dots.json

This file was deleted.

10 changes: 0 additions & 10 deletions libbeat/common/testdata/json_map_dedot.json

This file was deleted.

10 changes: 0 additions & 10 deletions libbeat/common/testdata/json_map_with_dots.json

This file was deleted.

0 comments on commit 1c4e218

Please sign in to comment.