This repository has been archived by the owner on May 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers_test.go
104 lines (88 loc) · 3.85 KB
/
handlers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
const testUUID = "bba39990-c78d-3629-ae83-808c333c6dbc"
const getTopicsResponse = `[{"apiUrl":"http://localhost:8080/transformers/topics/bba39990-c78d-3629-ae83-808c333c6dbc"}]`
const getTopicByUUIDResponse = `{"uuid":"bba39990-c78d-3629-ae83-808c333c6dbc","alternativeIdentifiers":{"TME":["MTE3-U3ViamVjdHM="],"uuids":["bba39990-c78d-3629-ae83-808c333c6dbc"]},"prefLabel":"Metals Markets","type":"Topic"}`
const getTopicsCountResponse = `1`
const getTopicsIdsResponse = `{"id":"bba39990-c78d-3629-ae83-808c333c6dbc"}`
func TestHandlers(t *testing.T) {
assert := assert.New(t)
tests := []struct {
name string
req *http.Request
dummyService topicService
statusCode int
contentType string // Contents of the Content-Type header
body string
}{
{"Success - get topic by uuid", newRequest("GET", fmt.Sprintf("/transformers/topics/%s", testUUID)), &dummyService{found: true, topics: []topic{getDummyTopic(testUUID, "Metals Markets", "MTE3-U3ViamVjdHM=")}}, http.StatusOK, "application/json", getTopicByUUIDResponse},
{"Not found - get topic by uuid", newRequest("GET", fmt.Sprintf("/transformers/topics/%s", testUUID)), &dummyService{found: false, topics: []topic{topic{}}}, http.StatusNotFound, "application/json", ""},
{"Success - get topics", newRequest("GET", "/transformers/topics"), &dummyService{found: true, topics: []topic{topic{UUID: testUUID}}}, http.StatusOK, "application/json", getTopicsResponse},
{"Not found - get topics", newRequest("GET", "/transformers/topics"), &dummyService{found: false, topics: []topic{}}, http.StatusNotFound, "application/json", ""},
{"Test Topic Count", newRequest("GET", "/transformers/topics/__count"), &dummyService{found: true, topics: []topic{topic{UUID: testUUID}}}, http.StatusOK, "text/plain", getTopicsCountResponse},
{"Test Topic Ids", newRequest("GET", "/transformers/topics/__ids"), &dummyService{found: true, topics: []topic{topic{UUID: testUUID}}}, http.StatusOK, "text/plain", getTopicsIdsResponse},
}
for _, test := range tests {
rec := httptest.NewRecorder()
router(test.dummyService).ServeHTTP(rec, test.req)
assert.True(test.statusCode == rec.Code, fmt.Sprintf("%s: Wrong response code, was %d, should be %d", test.name, rec.Code, test.statusCode))
assert.Equal(strings.TrimSpace(test.body), strings.TrimSpace(rec.Body.String()), fmt.Sprintf("%s: Wrong body", test.name))
}
}
func newRequest(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
return req
}
func router(s topicService) *mux.Router {
m := mux.NewRouter()
h := newTopicsHandler(s)
m.HandleFunc("/transformers/topics", h.getTopics).Methods("GET")
m.HandleFunc("/transformers/topics/__count", h.getCount).Methods("GET")
m.HandleFunc("/transformers/topics/__ids", h.getIds).Methods("GET")
m.HandleFunc("/transformers/topics/__reload", h.getIds).Methods("GET")
m.HandleFunc("/transformers/topics/{uuid}", h.getTopicByUUID).Methods("GET")
return m
}
type dummyService struct {
found bool
topics []topic
}
func (s *dummyService) getTopics() ([]topicLink, bool) {
var topicLinks []topicLink
for _, sub := range s.topics {
topicLinks = append(topicLinks, topicLink{APIURL: "http://localhost:8080/transformers/topics/" + sub.UUID})
}
return topicLinks, s.found
}
func (s *dummyService) getTopicByUUID(uuid string) (topic, bool) {
return s.topics[0], s.found
}
func (s *dummyService) checkConnectivity() error {
return nil
}
func (s *dummyService) getTopicCount() int {
return len(s.topics)
}
func (s *dummyService) getTopicIds() []string {
i := 0
keys := make([]string, len(s.topics))
for _, t := range s.topics {
keys[i] = t.UUID
i++
}
return keys
}
func (s *dummyService) reload() error {
return nil
}