github.com/psiphon-labs/goarista@v0.0.0-20160825065156-d002785f4c67/kafka/openconfig/encoder_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/openconfig"
    12  	"github.com/aristanetworks/goarista/test"
    13  )
    14  
    15  func TestJsonify(t *testing.T) {
    16  	var tests = []struct {
    17  		notification *openconfig.Notification
    18  		document     map[string]interface{}
    19  	}{{
    20  		notification: &openconfig.Notification{
    21  			Prefix: &openconfig.Path{Element: []string{"Sysdb", "a"}},
    22  			Update: []*openconfig.Update{
    23  				{
    24  					Path: &openconfig.Path{Element: []string{"b"}},
    25  					Value: &openconfig.Value{
    26  						Value: []byte{52, 50},
    27  						Type:  openconfig.Type_JSON,
    28  					},
    29  				},
    30  			},
    31  		},
    32  		document: map[string]interface{}{
    33  			"Sysdb": map[string]interface{}{
    34  				"a": map[string]interface{}{
    35  					"b": 42,
    36  				},
    37  			},
    38  			"_timestamp": 0,
    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(jsonTest.notification,
    48  			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  }