1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/telemetry/data_test.go
Christopher Fenner aa3fb8adb4
feat(go): add telemetry reporting (#1100)
* Add telemetry support

* First round telemetry

* Add telemetry flag

* fix: move files to avoid import cycles

* add noTelemetry as global config option

* Respect telemetry configuration for reporting

* add site id, swa endpoint

* correct logger initialization

* add http logic

* rename init method

* rename consts & types

* convert struct to payload

* convert data to payload string

* move activation flag out of data structure

* extract types to own file

* build query using net/url

* correct field mapping

* extract notify coding to own file

* cleanup parameter mapping

* preare base data

* fix codeclimate issue

* correct test case

* fill values from env

* test all fields

* untrack notify.go

* ignore empty custom values

* cleanup data.go

* add test cases

* cleanup

* add usage reporting to karma step

* add usage reporting to step generator

* externalise siteID

* correct custom field names

* test env handling

* simplify method signature

* revert parameter negation

* correct import

* adjust golden file

* inclease log level

* ignore test case

* Revert "inclease log level"

This reverts commit 70cae0e029.

* add test case for envvars

* remove duplicate reporting

* remove duplicate reporting

* correct format

* regenerate checkmarx file

* add log message on deactivation

* rename function

* add comments to understand SWA mapping

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-01-29 13:17:54 +01:00

68 lines
1.9 KiB
Go

package telemetry
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDataToMap(t *testing.T) {
// init
testData := Data{BaseData: BaseData{ActionName: "testAction"}, CustomData: CustomData{Custom2Label: "label", Custom2: "value"}}
// test
result := testData.toMap()
// assert
assert.Contains(t, result, "action_name")
assert.Contains(t, result, "event_type")
assert.Contains(t, result, "idsite")
assert.Contains(t, result, "url")
assert.Contains(t, result, "e_3")
assert.Contains(t, result, "e_4")
assert.Contains(t, result, "e_5")
assert.Contains(t, result, "e_10")
assert.Contains(t, result, "custom3")
assert.Contains(t, result, "custom4")
assert.Contains(t, result, "custom5")
assert.Contains(t, result, "custom10")
assert.Contains(t, result, "e_27")
assert.Contains(t, result, "custom27")
assert.Equal(t, 14, len(result))
}
func TestDataToPayload(t *testing.T) {
t.Run("with single parameter", func(t *testing.T) {
// init
testData := Data{BaseData: BaseData{ActionName: "testAction"}}
// test
result := testData.toPayloadString()
// assert
assert.Contains(t, result, "action_name=testAction")
assert.NotContains(t, result, "idsite=")
})
t.Run("with multiple parameters", func(t *testing.T) {
// init
testData := Data{BaseData: BaseData{ActionName: "testAction", SiteID: "gl8rkd6j211bw3j1fwb8rb4h0000gn"}}
// test
result := testData.toPayloadString()
// assert
assert.Contains(t, result, "&")
assert.Contains(t, result, "action_name=testAction")
assert.Contains(t, result, "idsite=gl8rkd6j211bw3j1fwb8rb4h0000gn")
})
t.Run("encoding", func(t *testing.T) {
// init
testData := Data{BaseData: BaseData{ActionName: "t€štÄçtïøñ"}}
// test
result := testData.toPayloadString()
// assert
assert.Contains(t, result, "t%E2%82%AC%C5%A1t%C3%84%C3%A7t%C3%AF%C3%B8%C3%B1")
assert.NotContains(t, result, "t€štÄçtïøñ")
})
}