increase test coverage for api/api.go

This commit is contained in:
Rafael Magana 2015-03-16 09:53:05 -06:00
parent 986a3e7e68
commit e31c205749
3 changed files with 119 additions and 6 deletions

View File

@ -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 // Start initializes the api by setting up c3pio routes and robeaux
func (a *API) Start() { func (a *API) Start() {
mcpCommandRoute := "/api/commands/:command" 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" robotCommandRoute := "/api/robots/:robot/commands/:command"
a.Get("/api/commands", a.mcpCommands) a.Get("/api/commands", a.mcpCommands)
@ -118,8 +118,8 @@ func (a *API) Start() {
a.Get("/api/robots/:robot/devices/:device", a.robotDevice) 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/events/:event", a.robotDeviceEvent)
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands) a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
a.Get(deviceCommandRoute, a.executeDeviceCommand) a.Get(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
a.Post(deviceCommandRoute, a.executeDeviceCommand) a.Post(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
a.Get("/api/robots/:robot/connections", a.robotConnections) a.Get("/api/robots/:robot/connections", a.robotConnections)
a.Get("/api/robots/:robot/connections/:connection", a.robotConnection) a.Get("/api/robots/:robot/connections/:connection", a.robotConnection)
a.Get("/api/", a.mcp) a.Get("/api/", a.mcp)
@ -306,8 +306,8 @@ func (a *API) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
) )
} }
// executeDeviceCommand calls a device command asociated to requested route // executeRobotDeviceCommand calls a device command asociated to requested route
func (a *API) executeDeviceCommand(res http.ResponseWriter, req *http.Request) { func (a *API) executeRobotDeviceCommand(res http.ResponseWriter, req *http.Request) {
if _, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"), if _, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"),
req.URL.Query().Get(":device")); err != nil { req.URL.Query().Get(":device")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res) a.writeJSON(map[string]interface{}{"error": err.Error()}, res)

View File

@ -55,6 +55,17 @@ func TestRobeaux(t *testing.T) {
gobot.Assert(t, response.Code, 404) 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) { func TestMcp(t *testing.T) {
a := initTestAPI() a := initTestAPI()
request, _ := http.NewRequest("GET", "/api/", nil) request, _ := http.NewRequest("GET", "/api/", nil)
@ -120,6 +131,8 @@ func TestRobots(t *testing.T) {
func TestRobot(t *testing.T) { func TestRobot(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1", nil) request, _ := http.NewRequest("GET", "/api/robots/Robot1", nil)
response := httptest.NewRecorder() response := httptest.NewRecorder()
a.ServeHTTP(response, request) a.ServeHTTP(response, request)
@ -127,10 +140,19 @@ func TestRobot(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1") 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) { func TestRobotDevices(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1/devices", nil) request, _ := http.NewRequest("GET", "/api/robots/Robot1/devices", nil)
response := httptest.NewRecorder() response := httptest.NewRecorder()
a.ServeHTTP(response, request) a.ServeHTTP(response, request)
@ -138,10 +160,19 @@ func TestRobotDevices(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body["devices"].([]interface{})), 3) 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) { func TestRobotCommands(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1/commands", nil) request, _ := http.NewRequest("GET", "/api/robots/Robot1/commands", nil)
response := httptest.NewRecorder() response := httptest.NewRecorder()
a.ServeHTTP(response, request) a.ServeHTTP(response, request)
@ -149,6 +180,13 @@ func TestRobotCommands(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["commands"], []interface{}{"robotTestFunction"}) 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) { func TestExecuteRobotCommand(t *testing.T) {
@ -177,10 +215,23 @@ func TestExecuteRobotCommand(t *testing.T) {
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command") 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) { func TestRobotDevice(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known device
request, _ := http.NewRequest("GET", request, _ := http.NewRequest("GET",
"/api/robots/Robot1/devices/Device1", "/api/robots/Robot1/devices/Device1",
nil, nil,
@ -191,10 +242,20 @@ func TestRobotDevice(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1") 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) { func TestRobotDeviceCommands(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known device
request, _ := http.NewRequest("GET", request, _ := http.NewRequest("GET",
"/api/robots/Robot1/devices/Device1/commands", "/api/robots/Robot1/devices/Device1/commands",
nil, nil,
@ -205,6 +266,15 @@ func TestRobotDeviceCommands(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body["commands"].([]interface{})), 2) 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) { func TestExecuteRobotDeviceCommand(t *testing.T) {
@ -234,10 +304,24 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command") 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) { func TestRobotConnections(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known robot
request, _ := http.NewRequest("GET", "/api/robots/Robot1/connections", nil) request, _ := http.NewRequest("GET", "/api/robots/Robot1/connections", nil)
response := httptest.NewRecorder() response := httptest.NewRecorder()
a.ServeHTTP(response, request) a.ServeHTTP(response, request)
@ -245,10 +329,19 @@ func TestRobotConnections(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body["connections"].([]interface{})), 3) 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) { func TestRobotConnection(t *testing.T) {
a := initTestAPI() a := initTestAPI()
// known connection
request, _ := http.NewRequest("GET", request, _ := http.NewRequest("GET",
"/api/robots/Robot1/connections/Connection1", "/api/robots/Robot1/connections/Connection1",
nil, nil,
@ -259,6 +352,15 @@ func TestRobotConnection(t *testing.T) {
var body map[string]interface{} var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body) json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1") 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) { func TestAPIRouter(t *testing.T) {

View File

@ -18,6 +18,7 @@ type testDriver struct {
name string name string
pin string pin string
connection Connection connection Connection
Eventer
Commander Commander
} }
@ -35,14 +36,24 @@ func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
name: name, name: name,
connection: adaptor, connection: adaptor,
pin: pin, pin: pin,
Eventer: NewEventer(),
Commander: NewCommander(), 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 return t
} }
func (t *testDriver) DriverCommand() string {
Publish(t.Event("DriverCommand"), "DriverCommand")
return "DriverCommand"
}
type testAdaptor struct { type testAdaptor struct {
name string name string
port string port string