-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
SendApiTest.java
641 lines (513 loc) · 28.9 KB
/
SendApiTest.java
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
/*
* Copyright (c) 2010-2021 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb.types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.restfb.AbstractJsonMapperTests;
import com.restfb.DefaultJsonMapper;
import com.restfb.exception.FacebookPreconditionException;
import com.restfb.testutils.AssertJson;
import com.restfb.types.send.*;
import com.restfb.types.send.Message;
import com.restfb.types.send.airline.AirlineCheckinTemplatePayload;
import com.restfb.types.send.media.MediaTemplateAttachmentElement;
import com.restfb.types.send.media.MediaTemplateUrlElement;
import static org.junit.jupiter.api.Assertions.*;
class SendApiTest extends AbstractJsonMapperTests {
@Test
void recipientWithId() {
IdMessageRecipient recipient = new IdMessageRecipient("12345");
assertTrue(recipient instanceof MessageRecipient);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"id\":\"12345\"}", recipientJsonString);
}
@Test
void recipientWithPhone() {
PhoneMessageRecipient recipient = new PhoneMessageRecipient("12345");
assertTrue(recipient instanceof MessageRecipient);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"phone_number\":\"12345\"}", recipientJsonString);
}
@Test
void recipientWithPhoneAndName() {
PhoneMessageRecipient recipient = new PhoneMessageRecipient("12345");
recipient.setName(new PhoneMessageRecipient.Name("Jane", "Doe"));
assertTrue(recipient instanceof MessageRecipient);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"phone_number\":\"12345\", \"name\":{\"first_name\":\"Jane\",\"last_name\":\"Doe\"}}",
recipientJsonString);
}
@Test
void recipientWithUserRef() {
UserRefMessageRecipient recipient = new UserRefMessageRecipient("UNIQUE_USER_REF");
assertTrue(recipient instanceof MessageRecipient);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"user_ref\":\"UNIQUE_USER_REF\"}", recipientJsonString);
}
@Test
void messageText() {
Message recipient = new Message("Just a Test");
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"text\":\"Just a Test\"}", recipientJsonString);
}
@Test
void messageImageAttachment() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.IMAGE, "IMAGE_URL");
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{\"url\":\"IMAGE_URL\"},\"type\":\"image\"}}",
recipientJsonString);
}
@Test
void messageVideoAttachment() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.VIDEO, "VIDEO_URL");
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{\"url\":\"VIDEO_URL\"},\"type\":\"video\"}}",
recipientJsonString);
}
@Test
void messageMediaAttachment_imageURL() {
MediaAttachment.MediaTemplateElement mediaTemplateElement =
new MediaTemplateUrlElement("https://business.facebook.com/<PAGE_NAME>/photos/<NUMERIC_ID>");
MediaAttachment attachment = new MediaAttachment(Collections.singletonList(mediaTemplateElement));
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"template_type\":\"media\",\"elements\":[{\"media_type\":\"image\",\"url\":\"https://business.facebook.com/<PAGE_NAME>/photos/<NUMERIC_ID>\"}]},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageMediaAttachment_videoURL() {
MediaAttachment.MediaTemplateElement mediaTemplateElement =
new MediaTemplateUrlElement("https://business.facebook.com/<PAGE_NAME>/videos/<NUMERIC_ID>");
MediaAttachment attachment = new MediaAttachment(Collections.singletonList(mediaTemplateElement));
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"template_type\":\"media\",\"elements\":[{\"media_type\":\"video\",\"url\":\"https://business.facebook.com/<PAGE_NAME>/videos/<NUMERIC_ID>\"}]},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageMediaAttachment_imageAttachment() {
MediaAttachment.MediaTemplateElement mediaTemplateElement =
new MediaTemplateAttachmentElement(MediaAttachment.MediaType.IMAGE, "<ATTACHMENT_ID>");
MediaAttachment attachment = new MediaAttachment(Collections.singletonList(mediaTemplateElement));
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"template_type\":\"media\",\"elements\":[{\"media_type\":\"image\",\"attachment_id\":\"<ATTACHMENT_ID>\"}]},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageMediaAttachment_imageAttachmentWithButton() {
WebButton button = new WebButton("Title", "<WEB_URL>");
MediaAttachment.MediaTemplateElement mediaTemplateElement =
new MediaTemplateAttachmentElement(MediaAttachment.MediaType.IMAGE, "<ATTACHMENT_ID>");
mediaTemplateElement.addButton(button);
MediaAttachment attachment = new MediaAttachment(Collections.singletonList(mediaTemplateElement));
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"template_type\":\"media\",\"elements\":[{\"media_type\":\"image\",\"attachment_id\":\"<ATTACHMENT_ID>\",\"buttons\":[{\"url\":\"<WEB_URL>\",\"type\":\"web_url\",\"title\":\"Title\"}]}]},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageMediaAttachment_videoAttachment() {
MediaAttachment.MediaTemplateElement mediaTemplateElement =
new MediaTemplateAttachmentElement(MediaAttachment.MediaType.VIDEO, "<ATTACHMENT_ID>");
MediaAttachment attachment = new MediaAttachment(Collections.singletonList(mediaTemplateElement));
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"template_type\":\"media\",\"elements\":[{\"media_type\":\"video\",\"attachment_id\":\"<ATTACHMENT_ID>\"}]},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageAudioAttachment() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.AUDIO, "AUDIO_URL");
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{\"url\":\"AUDIO_URL\"},\"type\":\"audio\"}}",
recipientJsonString);
}
@Test
void messageAudioAttachmentWithReuse() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.AUDIO, "AUDIO_URL");
attachment.setIsReusable(true);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"url\":\"AUDIO_URL\",\"is_reusable\":true},\"type\":\"audio\"}}",
recipientJsonString);
}
@Test
void messageAudioAttachmentReuseId() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.AUDIO, "123456789");
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{\"attachment_id\":\"123456789\"},\"type\":\"audio\"}}",
recipientJsonString);
}
@Test
void messageUploadAttachment() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.IMAGE);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{},\"type\":\"image\"}}",
recipientJsonString);
}
@Test
void messageUploadAttachmentReusable() {
MediaAttachment attachment = new MediaAttachment(MediaAttachment.Type.IMAGE);
attachment.setIsReusable(true);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{\"is_reusable\":true},\"type\":\"image\"}}",
recipientJsonString);
}
@Test
void messageLocationAttachment() {
LocationAttachment attachment = new LocationAttachment(20, 30);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"coordinates\":{\"lat\":20, \"long\":30}},\"type\":\"location\"}}",
recipientJsonString);
}
@Test
void messageOpenGraphTemplate() {
OpenGraphTemplatePayload payload = new OpenGraphTemplatePayload();
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals("{\"attachment\":{\"payload\":{\"template_type\":\"open_graph\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageOpenGraphTemplateWithButton() {
WebButton button = new WebButton("Check this", "http://www.google.com");
OpenGraphTemplatePayload payload = new OpenGraphTemplatePayload();
OpenGraphTemplatePayload.Element element = new OpenGraphTemplatePayload.Element("some url");
element.addButton(button);
payload.addElement(element);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"elements\":[{\"url\":\"some url\",\"buttons\":[{\"url\":\"http://www.google.com\",\"type\":\"web_url\",\"title\":\"Check this\"}]}],\"template_type\":\"open_graph\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageGenericAttachment() {
ButtonTemplatePayload payload = new ButtonTemplatePayload("TITLE");
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"text\":\"TITLE\", \"template_type\":\"button\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageGenericButtonAttachment() {
WebButton button = new WebButton("Check this", "http://www.google.com");
ButtonTemplatePayload payload = new ButtonTemplatePayload("MY TEXT");
payload.addButton(button);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"buttons\":[{\"type\":\"web_url\",\"title\":\"Check this\",\"url\":\"http://www.google.com\"}],\"template_type\":\"button\",\"text\":\"MY TEXT\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageGenericButtonsAttachment() {
WebButton button = new WebButton("Check this", "http://www.google.com");
WebButton buttonHeight = new WebButton("Check this", "http://www.google.com");
buttonHeight.setWebviewHeightRatio(WebviewHeightEnum.compact);
PostbackButton postbackButton = new PostbackButton("My Postback", "POSTBACK");
CallButton callButton = new CallButton("Call Support", "+1234567890");
ButtonTemplatePayload payload = new ButtonTemplatePayload("TITLE");
payload.addButton(button);
payload.addButton(buttonHeight);
payload.addButton(postbackButton);
payload.addButton(callButton);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"text\":\"TITLE\", \"buttons\":[{\"type\":\"web_url\",\"title\":\"Check this\",\"url\":\"http://www.google.com\"},{\"webview_height_ratio\":\"compact\",\"url\":\"http://www.google.com\",\"type\":\"web_url\",\"title\":\"Check this\"},{\"payload\":\"POSTBACK\",\"type\":\"postback\",\"title\":\"My Postback\"},{\"payload\":\"+1234567890\",\"type\":\"phone_number\",\"title\":\"Call Support\"}],\"template_type\":\"button\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageButtonsAttachment() {
WebButton button = new WebButton("Check this", "http://www.google.com");
PostbackButton postbackButton = new PostbackButton("My Postback", "POSTBACK");
CallButton callButton = new CallButton("Call Support", "+1234567890");
GenericTemplatePayload payload = new GenericTemplatePayload();
Bubble bubble = new Bubble("My Bubble");
bubble.addButton(button);
bubble.addButton(postbackButton);
bubble.addButton(callButton);
payload.addBubble(bubble);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"elements\":[{\"buttons\":[{\"type\":\"web_url\",\"title\":\"Check this\",\"url\":\"http://www.google.com\"},{\"payload\":\"POSTBACK\",\"type\":\"postback\",\"title\":\"My Postback\"},{\"payload\":\"+1234567890\",\"type\":\"phone_number\",\"title\":\"Call Support\"}],\"title\":\"My Bubble\"}],\"template_type\":\"generic\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageButtonsAttachmentNonSharable() {
WebButton button = new WebButton("Check this", "http://www.google.com");
PostbackButton postbackButton = new PostbackButton("My Postback", "POSTBACK");
CallButton callButton = new CallButton("Call Support", "+1234567890");
GenericTemplatePayload payload = new GenericTemplatePayload();
payload.setSharable(false);
Bubble bubble = new Bubble("My Bubble");
bubble.addButton(button);
bubble.addButton(postbackButton);
bubble.addButton(callButton);
payload.addBubble(bubble);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"elements\":[{\"title\":\"My Bubble\",\"buttons\":[{\"url\":\"http://www.google.com\",\"type\":\"web_url\",\"title\":\"Check this\"},{\"payload\":\"POSTBACK\",\"type\":\"postback\",\"title\":\"My Postback\"},{\"payload\":\"+1234567890\",\"type\":\"phone_number\",\"title\":\"Call Support\"}]}],\"sharable\":false,\"template_type\":\"generic\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageListTemplatePayload() {
ListViewElement element1 = getListViewElement(100, "Classic White T-Shirt");
ListViewElement element2 = getListViewElement(101, "Classic Blue T-Shirt");
PostbackButton postbackButton = new PostbackButton("View More", "payload");
List<ListViewElement> listViewElementList = new ArrayList<>();
listViewElementList.add(element1);
listViewElementList.add(element2);
ListTemplatePayload payload = new ListTemplatePayload(listViewElementList);
payload.setTopElementStyle(ListTemplatePayload.TopElementStyleEnum.compact);
payload.addButton(postbackButton);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
String expectedJson = "{\"attachment\":{\"payload\":{\"elements\":[{\"title\":\"Classic White T-Shirt\","
+ "\"subtitle\":\"100% Cotton, 200% Comfortable\","
+ "\"image_url\":\"https://peterssendreceiveapp.ngrok.io/img/white-t-shirt.png\"," + "\"default_action\":"
+ "{\"webview_height_ratio\":\"tall\","
+ "\"url\":\"https://peterssendreceiveapp.ngrok.io/view?item=100\",\"messenger_extensions\":true,"
+ "\"fallback_url\":\"https://peterssendreceiveapp.ngrok.io/\",\"type\":\"web_url\"},"
+ "\"buttons\":[{\"webview_height_ratio\":\"tall\","
+ "\"url\":\"https://peterssendreceiveapp.ngrok.io/shop?item=100\",\"messenger_extensions\":true,"
+ "\"fallback_url\":\"https://peterssendreceiveapp.ngrok.io/\",\"type\":\"web_url\","
+ "\"title\":\"Buy\"}]},{\"title\":\"Classic Blue T-Shirt\",\"subtitle\":\"100% Cotton, 200% Comfortable\","
+ "\"image_url\":\"https://peterssendreceiveapp.ngrok.io/img/white-t-shirt.png\",\"default_action\":"
+ "{\"webview_height_ratio\":\"tall\",\"url\":\"https://peterssendreceiveapp.ngrok.io/view?item=101\","
+ "\"messenger_extensions\":true,\"fallback_url\":\"https://peterssendreceiveapp.ngrok.io/\",\"type\":"
+ "\"web_url\"},\"buttons\":[{\"webview_height_ratio\":\"tall\","
+ "\"url\":\"https://peterssendreceiveapp.ngrok.io/shop?item=101\",\"messenger_extensions\":true,"
+ "\"fallback_url\":\"https://peterssendreceiveapp.ngrok.io/\",\"type\":\"web_url\",\"title\":\"Buy\"}]}],"
+ "\"top_element_style\":\"compact\",\"buttons\":[{\"payload\":\"payload\",\"type\":\"postback\","
+ "\"title\":\"View More\"}],\"template_type\":\"list\"},\"type\":\"template\"}}";
AssertJson.assertEquals(expectedJson, recipientJsonString);
}
private ListViewElement getListViewElement(int itemId, String title) {
DefaultAction defaultAction = new DefaultAction("https://peterssendreceiveapp.ngrok.io/view?item=" + itemId);
defaultAction.setMessengerExtensions(true, "https://peterssendreceiveapp.ngrok.io/");
defaultAction.setWebviewHeightRatio(WebviewHeightEnum.tall);
WebButton webButton = new WebButton("Buy", "https://peterssendreceiveapp.ngrok.io/shop?item=" + itemId);
webButton.setMessengerExtensions(true, "https://peterssendreceiveapp.ngrok.io/");
webButton.setWebviewHeightRatio(WebviewHeightEnum.tall);
ListViewElement element = new ListViewElement(title);
element.setSubtitle("100% Cotton, 200% Comfortable");
element.setImageUrl("https://peterssendreceiveapp.ngrok.io/img/white-t-shirt.png");
element.setDefaultAction(defaultAction);
element.addButton(webButton);
return element;
}
@Test
void accountLink() {
AccountLinkButton button = new AccountLinkButton("https://www.example.com/oauth/authorize");
GenericTemplatePayload payload = new GenericTemplatePayload();
Bubble bubble = new Bubble("Link Bubble");
bubble.addButton(button);
payload.addBubble(bubble);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message message = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String messageJsonString = mapper.toJson(message, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"elements\":[{\"buttons\":[{\"type\":\"account_link\",\"url\":\"https://www.example.com/oauth/authorize\"}],\"title\":\"Link Bubble\"}],\"template_type\":\"generic\"},\"type\":\"template\"}}",
messageJsonString);
}
@Test
void accountUnLink() {
AccountUnlinkButton button = new AccountUnlinkButton();
GenericTemplatePayload payload = new GenericTemplatePayload();
Bubble bubble = new Bubble("Link Bubble");
bubble.addButton(button);
payload.addBubble(bubble);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message message = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String messageJsonString = mapper.toJson(message, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"elements\":[{\"buttons\":[{\"type\":\"account_unlink\"}],\"title\":\"Link Bubble\"}],\"template_type\":\"generic\"},\"type\":\"template\"}}",
messageJsonString);
}
@Test
void messageTemplateGenericBubbleAttachment() {
WebButton button = new WebButton("Check this", "http://www.google.com");
ShareButton share = new ShareButton();
GenericTemplatePayload payload = new GenericTemplatePayload();
Bubble bubble = new Bubble("My Bubble");
bubble.setSubtitle("subtitle");
bubble.setItemUrl("ITEM_URL");
bubble.setImageUrl("IMAGE_URL");
bubble.addButton(button);
bubble.addButton(share);
payload.addBubble(bubble);
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"elements\":[{\"buttons\":[{\"type\":\"web_url\",\"title\":\"Check this\",\"url\":\"http://www.google.com\"}, {\"type\":\"element_share\"}],\"image_url\":\"IMAGE_URL\",\"item_url\":\"ITEM_URL\",\"subtitle\":\"subtitle\",\"title\":\"My Bubble\"}],\"template_type\":\"generic\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageTemplateAirlineAttachment() {
AirlineCheckinTemplatePayload payload =
new AirlineCheckinTemplatePayload("Intro Message", "en_US", "ABCDEF", "http://www.google.com/checkin");
TemplateAttachment attachment = new TemplateAttachment(payload);
Message recipient = new Message(attachment);
assertEquals(Collections.<QuickReply> emptyList(), recipient.getQuickReplies());
DefaultJsonMapper mapper = new DefaultJsonMapper();
String recipientJsonString = mapper.toJson(recipient, true);
AssertJson.assertEquals(
"{\"attachment\":{\"payload\":{\"checkin_url\":\"http://www.google.com/checkin\",\"intro_message\":\"Intro Message\",\"template_type\":\"airline_checkin\",\"locale\":\"en_US\",\"pnr_number\":\"ABCDEF\"},\"type\":\"template\"}}",
recipientJsonString);
}
@Test
void messageWithQuickRepliesAndMetadata() {
QuickReply quickReply1 = new QuickReply("title 1", "payload 1");
QuickReply quickReply2 = new QuickReply("title 2", "payload 2");
quickReply2.setImageUrl("http://example.org/test.jpg");
QuickReply quickReply5 = new QuickReply(QuickReply.QuickReplyType.USER_EMAIL);
QuickReply quickReply6 = new QuickReply(QuickReply.QuickReplyType.USER_PHONE_NUMBER);
Message message = new Message("message text");
message.setMetadata("metadata payload");
message.addQuickReply(quickReply1);
message.addQuickReply(quickReply2);
message.addQuickReply(quickReply5);
message.addQuickReply(quickReply6);
DefaultJsonMapper mapper = new DefaultJsonMapper();
String messageJsonString = mapper.toJson(message, true);
AssertJson.assertEquals(
"{\"text\":\"message text\",\"quick_replies\":[{\"content_type\":\"text\",\"title\":\"title 1\",\"payload\":\"payload 1\"},{\"content_type\":\"text\",\"title\":\"title 2\",\"payload\":\"payload 2\",\"image_url\":\"http://example.org/test.jpg\"},{\"content_type\":\"user_email\"},{\"content_type\":\"user_phone_number\"}],\"metadata\":\"metadata payload\"}",
messageJsonString);
}
@Test
void messageWithMultipleQuickRepliesAddedAtOnce() {
List<QuickReply> quickReplyList = new ArrayList<>();
quickReplyList.add(new QuickReply("title1", "payload 1"));
quickReplyList.add(new QuickReply("title2", "payload 2"));
Message message = new Message("message text");
message.addQuickReplies(quickReplyList);
assertEquals(quickReplyList, message.getQuickReplies());
DefaultJsonMapper mapper = new DefaultJsonMapper();
String messageJsonString = mapper.toJson(message, true);
AssertJson.assertEquals(
"{\"text\":\"message text\",\"quick_replies\":[{\"content_type\":\"text\",\"title\":\"title1\",\"payload\":\"payload 1\"},{\"content_type\":\"text\",\"title\":\"title2\",\"payload\":\"payload 2\"}]}",
messageJsonString);
}
@Test
void messageWithTooManyReplies_thirdteenPresentOneAdded() {
Message message = new Message("message text");
List<QuickReply> quickReplyList = new ArrayList<>();
quickReplyList.add(new QuickReply("title1", "payload 1"));
quickReplyList.add(new QuickReply("title2", "payload 2"));
quickReplyList.add(new QuickReply("title3", "payload 3"));
quickReplyList.add(new QuickReply("title4", "payload 4"));
quickReplyList.add(new QuickReply("title5", "payload 5"));
quickReplyList.add(new QuickReply("title6", "payload 6"));
quickReplyList.add(new QuickReply("title7", "payload 7"));
quickReplyList.add(new QuickReply("title8", "payload 8"));
quickReplyList.add(new QuickReply("title9", "payload 9"));
quickReplyList.add(new QuickReply("title10", "payload 10"));
quickReplyList.add(new QuickReply("title11", "payload 11"));
quickReplyList.add(new QuickReply("title12", "payload 12"));
quickReplyList.add(new QuickReply("title13", "payload 13"));
message.addQuickReplies(quickReplyList);
assertThrows(FacebookPreconditionException.class,
() -> message.addQuickReply(new QuickReply("last", "payload_last")));
}
@Test
void messageWithTooManyReplies_TenPresentFourAdded() {
Message message = new Message("message text");
List<QuickReply> quickReplyList = new ArrayList<>();
quickReplyList.add(new QuickReply("title1", "payload 1"));
quickReplyList.add(new QuickReply("title2", "payload 2"));
quickReplyList.add(new QuickReply("title3", "payload 3"));
quickReplyList.add(new QuickReply("title4", "payload 4"));
quickReplyList.add(new QuickReply("title5", "payload 5"));
quickReplyList.add(new QuickReply("title6", "payload 6"));
quickReplyList.add(new QuickReply("title7", "payload 7"));
quickReplyList.add(new QuickReply("title8", "payload 8"));
quickReplyList.add(new QuickReply("title9", "payload 9"));
quickReplyList.add(new QuickReply("title10", "payload 10"));
List<QuickReply> quickReplyListAdded = new ArrayList<>();
quickReplyListAdded.add(new QuickReply("title1a", "payload 1a"));
quickReplyListAdded.add(new QuickReply("title2a", "payload 2a"));
quickReplyListAdded.add(new QuickReply("title3a", "payload 3a"));
quickReplyListAdded.add(new QuickReply("title3a", "payload 4a"));
message.addQuickReplies(quickReplyList);
assertThrows(FacebookPreconditionException.class, () -> message.addQuickReplies(quickReplyListAdded));
}
}