-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapper_test.go
270 lines (246 loc) · 6.09 KB
/
mapper_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/Financial-Times/go-logger/v2"
"github.com/stretchr/testify/assert"
)
const (
testVideoUUID = "e2290d14-7e80-4db8-a715-949da4de9a07"
testContentCollectionUUID = "e2290d14-7e80-4db8-19d1-ea8e75cf09e8"
)
var testMap = make(map[string]interface{})
func init() {
testMap["string"] = "value1"
testMap["nullstring"] = nil
testMap["bool"] = true
var objArray = make([]interface{}, 0)
var obj = make(map[string]interface{})
obj["field1"] = "test"
obj["field2"] = true
objArray = append(objArray, obj)
testMap["objArray"] = objArray
testMap["emptyObjArray"] = make([]interface{}, 0)
}
func TestBuildRelatedItems(t *testing.T) {
log := logger.NewUPPLogger("video-mapper", "Debug")
m := relatedContentMapper{log: log}
tests := []struct {
nextRelatedItem []map[string]interface{}
expectedItems []Item
}{
{
[]map[string]interface{}{
newRelatedItem("c4cde316-128c-11e7-80f4-13e067d5072c"),
newRelatedItem("e2290d14-7e80-4db8-a715-949da4de9a07"),
},
[]Item{
{UUID: "c4cde316-128c-11e7-80f4-13e067d5072c"},
{UUID: "e2290d14-7e80-4db8-a715-949da4de9a07"},
},
},
{
[]map[string]interface{}{
newRelatedItem(nil),
},
[]Item{},
},
}
for _, test := range tests {
items := m.retrieveRelatedItems(test.nextRelatedItem, "")
assert.Equal(t, test.expectedItems, items, "Related items are wrong. Test input: [%v]", test.nextRelatedItem)
}
}
func TestMapNextVideoRelatedContentHappyFlows(t *testing.T) {
tests := []struct {
fileName string
expectedContent string
expectedVideoUUID string
expectedErrStatus bool
}{
{
"next-video-input.json",
newStringMappedContent(t, "c4cde316-128c-11e7-80f4-13e067d5072c", "", "", false),
testVideoUUID,
false,
},
{
"next-video-delete-input.json",
newStringMappedContent(t, "", "", "", true),
testVideoUUID,
false,
},
}
for _, test := range tests {
nextVideo, err := readContent(test.fileName)
if err != nil {
assert.Fail(t, err.Error())
}
m := relatedContentMapper{
sc: serviceConfig{},
unmarshalled: nextVideo,
}
marshalledContent, videoUUID, err := m.mapRelatedContent()
assert.Equal(t, test.expectedContent, string(marshalledContent), "Marshalled content wrong. Input JSON: %s", test.fileName)
assert.Equal(t, test.expectedVideoUUID, videoUUID, "Video UUID wrong. Input JSON: %s", test.fileName)
assert.Equal(t, test.expectedErrStatus, err != nil, "Error status wrong. Input JSON: %s", test.fileName)
}
}
func TestMapNextVideoRelatedContentMissingFields(t *testing.T) {
log := logger.NewUPPLogger("video-mapper", "Debug")
tests := []struct {
fileName string
expectedErrStatus bool
}{
{
"next-video-no-related-input.json",
false,
},
{
"next-video-empty-related-input.json",
false,
},
{
"next-video-invalid-related-input.json",
true,
},
{
"next-video-related-no-item-id-input.json",
false,
},
{
"next-video-no-videouuid-input.json",
true,
},
}
for _, test := range tests {
nextVideo, err := readContent(test.fileName)
if err != nil {
assert.Fail(t, err.Error())
}
m := relatedContentMapper{unmarshalled: nextVideo, log: log}
_, _, err = m.mapRelatedContent()
assert.Equal(t, test.expectedErrStatus, err != nil, "Error status wrong. Input JSON: %s", test.fileName)
}
}
func TestGetRequiredStringField(t *testing.T) {
tests := []struct {
key string
expectedValue interface{}
expectedIsErr bool
}{
{
"string",
"value1",
false,
},
{
"nullstring",
"",
true,
},
{
"bool",
"",
true,
},
{
"no_key",
"",
true,
},
}
for _, test := range tests {
result, err := getRequiredStringField(test.key, testMap)
assert.Equal(t, test.expectedValue, result, "Value is wrong. Map key: %s", test.key)
assert.Equal(t, test.expectedIsErr, err != nil, "Error status is wrong. Map key: %s", test.key)
}
}
func TestGetObjectsArrayField(t *testing.T) {
log := logger.NewUPPLogger("video-mapper", "Debug")
m := relatedContentMapper{log: log}
var objArray = make([]map[string]interface{}, 0)
var objValue = make(map[string]interface{})
objValue["field1"] = "test"
objValue["field2"] = true
objArray = append(objArray, objValue)
tests := []struct {
key string
expectedValue []map[string]interface{}
expectedIsErr bool
}{
{
"objArray",
objArray,
false,
},
{
"string",
nil,
true,
},
{
"no_key",
make([]map[string]interface{}, 0),
false,
},
{
"emptyObjArray",
make([]map[string]interface{}, 0),
false,
},
}
for _, test := range tests {
result, err := getObjectsArrayField(test.key, testMap, "videoUUID", &m)
assert.Equal(t, test.expectedValue, result, "Value is wrong. Map key: %s", test.key)
assert.Equal(t, test.expectedIsErr, err != nil, "Error status is wrong. Map key: %s", test.key)
}
}
func readContent(fileName string) (map[string]interface{}, error) {
data, err := ioutil.ReadFile("test-resources/" + fileName)
if err != nil {
return nil, err
}
var result = make(map[string]interface{})
err = json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return result, nil
}
func newRelatedItem(id interface{}) map[string]interface{} {
var obj = make(map[string]interface{})
if id != nil {
obj[relatedItemIDField] = id
}
return obj
}
func newStringMappedContent(t *testing.T, itemUUID string, tid string, msgDate string, deletePayload bool) string {
var cc ContentCollection
if itemUUID != "" {
items := []Item{{itemUUID}}
cc = ContentCollection{
UUID: testContentCollectionUUID,
Items: items,
PublishReference: tid,
LastModified: msgDate,
CollectionType: collectionType,
}
}
if deletePayload {
cc.Deleted = true
cc.UUID = testVideoUUID
}
mc := MappedContent{
Payload: cc,
ContentURI: contentURIPrefix + testContentCollectionUUID,
LastModified: msgDate,
UUID: testContentCollectionUUID,
}
marshalledContent, err := json.Marshal(mc)
if err != nil {
assert.Fail(t, err.Error())
}
return string(marshalledContent)
}