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

Commit

Permalink
Update alert notification and test has been added (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
quinont authored and mlclmj committed Oct 17, 2019
1 parent c88dfea commit 798a16a
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 5 deletions.
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": "[email protected]"
},
"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": "[email protected]"
},
"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": "[email protected]"
}
}
`
updatedAlertNotificationJSON = `
{
"uid": "new-alert-notification",
"name": "Team A",
"type": "email",
"isDefault": false,
"sendReminder": true,
"frequency": "15m",
"settings": {
"addresses": "[email protected]"
}
}
`
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": "[email protected]",
},
}
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": "[email protected]",
},
}

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)
}
}

0 comments on commit 798a16a

Please sign in to comment.