From 1c4e218d4702020d48bd98a818c2d8ac0679e782 Mon Sep 17 00:00:00 2001 From: Zhiheng Date: Sun, 28 Jan 2018 06:05:53 +0800 Subject: [PATCH] add and refactor tests for DeDotJSON --- libbeat/common/event_test.go | 104 ++++++++++-------- libbeat/common/testdata/json_array_dedot.json | 5 - .../common/testdata/json_array_with_dots.json | 5 - libbeat/common/testdata/json_map_dedot.json | 10 -- .../common/testdata/json_map_with_dots.json | 10 -- 5 files changed, 56 insertions(+), 78 deletions(-) delete mode 100644 libbeat/common/testdata/json_array_dedot.json delete mode 100644 libbeat/common/testdata/json_array_with_dots.json delete mode 100644 libbeat/common/testdata/json_map_dedot.json delete mode 100644 libbeat/common/testdata/json_map_with_dots.json diff --git a/libbeat/common/event_test.go b/libbeat/common/event_test.go index 24b0e040c16a..6f80dbef02cc 100644 --- a/libbeat/common/event_test.go +++ b/libbeat/common/event_test.go @@ -7,9 +7,6 @@ import ( "github.com/stretchr/testify/assert" - "io/ioutil" - "path/filepath" - "github.com/elastic/beats/libbeat/logp" ) @@ -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{})))) + } + } } diff --git a/libbeat/common/testdata/json_array_dedot.json b/libbeat/common/testdata/json_array_dedot.json deleted file mode 100644 index 08b6e863e5ff..000000000000 --- a/libbeat/common/testdata/json_array_dedot.json +++ /dev/null @@ -1,5 +0,0 @@ -[ - {"key_with_dot_1":"value1_1"}, - {"key_without_dot_2":"value1_2"}, - {"key_with_multiple_dots_3": {"key_with_dot_2":"value2_1"}} -] diff --git a/libbeat/common/testdata/json_array_with_dots.json b/libbeat/common/testdata/json_array_with_dots.json deleted file mode 100644 index d0e4db99a717..000000000000 --- a/libbeat/common/testdata/json_array_with_dots.json +++ /dev/null @@ -1,5 +0,0 @@ -[ - {"key_with_dot.1":"value1_1"}, - {"key_without_dot_2":"value1_2"}, - {"key_with_multiple.dots.3": {"key_with_dot.2":"value2_1"}} -] diff --git a/libbeat/common/testdata/json_map_dedot.json b/libbeat/common/testdata/json_map_dedot.json deleted file mode 100644 index 37d301433412..000000000000 --- a/libbeat/common/testdata/json_map_dedot.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "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 - } - } -} diff --git a/libbeat/common/testdata/json_map_with_dots.json b/libbeat/common/testdata/json_map_with_dots.json deleted file mode 100644 index e7fbde118220..000000000000 --- a/libbeat/common/testdata/json_map_with_dots.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "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 - } - } -}