1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/api/api_test.go

129 lines
4.6 KiB
Go
Raw Normal View History

2014-04-29 13:20:32 -07:00
package api
2014-04-15 17:59:44 -07:00
import (
"bytes"
"encoding/json"
2014-05-15 11:50:45 -07:00
"github.com/hybridgroup/gobot"
2014-04-15 17:59:44 -07:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"net/http"
"net/http/httptest"
)
var _ = Describe("Master", func() {
var (
2014-05-15 11:50:45 -07:00
m *gobot.Gobot
2014-04-26 09:21:48 -07:00
a *api
2014-04-15 17:59:44 -07:00
)
BeforeEach(func() {
2014-05-15 11:50:45 -07:00
m = gobot.NewGobot()
a = NewApi(m)
a.start = func(m *api) {}
2014-04-26 09:21:48 -07:00
2014-05-15 11:50:45 -07:00
m.Robots = []*gobot.Robot{
gobot.NewTestRobot("Robot 1"),
gobot.NewTestRobot("Robot 2"),
gobot.NewTestRobot("Robot 3"),
2014-04-15 17:59:44 -07:00
}
})
Context("when valid", func() {
It("should return all robots", func() {
request, _ := http.NewRequest("GET", "/robots", nil)
response := httptest.NewRecorder()
a.robots(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
Expect(len(i)).To(Equal(3))
})
It("should return robot", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201", nil)
response := httptest.NewRecorder()
a.robot("Robot 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
Expect(i["name"].(string)).To(Equal("Robot 1"))
})
2014-04-15 19:19:14 -07:00
It("should return all robot devices", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil)
response := httptest.NewRecorder()
a.robot_devices("Robot 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
Expect(len(i)).To(Equal(3))
})
2014-04-15 17:59:44 -07:00
It("should return robot commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.robot_commands("Robot 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
2014-04-15 19:19:14 -07:00
Expect(i).To(Equal([]string{"robotTestFunction"}))
2014-04-15 17:59:44 -07:00
})
2014-04-15 19:19:14 -07:00
It("should execute robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
2014-04-15 17:59:44 -07:00
response := httptest.NewRecorder()
2014-04-15 19:19:14 -07:00
a.executeRobotCommand("Robot 1", "robotTestFunction", response, request)
2014-04-15 17:59:44 -07:00
body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
2014-04-15 17:59:44 -07:00
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("hey Robot 1, Beep Boop"))
2014-04-15 19:19:14 -07:00
})
It("should not execute unknown robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.executeRobotCommand("Robot 1", "robotTestFunction1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
2014-04-15 19:19:14 -07:00
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("Unknown Command"))
2014-04-15 17:59:44 -07:00
})
It("should return robot device", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)
response := httptest.NewRecorder()
a.robot_device("Robot 1", "Device 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
Expect(i["name"].(string)).To(Equal("Device 1"))
})
It("should return device commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil)
response := httptest.NewRecorder()
a.robot_device_commands("Robot 1", "Device 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
2014-04-15 19:19:14 -07:00
Expect(i).To(Equal([]string{"TestDriverCommand", "DriverCommand"}))
2014-04-15 17:59:44 -07:00
})
It("should execute device command", func() {
2014-04-15 19:19:14 -07:00
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/TestDriverCommand", bytes.NewBufferString(`{"name":"human"}`))
2014-04-15 17:59:44 -07:00
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
2014-04-15 19:19:14 -07:00
a.executeCommand("Robot 1", "Device 1", "TestDriverCommand", response, request)
2014-04-15 17:59:44 -07:00
body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
2014-04-15 17:59:44 -07:00
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("hello human"))
2014-04-15 17:59:44 -07:00
})
It("should not execute unknown device command", func() {
2014-04-15 19:19:14 -07:00
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`))
2014-04-15 17:59:44 -07:00
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.executeCommand("Robot 1", "Device 1", "DriverCommand4", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []interface{}
2014-04-15 17:59:44 -07:00
json.Unmarshal(body, &i)
Expect(i[0]).To(Equal("Unknown Command"))
2014-04-15 17:59:44 -07:00
})
})
})