Skip to content

Commit

Permalink
increase test coverage for api/api.go
Browse files Browse the repository at this point in the history
  • Loading branch information
rafmagana committed Mar 16, 2015
1 parent 986a3e7 commit e31c205
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 6 deletions.
10 changes: 5 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request)) {
// Start initializes the api by setting up c3pio routes and robeaux
func (a *API) Start() {
mcpCommandRoute := "/api/commands/:command"
deviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
robotDeviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
robotCommandRoute := "/api/robots/:robot/commands/:command"

a.Get("/api/commands", a.mcpCommands)
Expand All @@ -118,8 +118,8 @@ func (a *API) Start() {
a.Get("/api/robots/:robot/devices/:device", a.robotDevice)
a.Get("/api/robots/:robot/devices/:device/events/:event", a.robotDeviceEvent)
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
a.Get(deviceCommandRoute, a.executeDeviceCommand)
a.Post(deviceCommandRoute, a.executeDeviceCommand)
a.Get(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
a.Post(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
a.Get("/api/robots/:robot/connections", a.robotConnections)
a.Get("/api/robots/:robot/connections/:connection", a.robotConnection)
a.Get("/api/", a.mcp)
Expand Down Expand Up @@ -306,8 +306,8 @@ func (a *API) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
)
}

// executeDeviceCommand calls a device command asociated to requested route
func (a *API) executeDeviceCommand(res http.ResponseWriter, req *http.Request) {
// executeRobotDeviceCommand calls a device command asociated to requested route
func (a *API) executeRobotDeviceCommand(res http.ResponseWriter, req *http.Request) {
if _, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"),
req.URL.Query().Get(":device")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
Expand Down
102 changes: 102 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ func TestRobeaux(t *testing.T) {
gobot.Assert(t, response.Code, 404)
}

func TestIndex(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()

a.ServeHTTP(response, request)

gobot.Assert(t, http.StatusMovedPermanently, response.Code)
gobot.Assert(t, "/index.html", response.HeaderMap["Location"][0])
}

func TestMcp(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/api/", nil)
Expand Down Expand Up @@ -120,35 +131,62 @@ func TestRobots(t *testing.T) {

func TestRobot(t *testing.T) {
a := initTestAPI()

// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

func TestRobotDevices(t *testing.T) {
a := initTestAPI()

// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1/devices", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body["devices"].([]interface{})), 3)

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/devices", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

func TestRobotCommands(t *testing.T) {
a := initTestAPI()

// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1/commands", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["commands"], []interface{}{"robotTestFunction"})

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/commands", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

func TestExecuteRobotCommand(t *testing.T) {
Expand Down Expand Up @@ -177,10 +215,23 @@ func TestExecuteRobotCommand(t *testing.T) {

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")

// uknown robot
request, _ = http.NewRequest("GET",
"/api/robots/UnknownRobot1/commands/robotTestFuntion1",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body.(map[string]interface{})["error"], "No Robot found with the name UnknownRobot1")
}

func TestRobotDevice(t *testing.T) {
a := initTestAPI()

// known device
request, _ := http.NewRequest("GET",
"/api/robots/Robot1/devices/Device1",
nil,
Expand All @@ -191,10 +242,20 @@ func TestRobotDevice(t *testing.T) {
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")

// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
}

func TestRobotDeviceCommands(t *testing.T) {
a := initTestAPI()

// known device
request, _ := http.NewRequest("GET",
"/api/robots/Robot1/devices/Device1/commands",
nil,
Expand All @@ -205,6 +266,15 @@ func TestRobotDeviceCommands(t *testing.T) {
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body["commands"].([]interface{})), 2)

// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1/commands",
nil,
)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
}

func TestExecuteRobotDeviceCommand(t *testing.T) {
Expand Down Expand Up @@ -234,21 +304,44 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")

// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1/commands/DriverCommand1",
bytes.NewBufferString(`{"name":"human"}`),
)
request.Header.Add("Content-Type", "application/json")
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body.(map[string]interface{})["error"], "No Device found with the name UnknownDevice1")

}

func TestRobotConnections(t *testing.T) {
a := initTestAPI()

// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1/connections", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body["connections"].([]interface{})), 3)

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/connections", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

func TestRobotConnection(t *testing.T) {
a := initTestAPI()

// known connection
request, _ := http.NewRequest("GET",
"/api/robots/Robot1/connections/Connection1",
nil,
Expand All @@ -259,6 +352,15 @@ func TestRobotConnection(t *testing.T) {
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")

// unknown connection
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/connections/UnknownConnection1",
nil,
)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["error"], "No Connection found with the name UnknownConnection1")
}

func TestAPIRouter(t *testing.T) {
Expand Down
13 changes: 12 additions & 1 deletion helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type testDriver struct {
name string
pin string
connection Connection
Eventer
Commander
}

Expand All @@ -35,14 +36,24 @@ func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
name: name,
connection: adaptor,
pin: pin,
Eventer: NewEventer(),
Commander: NewCommander(),
}

t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} { return nil })
t.AddEvent("DriverCommand")

t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
return t.DriverCommand()
})

return t
}

func (t *testDriver) DriverCommand() string {
Publish(t.Event("DriverCommand"), "DriverCommand")
return "DriverCommand"
}

type testAdaptor struct {
name string
port string
Expand Down

0 comments on commit e31c205

Please sign in to comment.