github.com/newrelic/newrelic-client-go@v1.1.0/pkg/apm/key_transactions_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package apm 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 var ( 15 testListKeyTransactionsResponseJSON = `{ 16 "key_transactions": [ 17 { 18 "id": 1, 19 "name": "get /", 20 "transaction_name": "get /", 21 "health_status": "unknown", 22 "reporting": true, 23 "last_reported_at": "2020-01-02T21:09:07+00:00", 24 "application_summary": { 25 "response_time": 0.381, 26 "throughput": 24, 27 "error_rate": 0, 28 "apdex_target": 0.01, 29 "apdex_score": 1 30 }, 31 "links": { 32 "application": 12345 33 } 34 } 35 ] 36 }` 37 38 testListKeyTransactionsWithParamsResponseJSON = `{ 39 "key_transactions": [ 40 { 41 "id": 456, 42 "name": "test-key-transaction", 43 "transaction_name": "test-key-transaction-name", 44 "health_status": "unknown", 45 "reporting": true, 46 "last_reported_at": "2020-01-02T21:09:07+00:00", 47 "application_summary": { 48 "response_time": 0.381, 49 "throughput": 24, 50 "error_rate": 0, 51 "apdex_target": 0.01, 52 "apdex_score": 1 53 }, 54 "links": { 55 "application": 54321 56 } 57 } 58 ] 59 }` 60 61 keyTransactionJSON = `{ 62 "key_transaction": { 63 "id": 1, 64 "name": "get /", 65 "transaction_name": "get /", 66 "health_status": "unknown", 67 "reporting": true, 68 "last_reported_at": "2020-01-02T21:56:07+00:00", 69 "application_summary": { 70 "response_time": 0.382, 71 "throughput": 24, 72 "error_rate": 0, 73 "apdex_target": 0.01, 74 "apdex_score": 1 75 }, 76 "links": { 77 "application": 12345 78 } 79 } 80 }` 81 ) 82 83 func TestListKeyTransactions(t *testing.T) { 84 t.Parallel() 85 apm := newMockResponse(t, testListKeyTransactionsResponseJSON, http.StatusOK) 86 87 expected := []*KeyTransaction{ 88 { 89 ID: 1, 90 Name: "get /", 91 TransactionName: "get /", 92 HealthStatus: "unknown", 93 LastReportedAt: "2020-01-02T21:09:07+00:00", 94 Reporting: true, 95 Summary: ApplicationSummary{ 96 ResponseTime: 0.381, 97 Throughput: 24, 98 ErrorRate: 0, 99 ApdexTarget: 0.01, 100 ApdexScore: 1, 101 }, 102 Links: KeyTransactionLinks{ 103 Application: 12345, 104 }, 105 }, 106 } 107 108 actual, err := apm.ListKeyTransactions(nil) 109 110 assert.NoError(t, err) 111 assert.NotNil(t, actual) 112 assert.Equal(t, expected, actual) 113 } 114 115 func TestListKeyTransactionsWithParams(t *testing.T) { 116 t.Parallel() 117 keyTransactionName := "test-key-transaction" 118 idsFilter := "123,456,789" 119 120 apm := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 121 values := r.URL.Query() 122 name := values.Get("filter[name]") 123 124 require.Equal(t, keyTransactionName, name) 125 126 ids := values.Get("filter[ids]") 127 128 require.Equal(t, idsFilter, ids) 129 130 w.Header().Set("Content-Type", "application/json") 131 _, err := w.Write([]byte(testListKeyTransactionsWithParamsResponseJSON)) 132 133 require.NoError(t, err) 134 })) 135 136 expected := []*KeyTransaction{ 137 { 138 ID: 456, 139 Name: keyTransactionName, 140 TransactionName: "test-key-transaction-name", 141 HealthStatus: "unknown", 142 LastReportedAt: "2020-01-02T21:09:07+00:00", 143 Reporting: true, 144 Summary: ApplicationSummary{ 145 ResponseTime: 0.381, 146 Throughput: 24, 147 ErrorRate: 0, 148 ApdexTarget: 0.01, 149 ApdexScore: 1, 150 }, 151 Links: KeyTransactionLinks{ 152 Application: 54321, 153 }, 154 }, 155 } 156 157 params := ListKeyTransactionsParams{ 158 Name: keyTransactionName, 159 IDs: []int{123, 456, 789}, 160 } 161 162 actual, err := apm.ListKeyTransactions(¶ms) 163 164 assert.NoError(t, err) 165 assert.NotNil(t, actual) 166 assert.Equal(t, expected, actual) 167 } 168 169 func TestGetKeyTransaction(t *testing.T) { 170 t.Parallel() 171 apm := newMockResponse(t, keyTransactionJSON, http.StatusOK) 172 173 expected := &KeyTransaction{ 174 ID: 1, 175 Name: "get /", 176 TransactionName: "get /", 177 HealthStatus: "unknown", 178 LastReportedAt: "2020-01-02T21:56:07+00:00", 179 Reporting: true, 180 Summary: ApplicationSummary{ 181 ResponseTime: 0.382, 182 Throughput: 24, 183 ErrorRate: 0, 184 ApdexTarget: 0.01, 185 ApdexScore: 1, 186 }, 187 Links: KeyTransactionLinks{ 188 Application: 12345, 189 }, 190 } 191 192 actual, err := apm.GetKeyTransaction(1) 193 194 assert.NoError(t, err) 195 assert.NotNil(t, actual) 196 assert.Equal(t, expected, actual) 197 }