encoder_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package openconfig
  5. import (
  6. "encoding/json"
  7. "testing"
  8. "notabug.org/themusicgod1/goarista/openconfig"
  9. "notabug.org/themusicgod1/goarista/test"
  10. pb "github.com/openconfig/reference/rpc/openconfig"
  11. )
  12. func TestJsonify(t *testing.T) {
  13. var tests = []struct {
  14. notification *pb.Notification
  15. document map[string]interface{}
  16. }{{
  17. notification: &pb.Notification{
  18. Prefix: &pb.Path{Element: []string{"Sysdb", "a"}},
  19. Update: []*pb.Update{
  20. {
  21. Path: &pb.Path{Element: []string{"b"}},
  22. Value: &pb.Value{
  23. Value: []byte{52, 50},
  24. Type: pb.Type_JSON,
  25. },
  26. },
  27. },
  28. },
  29. document: map[string]interface{}{
  30. "timestamp": int64(0),
  31. "dataset": "foo",
  32. "update": map[string]interface{}{
  33. "Sysdb": map[string]interface{}{
  34. "a": map[string]interface{}{
  35. "b": 42,
  36. },
  37. },
  38. },
  39. },
  40. },
  41. }
  42. for _, jsonTest := range tests {
  43. expected, err := json.Marshal(jsonTest.document)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. actual, err := openconfig.NotificationToJSONDocument("foo",
  48. jsonTest.notification, nil)
  49. if err != nil {
  50. t.Error(err)
  51. }
  52. diff := test.Diff(actual, expected)
  53. if len(diff) > 0 {
  54. t.Errorf("Unexpected diff: %s", diff)
  55. }
  56. }
  57. }