github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/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  	"github.com/openconfig/gnmi/proto/gnmi"
    14  )
    15  
    16  func TestNotificationToMap(t *testing.T) {
    17  	value := map[string]interface{}{
    18  		"239.255.255.250_0.0.0.0": map[string]interface{}{
    19  			"creationTime": 4.567969230573434e+06,
    20  		},
    21  	}
    22  	valueJSON, err := json.Marshal(value)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	tests := []struct {
    27  		notification gnmi.Notification
    28  		json         map[string]interface{}
    29  	}{{
    30  		notification: gnmi.Notification{
    31  			Prefix: &gnmi.Path{
    32  				Element: []string{
    33  					"foo",
    34  				},
    35  			},
    36  			Update: []*gnmi.Update{
    37  				{
    38  					Path: &gnmi.Path{
    39  						Element: []string{
    40  							"route1",
    41  						},
    42  					},
    43  					Value: &gnmi.Value{
    44  						Value: valueJSON,
    45  					},
    46  				}, {
    47  					Path: &gnmi.Path{
    48  						Element: []string{
    49  							"route2",
    50  						},
    51  					},
    52  					Value: &gnmi.Value{
    53  						Value: valueJSON,
    54  					},
    55  				}},
    56  		},
    57  		json: map[string]interface{}{
    58  			"timestamp": int64(0),
    59  			"dataset":   "cairo",
    60  			"update": map[string]interface{}{
    61  				"foo": map[string]interface{}{
    62  					"route1": map[string]interface{}{
    63  						"239.255.255.250_0.0.0.0": map[string]interface{}{
    64  							"creationTime": 4.567969230573434e+06,
    65  						},
    66  					},
    67  					"route2": map[string]interface{}{
    68  						"239.255.255.250_0.0.0.0": map[string]interface{}{
    69  							"creationTime": 4.567969230573434e+06,
    70  						},
    71  					},
    72  				},
    73  			},
    74  		},
    75  	}, {
    76  		notification: gnmi.Notification{
    77  			Prefix: &gnmi.Path{
    78  				Element: []string{
    79  					"foo", "bar",
    80  				},
    81  			},
    82  			Delete: []*gnmi.Path{
    83  				{
    84  					Element: []string{
    85  						"route", "237.255.255.250_0.0.0.0",
    86  					}},
    87  				{
    88  					Element: []string{
    89  						"route", "238.255.255.250_0.0.0.0",
    90  					},
    91  				},
    92  			},
    93  			Update: []*gnmi.Update{{
    94  				Path: &gnmi.Path{
    95  					Element: []string{
    96  						"route",
    97  					},
    98  				},
    99  				Value: &gnmi.Value{
   100  					Value: valueJSON,
   101  				},
   102  			}},
   103  		},
   104  		json: map[string]interface{}{
   105  			"timestamp": int64(0),
   106  			"dataset":   "cairo",
   107  			"delete": map[string]interface{}{
   108  				"foo": map[string]interface{}{
   109  					"bar": map[string]interface{}{
   110  						"route": map[string]interface{}{
   111  							"237.255.255.250_0.0.0.0": map[string]interface{}{},
   112  							"238.255.255.250_0.0.0.0": map[string]interface{}{},
   113  						},
   114  					},
   115  				},
   116  			},
   117  			"update": map[string]interface{}{
   118  				"foo": map[string]interface{}{
   119  					"bar": map[string]interface{}{
   120  						"route": map[string]interface{}{
   121  							"239.255.255.250_0.0.0.0": map[string]interface{}{
   122  								"creationTime": 4.567969230573434e+06,
   123  							},
   124  						},
   125  					},
   126  				},
   127  			},
   128  		},
   129  	}}
   130  	for i := 0; i < len(tests); i++ {
   131  		tcase := &tests[i] // index slice to avoid copying struct with mutex in it
   132  		actual, err := NotificationToMap("cairo", &tcase.notification, nil)
   133  		if err != nil {
   134  			t.Fatal(err)
   135  		}
   136  		diff := test.Diff(tcase.json, actual)
   137  		if len(diff) > 0 {
   138  			expectedJSON, _ := json.Marshal(tcase.json)
   139  			actualJSON, _ := json.Marshal(actual)
   140  			t.Fatalf("Unexpected diff: %s\nExpected:\n%s\nGot:\n%s\n)", diff, expectedJSON,
   141  				actualJSON)
   142  		}
   143  	}
   144  }