|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gobs/pretty" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + getAlertNotificationsJSON = ` |
| 10 | +[ |
| 11 | + { |
| 12 | + "id": 1, |
| 13 | + "uid": "team-a-email-notifier", |
| 14 | + "name": "Team A", |
| 15 | + "type": "email", |
| 16 | + "isDefault": false, |
| 17 | + "sendReminder": false, |
| 18 | + "disableResolveMessage": false, |
| 19 | + "settings": { |
| 20 | + |
| 21 | + }, |
| 22 | + "created": "2018-04-23T14:44:09+02:00", |
| 23 | + "updated": "2018-08-20T15:47:49+02:00" |
| 24 | + } |
| 25 | +] |
| 26 | + ` |
| 27 | + getAlertNotificationJSON = ` |
| 28 | +{ |
| 29 | + "id": 1, |
| 30 | + "uid": "team-a-email-notifier", |
| 31 | + "name": "Team A", |
| 32 | + "type": "email", |
| 33 | + "isDefault": false, |
| 34 | + "sendReminder": false, |
| 35 | + "disableResolveMessage": false, |
| 36 | + "settings": { |
| 37 | + |
| 38 | + }, |
| 39 | + "created": "2018-04-23T14:44:09+02:00", |
| 40 | + "updated": "2018-08-20T15:47:49+02:00" |
| 41 | +} |
| 42 | +` |
| 43 | + createdAlertNotificationJSON = ` |
| 44 | +{ |
| 45 | + "id": 1, |
| 46 | + "uid": "new-alert-notification", |
| 47 | + "name": "Team A", |
| 48 | + "type": "email", |
| 49 | + "isDefault": false, |
| 50 | + "sendReminder": true, |
| 51 | + "frequency": "15m", |
| 52 | + "settings": { |
| 53 | + |
| 54 | + } |
| 55 | +} |
| 56 | +` |
| 57 | + updatedAlertNotificationJSON = ` |
| 58 | +{ |
| 59 | + "uid": "new-alert-notification", |
| 60 | + "name": "Team A", |
| 61 | + "type": "email", |
| 62 | + "isDefault": false, |
| 63 | + "sendReminder": true, |
| 64 | + "frequency": "15m", |
| 65 | + "settings": { |
| 66 | + |
| 67 | + } |
| 68 | +} |
| 69 | +` |
| 70 | + deletedAlertNotificationJSON = ` |
| 71 | +{ |
| 72 | + "message":"Notification deleted" |
| 73 | +} |
| 74 | +` |
| 75 | +) |
| 76 | + |
| 77 | +func TestAlertNotifications(t *testing.T) { |
| 78 | + server, client := gapiTestTools(200, getAlertNotificationsJSON) |
| 79 | + defer server.Close() |
| 80 | + |
| 81 | + alertnotifications, err := client.AlertNotifications() |
| 82 | + if err != nil { |
| 83 | + t.Error(err) |
| 84 | + } |
| 85 | + |
| 86 | + t.Log(pretty.PrettyFormat(alertnotifications)) |
| 87 | + |
| 88 | + if len(alertnotifications) != 1 { |
| 89 | + t.Error("Length of returned alert notifications should be 1") |
| 90 | + } |
| 91 | + if alertnotifications[0].Id != 1 || alertnotifications[0].Name != "Team A" { |
| 92 | + t.Error("Not correctly parsing returned alert notifications.") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestAlertNotification(t *testing.T) { |
| 97 | + server, client := gapiTestTools(200, getAlertNotificationJSON) |
| 98 | + defer server.Close() |
| 99 | + |
| 100 | + alertnotification := int64(1) |
| 101 | + resp, err := client.AlertNotification(alertnotification) |
| 102 | + if err != nil { |
| 103 | + t.Error(err) |
| 104 | + } |
| 105 | + |
| 106 | + t.Log(pretty.PrettyFormat(resp)) |
| 107 | + |
| 108 | + if resp.Id != alertnotification || resp.Name != "Team A" { |
| 109 | + t.Error("Not correctly parsing returned alert notification.") |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestNewAlertNotification(t *testing.T) { |
| 114 | + server, client := gapiTestTools(200, createdAlertNotificationJSON) |
| 115 | + defer server.Close() |
| 116 | + |
| 117 | + an := &AlertNotification{ |
| 118 | + Name: "Team A", |
| 119 | + Type: "email", |
| 120 | + IsDefault: false, |
| 121 | + DisableResolveMessage: true, |
| 122 | + SendReminder: true, |
| 123 | + Frequency: "15m", |
| 124 | + Settings: map[string]string{ |
| 125 | + |
| 126 | + }, |
| 127 | + } |
| 128 | + resp, err := client.NewAlertNotification(an) |
| 129 | + if err != nil { |
| 130 | + t.Error(err) |
| 131 | + } |
| 132 | + |
| 133 | + t.Log(pretty.PrettyFormat(resp)) |
| 134 | + |
| 135 | + if resp != 1 { |
| 136 | + t.Error("Not correctly parsing returned creation message.") |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestUpdateAlertNotification(t *testing.T) { |
| 141 | + server, client := gapiTestTools(200, updatedAlertNotificationJSON) |
| 142 | + defer server.Close() |
| 143 | + |
| 144 | + an := &AlertNotification{ |
| 145 | + Id: 1, |
| 146 | + Name: "Team A", |
| 147 | + Type: "email", |
| 148 | + IsDefault: false, |
| 149 | + DisableResolveMessage: true, |
| 150 | + SendReminder: true, |
| 151 | + Frequency: "15m", |
| 152 | + Settings: map[string]string{ |
| 153 | + |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + err := client.UpdateAlertNotification(an) |
| 158 | + if err != nil { |
| 159 | + t.Error(err) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestDeleteAlertNotification(t *testing.T) { |
| 164 | + server, client := gapiTestTools(200, deletedAlertNotificationJSON) |
| 165 | + defer server.Close() |
| 166 | + |
| 167 | + err := client.DeleteAlertNotification(1) |
| 168 | + if err != nil { |
| 169 | + t.Error(err) |
| 170 | + } |
| 171 | +} |
0 commit comments