github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/kafka/gnmi/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 gnmi
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/aristanetworks/goarista/elasticsearch"
    11  	"github.com/aristanetworks/goarista/test"
    12  
    13  	"github.com/openconfig/gnmi/proto/gnmi"
    14  )
    15  
    16  func ToStringPtr(str string) *string {
    17  	return &str
    18  }
    19  
    20  func ToFloatPtr(flt float64) *float64 {
    21  	return &flt
    22  }
    23  
    24  func TestJsonify(t *testing.T) {
    25  	var tests = []struct {
    26  		notification *gnmi.Notification
    27  		document     map[string]interface{}
    28  	}{{
    29  		notification: &gnmi.Notification{
    30  			Prefix: &gnmi.Path{Elem: []*gnmi.PathElem{
    31  				&gnmi.PathElem{Name: "Sysdb"}, &gnmi.PathElem{Name: "a"}}},
    32  			Update: []*gnmi.Update{
    33  				{
    34  					Path: &gnmi.Path{Elem: []*gnmi.PathElem{&gnmi.PathElem{Name: "b"}}},
    35  					Val: &gnmi.TypedValue{
    36  						Value: &gnmi.TypedValue_JsonVal{
    37  							JsonVal: []byte{52, 50},
    38  						},
    39  					},
    40  				},
    41  			},
    42  		},
    43  		document: map[string]interface{}{
    44  			"Timestamp":   uint64(0),
    45  			"DatasetID":   "foo",
    46  			"Path":        "/Sysdb/a/b",
    47  			"Key":         []byte("/b"),
    48  			"KeyString":   ToStringPtr("/b"),
    49  			"ValueDouble": ToFloatPtr(float64(42))},
    50  	},
    51  	}
    52  	for _, jsonTest := range tests {
    53  		actual, err := elasticsearch.NotificationToMaps("foo", jsonTest.notification)
    54  		if err != nil {
    55  			t.Error(err)
    56  		}
    57  		diff := test.Diff(jsonTest.document, actual[0])
    58  		if len(diff) > 0 {
    59  			t.Errorf("Unexpected diff: %s", diff)
    60  		}
    61  	}
    62  }