diff --git a/api/api.go b/api/api.go index 78b72082c..da14a951c 100644 --- a/api/api.go +++ b/api/api.go @@ -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) @@ -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) @@ -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) diff --git a/api/api_test.go b/api/api_test.go index 1c2436db0..6d06d6bd6 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -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) @@ -120,6 +131,8 @@ 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) @@ -127,10 +140,19 @@ func TestRobot(t *testing.T) { 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) @@ -138,10 +160,19 @@ func TestRobotDevices(t *testing.T) { 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) @@ -149,6 +180,13 @@ func TestRobotCommands(t *testing.T) { 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) { @@ -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, @@ -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, @@ -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) { @@ -234,10 +304,24 @@ 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) @@ -245,10 +329,19 @@ func TestRobotConnections(t *testing.T) { 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, @@ -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) { diff --git a/helpers_test.go b/helpers_test.go index 3c1fdb27c..e0f68dd4a 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -18,6 +18,7 @@ type testDriver struct { name string pin string connection Connection + Eventer Commander } @@ -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