github.com/newrelic/newrelic-client-go@v1.1.0/pkg/apm/labels_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 ) 12 13 var ( 14 testListLabelsResponseJSON = `{ 15 "labels": [ 16 { 17 "key": "Project:example-project", 18 "category": "Project", 19 "name": "example-project", 20 "application_health_status": { 21 "green": [], 22 "orange": [], 23 "red": [], 24 "gray": [] 25 }, 26 "server_health_status": { 27 "green": [], 28 "orange": [], 29 "red": [], 30 "gray": [] 31 }, 32 "origins": { 33 "apm": [], 34 "synthetics": [], 35 "agents": [ 36 12345 37 ] 38 }, 39 "links": { 40 "applications": [ 41 12345 42 ], 43 "servers": [] 44 } 45 } 46 ] 47 }` 48 49 testLabelJSON = `{ 50 "label": { 51 "key": "Project:example-project-label", 52 "category": "Project", 53 "name": "example-project-label", 54 "links": { 55 "applications": [ 56 12345 57 ], 58 "servers": [] 59 } 60 } 61 }` 62 ) 63 64 func TestListLabels(t *testing.T) { 65 t.Parallel() 66 apm := newMockResponse(t, testListLabelsResponseJSON, http.StatusOK) 67 68 expected := []*Label{ 69 { 70 Key: "Project:example-project", 71 Category: "Project", 72 Name: "example-project", 73 Links: LabelLinks{ 74 Applications: []int{12345}, 75 Servers: []int{}, 76 }, 77 }, 78 } 79 80 actual, err := apm.ListLabels() 81 82 assert.NoError(t, err) 83 assert.NotNil(t, actual) 84 assert.Equal(t, expected, actual) 85 } 86 87 func TestGetLabel(t *testing.T) { 88 t.Parallel() 89 apm := newMockResponse(t, testListLabelsResponseJSON, http.StatusOK) 90 91 expected := &Label{ 92 Key: "Project:example-project", 93 Category: "Project", 94 Name: "example-project", 95 Links: LabelLinks{ 96 Applications: []int{12345}, 97 Servers: []int{}, 98 }, 99 } 100 101 actual, err := apm.GetLabel("Project:example-project") 102 103 assert.NoError(t, err) 104 assert.NotNil(t, actual) 105 assert.Equal(t, expected, actual) 106 } 107 108 func TestCreateLabel(t *testing.T) { 109 t.Parallel() 110 apm := newMockResponse(t, testLabelJSON, http.StatusOK) 111 112 label := Label{ 113 Category: "Project", 114 Name: "example-project-label", 115 Links: LabelLinks{ 116 Applications: []int{12345}, 117 Servers: []int{}, 118 }, 119 } 120 121 expected := &Label{ 122 Key: "Project:example-project-label", 123 Category: "Project", 124 Name: "example-project-label", 125 Links: LabelLinks{ 126 Applications: []int{12345}, 127 Servers: []int{}, 128 }, 129 } 130 131 actual, err := apm.CreateLabel(label) 132 133 assert.NoError(t, err) 134 assert.NotNil(t, actual) 135 assert.Equal(t, expected, actual) 136 } 137 138 func TestDeleteLabel(t *testing.T) { 139 t.Parallel() 140 apm := newMockResponse(t, testLabelJSON, http.StatusCreated) 141 142 expected := &Label{ 143 Key: "Project:example-project-label", 144 Category: "Project", 145 Name: "example-project-label", 146 Links: LabelLinks{ 147 Applications: []int{12345}, 148 Servers: []int{}, 149 }, 150 } 151 152 actual, err := apm.DeleteLabel("Project:example-project-label") 153 154 assert.NoError(t, err) 155 assert.NotNil(t, actual) 156 assert.Equal(t, expected, actual) 157 }