Skip to content

Commit

Permalink
Added unit tests for queue handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianp26 committed May 8, 2017
1 parent 900e603 commit 97af385
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
157 changes: 157 additions & 0 deletions queuehandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package main

import (
"encoding/json"
"github.com/Financial-Times/message-queue-go-producer/producer"
"github.com/Financial-Times/message-queue-gonsumer/consumer"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
"time"
)

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

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

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

type mockMessageProducer struct {
message string
sendCalled bool
}

func TestQueueConsume(t *testing.T) {
assert := assert.New(t)
tests := []struct {
fileName string
originSystem string
tid string
expectedMsgSent bool
expectedContent string
}{
{
"next-video-input.json",
nextVideoOrigin,
"1234",
true,
newStringMappedContent(t, "c4cde316-128c-11e7-80f4-13e067d5072c", "1234"),
},
{
"next-video-input.json",
"other",
"1234",
false,
"",
},
{
"next-video-input.json",
nextVideoOrigin,
"",
false,
"",
},
{
"invalid-format.json",
nextVideoOrigin,
"1234",
false,
"",
},
{
"next-video-invalid-related-input.json",
nextVideoOrigin,
"1234",
false,
"",
},
{
"next-video-empty-related-input.json",
nextVideoOrigin,
"1234",
true,
newStringMappedContent(t, "", "1234"),
},
}

for _, test := range tests {
mockMsgProducer := mockMessageProducer{}
var msgProducer = &mockMsgProducer
h := queueHandler{
sc: serviceConfig{},
messageProducer: msgProducer,
}

msg := consumer.Message{
Headers: createHeaders(test.originSystem, test.tid),
Body: string(getBytes(test.fileName, t)),
}
h.queueConsume(msg)

assert.Equal(test.expectedMsgSent, mockMsgProducer.sendCalled,
"Message sending check is wrong. Input JSON file: %s, Origin-System-Id: %s, X-Request-Id: %s", test.fileName, test.originSystem, test.tid)
assert.Equal(test.expectedContent, mockMsgProducer.message,
"Marshalled content wrong. Input JSON file: %s, Origin-System-Id: %s, X-Request-Id: %s", test.fileName, test.originSystem, test.tid)
}
}

func createHeaders(originSystem string, requestID string) map[string]string {
var result = make(map[string]string)
result["Origin-System-Id"] = originSystem
result["X-Request-Id"] = requestID
result["Message-Timestamp"] = msgDate
return result
}

func (mock *mockMessageProducer) SendMessage(uuid string, message producer.Message) error {
mock.message = message.Body
mock.sendCalled = true
return nil
}

func (mock *mockMessageProducer) ConnectivityCheck() (string, error) {
// do nothing
return "", nil
}

func getBytes(fileName string, t *testing.T) []byte {
bytes, err := ioutil.ReadFile("test-resources/" + fileName)
if err != nil {
assert.Fail(t, err.Error())
return nil
}

return bytes
}

