summaryrefslogtreecommitdiffstats
path: root/src/dkv/api/endpointViews_test.go
diff options
context:
space:
mode:
authorShashank Kumar Shankar <shashank.kumar.shankar@intel.com>2018-02-15 14:48:19 -0800
committerShashank Kumar Shankar <shashank.kumar.shankar@intel.com>2018-02-21 13:51:08 -0800
commit3e92cb38f9be554afccf7ac409dfbedbeec769a2 (patch)
treeb09247734194975c161987e06d7e1aa0abf8ae24 /src/dkv/api/endpointViews_test.go
parent88a7fd4b899bbd1230852be63e380ce843dec338 (diff)
Update tests and refactor code
This patch adds some more tests and refactors the codebase. Change-Id: Iee669b85c5c7f9bdd01271fe86df20506f567d23 Issue-ID: MUSIC-23 Signed-off-by: Shashank Kumar Shankar <shashank.kumar.shankar@intel.com>
Diffstat (limited to 'src/dkv/api/endpointViews_test.go')
-rw-r--r--src/dkv/api/endpointViews_test.go182
1 files changed, 168 insertions, 14 deletions
diff --git a/src/dkv/api/endpointViews_test.go b/src/dkv/api/endpointViews_test.go
index f603af4..1f7abf3 100644
--- a/src/dkv/api/endpointViews_test.go
+++ b/src/dkv/api/endpointViews_test.go
@@ -17,7 +17,8 @@
package api
import (
- //"encoding/json"
+ "bytes"
+ "encoding/json"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"net/http"
@@ -27,32 +28,185 @@ import (
func Router() *mux.Router {
router := mux.NewRouter()
- router.HandleFunc("/getconfigs", HandleGETS).Methods("GET")
router.HandleFunc("/loadconfigs", HandlePOST).Methods("POST")
+ router.HandleFunc("/getconfig/{key}", HandleGET).Methods("GET")
+ router.HandleFunc("/deleteconfig/{key}", HandleDELETE).Methods("DELETE")
+ router.HandleFunc("/getconfigs", HandleGETS).Methods("GET")
return router
}
-func TestHandlePOST(t *testing.T) {
- // TODO(sshank)
- assert.Equal(t, 0, 0, "Not passed.")
+func TestHandleGETS(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsul{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfigs", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
+}
+
+func TestHandleGETS_err(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsulErr{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfigs", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
}
func TestHandleGET(t *testing.T) {
- // TODO(sshank)
- assert.Equal(t, 0, 0, "Not passed.")
+ oldConsul := Consul
+ Consul = &FakeConsul{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfig/key1", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
}
-func TestHandleGETS(t *testing.T) {
- getkvOld := getkvs
- defer func() { getkvs = getkvOld }()
+func TestHandleGET_err(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsulErr{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfig/key1", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
+}
+
+func TestHandlePOST(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsul{}
+ KeyValues = &FakeKeyValues{}
- getkvs = func() ([]string, error) {
- return nil, nil
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{
+ Type: &TypeStruct{
+ FilePath: "default",
+ },
}
- request, _ := http.NewRequest("GET", "/getconfigs", nil)
+ b, _ := json.Marshal(body)
+
+ // json Marshal converts struct to json in Bytes. But bytes doesn't have
+ // io reader needed. So the byte is passed to NewBuffer.
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
+}
+
+func TestHandlePOST_no_body(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsul{}
+ KeyValues = &FakeKeyValues{}
+
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{}
+
+ b, _ := json.Marshal(body)
+
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
+}
+
+func TestHandlePOST_no_filepath(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsul{}
+ KeyValues = &FakeKeyValues{}
+
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{
+ Type: &TypeStruct{},
+ }
+
+ b, _ := json.Marshal(body)
+
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
+}
+
+func TestHandlePOST_ConsulError(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsulErr{}
+ KeyValues = &FakeKeyValuesErr{}
+
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{
+ Type: &TypeStruct{
+ FilePath: "default",
+ },
+ }
+
+ b, _ := json.Marshal(body)
+
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 500, response.Code, "500 response is expected")
+}
+
+func TestHandleDELETE(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsul{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("DELETE", "/deleteconfig/key1", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
+}
+
+func TestHandleDELETE_err(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsulErr{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("DELETE", "/deleteconfig/key1", nil)
response := httptest.NewRecorder()
Router().ServeHTTP(response, request)
- assert.Equal(t, 200, response.Code, "OK response is expected")
+ assert.Equal(t, 400, response.Code, "400 response is expected")
}