github.com/newrelic/newrelic-client-go@v1.1.0/pkg/synthetics/monitors_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package synthetics 5 6 import ( 7 "fmt" 8 "net/http" 9 "testing" 10 "time" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 var ( 16 testMonitorID = "72733a02-9701-4279-8ac3-8f6281a5a1a9" 17 testTime, _ = time.Parse(syntheticsTimeFormat, "2019-11-27T19:11:05.076+0000") 18 testTimestamp = Time(testTime) 19 20 testMonitorOptions = MonitorOptions{ 21 ValidationString: "", 22 VerifySSL: false, 23 BypassHEADRequest: false, 24 TreatRedirectAsFailure: false, 25 } 26 27 testMonitor = Monitor{ 28 ID: testMonitorID, 29 Name: "test-synthetics-monitor", 30 Type: MonitorTypes.Ping, 31 Frequency: 15, 32 URI: "https://google.com", 33 Locations: []string{"AWS_US_EAST_1"}, 34 Status: MonitorStatus.Disabled, 35 SLAThreshold: 7, 36 UserID: 0, 37 APIVersion: "LATEST", 38 ModifiedAt: &testTimestamp, 39 CreatedAt: &testTimestamp, 40 Options: testMonitorOptions, 41 } 42 43 testMonitorJson = ` 44 { 45 "id": "72733a02-9701-4279-8ac3-8f6281a5a1a9", 46 "name": "test-synthetics-monitor", 47 "type": "SIMPLE", 48 "frequency": 15, 49 "uri": "https://google.com", 50 "locations": [ 51 "AWS_US_EAST_1" 52 ], 53 "status": "DISABLED", 54 "slaThreshold": 7, 55 "options": { 56 57 }, 58 "modifiedAt": "2019-11-27T19:11:05.076+0000", 59 "createdAt": "2019-11-27T19:11:05.076+0000", 60 "userId": 0, 61 "apiVersion": "LATEST" 62 } 63 ` 64 ) 65 66 func TestListMonitors(t *testing.T) { 67 t.Parallel() 68 respJSON := fmt.Sprintf(`{ "monitors": [%s] }`, testMonitorJson) 69 synthetics := newMockResponse(t, respJSON, http.StatusOK) 70 71 expected := []*Monitor{ 72 &testMonitor, 73 } 74 75 actual, err := synthetics.ListMonitors() 76 77 assert.NoError(t, err) 78 assert.NotNil(t, actual) 79 assert.Equal(t, expected, actual) 80 } 81 82 func TestGetMonitor(t *testing.T) { 83 t.Parallel() 84 synthetics := newMockResponse(t, testMonitorJson, http.StatusOK) 85 86 actual, err := synthetics.GetMonitor(testMonitorID) 87 88 assert.NoError(t, err) 89 assert.NotNil(t, actual) 90 assert.Equal(t, &testMonitor, actual) 91 } 92 93 func TestCreateMonitor(t *testing.T) { 94 t.Parallel() 95 synthetics := newMockResponse(t, testMonitorJson, http.StatusOK) 96 97 result, err := synthetics.CreateMonitor(testMonitor) 98 99 assert.NoError(t, err) 100 assert.NotNil(t, result) 101 } 102 103 func TestUpdateMonitor(t *testing.T) { 104 t.Parallel() 105 synthetics := newMockResponse(t, testMonitorJson, http.StatusOK) 106 107 result, err := synthetics.UpdateMonitor(testMonitor) 108 109 assert.NoError(t, err) 110 assert.NotNil(t, result) 111 } 112 113 func TestDeleteMonitor(t *testing.T) { 114 t.Parallel() 115 synthetics := newMockResponse(t, testMonitorJson, http.StatusOK) 116 117 err := synthetics.DeleteMonitor(testMonitor.ID) 118 119 assert.NoError(t, err) 120 }