Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 798a16a

Browse files
quinontmlclmj
authored andcommitted
Update alert notification and test has been added (#24)
1 parent c88dfea commit 798a16a

File tree

2 files changed

+204
-5
lines changed

2 files changed

+204
-5
lines changed

alertnotification.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,39 @@ import (
99
)
1010

1111
type AlertNotification struct {
12-
Id int64 `json:"id,omitempty"`
13-
Name string `json:"name"`
14-
Type string `json:"type"`
15-
IsDefault bool `json:"isDefault"`
16-
Settings interface{} `json:"settings"`
12+
Id int64 `json:"id,omitempty"`
13+
Name string `json:"name"`
14+
Type string `json:"type"`
15+
IsDefault bool `json:"isDefault"`
16+
DisableResolveMessage bool `json:"disableResolveMessage"`
17+
SendReminder bool `json:"sendReminder"`
18+
Frequency string `json:"frequency"`
19+
Settings interface{} `json:"settings"`
20+
}
21+
22+
func (c *Client) AlertNotifications() ([]AlertNotification, error) {
23+
alertnotifications := make([]AlertNotification, 0)
24+
25+
req, err := c.newRequest("GET", "/api/alert-notifications/", nil, nil)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
resp, err := c.Do(req)
31+
if err != nil {
32+
return nil, err
33+
}
34+
if resp.StatusCode != 200 {
35+
return nil, errors.New(resp.Status)
36+
}
37+
38+
data, err := ioutil.ReadAll(resp.Body)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
err = json.Unmarshal(data, &alertnotifications)
44+
return alertnotifications, err
1745
}
1846

1947
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {

alertnotification_test.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
"addresses": "[email protected]"
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+
"addresses": "[email protected]"
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+
"addresses": "[email protected]"
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+
"addresses": "[email protected]"
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+
"addresses": "[email protected]",
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+
"addresses": "[email protected]",
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

Comments
 (0)