func newStringMappedContent(t *testing.T, itemUUID string, tid string) string {
ccUUID := NewNameUUIDFromBytes([]byte(videoUUID)).String()
var items []Item
if itemUUID != "" {
items = []Item{{itemUUID}}
}

cc := ContentCollection{
UUID: ccUUID,
Items: items,
PublishReference: tid,
LastModified: msgDate,
CollectionType: collectionType,
}

mc := MappedContent{
Payload: cc,
ContentURI: contentURIPrefix + ccUUID,
UUID: ccUUID,
LastModified: msgDate,
}

marshalledContent, err := json.Marshal(mc)
if err != nil {
assert.Fail(t, err.Error())
}
return string(marshalledContent)

}
1 change: 1 addition & 0 deletions test-resources/invalid-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid content
1 change: 1 addition & 0 deletions test-resources/next-video-delete-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"deleted": true,"lastModified": "2017-04-04T14:42:58.920Z","publishReference": "tid_bycjmmcj4r","type": "video","id": "e2290d14-7e80-4db8-a715-949da4de9a07"}
1 change: 1 addition & 0 deletions test-resources/next-video-empty-related-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_id":"58d8d6cc789d4c000f6b0169","updatedAt":"2017-04-03T16:30:11.106Z","createdAt":"2017-03-27T09:09:32.541Z","mioId":762380,"title":"Trump trade under scrutiny","createdBy":"seb.morton-clark","encoding":{"job":358759376,"status":"COMPLETE","outputs":[{"audioCodec":"mp3","duration":65904,"mediaType":"audio/mpeg","url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":360,"width":640,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":720,"width":1280,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4"}]},"__v":0,"byline":"Filmed by Niclola Stansfield. Produced by Seb Morton-Clark.","description":"Global equities are on the defensive, led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The FT's Mike Mackenzie reports.","image":"https://api.ft.com/content/ffc60243-2b77-439a-a6c9-0f3603ee5f83","standfirst":"Mike Mackenzie provides analysis of the morning's market news","updatedBy":"seb.morton-clark","isPublished":false,"related":[],"annotations":[{"id":"http://api.ft.com/things/71a5efa5-e6e0-3ce1-9190-a7eac8bef325","predicate":"http://www.ft.com/ontology/classification/isClassifiedBy"}],"encodings":[{"mioId":762380,"name":"Trump trade under scrutiny","primary":true,"status":"COMPLETE","job":358759376,"outputs":[{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3","mediaType":"audio/mpeg","duration":65904,"audioCodec":"mp3"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4","width":640,"height":360,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4","width":1280,"height":720,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"}]}],"canBeSyndicated":true,"transcription":{"status":"COMPLETE","job":"1579674","transcript":"<p>Here's what we're watching with trading underway in London. Global equities under pressure led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The dollar is weaker. Havens like yen, gold, and government bonds finding buyers. </p><p>As the dust settles over the failure to replace Obamacare, focus now on whether tax reform and other fiscal measures will eventuate. This is where the rubber meets the road for the Trump trade. High flying equity markets had been underpinned by the promise of big tax cuts and fiscal stimulus. And Wall Street is souring. </p><p>One big beneficiary of lower corporate taxes under Trump are small caps. They are now down 2 and 1/2% for the year. While the sector is still much higher since November, this is a key market barometer of prospects for the Trump trade. </p><p>Now while many still think some measure of tax reform or spending will eventuate, markets are very wary, namely of the risk that Congress and the Trump administration fail to reach agreement on legislation, that unlike health care reform, matters a great deal more to investors. </p><p>[MUSIC PLAYING] </p>","captions":[{"format":"vtt","url":"https://next-video-editor.ft.com/e2290d14-7e80-4db8-a715-949da4de9a07.vtt","mediaType":"text/vtt"}]},"format":[],"type":"video","id":"e2290d14-7e80-4db8-a715-949da4de9a07"}
1 change: 1 addition & 0 deletions test-resources/next-video-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_id":"58d8d6cc789d4c000f6b0169","updatedAt":"2017-04-03T16:30:11.106Z","createdAt":"2017-03-27T09:09:32.541Z","mioId":762380,"title":"Trump trade under scrutiny","createdBy":"seb.morton-clark","encoding":{"job":358759376,"status":"COMPLETE","outputs":[{"audioCodec":"mp3","duration":65904,"mediaType":"audio/mpeg","url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":360,"width":640,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":720,"width":1280,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4"}]},"__v":0,"byline":"Filmed by Niclola Stansfield. Produced by Seb Morton-Clark.","description":"Global equities are on the defensive, led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The FT's Mike Mackenzie reports.","image":"https://api.ft.com/content/ffc60243-2b77-439a-a6c9-0f3603ee5f83","standfirst":"Mike Mackenzie provides analysis of the morning's market news","updatedBy":"seb.morton-clark","isPublished":false,"related":[{"id":"c4cde316-128c-11e7-80f4-13e067d5072c","title":"Stocks and dollar slide as ‘Trump trade’ fades"}],"annotations":[{"id":"http://api.ft.com/things/71a5efa5-e6e0-3ce1-9190-a7eac8bef325","predicate":"http://www.ft.com/ontology/classification/isClassifiedBy"}],"encodings":[{"mioId":762380,"name":"Trump trade under scrutiny","primary":true,"status":"COMPLETE","job":358759376,"outputs":[{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3","mediaType":"audio/mpeg","duration":65904,"audioCodec":"mp3"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4","width":640,"height":360,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4","width":1280,"height":720,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"}]}],"canBeSyndicated":true,"transcription":{"status":"COMPLETE","job":"1579674","transcript":"<p>Here's what we're watching with trading underway in London. Global equities under pressure led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The dollar is weaker. Havens like yen, gold, and government bonds finding buyers. </p><p>As the dust settles over the failure to replace Obamacare, focus now on whether tax reform and other fiscal measures will eventuate. This is where the rubber meets the road for the Trump trade. High flying equity markets had been underpinned by the promise of big tax cuts and fiscal stimulus. And Wall Street is souring. </p><p>One big beneficiary of lower corporate taxes under Trump are small caps. They are now down 2 and 1/2% for the year. While the sector is still much higher since November, this is a key market barometer of prospects for the Trump trade. </p><p>Now while many still think some measure of tax reform or spending will eventuate, markets are very wary, namely of the risk that Congress and the Trump administration fail to reach agreement on legislation, that unlike health care reform, matters a great deal more to investors. </p><p>[MUSIC PLAYING] </p>","captions":[{"format":"vtt","url":"https://next-video-editor.ft.com/e2290d14-7e80-4db8-a715-949da4de9a07.vtt","mediaType":"text/vtt"}]},"format":[],"type":"video","id":"e2290d14-7e80-4db8-a715-949da4de9a07"}
1 change: 1 addition & 0 deletions test-resources/next-video-invalid-related-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_id":"58d8d6cc789d4c000f6b0169","updatedAt":"2017-04-03T16:30:11.106Z","createdAt":"2017-03-27T09:09:32.541Z","mioId":762380,"title":"Trump trade under scrutiny","createdBy":"seb.morton-clark","encoding":{"job":358759376,"status":"COMPLETE","outputs":[{"audioCodec":"mp3","duration":65904,"mediaType":"audio/mpeg","url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":360,"width":640,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":720,"width":1280,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4"}]},"__v":0,"byline":"Filmed by Niclola Stansfield. Produced by Seb Morton-Clark.","description":"Global equities are on the defensive, led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The FT's Mike Mackenzie reports.","image":"https://api.ft.com/content/ffc60243-2b77-439a-a6c9-0f3603ee5f83","standfirst":"Mike Mackenzie provides analysis of the morning's market news","updatedBy":"seb.morton-clark","isPublished":false,"related":["test"],"annotations":[{"id":"http://api.ft.com/things/71a5efa5-e6e0-3ce1-9190-a7eac8bef325","predicate":"http://www.ft.com/ontology/classification/isClassifiedBy"}],"encodings":[{"mioId":762380,"name":"Trump trade under scrutiny","primary":true,"status":"COMPLETE","job":358759376,"outputs":[{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3","mediaType":"audio/mpeg","duration":65904,"audioCodec":"mp3"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4","width":640,"height":360,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4","width":1280,"height":720,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"}]}],"canBeSyndicated":true,"transcription":{"status":"COMPLETE","job":"1579674","transcript":"<p>Here's what we're watching with trading underway in London. Global equities under pressure led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The dollar is weaker. Havens like yen, gold, and government bonds finding buyers. </p><p>As the dust settles over the failure to replace Obamacare, focus now on whether tax reform and other fiscal measures will eventuate. This is where the rubber meets the road for the Trump trade. High flying equity markets had been underpinned by the promise of big tax cuts and fiscal stimulus. And Wall Street is souring. </p><p>One big beneficiary of lower corporate taxes under Trump are small caps. They are now down 2 and 1/2% for the year. While the sector is still much higher since November, this is a key market barometer of prospects for the Trump trade. </p><p>Now while many still think some measure of tax reform or spending will eventuate, markets are very wary, namely of the risk that Congress and the Trump administration fail to reach agreement on legislation, that unlike health care reform, matters a great deal more to investors. </p><p>[MUSIC PLAYING] </p>","captions":[{"format":"vtt","url":"https://next-video-editor.ft.com/e2290d14-7e80-4db8-a715-949da4de9a07.vtt","mediaType":"text/vtt"}]},"format":[],"type":"video","id":"e2290d14-7e80-4db8-a715-949da4de9a07"}
1 change: 1 addition & 0 deletions test-resources/next-video-no-related-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_id":"58d8d6cc789d4c000f6b0169","updatedAt":"2017-04-03T16:30:11.106Z","createdAt":"2017-03-27T09:09:32.541Z","mioId":762380,"title":"Trump trade under scrutiny","createdBy":"seb.morton-clark","encoding":{"job":358759376,"status":"COMPLETE","outputs":[{"audioCodec":"mp3","duration":65904,"mediaType":"audio/mpeg","url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":360,"width":640,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4"},{"audioCodec":"aac","videoCodec":"h264","duration":65940,"mediaType":"video/mp4","height":720,"width":1280,"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4"}]},"__v":0,"byline":"Filmed by Niclola Stansfield. Produced by Seb Morton-Clark.","description":"Global equities are on the defensive, led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The FT's Mike Mackenzie reports.","image":"https://api.ft.com/content/ffc60243-2b77-439a-a6c9-0f3603ee5f83","standfirst":"Mike Mackenzie provides analysis of the morning's market news","updatedBy":"seb.morton-clark","isPublished":false,"annotations":[{"id":"http://api.ft.com/things/71a5efa5-e6e0-3ce1-9190-a7eac8bef325","predicate":"http://www.ft.com/ontology/classification/isClassifiedBy"}],"encodings":[{"mioId":762380,"name":"Trump trade under scrutiny","primary":true,"status":"COMPLETE","job":358759376,"outputs":[{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/0x0.mp3","mediaType":"audio/mpeg","duration":65904,"audioCodec":"mp3"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/640x360.mp4","width":640,"height":360,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"},{"url":"http://ftvideo.prod.zencoder.outputs.s3.amazonaws.com/e2290d14-7e80-4db8-a715-949da4de9a07/1280x720.mp4","width":1280,"height":720,"mediaType":"video/mp4","duration":65940,"videoCodec":"h264","audioCodec":"aac"}]}],"canBeSyndicated":true,"transcription":{"status":"COMPLETE","job":"1579674","transcript":"<p>Here's what we're watching with trading underway in London. Global equities under pressure led by weaker commodities and financials as investors scrutinise the viability of the Trump trade. The dollar is weaker. Havens like yen, gold, and government bonds finding buyers. </p><p>As the dust settles over the failure to replace Obamacare, focus now on whether tax reform and other fiscal measures will eventuate. This is where the rubber meets the road for the Trump trade. High flying equity markets had been underpinned by the promise of big tax cuts and fiscal stimulus. And Wall Street is souring. </p><p>One big beneficiary of lower corporate taxes under Trump are small caps. They are now down 2 and 1/2% for the year. While the sector is still much higher since November, this is a key market barometer of prospects for the Trump trade. </p><p>Now while many still think some measure of tax reform or spending will eventuate, markets are very wary, namely of the risk that Congress and the Trump administration fail to reach agreement on legislation, that unlike health care reform, matters a great deal more to investors. </p><p>[MUSIC PLAYING] </p>","captions":[{"format":"vtt","url":"https://next-video-editor.ft.com/e2290d14-7e80-4db8-a715-949da4de9a07.vtt","mediaType":"text/vtt"}]},"format":[],"type":"video","id":"e2290d14-7e80-4db8-a715-949da4de9a07"}
Loading

0 comments on commit 97af385

Please sign in to comment.