forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
236 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package gobot | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/go-martini/martini" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
) | ||
|
||
var _ = Describe("Master", func() { | ||
var ( | ||
myMaster *Master | ||
a *api | ||
) | ||
|
||
BeforeEach(func() { | ||
myMaster = GobotMaster() | ||
startApi = func(m *martini.ClassicMartini) {} | ||
a = Api(myMaster) | ||
myMaster.Robots = []*Robot{ | ||
newTestRobot("Robot 1"), | ||
newTestRobot("Robot 2"), | ||
newTestRobot("Robot 3"), | ||
} | ||
trap = func(c chan os.Signal) { | ||
c <- os.Interrupt | ||
} | ||
myMaster.Start() | ||
}) | ||
|
||
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")) | ||
}) | ||
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) | ||
Expect(i).To(Equal([]string{"Command1", "Command2"})) | ||
}) | ||
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)) | ||
}) | ||
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) | ||
Expect(i).To(Equal([]string{"DriverCommand1", "DriverCommand2", "DriverCommand3"})) | ||
}) | ||
It("should execute device command", func() { | ||
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/Command1", bytes.NewBufferString(`{"name":"human"}`)) | ||
request.Header.Add("Content-Type", "application/json") | ||
response := httptest.NewRecorder() | ||
a.executeCommand("Robot 1", "Device 1", "DriverCommand1", response, request) | ||
body, _ := ioutil.ReadAll(response.Body) | ||
var i map[string]interface{} | ||
json.Unmarshal(body, &i) | ||
Expect(i["result"]).To(Equal("hello human")) | ||
}) | ||
It("should not execute unknown device command", func() { | ||
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/Command1", bytes.NewBufferString(`{"name":"human"}`)) | ||
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 map[string]interface{} | ||
json.Unmarshal(body, &i) | ||
Expect(i["result"]).To(Equal("Unknown Command")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.