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

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import (
     7  	"encoding/json"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/newrelic/go-agent/internal/crossagent"
    12  )
    13  
    14  func TestCrossAgentSegmentTerms(t *testing.T) {
    15  	var tcs []struct {
    16  		Testname string       `json:"testname"`
    17  		Rules    segmentRules `json:"transaction_segment_terms"`
    18  		Tests    []struct {
    19  			Input    string `json:"input"`
    20  			Expected string `json:"expected"`
    21  		} `json:"tests"`
    22  	}
    23  
    24  	err := crossagent.ReadJSON("transaction_segment_terms.json", &tcs)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	for _, tc := range tcs {
    30  		for _, test := range tc.Tests {
    31  			out := tc.Rules.apply(test.Input)
    32  			if out != test.Expected {
    33  				t.Fatal(tc.Testname, test.Input, out, test.Expected)
    34  			}
    35  		}
    36  	}
    37  }
    38  
    39  func TestSegmentTerms(t *testing.T) {
    40  	js := `[
    41        {
    42           "prefix":"WebTransaction\/Uri",
    43           "terms":[
    44              "two",
    45              "Users",
    46              "willhf",
    47              "dev",
    48              "php",
    49              "one",
    50              "alpha",
    51              "zap"
    52           ]
    53        }
    54     ]`
    55  	var rules segmentRules
    56  	if err := json.Unmarshal([]byte(js), &rules); nil != err {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	out := rules.apply("WebTransaction/Uri/pen/two/pencil/dev/paper")
    61  	if out != "WebTransaction/Uri/*/two/*/dev/*" {
    62  		t.Fatal(out)
    63  	}
    64  }
    65  
    66  func TestEmptySegmentTerms(t *testing.T) {
    67  	var rules segmentRules
    68  
    69  	input := "my/name"
    70  	out := rules.apply(input)
    71  	if out != input {
    72  		t.Error(input, out)
    73  	}
    74  }
    75  
    76  func BenchmarkSegmentTerms(b *testing.B) {
    77  	js := `[
    78        {
    79           "prefix":"WebTransaction\/Uri",
    80           "terms":[
    81              "two",
    82              "Users",
    83              "willhf",
    84              "dev",
    85              "php",
    86              "one",
    87              "alpha",
    88              "zap"
    89           ]
    90        }
    91     ]`
    92  	var rules segmentRules
    93  	if err := json.Unmarshal([]byte(js), &rules); nil != err {
    94  		b.Fatal(err)
    95  	}
    96  
    97  	b.ResetTimer()
    98  	b.ReportAllocs()
    99  
   100  	input := "WebTransaction/Uri/pen/two/pencil/dev/paper"
   101  	expected := "WebTransaction/Uri/*/two/*/dev/*"
   102  	for i := 0; i < b.N; i++ {
   103  		out := rules.apply(input)
   104  		if out != expected {
   105  			b.Fatal(out, expected)
   106  		}
   107  	}
   108  }
   109  
   110  func TestCollapsePlaceholders(t *testing.T) {
   111  	testcases := []struct {
   112  		input  string
   113  		expect string
   114  	}{
   115  		{input: "", expect: ""},
   116  		{input: "/", expect: "/"},
   117  		{input: "*", expect: "*"},
   118  		{input: "*/*", expect: "*"},
   119  		{input: "a/b/c", expect: "a/b/c"},
   120  		{input: "*/*/*", expect: "*"},
   121  		{input: "a/*/*/*/b", expect: "a/*/b"},
   122  		{input: "a/b/*/*/*/", expect: "a/b/*/"},
   123  		{input: "a/b/*/*/*", expect: "a/b/*"},
   124  		{input: "*/*/a/b/*/*/*", expect: "*/a/b/*"},
   125  		{input: "*/*/a/b/*/c/*/*/d/e/*/*/*", expect: "*/a/b/*/c/*/d/e/*"},
   126  		{input: "a/*/b", expect: "a/*/b"},
   127  	}
   128  
   129  	for _, tc := range testcases {
   130  		segments := strings.Split(tc.input, "/")
   131  		segments = collapsePlaceholders(segments)
   132  		out := strings.Join(segments, "/")
   133  		if out != tc.expect {
   134  			t.Error(tc.input, tc.expect, out)
   135  		}
   136  	}
   137  }