Skip to content

Commit

Permalink
fix(alert): remove the getAlertRule method
Browse files Browse the repository at this point in the history
  • Loading branch information
johntdyer authored Apr 8, 2020
1 parent f0ccc49 commit a02a469
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
24 changes: 13 additions & 11 deletions alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import (
)

func TestClient_GetAlertRule(t *testing.T) {
out := `{"RuleID":1, "ruleName": "test", "roundsViolatingOutOf": 1, "roundsViolatingRequired": 1}`
out := `{"alertRules" : [ {"RuleID":1, "ruleName": "test" }]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/1.json", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.Write([]byte(out))
})

// Define expected values from the API (based on the JSON we print out above)
expected := AlertRule{RuleID: 1, RuleName: "test", RoundsViolatingOutOf: 1, RoundsViolatingRequired: 1}
expected := AlertRules{
AlertRule{RuleID: 1, RuleName: "test"},
}

res, err := client.GetAlertRule(1)
res, err := client.GetAlertRules()
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
Expand All @@ -28,12 +30,12 @@ func TestClient_GetAlertRule(t *testing.T) {
func TestClient_GetAlertError(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/1.json", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetAlertRule(1)
_, err := client.GetAlertRules()
teardown()
assert.Error(t, err)
}
Expand Down Expand Up @@ -97,26 +99,26 @@ func TestClient_AlertJsonError(t *testing.T) {
out := `{"alertRules": [test]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/1.json", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte(out))
})
_, err := client.GetAlertRule(1)
_, err := client.GetAlertRules()
assert.Error(t, err)
assert.EqualError(t, err, "could not decode JSON response: invalid character 'e' in literal true (expecting 'r')")
}

func TestClient_GetAlertStatusCode(t *testing.T) {
setup()
out := `{"test":[{"testId":1,"testName":"test123"}]}`
out := `{"alertRules":[{"ruleId":1,"ruleName":"test123"}]}`
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/1.json", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(out))
})

_, err := client.GetAlertRule(1)
_, err := client.GetAlertRules()
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}
Expand Down
16 changes: 10 additions & 6 deletions alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AlertRules []AlertRule

// AlertRule - An alert rule
type AlertRule struct {
RuleID int `json:"RuleID,omitempty"`
RuleID int `json:"ruleId,omitempty"`
RuleName string `json:"ruleName,omitempty"`
Expression string `json:"expression,omitempty"`
Direction string `json:"direction,omitempty"`
Expand Down Expand Up @@ -61,20 +61,24 @@ func (c Client) CreateAlertRule(a AlertRule) (*AlertRule, error) {
return &target, nil
}

//GetAlertRule - Get alert rule
func (c Client) GetAlertRule(id int) (*AlertRule, error) {
resp, err := c.get(fmt.Sprintf("/alert-rules/%d", id))
//GetAlertRules - Get alert rules
func (c Client) GetAlertRules() (*AlertRules, error) {
resp, err := c.get(fmt.Sprintf("/alert-rules"))
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get alert rule, response code %d", resp.StatusCode)
}
var target AlertRule

var target map[string]AlertRules

if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
return &target, nil
alertRules := target["alertRules"]

return &alertRules, nil
}

//DeleteAlertRule - delete alert rule
Expand Down

0 comments on commit a02a469

Please sign in to comment.