-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
860 lines (695 loc) · 36.6 KB
/
main_test.go
File metadata and controls
860 lines (695 loc) · 36.6 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
package main
import (
"bytes"
"github.com/guilhebl/go-offer/common/model"
"github.com/guilhebl/go-offer/offer"
"github.com/stretchr/testify/assert"
"gopkg.in/jarcoal/httpmock.v1"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
var app offer.Module
func init() {
runtime.LockOSThread()
}
// TestMain builds an instance of the application in test mode to run e2e tests. Every external calls are intercepted through stubs
// that returns test JSON data instead of really calling external services.
// This way it's possible to build and run offline Functional Tests on top of the actual running stack and test multiple end-to-end scenarios.
func TestMain(m *testing.M) {
setup()
go func() {
exitVal := m.Run()
teardown()
os.Exit(exitVal)
}()
log.Println("setting up test server...")
run("test")
}
func setup() {
log.Println("SETUP")
}
func teardown() {
log.Println("TEARDOWN")
}
func check(e error) {
if e != nil {
panic(e)
}
}
func readFile(path string) []byte {
absPath, _ := filepath.Abs("./" + path)
dat, err := ioutil.ReadFile(absPath)
check(err)
return dat
}
const (
WalmartTrendingUrl = "http://api.walmartlabs.com/v1/trends"
WalmartSearchUrl = "http://api.walmartlabs.com/v1/search"
WalmartGetDetailUrl = "http://api.walmartlabs.com/v1/items/53966162"
WalmartGetDetailByUpcUrl = "http://api.walmartlabs.com/v1/items"
BestBuyTrendingUrl = "https://api.bestbuy.com/beta/products/trendingViewed"
BestBuySearchUrl = "https://api.bestbuy.com/v1/products(search=skyrim)"
BestBuyGetDetailUrl = "https://api.bestbuy.com/v1/products(productId=5529006)"
BestBuyGetDetailByUpcUrl = "https://api.bestbuy.com/v1/products(upc=065857174434)"
EbaySearchUrl = "http://svcs.ebay.com/services/search/FindingService/v1"
EbayGetDetailUrl = "http://svcs.ebay.com/services/search/FindingService/v1"
AmazonSearchUrl = "https://webservices.amazon.com/onca/xml"
AmazonGetDetailUrl = "https://webservices.amazon.com/onca/xml"
)
// returns the bytes of a corresponding mock API call for an external resource for the 'Trending' API CALL
func getJsonBytesTrendingMock(url string) []byte {
switch url {
case WalmartTrendingUrl:
return readFile("offer/walmart/testdata/walmart_sample_trending_response.json")
case BestBuyTrendingUrl:
return readFile("offer/bestbuy/testdata/bestbuy_sample_trending_response.json")
case EbaySearchUrl:
return readFile("offer/ebay/testdata/ebay_sample_trending_response.json")
case AmazonSearchUrl:
return readFile("offer/amazon/testdata/amazon_sample_trending_response.xml")
default:
return nil
}
}
// returns the bytes of a corresponding mock API call for an external resource for the 'Search' API CALL
func getJsonBytesSearchMock(url string) []byte {
switch url {
case WalmartSearchUrl:
return readFile("offer/walmart/testdata/walmart_sample_search_response.json")
case BestBuySearchUrl:
return readFile("offer/bestbuy/testdata/bestbuy_sample_search_response.json")
case EbaySearchUrl:
return readFile("offer/ebay/testdata/ebay_sample_search_response.json")
case AmazonSearchUrl:
return readFile("offer/amazon/testdata/amazon_sample_search_response.xml")
default:
return nil
}
}
// returns the bytes of a corresponding mock API call for an external resource for the 'Search No results' API CALL
func getJsonBytesSearchNoResultsMock(url string) []byte {
switch url {
case WalmartSearchUrl:
return readFile("offer/walmart/testdata/walmart_search_no_results.json")
case BestBuySearchUrl:
return readFile("offer/bestbuy/testdata/bestbuy_search_no_results.json")
case EbaySearchUrl:
return readFile("offer/ebay/testdata/ebay_search_no_results.json")
case AmazonSearchUrl:
return readFile("offer/amazon/testdata/amazon_search_no_results.xml")
default:
return nil
}
}
// returns the bytes of a corresponding mock API call for an external resource for the 'GetDetail' API CALL
func getJsonBytesGetDetailByIdMock(url string) []byte {
switch url {
case WalmartGetDetailUrl:
return readFile("offer/walmart/testdata/walmart_sample_get_detail_by_id_response.json")
case BestBuyGetDetailUrl:
return readFile("offer/bestbuy/testdata/bestbuy_sample_get_detail_by_id_response.json")
case EbayGetDetailUrl:
return readFile("offer/ebay/testdata/ebay_sample_get_detail_by_id_response.json")
case AmazonGetDetailUrl:
return readFile("offer/amazon/testdata/amazon_sample_get_detail_by_id_response.xml")
default:
return nil
}
}
// returns the bytes of a corresponding mock API call for an external resource for the 'GetDetail' API CALL By UPC
func getJsonBytesGetDetailByUpcMock(url string) []byte {
switch url {
case WalmartGetDetailByUpcUrl:
return readFile("offer/walmart/testdata/walmart_sample_get_detail_by_upc_response.json")
case BestBuyGetDetailByUpcUrl:
return readFile("offer/bestbuy/testdata/bestbuy_sample_get_detail_by_upc_response.json")
case EbayGetDetailUrl:
return readFile("offer/ebay/testdata/ebay_find_by_upc.json")
case AmazonGetDetailUrl:
return readFile("offer/amazon/testdata/amazon_sample_get_detail_by_upc_response.xml")
default:
return nil
}
}
// returns the bytes of a corresponding mock API call for an external resource for the 'GetDetail' API CALL By UPC With no Results
func getJsonBytesGetDetailByUpcNoResultsMock(url string) []byte {
switch url {
case WalmartGetDetailByUpcUrl:
return readFile("offer/walmart/testdata/walmart_get_by_upc_not_found.json")
case BestBuyGetDetailByUpcUrl:
return readFile("offer/bestbuy/testdata/best_buy_get_by_upc_prod_detail_not_found.json")
case EbayGetDetailUrl:
return readFile("offer/ebay/testdata/ebay_find_by_upc_no_result.json")
case AmazonGetDetailUrl:
return readFile("offer/amazon/testdata/amazon_get_product_detail_by_upc_not_found.xml")
default:
return nil
}
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
offer.GetInstance().Router.ServeHTTP(rr, req)
return rr
}
func assertCallsMade(t *testing.T, httpMethod, url string, expected int) {
info := httpmock.GetCallCountInfo()
count := info[httpMethod+" "+url]
assert.Equal(t, expected, count)
log.Printf("Total External API Calls made to %s: %d", url, count)
}
// Registers Mock endpoint responders for Search based API calls
func registerMockResponderSearch(httpMethod, apiUrl, apiType string, status int) {
log.Printf("Mocking Search: %s %d - %s", httpMethod, status, apiUrl)
switch apiType {
case model.Trending:
httpmock.RegisterResponder(httpMethod, apiUrl, httpmock.NewBytesResponder(status, getJsonBytesTrendingMock(apiUrl)))
break
case model.Search:
httpmock.RegisterResponder(httpMethod, apiUrl, httpmock.NewBytesResponder(status, getJsonBytesSearchMock(apiUrl)))
break
case model.NoResults:
httpmock.RegisterResponder(httpMethod, apiUrl, httpmock.NewBytesResponder(status, getJsonBytesSearchNoResultsMock(apiUrl)))
break
}
}
// Registers Mock endpoint responders for Get Detail based API calls
func registerMockResponderGetDetail(httpMethod, apiUrl, apiType string, status int) {
log.Printf("Mocking GetDetail: %s %d - %s", httpMethod, status, apiUrl)
switch apiType {
case model.Id:
httpmock.RegisterResponder(httpMethod, apiUrl, httpmock.NewBytesResponder(status, getJsonBytesGetDetailByIdMock(apiUrl)))
break
case model.Upc:
httpmock.RegisterResponder(httpMethod, apiUrl, httpmock.NewBytesResponder(status, getJsonBytesGetDetailByUpcMock(apiUrl)))
break
case model.NoResults:
httpmock.RegisterResponder(httpMethod, apiUrl, httpmock.NewBytesResponder(status, getJsonBytesGetDetailByUpcNoResultsMock(apiUrl)))
break
}
}
// Tests basic Search (no keywords) that returns trending results from external APIs
func TestSearch(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderSearch(http.MethodGet, WalmartTrendingUrl, model.Trending, 200)
registerMockResponderSearch(http.MethodGet, BestBuyTrendingUrl, model.Trending, 200)
registerMockResponderSearch(http.MethodGet, EbaySearchUrl, model.Trending, 200)
registerMockResponderSearch(http.MethodGet, AmazonSearchUrl, model.Trending, 200)
// call our local server API
endpoint := "http://localhost:8080/"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"list":[{"`))
walmartSnippet := `"externalId":"348726849","upc":"816586026705","name":"Best Choice Products 6' Exercise Tri-Fold Gym Mat For Gymnastics, Aerobics, Yoga, Martial Arts - Pink","partyName":"walmart.com"`
assert.True(t, strings.Contains(body, walmartSnippet))
ebaySnippet := `"externalId":"282629961650","upc":"","name":"Reverb Cross Men s Running Shoes","partyName":"ebay.com"`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `"externalId":"B0743W4Y75","upc":"701649356113","name":"Bluetooth Smart Watch with Camera, Aosmart B23 Smart Watch for Android Smartphones (White)","partyName":"amazon.com"`
assert.True(t, strings.Contains(body, amazonSnippet))
bestBuySnippet := `"externalId":"5714687","upc":"","name":"Alienware - Aurora R6 Desktop - Intel Core i7 - 16GB Memory - NVIDIA GeForce GTX 1070 - 256GB Solid State Drive + 1TB Hard Drive - Silver","partyName":"bestbuy.com"`
assert.True(t, strings.Contains(body, bestBuySnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartTrendingUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuyTrendingUrl, 1)
assertCallsMade(t, http.MethodGet, EbaySearchUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonSearchUrl, 1)
}
// Tests Search with keywords that returns results from external APIs
func TestSearchWithKeywords(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderSearch(http.MethodGet, WalmartSearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, BestBuySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, EbaySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, AmazonSearchUrl, model.Search, 200)
// call our local server API
endpoint := "http://localhost:8080/offers"
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortOrder":"asc","page":1,"rowsPerPage":10}`)
req, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonRequest))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"list":[{"`))
walmartSnippet := `"externalId":"53966162","upc":"093155171244","name":"Skyrim Special Edition (Xbox One)","partyName":"walmart.com",`
assert.True(t, strings.Contains(body, walmartSnippet))
bestBuySnippet := `"externalId":"5626200","upc":"600603210488","name":"The Elder Scrolls V: Skyrim Special Edition Best Buy Exclusive Dragonborn Bundle - Xbox One","partyName":"bestbuy.com"`
assert.True(t, strings.Contains(body, bestBuySnippet))
ebaySnippet := `"externalId":"223482818","upc":"","name":"Elder Scrolls V: Skyrim - Special Edition With Bonus Steelbook Case PS4 ","partyName":"ebay.com"`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `"externalId":"B01GW8XJVU","upc":"093155171251","name":"The Elder Scrolls V: Skyrim - Special Edition - PlayStation 4","partyName":"amazon.com"`
assert.True(t, strings.Contains(body, amazonSnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartSearchUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuySearchUrl, 1)
assertCallsMade(t, http.MethodGet, EbaySearchUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonSearchUrl, 1)
}
// Tests Search with keywords that returns results from external APIs - SORT by name DESC
func TestSearchWithKeywordsSortByNameDesc(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderSearch(http.MethodGet, WalmartSearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, BestBuySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, EbaySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, AmazonSearchUrl, model.Search, 200)
// call our local server API
endpoint := "http://localhost:8080/offers"
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortBy":"name", "sortOrder":"desc","page":1,"rowsPerPage":10}`)
req, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonRequest))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"list":[{"`))
assert.True(t, strings.Contains(body, `"externalId":"0","upc":"","name":"Skyrim VR Digital - PlayStation 4`))
walmartSnippet := `"externalId":"53966162","upc":"093155171244","name":"Skyrim Special Edition (Xbox One)","partyName":"walmart.com",`
assert.True(t, strings.Contains(body, walmartSnippet))
bestBuySnippet := `"externalId":"5626200","upc":"600603210488","name":"The Elder Scrolls V: Skyrim Special Edition Best Buy Exclusive Dragonborn Bundle - Xbox One","partyName":"bestbuy.com"`
assert.True(t, strings.Contains(body, bestBuySnippet))
ebaySnippet := `"externalId":"223482818","upc":"","name":"Elder Scrolls V: Skyrim - Special Edition With Bonus Steelbook Case PS4 ","partyName":"ebay.com"`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `"externalId":"B01GW8XJVU","upc":"093155171251","name":"The Elder Scrolls V: Skyrim - Special Edition - PlayStation 4","partyName":"amazon.com"`
assert.True(t, strings.Contains(body, amazonSnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartSearchUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuySearchUrl, 1)
assertCallsMade(t, http.MethodGet, EbaySearchUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonSearchUrl, 1)
}
// Tests Search SORT by price ASC
func TestSearchWithKeywordsSortByPriceAsc(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderSearch(http.MethodGet, WalmartSearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, BestBuySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, EbaySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, AmazonSearchUrl, model.Search, 200)
// call our local server API
endpoint := "http://localhost:8080/offers"
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortBy":"price", "sortOrder":"asc","page":1,"rowsPerPage":10}`)
req, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonRequest))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"list":[{"`))
assert.True(t, strings.Contains(body, `"externalId":"0","upc":"","name":"Skyrim VR Digital - PlayStation 4`))
walmartSnippet := `"externalId":"53966162","upc":"093155171244","name":"Skyrim Special Edition (Xbox One)","partyName":"walmart.com",`
assert.True(t, strings.Contains(body, walmartSnippet))
bestBuySnippet := `"externalId":"5626200","upc":"600603210488","name":"The Elder Scrolls V: Skyrim Special Edition Best Buy Exclusive Dragonborn Bundle - Xbox One","partyName":"bestbuy.com"`
assert.True(t, strings.Contains(body, bestBuySnippet))
ebaySnippet := `"externalId":"223482818","upc":"","name":"Elder Scrolls V: Skyrim - Special Edition With Bonus Steelbook Case PS4 ","partyName":"ebay.com"`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `"externalId":"B01GW8XJVU","upc":"093155171251","name":"The Elder Scrolls V: Skyrim - Special Edition - PlayStation 4","partyName":"amazon.com"`
assert.True(t, strings.Contains(body, amazonSnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartSearchUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuySearchUrl, 1)
assertCallsMade(t, http.MethodGet, EbaySearchUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonSearchUrl, 1)
}
// tests Reset and List from Datastore - Cassandra must be running
func TestSearchDatastore(t *testing.T) {
// 1st call Reset to reset database and re-create keystore
endpoint1 := "http://localhost:8080/reset"
req1, _ := http.NewRequest(http.MethodGet, endpoint1, nil)
response1 := executeRequest(req1)
assert.Equal(t, 200, response1.Code)
// 2nd call our local server API to fetch list
endpoint := "http://localhost:8080/offerlist"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"list":[{"`))
assert.True(t, strings.Contains(body, `"externalId":"1","upc":"upc12345678","name":"offer 1"`))
assert.True(t, strings.Contains(body, `"externalId":"2","upc":"upc22345678","name":"offer 2"`))
assert.True(t, strings.Contains(body, `"externalId":"3","upc":"upc32345678","name":"offer 3"`))
assert.True(t, strings.Contains(body, `"externalId":"4","upc":"upc42345678","name":"offer 4"`))
}
// tests Reset and Add to Datastore - Cassandra must be running
func TestResetAddOfferDatastore(t *testing.T) {
// 1st call Reset to reset database and re-create keystore
endpoint1 := "http://localhost:8080/reset"
req1, _ := http.NewRequest(http.MethodGet, endpoint1, nil)
response1 := executeRequest(req1)
assert.Equal(t, 200, response1.Code)
// call our local server API to add
endpoint := "http://localhost:8080/offerlist"
var jsonRequest = []byte(`{"externalId":"1", "upc":"upc999","name":"test record","partyName":"amazon.com","semanticName":"http:/item01","mainImageFileUrl":"http:/item01.jpg","partyImageFileUrl":"amazon-logo.jpg","productCategory":"laptops","price":500,"rating":3.88,"numReviews":120, "created": "2017-11-01T22:08:41+00:00"}`)
req, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonRequest))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
assert.Equal(t, 201, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.Contains(body, `"externalId":"1","upc":"upc999","name":"test record","partyName":"amazon.com","semanticName":"http:/item01"`))
}
// Tests Search with keywords invalid expects Bad Request 400
func testSearchWithKeywordsInvalidRequest(t *testing.T, json []byte) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderSearch(http.MethodGet, WalmartSearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, BestBuySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, EbaySearchUrl, model.Search, 200)
registerMockResponderSearch(http.MethodGet, AmazonSearchUrl, model.Search, 200)
// call our local server API
endpoint := "http://localhost:8080/offers"
req, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(json))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
assert.Equal(t, 400, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.Contains(body, model.InvalidRequest))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartSearchUrl, 0)
assertCallsMade(t, http.MethodGet, BestBuySearchUrl, 0)
assertCallsMade(t, http.MethodGet, EbaySearchUrl, 0)
assertCallsMade(t, http.MethodGet, AmazonSearchUrl, 0)
}
// Tests Search with keywords invalid expects Bad Request 400 - sort order
func TestSearchWithKeywordsInvalidSortOrder(t *testing.T) {
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortBy":"name", "sortOrder":"abc","page":1,"rowsPerPage":10}`)
testSearchWithKeywordsInvalidRequest(t, jsonRequest)
}
// Tests Search with keywords invalid expects Bad Request 400 - page
func TestSearchWithKeywordsInvalidPage(t *testing.T) {
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortBy":"name","sortOrder":"asc","page":-1,"rowsPerPage":10}`)
testSearchWithKeywordsInvalidRequest(t, jsonRequest)
}
// Tests Search with keywords invalid expects Bad Request 400 - rowsPerPage
func TestSearchWithKeywordsInvalidRowsPerPage(t *testing.T) {
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortBy":"name","sortOrder":"asc","page":1,"rowsPerPage":-10}`)
testSearchWithKeywordsInvalidRequest(t, jsonRequest)
}
// Tests Search No results
func TestSearchNoResults(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderSearch(http.MethodGet, WalmartSearchUrl, model.NoResults, 200)
registerMockResponderSearch(http.MethodGet, BestBuySearchUrl, model.NoResults, 200)
registerMockResponderSearch(http.MethodGet, EbaySearchUrl, model.NoResults, 200)
registerMockResponderSearch(http.MethodGet, AmazonSearchUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers"
var jsonRequest = []byte(`{"searchColumns":[{"name":"name","value":"skyrim"}],"sortOrder":"asc","page":1,"rowsPerPage":10}`)
req, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonRequest))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"list":[],"summary":{"page":1,"pageCount":1,"totalCount":0}`))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartSearchUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuySearchUrl, 1)
assertCallsMade(t, http.MethodGet, EbaySearchUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonSearchUrl, 1)
}
// Tests GetDetail By Id - walmart
func TestGetDetailByIdWalmart(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailUrl, model.Id, 200)
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailByUpcUrl, model.Upc, 200)
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.Upc, 200)
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.Upc, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/53966162?idType=id&source=walmart.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
//assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"55760264","upc":"065857174434",`))
walmartSnippet := `"name":"Better Homes and Gardens Leighton Twin-Over-Full Bunk Bed, Multiple Colors","partyName":"walmart.com",`
assert.True(t, strings.Contains(body, walmartSnippet))
bestBuySnippet := `{"partyName":"bestbuy.com","semanticName":"https://api.bestbuy.com/click/-/5529006/pdp"`
assert.True(t, strings.Contains(body, bestBuySnippet))
ebaySnippet := `{"partyName":"ebay.com","semanticName":"http://www.ebay.com/itm/New-Laptop-Toshiba-Satellite-L355-S7907-17-Intel-Pentium-Dual-core-T3400-2-16Gh`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `{"partyName":"amazon.com","semanticName":"https://www.amazon.com/Elder-Scrolls-Skyrim-strategy-bundle-Playstation`
assert.True(t, strings.Contains(body, amazonSnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuyGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
}
// Tests GetDetail By Upc Not Found Walmart
func TestGetDetailByUpcNotFoundWalmart(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailByUpcUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/12345678?idType=upc&source=walmart.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 404, response.Code)
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartGetDetailByUpcUrl, 1)
}
// Tests GetDetail By Id - No Competitors search by UPC detail items found - walmart
func TestGetDetailByIdWalmartNoDetailItems(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailUrl, model.Id, 200)
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailByUpcUrl, model.NoResults, 200)
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.NoResults, 200)
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/53966162?idType=id&source=walmart.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"`))
assert.True(t, strings.Contains(body, `"externalId":"55760264","upc":"065857174434","name":"Better Homes`))
assert.True(t, strings.Contains(body, `"productDetailItems":[]`))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, WalmartGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuyGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
}
// Tests GetDetail By Id BestBuy
func TestGetDetailByIdBestBuy(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailUrl, model.Id, 200)
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailByUpcUrl, model.Upc, 200)
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.Upc, 200)
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.Upc, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/5529006?idType=id&source=bestbuy.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"`))
assert.True(t, strings.Contains(body, `"externalId":"5529006","upc":"849803052423","name":"Funko - Elder Scrolls V`))
walmartSnippet := `{"partyName":"walmart.com","semanticName":"http://linksynergy.walmart.com/fs-bin/click?id=12345678`
assert.True(t, strings.Contains(body, walmartSnippet))
bestBuySnippet := `"partyName":"bestbuy.com","semanticName":"https://api.bestbuy.com/click`
assert.True(t, strings.Contains(body, bestBuySnippet))
ebaySnippet := `{"partyName":"ebay.com","semanticName":"http://www.ebay.com/itm`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `{"partyName":"amazon.com","semanticName":"https://www.amazon.com/Elder-Scrolls-Skyrim-strategy-bundle-Playstation`
assert.True(t, strings.Contains(body, amazonSnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, BestBuyGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, WalmartGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
}
// Tests GetDetail By Upc Not Found - Best Buy
func TestGetDetailByUpcNotFoundBestBuy(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailByUpcUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/065857174434?idType=upc&source=bestbuy.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 404, response.Code)
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, BestBuyGetDetailByUpcUrl, 1)
}
// Tests GetDetail By Id - No Competitors search by UPC detail items found - BestBuy
func TestGetDetailByIdBestBuyNoDetailItems(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailUrl, model.Id, 200)
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailByUpcUrl, model.NoResults, 200)
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.NoResults, 200)
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/5529006?idType=id&source=bestbuy.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
//assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"5529006","upc":"849803052423","name":"Funko - Elder Scrolls V: Skyrim`))
assert.True(t, strings.Contains(body, `"productDetailItems":[]`))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, BestBuyGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, WalmartGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
}
// Tests GetDetail By Id Ebay returns no UPC so product detail items is empty (not fetching others competitors prices)
func TestGetDetailByIdEbay(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.Id, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/62923188?idType=id&source=ebay.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"`))
assert.True(t, strings.Contains(body, `"externalId":"62923188","upc":"","name":"Harry Potter and the Order of the Phoenix-(DVD, Widescreen`))
ebaySnippet := `partyName":"ebay.com","semanticName":"http://www.ebay.com/itm/Harry-Potter-and-Order-Phoenix-DVD-Widescreen-Edition-BRAND-NEW`
assert.True(t, strings.Contains(body, ebaySnippet))
assert.True(t, strings.Contains(body, `"description":"","attributes":[],"productDetailItems":[]}`))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
}
// Tests GetDetail By Upc Not Found - Ebay
func TestGetDetailByUpcNotFoundEbay(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/123456789?idType=upc&source=ebay.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 404, response.Code)
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
}
// Tests GetDetail By Id Amazon
func TestGetDetailByIdAmazon(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.Id, 200)
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailByUpcUrl, model.Upc, 200)
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailByUpcUrl, model.Upc, 200)
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.Upc, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/5529006?idType=id&source=amazon.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"`))
assert.True(t, strings.Contains(body, `"externalId":"B01GW8XJVU","upc":"065857174434","name":"The Elder Scrolls V: Skyrim - Special Edition - PlayStation 4"`))
desc := `"description":"All-new features include remastered art and effects, volumetric god rays, dynamic depth of field, screen-space reflections, and more."`
assert.True(t, strings.Contains(body, desc))
assert.True(t, strings.Contains(body, `"name":"manufacturer","value":"Bethesda"`))
walmartSnippet := `{"partyName":"walmart.com","semanticName":"http://linksynergy.walmart.com/fs-bin/click?id=12345678`
assert.True(t, strings.Contains(body, walmartSnippet))
bestBuySnippet := `{"partyName":"bestbuy.com","semanticName":"https://api.bestbuy.com/click/-/5529006/pdp"`
assert.True(t, strings.Contains(body, bestBuySnippet))
ebaySnippet := `{"partyName":"ebay.com","semanticName":"http://www.ebay.com/itm`
assert.True(t, strings.Contains(body, ebaySnippet))
amazonSnippet := `"partyName":"amazon.com","semanticName":"https://www.amazon.com/Elder-Scrolls-Skyrim-Special-PlayStation-4`
assert.True(t, strings.Contains(body, amazonSnippet))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuyGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, WalmartGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
}
// Tests GetDetail By Upc Not Found - Amazon
func TestGetDetailByUpcNotFoundAmazon(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/123456789?idType=upc&source=amazon.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 404, response.Code)
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
}
// Tests GetDetail By Id - No Competitors search by UPC detail items found - Amazon
func TestGetDetailByIdAmazonNoDetailItems(t *testing.T) {
// register mock for external API endpoints
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// External Vendor Apis
registerMockResponderGetDetail(http.MethodGet, AmazonGetDetailUrl, model.Id, 200)
registerMockResponderGetDetail(http.MethodGet, BestBuyGetDetailByUpcUrl, model.NoResults, 200)
registerMockResponderGetDetail(http.MethodGet, WalmartGetDetailByUpcUrl, model.NoResults, 200)
registerMockResponderGetDetail(http.MethodGet, EbayGetDetailUrl, model.NoResults, 200)
// call our local server API
endpoint := "http://localhost:8080/offers/123456789?idType=id&source=amazon.com"
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
response := executeRequest(req)
assert.Equal(t, 200, response.Code)
// verify responses
body := response.Body.String()
assert.True(t, strings.HasPrefix(body, `{"offer":{"id":"`))
assert.True(t, strings.Contains(body, `"externalId":"B01GW8XJVU","upc":"065857174434","name":"The Elder Scrolls V: Skyrim - Special Edition`))
assert.True(t, strings.Contains(body, `"productDetailItems":[]`))
// get the amount of calls for the registered responders
assertCallsMade(t, http.MethodGet, AmazonGetDetailUrl, 1)
assertCallsMade(t, http.MethodGet, BestBuyGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, WalmartGetDetailByUpcUrl, 1)
assertCallsMade(t, http.MethodGet, EbayGetDetailUrl, 1)
}