github.com/psiphon-inc/goarista@v0.0.0-20160825065156-d002785f4c67/openconfig/json_test.go (about) 1 // Copyright (C) 2016 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package openconfig 6 7 import ( 8 "encoding/json" 9 "testing" 10 11 "github.com/aristanetworks/goarista/test" 12 ) 13 14 func TestNotificationToMap(t *testing.T) { 15 value := map[string]interface{}{ 16 "239.255.255.250_0.0.0.0": map[string]interface{}{ 17 "creationTime": 4.567969230573434e+06, 18 }, 19 } 20 valueJSON, err := json.Marshal(value) 21 if err != nil { 22 t.Fatal(err) 23 } 24 notification := Notification{ 25 Prefix: &Path{ 26 Element: []string{ 27 "Smash", "routing", "pim", "sparsemode", "status", "default", 28 }, 29 }, 30 Update: []*Update{{ 31 Path: &Path{ 32 Element: []string{ 33 "route", 34 }, 35 }, 36 Value: &Value{ 37 Value: valueJSON, 38 }, 39 }}, 40 } 41 expected := map[string]interface{}{ 42 "Smash": map[string]interface{}{ 43 "routing": map[string]interface{}{ 44 "pim": map[string]interface{}{ 45 "sparsemode": map[string]interface{}{ 46 "status": map[string]interface{}{ 47 "default": map[string]interface{}{ 48 "route": map[string]interface{}{ 49 "239.255.255.250_0.0.0.0": map[string]interface{}{ 50 "creationTime": 4.567969230573434e+06, 51 }, 52 }, 53 }, 54 }, 55 }, 56 }, 57 }, 58 }, 59 } 60 actual, err := NotificationToMap(¬ification, nil) 61 if err != nil { 62 t.Fatal(err) 63 } 64 delete(actual, "_timestamp") 65 diff := test.Diff(expected, actual) 66 if len(diff) > 0 { 67 expectedJSON, _ := json.Marshal(expected) 68 actualJSON, _ := json.Marshal(actual) 69 t.Fatalf("Unexpected diff: %s (expected: %s, got: %s)", diff, expectedJSON, 70 actualJSON) 71 } 72 }