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

Update alert notification and test has been added #24

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions alertnotification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,39 @@ import (
)

type AlertNotification struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
Settings interface{} `json:"settings"`
Id int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
DisableResolveMessage bool `json:"disableResolveMessage"`
SendReminder bool `json:"sendReminder"`
Frequency string `json:"frequency"`
Settings interface{} `json:"settings"`
}

func (c *Client) AlertNotifications() ([]AlertNotification, error) {
alertnotifications := make([]AlertNotification, 0)

req, err := c.newRequest("GET", "/api/alert-notifications/", nil, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

err = json.Unmarshal(data, &alertnotifications)
return alertnotifications, err
}

func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
Expand Down
171 changes: 171 additions & 0 deletions alertnotification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package gapi

import (
"github.com/gobs/pretty"
"testing"
)

const (
getAlertNotificationsJSON = `
[
{
"id": 1,
"uid": "team-a-email-notifier",
"name": "Team A",
"type": "email",
"isDefault": false,
"sendReminder": false,
"disableResolveMessage": false,
"settings": {
"addresses": "dev@grafana.com"
},
"created": "2018-04-23T14:44:09+02:00",
"updated": "2018-08-20T15:47:49+02:00"
}
]
`
getAlertNotificationJSON = `
{
"id": 1,
"uid": "team-a-email-notifier",
"name": "Team A",
"type": "email",
"isDefault": false,
"sendReminder": false,
"disableResolveMessage": false,
"settings": {
"addresses": "dev@grafana.com"
},
"created": "2018-04-23T14:44:09+02:00",
"updated": "2018-08-20T15:47:49+02:00"
}
`
createdAlertNotificationJSON = `
{
"id": 1,
"uid": "new-alert-notification",
"name": "Team A",
"type": "email",
"isDefault": false,
"sendReminder": true,
"frequency": "15m",
"settings": {
"addresses": "dev@grafana.com"
}
}
`
updatedAlertNotificationJSON = `
{
"uid": "new-alert-notification",
"name": "Team A",
"type": "email",
"isDefault": false,
"sendReminder": true,
"frequency": "15m",
"settings": {
"addresses": "dev@grafana.com"
}
}
`
deletedAlertNotificationJSON = `
{
"message":"Notification deleted"
}
`
)

func TestAlertNotifications(t *testing.T) {
server, client := gapiTestTools(200, getAlertNotificationsJSON)
defer server.Close()

alertnotifications, err := client.AlertNotifications()
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(alertnotifications))

if len(alertnotifications) != 1 {
t.Error("Length of returned alert notifications should be 1")
}
if alertnotifications[0].Id != 1 || alertnotifications[0].Name != "Team A" {
t.Error("Not correctly parsing returned alert notifications.")
}
}

func TestAlertNotification(t *testing.T) {
server, client := gapiTestTools(200, getAlertNotificationJSON)
defer server.Close()

alertnotification := int64(1)
resp, err := client.AlertNotification(alertnotification)
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(resp))

if resp.Id != alertnotification || resp.Name != "Team A" {
t.Error("Not correctly parsing returned alert notification.")
}
}

func TestNewAlertNotification(t *testing.T) {
server, client := gapiTestTools(200, createdAlertNotificationJSON)
defer server.Close()

an := &AlertNotification{
Name: "Team A",
Type: "email",
IsDefault: false,
DisableResolveMessage: true,
SendReminder: true,
Frequency: "15m",
Settings: map[string]string{
"addresses": "dev@grafana.com",
},
}
resp, err := client.NewAlertNotification(an)
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(resp))

if resp != 1 {
t.Error("Not correctly parsing returned creation message.")
}
}

func TestUpdateAlertNotification(t *testing.T) {
server, client := gapiTestTools(200, updatedAlertNotificationJSON)
defer server.Close()

an := &AlertNotification{
Id: 1,
Name: "Team A",
Type: "email",
IsDefault: false,
DisableResolveMessage: true,
SendReminder: true,
Frequency: "15m",
Settings: map[string]string{
"addresses": "dev@grafana.com",
},
}

err := client.UpdateAlertNotification(an)
if err != nil {
t.Error(err)
}
}

func TestDeleteAlertNotification(t *testing.T) {
server, client := gapiTestTools(200, deletedAlertNotificationJSON)
defer server.Close()

err := client.DeleteAlertNotification(1)
if err != nil {
t.Error(err)
}
}