github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/policies_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package alerts 5 6 import ( 7 "net/http" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/newrelic/newrelic-client-go/internal/serialization" 14 ) 15 16 var ( 17 testTimestampStringMs = "1575438237690" 18 testTimestamp = serialization.EpochTime(time.Unix(0, 1575438237690*int64(time.Millisecond)).UTC()) 19 20 testPoliciesResponseJSON = `{ 21 "policies": [ 22 { 23 "id": 579506, 24 "incident_preference": "PER_POLICY", 25 "name": "test-alert-policy-1", 26 "created_at": ` + testTimestampStringMs + `, 27 "updated_at": ` + testTimestampStringMs + ` 28 }, 29 { 30 "id": 579509, 31 "incident_preference": "PER_POLICY", 32 "name": "test-alert-policy-2", 33 "created_at": ` + testTimestampStringMs + `, 34 "updated_at": ` + testTimestampStringMs + ` 35 } 36 ] 37 }` 38 39 testPolicyResponseJSON = `{ 40 "policy": { 41 "id": 579506, 42 "incident_preference": "PER_POLICY", 43 "name": "test-alert-policy-1", 44 "created_at": ` + testTimestampStringMs + `, 45 "updated_at": ` + testTimestampStringMs + ` 46 } 47 }` 48 49 testPolicyResponseUpdatedJSON = `{ 50 "policy": { 51 "id": 579506, 52 "incident_preference": "PER_CONDITION", 53 "name": "test-alert-policy-updated", 54 "created_at": ` + testTimestampStringMs + `, 55 "updated_at": ` + testTimestampStringMs + ` 56 } 57 }` 58 ) 59 60 func TestGetPolicy(t *testing.T) { 61 t.Parallel() 62 alerts := newMockResponse(t, testPoliciesResponseJSON, http.StatusOK) 63 64 expected := &Policy{ 65 ID: 579506, 66 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 67 Name: "test-alert-policy-1", 68 CreatedAt: &testTimestamp, 69 UpdatedAt: &testTimestamp, 70 } 71 72 actual, err := alerts.GetPolicy(579506) 73 74 require.NoError(t, err) 75 require.NotNil(t, actual) 76 require.Equal(t, expected, actual) 77 } 78 79 func TestListPolicies(t *testing.T) { 80 t.Parallel() 81 alerts := newMockResponse(t, testPoliciesResponseJSON, http.StatusOK) 82 83 expected := []Policy{ 84 { 85 ID: 579506, 86 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 87 Name: "test-alert-policy-1", 88 CreatedAt: &testTimestamp, 89 UpdatedAt: &testTimestamp, 90 }, 91 { 92 ID: 579509, 93 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 94 Name: "test-alert-policy-2", 95 CreatedAt: &testTimestamp, 96 UpdatedAt: &testTimestamp, 97 }, 98 } 99 100 actual, err := alerts.ListPolicies(nil) 101 102 require.NoError(t, err) 103 require.NotNil(t, actual) 104 require.Equal(t, expected, actual) 105 } 106 107 func TestListPoliciesWithParams(t *testing.T) { 108 t.Parallel() 109 expectedName := "does-not-exist" 110 111 alerts := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 112 values := r.URL.Query() 113 114 name := values.Get("filter[name]") 115 require.Equal(t, expectedName, name) 116 117 w.Header().Set("Content-Type", "application/json") 118 _, err := w.Write([]byte(`{ "policies": [] }`)) 119 120 require.NoError(t, err) 121 })) 122 123 params := ListPoliciesParams{ 124 Name: expectedName, 125 } 126 127 expectedCount := 0 128 129 actual, err := alerts.ListPolicies(¶ms) 130 131 require.NoError(t, err) 132 require.NotNil(t, actual) 133 require.Equal(t, expectedCount, len(actual)) 134 } 135 136 func TestCreatePolicy(t *testing.T) { 137 t.Parallel() 138 alerts := newMockResponse(t, testPolicyResponseJSON, http.StatusOK) 139 140 policy := Policy{ 141 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 142 Name: "test-alert-policy-1", 143 } 144 145 expected := &Policy{ 146 ID: 579506, 147 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 148 Name: "test-alert-policy-1", 149 CreatedAt: &testTimestamp, 150 UpdatedAt: &testTimestamp, 151 } 152 153 actual, err := alerts.CreatePolicy(policy) 154 155 require.NoError(t, err) 156 require.NotNil(t, actual) 157 require.Equal(t, expected, actual) 158 } 159 160 func TestUpdatePolicy(t *testing.T) { 161 t.Parallel() 162 alerts := newMockResponse(t, testPolicyResponseUpdatedJSON, http.StatusOK) 163 164 policy := Policy{ 165 ID: 579506, 166 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 167 Name: "test-alert-policy-1", 168 } 169 170 expected := &Policy{ 171 ID: 579506, 172 IncidentPreference: IncidentPreferenceTypes.PerCondition, 173 Name: "test-alert-policy-updated", 174 CreatedAt: &testTimestamp, 175 UpdatedAt: &testTimestamp, 176 } 177 178 actual, err := alerts.UpdatePolicy(policy) 179 180 require.NoError(t, err) 181 require.NotNil(t, actual) 182 require.Equal(t, expected, actual) 183 } 184 185 func TestDeletePolicy(t *testing.T) { 186 t.Parallel() 187 alerts := newMockResponse(t, testPolicyResponseJSON, http.StatusOK) 188 189 expected := &Policy{ 190 ID: 579506, 191 IncidentPreference: IncidentPreferenceTypes.PerPolicy, 192 Name: "test-alert-policy-1", 193 CreatedAt: &testTimestamp, 194 UpdatedAt: &testTimestamp, 195 } 196 197 actual, err := alerts.DeletePolicy(579506) 198 199 require.NoError(t, err) 200 require.NotNil(t, actual) 201 require.Equal(t, expected, actual) 202 }