Skip to content

Commit

Permalink
Added unit tests for service handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianp26 committed May 8, 2017
1 parent 97af385 commit 9dcdbfc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
14 changes: 7 additions & 7 deletions queuehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const videoUUID = "e2290d14-7e80-4db8-a715-949da4de9a07"

var msgDate = time.Now().Format(dateFormat)
var lastModified = time.Now().Format(dateFormat)

func init() {
logger = newAppLogger("test")
Expand All @@ -37,7 +37,7 @@ func TestQueueConsume(t *testing.T) {
nextVideoOrigin,
"1234",
true,
newStringMappedContent(t, "c4cde316-128c-11e7-80f4-13e067d5072c", "1234"),
newStringMappedContent(t, "c4cde316-128c-11e7-80f4-13e067d5072c", "1234", lastModified),
},
{
"next-video-input.json",
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestQueueConsume(t *testing.T) {
nextVideoOrigin,
"1234",
true,
newStringMappedContent(t, "", "1234"),
newStringMappedContent(t, "", "1234", lastModified),
},
}

Expand All @@ -85,7 +85,7 @@ func TestQueueConsume(t *testing.T) {
}

msg := consumer.Message{
Headers: createHeaders(test.originSystem, test.tid),
Headers: createHeaders(test.originSystem, test.tid, lastModified),
Body: string(getBytes(test.fileName, t)),
}
h.queueConsume(msg)
Expand All @@ -97,7 +97,7 @@ func TestQueueConsume(t *testing.T) {
}
}

func createHeaders(originSystem string, requestID string) map[string]string {
func createHeaders(originSystem string, requestID string, msgDate string) map[string]string {
var result = make(map[string]string)
result["Origin-System-Id"] = originSystem
result["X-Request-Id"] = requestID
Expand Down Expand Up @@ -126,7 +126,7 @@ func getBytes(fileName string, t *testing.T) []byte {
return bytes
}

func newStringMappedContent(t *testing.T, itemUUID string, tid string) string {
func newStringMappedContent(t *testing.T, itemUUID string, tid string, msgDate string) string {
ccUUID := NewNameUUIDFromBytes([]byte(videoUUID)).String()
var items []Item
if itemUUID != "" {
Expand All @@ -144,8 +144,8 @@ func newStringMappedContent(t *testing.T, itemUUID string, tid string) string {
mc := MappedContent{
Payload: cc,
ContentURI: contentURIPrefix + ccUUID,
UUID: ccUUID,
LastModified: msgDate,
UUID: ccUUID,
}

marshalledContent, err := json.Marshal(mc)
Expand Down
73 changes: 73 additions & 0 deletions servicehandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func init() {
logger = newAppLogger("test")
}

func TestMapRequest(t *testing.T) {
h := serviceHandler{
sc: serviceConfig{},
}

assert := assert.New(t)
tests := []struct {
fileName string
expectedContent string
expectedHTTPStatus int
}{
{
"next-video-input.json",
newStringMappedContent(t, "c4cde316-128c-11e7-80f4-13e067d5072c", "", ""),
http.StatusOK,
},
{
"next-video-invalid-related-input.json",
"",
http.StatusBadRequest,
},
{
"invalid-format.json",
"",
http.StatusBadRequest,
},
}

for _, test := range tests {
fileReader := getReader(test.fileName, t)
req, _ := http.NewRequest("POST", "http://next-video-content-collection-mapper.ft.com/map", fileReader)
w := httptest.NewRecorder()

h.mapRequest(w, req)

body, err := ioutil.ReadAll(w.Body)

switch {
case err != nil:
assert.Fail(err.Error())
case test.expectedHTTPStatus != http.StatusOK:
assert.Equal(test.expectedHTTPStatus, http.StatusBadRequest, "HTTP status wrong. Input JSON: %s", test.fileName)
default:
assert.Equal(test.expectedHTTPStatus, http.StatusOK, "HTTP status wrong. Input JSON: %s", test.fileName)
assert.Equal(test.expectedContent, string(body), "Marshalled content wrong. Input JSON: %s", test.fileName)
}
}
}

func getReader(fileName string, t *testing.T) *os.File {
file, err := os.Open("test-resources/" + fileName)
if err != nil {
assert.Fail(t, err.Error())
return nil
}

return file
}

0 comments on commit 9dcdbfc

Please sign in to comment.