Skip to content

Commit

Permalink
feat(agents): GetAgents client func and more tests (#13)
Browse files Browse the repository at this point in the history
* feat(agents): `GetAgents` client func and more tests
  • Loading branch information
William Fleming authored Feb 17, 2020
1 parent c6eca40 commit 0d6a7e9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
15 changes: 15 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package thousandeyes

import "fmt"

type Agents []Agent

type Agent struct {
Expand Down Expand Up @@ -43,3 +45,16 @@ type AgentErrorDetails struct {
Code string `json:"code,omitempty"`
Description string `json:"description,omitempty"`
}

func (c *Client) GetAgents() (*Agents, error) {
resp, err := c.get("/agents")
if err != nil {
return &Agents{}, err
}
var target map[string]Agents
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
agents := target["agents"]
return &agents, nil
}
44 changes: 44 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package thousandeyes

import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestClient_GetAgents(t *testing.T) {
out := `{"agents":[{"agentId": 1}, {"agentId": 2}]}`
setup()
var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/agents.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 := Agents{
Agent{
AgentId: 1,
},
Agent{
AgentId: 2,
},
}
res, err := client.GetAgents()
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}

func TestClient_GetAgentsError(t *testing.T) {
setup()
var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/agents.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetAgents()
teardown()
assert.Error(t, err)
}
13 changes: 13 additions & 0 deletions alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ func TestClient_GetAlertRule(t *testing.T) {
assert.Equal(t, &expected, res)
}

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) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetAlertRule(1)
teardown()
assert.Error(t, err)
}

func TestClient_DeleteAlertRule(t *testing.T) {
setup()
defer teardown()
Expand Down
13 changes: 13 additions & 0 deletions http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,16 @@ func TestHttpServer_AddAgent(t *testing.T) {
test.AddAgent(1)
assert.Equal(t, expected, test)
}

func TestClient_GetHttpServerError(t *testing.T) {
setup()
var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/http-server/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetHttpServer(1)
teardown()
assert.Error(t, err)
}
13 changes: 13 additions & 0 deletions page_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,16 @@ func TestPageLoad_AddAgent(t *testing.T) {
test.AddAgent(1)
assert.Equal(t, expected, test)
}

func TestClient_GetPageLoadError(t *testing.T) {
setup()
var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/page-load/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetPageLoad(1)
teardown()
assert.Error(t, err)
}
13 changes: 13 additions & 0 deletions web_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,16 @@ func TestClient_UpdateWebTransaction(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}

func TestClient_GetWebTransactionError(t *testing.T) {
setup()
var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/web-transactions/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})

_, err := client.GetWebTransaction(1)
teardown()
assert.Error(t, err)
}

0 comments on commit 0d6a7e9

Please sign in to comment.