From 2e12382af2f7fee0c5310ab4eb9b77b78212ed09 Mon Sep 17 00:00:00 2001 From: Casey Hilland Date: Mon, 31 Jul 2017 18:55:36 -0400 Subject: [PATCH] Fix JSON comparisons (#48) --- kazaam_int_test.go | 70 +++++++++++++++++++---------- transform/coalesce_test.go | 9 ++-- transform/concat_test.go | 28 +++++++----- transform/default_test.go | 4 +- transform/extract_test.go | 5 ++- transform/shift_test.go | 21 ++++++--- transform/timestamp_test.go | 12 +++-- transform/util.go | 18 ++++++-- transform/util_test.go | 90 +++++++++++++++++++++++++++---------- transform/uuid_test.go | 30 ++++++++----- 10 files changed, 199 insertions(+), 88 deletions(-) diff --git a/kazaam_int_test.go b/kazaam_int_test.go index 1176307..afa2249 100644 --- a/kazaam_int_test.go +++ b/kazaam_int_test.go @@ -1,6 +1,8 @@ package kazaam_test import ( + "encoding/json" + "reflect" "testing" "github.com/buger/jsonparser" @@ -10,6 +12,22 @@ import ( const testJSONInput = `{"rating":{"example":{"value":3},"primary":{"value":3}}}` +func checkJSONStringsEqual(item1, item2 string) (bool, error) { + var out1, out2 interface{} + + err := json.Unmarshal([]byte(item1), &out1) + if err != nil { + return false, nil + } + + err = json.Unmarshal([]byte(item2), &out2) + if err != nil { + return false, nil + } + + return reflect.DeepEqual(out1, out2), nil +} + func TestKazaamBadInput(t *testing.T) { jsonOut := `` spec := `[{"operation": "shift","spec": {"Rating": "rating.primary.value","example.old": "rating.example"}}]` @@ -66,18 +84,20 @@ func TestKazaamMultipleTransforms(t *testing.T) { transform1, _ := kazaam.NewKazaam(spec1) kazaamOut1, _ := transform1.TransformJSONStringToString(testJSONInput) + areEqual1, _ := checkJSONStringsEqual(kazaamOut1, jsonOut1) transform2, _ := kazaam.NewKazaam(spec2) kazaamOut2, _ := transform2.TransformJSONStringToString(testJSONInput) + areEqual2, _ := checkJSONStringsEqual(kazaamOut2, jsonOut2) - if kazaamOut1 != jsonOut1 { + if !areEqual1 { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut1) t.Log("Actual: ", kazaamOut1) t.FailNow() } - if kazaamOut2 != jsonOut2 { + if !areEqual2 { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut2) t.Log("Actual: ", kazaamOut2) @@ -95,13 +115,14 @@ func TestKazaamMultipleTransformsRequire(t *testing.T) { transform2, _ := kazaam.NewKazaam(spec2) kazaamOut2, _ := transform2.TransformJSONStringToString(testJSONInput) + areEqual2, _ := checkJSONStringsEqual(kazaamOut2, jsonOut2) if out1Err == nil { t.Error("Transform path does not exist in message and should throw an error.") t.FailNow() } - if kazaamOut2 != jsonOut2 { + if !areEqual2 { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut2) t.Log("Actual: ", kazaamOut2) @@ -115,8 +136,9 @@ func TestKazaamNoTransform(t *testing.T) { kazaamTransform, _ := kazaam.NewKazaam(spec) kazaamOut, _ := kazaamTransform.TransformJSONStringToString(testJSONInput) + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -132,16 +154,13 @@ func TestKazaamCoalesceTransformAndShift(t *testing.T) { "operation": "shift", "spec": {"rating.foo": "foo", "rating.example.value": "rating.primary.value"} }]` - - // for some reason, keys are inserted in different order on different runs locally and in CI - // so without the alt we get sporadic failures. jsonOut := `{"rating":{"foo":{"value":3},"example":{"value":3}}}` - altJsonOut := `{"rating":{"example":{"value":3},"foo":{"value":3}}}` kazaamTransform, _ := kazaam.NewKazaam(spec) kazaamOut, _ := kazaamTransform.TransformJSONStringToString(testJSONInput) + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) - if kazaamOut != jsonOut && kazaamOut != altJsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -157,17 +176,14 @@ func TestKazaamShiftTransformWithTimestamp(t *testing.T) { "operation": "timestamp", "spec": {"newTimestamp":{"inputFormat":"Mon Jan _2 15:04:05 -0700 2006","outputFormat":"2006-01-02T15:04:05-0700"}} }]` - - // for some reason, keys are inserted in different order on different runs locally and in CI - // so without the alt we get sporadic failures. jsonIn := `{"oldTimestamp":"Fri Jul 21 08:15:27 +0000 2017"}` jsonOut := `{"oldTimestamp":"Fri Jul 21 08:15:27 +0000 2017","newTimestamp":"2017-07-21T08:15:27+0000"}` - altJsonOut := `{"newTimestamp":"2017-07-21T08:15:27+0000","oldTimestamp":"Fri Jul 21 08:15:27 +0000 2017"}` kazaamTransform, _ := kazaam.NewKazaam(spec) kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn) + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) - if kazaamOut != jsonOut && kazaamOut != altJsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -191,7 +207,8 @@ func TestShiftWithOverAndWildcard(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -213,8 +230,9 @@ func TestKazaamTransformMultiOpWithOver(t *testing.T) { kazaamTransform, _ := kazaam.NewKazaam(spec) kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn) + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -229,8 +247,9 @@ func TestShiftWithOver(t *testing.T) { kazaamTransform, _ := kazaam.NewKazaam(spec) kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn) + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -283,20 +302,21 @@ func TestMissingRequiredField(t *testing.T) { func TestKazaamNoModify(t *testing.T) { spec := `[{"operation": "shift","spec": {"Rating": "rating.primary.value","example.old": "rating.example"}}]` msgOut := `{"Rating":3,"example":{"old":{"value":3}}}` - altMsgOut := `{"example":{"old":{"value":3}},"Rating":3}` tf, _ := kazaam.NewKazaam(spec) data := []byte(testJSONInput) jsonOut, _ := tf.Transform(data) - jsonOutStr := string(jsonOut) + areEqual1, _ := checkJSONStringsEqual(string(jsonOut), msgOut) - if !(jsonOutStr == msgOut || jsonOutStr == altMsgOut) || jsonOutStr == testJSONInput { + if !areEqual1 { t.Error("Unexpected transformation result") - t.Error("Actual:", jsonOutStr) + t.Error("Actual:", string(jsonOut)) t.Error("Expected:", msgOut) } - if string(data) != testJSONInput { + areEqual2, _ := checkJSONStringsEqual(string(data), testJSONInput) + + if !areEqual2 { t.Error("Unexpected modification") t.Error("Actual:", string(data)) t.Error("Expected:", testJSONInput) @@ -313,7 +333,8 @@ func TestConfigdKazaamGet3rdPartyTransform(t *testing.T) { k, _ := kazaam.New(`[{"operation": "3rd-party"}]`, kc) kazaamOut, _ := k.TransformJSONStringToString(`{"test":"data"}`) - if kazaamOut != msgOut { + areEqual, _ := checkJSONStringsEqual(kazaamOut, msgOut) + if !areEqual { t.Error("Unexpected transform output") t.Log("Actual: ", kazaamOut) t.Log("Expected: ", msgOut) @@ -339,8 +360,9 @@ func TestKazaamTransformThreeOpWithOver(t *testing.T) { kazaamTransform, _ := kazaam.NewKazaam(spec) kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn) + areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/coalesce_test.go b/transform/coalesce_test.go index bb2179b..49698a6 100644 --- a/transform/coalesce_test.go +++ b/transform/coalesce_test.go @@ -8,8 +8,9 @@ func TestCoalesce(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Coalesce, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -35,8 +36,9 @@ func TestCoalesceWithMulti(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Coalesce, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -50,8 +52,9 @@ func TestCoalesceWithNotFound(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Coalesce, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/concat_test.go b/transform/concat_test.go index 2ca1df8..2910338 100644 --- a/transform/concat_test.go +++ b/transform/concat_test.go @@ -9,8 +9,9 @@ func TestConcat(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -64,10 +65,11 @@ func TestConcatWithReplaceSimplePath(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") - t.Log("Expected: ", jsonOut) + t.Log("Expected: ", []byte(jsonOut)) t.Log("Actual: ", kazaamOut) t.FailNow() } @@ -80,8 +82,9 @@ func TestConcatWithNoDelimiter(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -96,8 +99,9 @@ func TestConcatWithWildcard(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -112,8 +116,9 @@ func TestConcatWithWildcardNested(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -128,8 +133,9 @@ func TestConcatWithBadPath(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -147,7 +153,7 @@ func TestConcatWithBadSpec(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) - if kazaamOut != jsonOut { + if string(kazaamOut) != jsonOut { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -162,8 +168,9 @@ func TestConcatWithMultiMulti(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -178,8 +185,9 @@ func TestConcatWithLargeNumbers(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/default_test.go b/transform/default_test.go index 2e07d06..3114b59 100644 --- a/transform/default_test.go +++ b/transform/default_test.go @@ -14,7 +14,9 @@ func TestDefault(t *testing.T) { t.Log("Error: ", err.Error()) t.FailNow() } - if kazaamOut != jsonOut { + + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/extract_test.go b/transform/extract_test.go index 2ba623b..c52183b 100644 --- a/transform/extract_test.go +++ b/transform/extract_test.go @@ -9,8 +9,9 @@ func TestExtract(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Extract, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -39,7 +40,7 @@ func TestExtractWithBadPath(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Extract, cfg, jsonIn) - if kazaamOut != jsonOut { + if string(kazaamOut) != jsonOut { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/shift_test.go b/transform/shift_test.go index 6e2e5e6..27d99b2 100644 --- a/transform/shift_test.go +++ b/transform/shift_test.go @@ -8,8 +8,9 @@ func TestShift(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Shift, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -24,8 +25,9 @@ func TestShiftWithWildcard(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Shift, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -40,8 +42,9 @@ func TestShiftWithWildcardEmptySlice(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Shift, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -55,8 +58,9 @@ func TestShiftWithMissingKey(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Shift, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -71,8 +75,9 @@ func TestShiftDeepExistsRequire(t *testing.T) { cfg := getConfig(spec, true) kazaamOut, _ := getTransformTestWrapper(Shift, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -122,8 +127,9 @@ func TestShiftWithEncapsulate(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Shift, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -178,7 +184,8 @@ func TestShiftWithEndArrayAccess(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/timestamp_test.go b/transform/timestamp_test.go index f20471b..782ddb1 100644 --- a/transform/timestamp_test.go +++ b/transform/timestamp_test.go @@ -13,8 +13,9 @@ func TestTimestamp(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Timestamp, cfg, timestampJSON) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -28,8 +29,9 @@ func TestTimestampWithIndex(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Timestamp, cfg, timestampJSON) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -43,8 +45,9 @@ func TestTimestampWithWildcard(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Timestamp, cfg, timestampJSON) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -59,8 +62,9 @@ func TestTimestampWithMissingKey(t *testing.T) { cfg := getConfig(spec, false) kazaamOut, _ := getTransformTestWrapper(Timestamp, cfg, jsonIn) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) - if kazaamOut != jsonOut { + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) diff --git a/transform/util.go b/transform/util.go index ffb3ee6..9915aae 100644 --- a/transform/util.go +++ b/transform/util.go @@ -65,7 +65,7 @@ func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) { // use jsonparser.ArrayEach to copy the array into results _, err := jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) { - results = append(results, value) + results = append(results, handleUnquotedStrings(value, dataType)) }, beforePath...) if err == jsonparser.KeyPathNotFoundError { if pathRequired { @@ -113,9 +113,7 @@ func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) { if dataType == jsonparser.String { // bookend() is destructive to underlying slice, need to copy. // extra capacity saves an allocation and copy during bookend. - tmp := make([]byte, len(result), len(result)+2) - copy(tmp, result) - result = bookend(tmp, '"', '"') + result = handleUnquotedStrings(result, dataType) } if len(result) == 0 { result = []byte("null") @@ -231,3 +229,15 @@ func bookend(value []byte, bef, aft byte) []byte { value[0] = bef return value } + +// jsonparser strips quotes from returned strings, this adds them back +func handleUnquotedStrings(value []byte, dt jsonparser.ValueType) []byte { + if dt == jsonparser.String { + // bookend() is destructive to underlying slice, need to copy. + // extra capacity saves an allocation and copy during bookend. + tmp := make([]byte, len(value), len(value)+2) + copy(tmp, value) + value = bookend(tmp, '"', '"') + } + return value +} diff --git a/transform/util_test.go b/transform/util_test.go index f3add96..f2712d7 100644 --- a/transform/util_test.go +++ b/transform/util_test.go @@ -3,6 +3,7 @@ package transform import ( "encoding/json" "fmt" + "reflect" "testing" ) @@ -14,12 +15,52 @@ func getConfig(spec string, require bool) Config { return Config{Spec: &f, Require: require} } -func getTransformTestWrapper(tform func(spec *Config, data []byte) ([]byte, error), cfg Config, input string) (string, error) { +func getTransformTestWrapper(tform func(spec *Config, data []byte) ([]byte, error), cfg Config, input string) ([]byte, error) { output, e := tform(&cfg, []byte(input)) if e != nil { - return "", e + return nil, e + } + return output, nil +} + +func checkJSONBytesEqual(item1, item2 []byte) (bool, error) { + var out1, out2 interface{} + + err := json.Unmarshal(item1, &out1) + if err != nil { + return false, nil + } + + err = json.Unmarshal(item2, &out2) + if err != nil { + return false, nil + } + + return reflect.DeepEqual(out1, out2), nil +} + +func TestCheckJSONBytesAreEqual(t *testing.T) { + item1 := []byte(`{"test":["data1", "data2"],"key":"value"}`) + item2 := []byte(`{"key":"value","test":["data1", "data2"]}`) + areEqual, _ := checkJSONBytesEqual(item1, item2) + if !areEqual { + t.Error("JSON equality check failed") + t.Log("Item 1: ", string(item1)) + t.Log("Item 2: ", string(item2)) + t.FailNow() + } +} + +func TestCheckJSONBytesAreNotEqual(t *testing.T) { + item1 := []byte(`{"test":["data1", "data2"]}`) + item2 := []byte(`{"test":["data1", "data1"]}`) + areEqual, _ := checkJSONBytesEqual(item1, item2) + if areEqual { + t.Error("JSON inequality check failed") + t.Log("Item 1: ", string(item1)) + t.Log("Item 2: ", string(item2)) + t.FailNow() } - return string(output), nil } func TestBookend(t *testing.T) { @@ -37,7 +78,8 @@ func TestBookend(t *testing.T) { input = []byte("fooString") expected = []byte(`"fooString"`) result = bookend(input, '"', '"') - if string(result) != string(expected) { + areEqual, _ := checkJSONBytesEqual(result, expected) + if !areEqual { t.Error("Bookend result does not match expectation.") t.Log("Expected: ", expected) t.Log("Actual: ", result) @@ -50,18 +92,19 @@ func TestSetJSONRaw(t *testing.T) { inputData []byte inputValue []byte path string - expectedOutput string + expectedOutput []byte }{ - {[]byte(`{"data":"value"}`), []byte(`"newValue"`), "data", `{"data":"newValue"}`}, - {[]byte(`{"data":["value", "notValue"]}`), []byte(`"newValue"`), "data[0]", `{"data":["newValue", "notValue"]}`}, - {[]byte(`{"data":["value", "notValue"]}`), []byte(`"newValue"`), "data[*]", `{"data":["newValue", "newValue"]}`}, - {[]byte(`{"data":[{"key": "value"}, {"key": "value"}]}`), []byte(`"newValue"`), "data[*].key", `{"data":[{"key": "newValue"}, {"key": "newValue"}]}`}, - {[]byte(`{"data":[{"key": "value"}, {"key": "value"}]}`), []byte(`"newValue"`), "data[1].key", `{"data":[{"key": "value"}, {"key": "newValue"}]}`}, - {[]byte(`{"data":{"subData":[{"key": "value"}, {"key": "value"}]}}`), []byte(`"newValue"`), "data.subData[*].key", `{"data":{"subData":[{"key": "newValue"}, {"key": "newValue"}]}}`}, + {[]byte(`{"data":"value"}`), []byte(`"newValue"`), "data", []byte(`{"data":"newValue"}`)}, + {[]byte(`{"data":["value", "notValue"]}`), []byte(`"newValue"`), "data[0]", []byte(`{"data":["newValue", "notValue"]}`)}, + {[]byte(`{"data":["value", "notValue"]}`), []byte(`"newValue"`), "data[*]", []byte(`{"data":["newValue", "newValue"]}`)}, + {[]byte(`{"data":[{"key": "value"}, {"key": "value"}]}`), []byte(`"newValue"`), "data[*].key", []byte(`{"data":[{"key": "newValue"}, {"key": "newValue"}]}`)}, + {[]byte(`{"data":[{"key": "value"}, {"key": "value"}]}`), []byte(`"newValue"`), "data[1].key", []byte(`{"data":[{"key": "value"}, {"key": "newValue"}]}`)}, + {[]byte(`{"data":{"subData":[{"key": "value"}, {"key": "value"}]}}`), []byte(`"newValue"`), "data.subData[*].key", []byte(`{"data":{"subData":[{"key": "newValue"}, {"key": "newValue"}]}}`)}, } for _, testItem := range setPathTests { actual, _ := setJSONRaw(testItem.inputData, testItem.inputValue, testItem.path) - if string(actual) != testItem.expectedOutput { + areEqual, _ := checkJSONBytesEqual(actual, testItem.expectedOutput) + if !areEqual { t.Error("Error data does not match expectation.") t.Log("Expected: ", testItem.expectedOutput) t.Log("Actual: ", string(actual)) @@ -87,22 +130,23 @@ func TestGetJSONRaw(t *testing.T) { inputData []byte path string required bool - expectedOutput string + expectedOutput []byte }{ - {[]byte(`{"data":"value"}`), "data", true, `"value"`}, - {[]byte(`{"data":"value"}`), "data", false, `"value"`}, - {[]byte(`{"notData":"value"}`), "data", false, `null`}, - {[]byte(`{"data":["value", "notValue"]}`), "data[0]", true, `"value"`}, - {[]byte(`{"data":["value", "notValue"]}`), "data[*]", true, `[value,notValue]`}, - {[]byte(`{"data":[{"key": "value"}, {"key": "value"}]}`), "data[*].key", true, `["value","value"]`}, - {[]byte(`{"data":[{"key": "value"}, {"key": "otherValue"}]}`), "data[1].key", true, `"otherValue"`}, - {[]byte(`{"data":{"subData":[{"key": "value"}, {"key": "value"}]}}`), "data.subData[*].key", true, `["value","value"]`}, + {[]byte(`{"data":"value"}`), "data", true, []byte(`"value"`)}, + {[]byte(`{"data":"value"}`), "data", false, []byte(`"value"`)}, + {[]byte(`{"notData":"value"}`), "data", false, []byte(`null`)}, + {[]byte(`{"data":["value", "notValue"]}`), "data[0]", true, []byte(`"value"`)}, + {[]byte(`{"data":["value", "notValue"]}`), "data[*]", true, []byte(`["value","notValue"]`)}, + {[]byte(`{"data":[{"key": "value"}, {"key": "value"}]}`), "data[*].key", true, []byte(`["value","value"]`)}, + {[]byte(`{"data":[{"key": "value"}, {"key": "otherValue"}]}`), "data[1].key", true, []byte(`"otherValue"`)}, + {[]byte(`{"data":{"subData":[{"key": "value"}, {"key": "value"}]}}`), "data.subData[*].key", true, []byte(`["value","value"]`)}, } for _, testItem := range getPathTests { actual, _ := getJSONRaw(testItem.inputData, testItem.path, testItem.required) - if string(actual) != testItem.expectedOutput { + areEqual, _ := checkJSONBytesEqual(actual, testItem.expectedOutput) + if !areEqual { t.Error("Error data does not match expectation.") - t.Log("Expected: ", testItem.expectedOutput) + t.Log("Expected: ", string(testItem.expectedOutput)) t.Log("Actual: ", string(actual)) } } diff --git a/transform/uuid_test.go b/transform/uuid_test.go index c7bbedf..69473f9 100644 --- a/transform/uuid_test.go +++ b/transform/uuid_test.go @@ -37,7 +37,7 @@ func TestUUIDVersionError(t *testing.T) { } } -func TestUUIDNoVersionErro(t *testing.T) { +func TestUUIDNoVersionError(t *testing.T) { spec := `{"a.uuid":{}` jsonIn := `{"a":{"author":"jason","id":2323223}` @@ -115,7 +115,8 @@ func TestUUIDV3URLNameSpace(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -147,7 +148,8 @@ func TestUUIDV3DNSNameSpace(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -179,7 +181,8 @@ func TestUUIDV3OIDNameSpace(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -211,7 +214,8 @@ func TestUUIDV3X500NameSpace(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -243,7 +247,8 @@ func TestUUIDWithCustomeNameSpace(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -275,7 +280,8 @@ func TestUUIDV3UsingDefaultField(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -324,7 +330,8 @@ func TestUUIDV5(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -340,6 +347,7 @@ func TestUUIDV5(t *testing.T) { t.FailNow() } } + func TestUUIDV5UsingDefaultField(t *testing.T) { spec := `{"a.uuid": {"version": 5, "nameSpace": "URL", "names": [{"path": "a.id", "default": "test"}, @@ -355,7 +363,8 @@ func TestUUIDV5UsingDefaultField(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut) @@ -418,7 +427,8 @@ func TestUUIDV5ArrayIndex(t *testing.T) { t.FailNow() } - if kazaamOut != jsonOut { + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + if !areEqual { t.Error("Transformed data does not match expectation.") t.Log("Expected: ", jsonOut) t.Log("Actual: ", kazaamOut)