github.com/newrelic/go-agent@v3.26.0+incompatible/internal/cat/txndata_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package cat
     5  
     6  import (
     7  	"encoding/json"
     8  	"testing"
     9  )
    10  
    11  func TestTxnDataRoundTrip(t *testing.T) {
    12  	for _, test := range []struct {
    13  		input   string
    14  		output  string
    15  		txnData TxnDataHeader
    16  	}{
    17  		{
    18  			input:  `["guid",false]`,
    19  			output: `["guid",false,"",""]`,
    20  			txnData: TxnDataHeader{
    21  				GUID:     "guid",
    22  				TripID:   "",
    23  				PathHash: "",
    24  			},
    25  		},
    26  		{
    27  			input:  `["guid",false,"trip"]`,
    28  			output: `["guid",false,"trip",""]`,
    29  			txnData: TxnDataHeader{
    30  				GUID:     "guid",
    31  				TripID:   "trip",
    32  				PathHash: "",
    33  			},
    34  		},
    35  		{
    36  			input:  `["guid",false,null]`,
    37  			output: `["guid",false,"",""]`,
    38  			txnData: TxnDataHeader{
    39  				GUID:     "guid",
    40  				TripID:   "",
    41  				PathHash: "",
    42  			},
    43  		},
    44  		{
    45  			input:  `["guid",false,"trip",null]`,
    46  			output: `["guid",false,"trip",""]`,
    47  			txnData: TxnDataHeader{
    48  				GUID:     "guid",
    49  				TripID:   "trip",
    50  				PathHash: "",
    51  			},
    52  		},
    53  		{
    54  			input:  `["guid",false,"trip","hash"]`,
    55  			output: `["guid",false,"trip","hash"]`,
    56  			txnData: TxnDataHeader{
    57  				GUID:     "guid",
    58  				TripID:   "trip",
    59  				PathHash: "hash",
    60  			},
    61  		},
    62  	} {
    63  		// Test unmarshalling.
    64  		txnData := &TxnDataHeader{}
    65  		if err := json.Unmarshal([]byte(test.input), txnData); err != nil {
    66  			t.Errorf("given %s: error expected to be nil; got %v", test.input, err)
    67  		}
    68  
    69  		if test.txnData.GUID != txnData.GUID {
    70  			t.Errorf("given %s: GUID expected to be %s; got %s", test.input, test.txnData.GUID, txnData.GUID)
    71  		}
    72  
    73  		if test.txnData.TripID != txnData.TripID {
    74  			t.Errorf("given %s: TripID expected to be %s; got %s", test.input, test.txnData.TripID, txnData.TripID)
    75  		}
    76  
    77  		if test.txnData.PathHash != txnData.PathHash {
    78  			t.Errorf("given %s: PathHash expected to be %s; got %s", test.input, test.txnData.PathHash, txnData.PathHash)
    79  		}
    80  
    81  		// Test marshalling.
    82  		data, err := json.Marshal(&test.txnData)
    83  		if err != nil {
    84  			t.Errorf("given %s: error expected to be nil; got %v", test.output, err)
    85  		}
    86  
    87  		if string(data) != test.output {
    88  			t.Errorf("given %s: unexpected JSON %s", test.output, string(data))
    89  		}
    90  	}
    91  }
    92  
    93  func TestTxnDataUnmarshal(t *testing.T) {
    94  	// Test error cases where we get a generic error from the JSON package.
    95  	for _, input := range []string{
    96  		// Basic malformed JSON test: beyond this, we're not going to unit test the
    97  		// Go standard library's JSON package.
    98  		``,
    99  	} {
   100  		txnData := &TxnDataHeader{}
   101  
   102  		if err := json.Unmarshal([]byte(input), txnData); err == nil {
   103  			t.Errorf("given %s: error expected to be non-nil; got nil", input)
   104  		}
   105  	}
   106  
   107  	// Test error cases where the incorrect number of elements was provided.
   108  	for _, input := range []string{
   109  		`[]`,
   110  		`[1]`,
   111  	} {
   112  		txnData := &TxnDataHeader{}
   113  
   114  		err := json.Unmarshal([]byte(input), txnData)
   115  		if _, ok := err.(errUnexpectedArraySize); !ok {
   116  			t.Errorf("given %s: error expected to be errUnexpectedArraySize; got %v", input, err)
   117  		}
   118  	}
   119  
   120  	// Test error cases where a specific variable is returned.
   121  	for _, tc := range []struct {
   122  		input string
   123  		err   error
   124  	}{
   125  		// Unexpected JSON types.
   126  		{`false`, errInvalidTxnDataJSON},
   127  		{`true`, errInvalidTxnDataJSON},
   128  		{`1234`, errInvalidTxnDataJSON},
   129  		{`{}`, errInvalidTxnDataJSON},
   130  		{`""`, errInvalidTxnDataJSON},
   131  
   132  		// Invalid data types for each field in turn.
   133  		{`[false,false,"trip","hash"]`, errInvalidTxnDataGUID},
   134  		{`["guid",false,0,"hash"]`, errInvalidTxnDataTripID},
   135  		{`["guid",false,"trip",[]]`, errInvalidTxnDataPathHash},
   136  	} {
   137  		txnData := &TxnDataHeader{}
   138  
   139  		if err := json.Unmarshal([]byte(tc.input), txnData); err != tc.err {
   140  			t.Errorf("given %s: error expected to be %v; got %v", tc.input, tc.err, err)
   141  		}
   142  	}
   143  }