From d6c90cf8a5537535b7735392ba043974c02c6282 Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Mon, 27 Mar 2023 20:04:57 -0300 Subject: [PATCH 1/5] updating tests --- .../impl/WhatsappBusinessCloudApiTest.java | 73 ++++++++++++------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index 7e992c76b..6979aa51f 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -304,7 +304,6 @@ void testSendTemplateButtonMessage() throws IOException, URISyntaxException, Int RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - // System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); @@ -357,7 +356,6 @@ void testSendTemplateButtonMessageWithDateTimeParam() throws IOException, URISyn RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - //System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); @@ -409,7 +407,6 @@ void testSendTemplateButtonMessageMarketing() throws IOException, URISyntaxExcep RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - //System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); @@ -447,7 +444,6 @@ void testSendTemplateDocumentPdfMessage() throws IOException, URISyntaxException RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - //System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); @@ -481,7 +477,6 @@ void testSendTemplateVideoMessage() throws IOException, URISyntaxException, Inte RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - //System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); @@ -514,7 +509,6 @@ void testSendTemplateAuthMessage() throws IOException, URISyntaxException, Inter RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - //System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); @@ -539,8 +533,6 @@ void testSendAudioMessage() throws IOException, URISyntaxException, InterruptedE Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - //System.out.println(recordedRequest.getBody().readUtf8()); - JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); @@ -610,16 +602,27 @@ void testSendVideoMessage() throws IOException, URISyntaxException, InterruptedE RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - // System.out.println(recordedRequest.getBody().readUtf8()); JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); } @Test - void testSendImageMessage() throws IOException, URISyntaxException, InterruptedException { + void testSendImageMessage() throws InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + var expectedJson = """ + { + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "image", + "image": { + "id": "75457812459735784", + "caption": "See the image" + } + } + """; var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildImageMessage(new ImageMessage()// @@ -627,45 +630,65 @@ void testSendImageMessage() throws IOException, URISyntaxException, InterruptedE .setCaption("See the image")); - var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); - - Assertions.assertEquals(String.format("{\"messaging_product\":\"whatsapp\",\"recipient_type\":\"individual\",\"to\":\"%s\",\"type\":\"image\",\"image\":{\"id\":\"75457812459735784\",\"caption\":\"See the image\"}}", PHONE_NUMBER_1), recordedRequest.getBody().readUtf8()); - - Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); } @Test - void testSendDocumentMessage() throws IOException, URISyntaxException, InterruptedException { + void testSendDocumentMessage() throws InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + var expectedJson = """ + { + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "document", + "document": { + "id": "78548846588564", + "caption": "My document", + "filename": "test.pdf" + } + } + """; var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildDocumentMessage(new DocumentMessage()// .setId("78548846588564")// .setFileName("test.pdf").setCaption("My document")); - - var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); - Assertions.assertEquals(String.format("{\"messaging_product\":\"whatsapp\",\"recipient_type\":\"individual\",\"to\":\"%s\",\"type\":\"document\",\"document\":{\"id\":\"78548846588564\",\"caption\":\"My document\",\"filename\":\"test.pdf\"}}", PHONE_NUMBER_1), recordedRequest.getBody().readUtf8()); - Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); } @Test - void testSendStickerMessage() throws IOException, URISyntaxException, InterruptedException { + void testSendStickerMessage() throws InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + var expectedJson = """ + { + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "sticker", + "sticker": { + "id": "78548846588564" + } + } + """; + var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildStickerMessage(new StickerMessage()// @@ -673,15 +696,13 @@ void testSendStickerMessage() throws IOException, URISyntaxException, Interrupte var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); - + Assertions.assertNotNull(response); RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); - Assertions.assertEquals(String.format("{\"messaging_product\":\"whatsapp\",\"recipient_type\":\"individual\",\"to\":\"%s\",\"type\":\"sticker\",\"sticker\":{\"id\":\"78548846588564\"}}", PHONE_NUMBER_1), recordedRequest.getBody().readUtf8()); - - Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); } @Test @@ -696,7 +717,7 @@ void testUploadMedia() throws IOException, URISyntaxException, InterruptedExcept RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/media", recordedRequest.getPath()); - + Assertions.assertEquals(103923, recordedRequest.getBodySize()); Assertions.assertEquals("985569392615996", response.id()); } From dde1a8258ed86fa787feceda21feee4d27a00a7a Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Mon, 27 Mar 2023 23:46:43 -0300 Subject: [PATCH 2/5] interactive message test (buttons) --- .../impl/WhatsappBusinessCloudApiTest.java | 63 +++++++++++++++++-- .../expected/message/expectedMessage11.json | 44 +++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/expected/message/expectedMessage11.json diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index 6979aa51f..b81661338 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -2,9 +2,12 @@ import com.whatsapp.api.MockServerUtilsTest; import com.whatsapp.api.domain.media.FileType; +import com.whatsapp.api.domain.messages.Action; import com.whatsapp.api.domain.messages.Address; import com.whatsapp.api.domain.messages.AudioMessage; +import com.whatsapp.api.domain.messages.Body; import com.whatsapp.api.domain.messages.BodyComponent; +import com.whatsapp.api.domain.messages.Button; import com.whatsapp.api.domain.messages.ButtonComponent; import com.whatsapp.api.domain.messages.ButtonPayloadParameter; import com.whatsapp.api.domain.messages.ButtonTextParameter; @@ -18,15 +21,19 @@ import com.whatsapp.api.domain.messages.DocumentMessage; import com.whatsapp.api.domain.messages.DocumentParameter; import com.whatsapp.api.domain.messages.Email; +import com.whatsapp.api.domain.messages.Footer; +import com.whatsapp.api.domain.messages.Header; import com.whatsapp.api.domain.messages.HeaderComponent; import com.whatsapp.api.domain.messages.Image; import com.whatsapp.api.domain.messages.ImageMessage; import com.whatsapp.api.domain.messages.ImageParameter; +import com.whatsapp.api.domain.messages.InteractiveMessage; import com.whatsapp.api.domain.messages.Language; import com.whatsapp.api.domain.messages.Message.MessageBuilder; import com.whatsapp.api.domain.messages.Name; import com.whatsapp.api.domain.messages.Org; import com.whatsapp.api.domain.messages.Phone; +import com.whatsapp.api.domain.messages.Reply; import com.whatsapp.api.domain.messages.StickerMessage; import com.whatsapp.api.domain.messages.TemplateMessage; import com.whatsapp.api.domain.messages.TextMessage; @@ -37,8 +44,11 @@ import com.whatsapp.api.domain.messages.VideoParameter; import com.whatsapp.api.domain.messages.type.AddressType; import com.whatsapp.api.domain.messages.type.ButtonSubType; +import com.whatsapp.api.domain.messages.type.ButtonType; import com.whatsapp.api.domain.messages.type.CalendarType; import com.whatsapp.api.domain.messages.type.EmailType; +import com.whatsapp.api.domain.messages.type.HeaderType; +import com.whatsapp.api.domain.messages.type.InteractiveMessageType; import com.whatsapp.api.domain.messages.type.PhoneType; import com.whatsapp.api.domain.messages.type.UrlType; import com.whatsapp.api.domain.templates.type.LanguageType; @@ -142,7 +152,7 @@ void testSendTextMessage() throws IOException, URISyntaxException, InterruptedEx } @Test - void testContactMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { + void testSendContactMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage10.json"); @@ -194,7 +204,7 @@ void testContactMessage() throws IOException, URISyntaxException, InterruptedExc } @Test - void testContactMessage2() throws IOException, URISyntaxException, InterruptedException, JSONException { + void testSendContactMessage2() throws IOException, URISyntaxException, InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage10.json"); @@ -574,7 +584,7 @@ void testSendAudioLinkMessage() throws InterruptedException, JSONException { } @Test - void testSendVideoMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { + void testSendVideoMessage() throws InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); var expectedJson = """ @@ -705,6 +715,51 @@ void testSendStickerMessage() throws InterruptedException, JSONException { } + @Test + //TODO: Header is incomplete + void testSendInteractiveMessageWithButtons() throws InterruptedException, JSONException, IOException, URISyntaxException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage11.json"); + + var interactive = InteractiveMessage.build() // + .setAction(new Action() // + .addButton(new Button() // + .setType(ButtonType.REPLY).setReply(new Reply() // + .setId("1278454") // + .setTitle("YES"))) // + .addButton(new Button() // + .setType(ButtonType.REPLY).setReply(new Reply() // + .setId("1278455") // + .setTitle("NO"))) // + .addButton(new Button() // + .setType(ButtonType.REPLY).setReply(new Reply() // + .setId("1278456") // + .setTitle("CHANGE")))) // + .setType(InteractiveMessageType.BUTTON) // + .setHeader(new Header()// + .setType(HeaderType.TEXT)// + .setText("Appointment confirmation.")// + + ).setBody(new Body() // + .setText("Would you like to confirm your appointment for tomorrow?")) // + .setFooter(new Footer().setText("Choose an option:")); + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildInteractiveMessage(interactive); + + + var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + Assertions.assertNotNull(response); + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + Assertions.assertEquals("POST", recordedRequest.getMethod()); + Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); + //System.out.println(recordedRequest.getBody().readUtf8()); + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); + + } + @Test void testUploadMedia() throws IOException, URISyntaxException, InterruptedException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/uploadResponse.json"))); @@ -717,7 +772,7 @@ void testUploadMedia() throws IOException, URISyntaxException, InterruptedExcept RecordedRequest recordedRequest = mockWebServer.takeRequest(); Assertions.assertEquals("POST", recordedRequest.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/media", recordedRequest.getPath()); - Assertions.assertEquals(103923, recordedRequest.getBodySize()); + Assertions.assertEquals(103923, recordedRequest.getBodySize()); Assertions.assertEquals("985569392615996", response.id()); } diff --git a/src/test/resources/expected/message/expectedMessage11.json b/src/test/resources/expected/message/expectedMessage11.json new file mode 100644 index 000000000..22b0226b8 --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage11.json @@ -0,0 +1,44 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "interactive": { + "action": { + "buttons": [ + { + "type": "reply", + "reply": { + "id": "1278454", + "title": "YES" + } + }, + { + "type": "reply", + "reply": { + "id": "1278455", + "title": "NO" + } + }, + { + "type": "reply", + "reply": { + "id": "1278456", + "title": "CHANGE" + } + } + ] + }, + "type": "BUTTON", + "header": { + "type": "text", + "text": "Appointment confirmation." + }, + "body": { + "text": "Would you like to confirm your appointment for tomorrow?" + }, + "footer": { + "text": "Choose an option:" + } + }, + "to": "121212121212", + "type": "interactive" +} \ No newline at end of file From 492c81c22839de13017d3e6e8a9ada00a79d5156 Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Tue, 28 Mar 2023 00:35:45 -0300 Subject: [PATCH 3/5] test interactive message with list --- .../whatsapp/api/domain/messages/Header.java | 2 +- .../impl/WhatsappBusinessCloudApiTest.java | 65 ++++++++++++++++++- .../expected/message/expectedMessage12.json | 64 ++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/expected/message/expectedMessage12.json diff --git a/src/main/java/com/whatsapp/api/domain/messages/Header.java b/src/main/java/com/whatsapp/api/domain/messages/Header.java index f887f89ee..e28f6d623 100644 --- a/src/main/java/com/whatsapp/api/domain/messages/Header.java +++ b/src/main/java/com/whatsapp/api/domain/messages/Header.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.whatsapp.api.domain.messages.type.HeaderType; - +//TODO: Header is incomplete /** *

Header content displayed on top of a message. You cannot set a header if your interactive object is of product type

*/ diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index b81661338..1690a31c4 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -34,6 +34,8 @@ import com.whatsapp.api.domain.messages.Org; import com.whatsapp.api.domain.messages.Phone; import com.whatsapp.api.domain.messages.Reply; +import com.whatsapp.api.domain.messages.Row; +import com.whatsapp.api.domain.messages.Section; import com.whatsapp.api.domain.messages.StickerMessage; import com.whatsapp.api.domain.messages.TemplateMessage; import com.whatsapp.api.domain.messages.TextMessage; @@ -716,7 +718,6 @@ void testSendStickerMessage() throws InterruptedException, JSONException { } @Test - //TODO: Header is incomplete void testSendInteractiveMessageWithButtons() throws InterruptedException, JSONException, IOException, URISyntaxException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); @@ -760,6 +761,68 @@ void testSendInteractiveMessageWithButtons() throws InterruptedException, JSONEx } + @Test + void testSendInteractiveMessageWithList() throws InterruptedException, JSONException, IOException, URISyntaxException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage12.json"); + + var interactive = InteractiveMessage.build() // + .setAction(new Action() // + .setButtonText("choose an option") // + .addSection(new Section() // + .setTitle("Section 1") // + .addRow(new Row() // + .setId("SECTION_1_ROW_1_ID") // + .setTitle("Title 1") // + .setDescription("SECTION_1_ROW_1_DESCRIPTION")) // + .addRow(new Row() // + .setId("SECTION_1_ROW_2_ID") // + .setTitle("Title 2") // + .setDescription("SECTION_1_ROW_2_DESCRIPTION")) // + .addRow(new Row() // + .setId("SECTION_1_ROW_3_ID") // + .setTitle("Title 3") // + .setDescription("SECTION_1_ROW_3_DESCRIPTION")) // + ) // + .addSection(new Section() // + .setTitle("Section 2") // + .addRow(new Row() // + .setId("SECTION_2_ROW_1_ID") // + .setTitle("Title 1") // + .setDescription("SECTION_2_ROW_1_DESCRIPTION")) // + .addRow(new Row() // + .setId("SECTION_2_ROW_2_ID") // + .setTitle("Title 2") // + .setDescription("SECTION_2_ROW_2_DESCRIPTION")) // + .addRow(new Row() // + .setId("SECTION_2_ROW_3_ID") // + .setTitle("Title 3") // + .setDescription("SECTION_2_ROW_3_DESCRIPTION")) // + )) // + .setType(InteractiveMessageType.LIST) // + .setHeader(new Header() // + .setType(HeaderType.TEXT) // + .setText("Header Text")) // + .setBody(new Body() // + .setText("Body message")) // + .setFooter(new Footer() // + .setText("Footer Text")); + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildInteractiveMessage(interactive); + + var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + Assertions.assertNotNull(response); + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + Assertions.assertEquals("POST", recordedRequest.getMethod()); + Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); + + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); + + } + @Test void testUploadMedia() throws IOException, URISyntaxException, InterruptedException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/uploadResponse.json"))); diff --git a/src/test/resources/expected/message/expectedMessage12.json b/src/test/resources/expected/message/expectedMessage12.json new file mode 100644 index 000000000..92604b043 --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage12.json @@ -0,0 +1,64 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "interactive": { + "action": { + "button": "choose an option", + "sections": [ + { + "title": "Section 1", + "rows": [ + { + "id": "SECTION_1_ROW_1_ID", + "title": "Title 1", + "description": "SECTION_1_ROW_1_DESCRIPTION" + }, + { + "id": "SECTION_1_ROW_2_ID", + "title": "Title 2", + "description": "SECTION_1_ROW_2_DESCRIPTION" + }, + { + "id": "SECTION_1_ROW_3_ID", + "title": "Title 3", + "description": "SECTION_1_ROW_3_DESCRIPTION" + } + ] + }, + { + "title": "Section 2", + "rows": [ + { + "id": "SECTION_2_ROW_1_ID", + "title": "Title 1", + "description": "SECTION_2_ROW_1_DESCRIPTION" + }, + { + "id": "SECTION_2_ROW_2_ID", + "title": "Title 2", + "description": "SECTION_2_ROW_2_DESCRIPTION" + }, + { + "id": "SECTION_2_ROW_3_ID", + "title": "Title 3", + "description": "SECTION_2_ROW_3_DESCRIPTION" + } + ] + } + ] + }, + "type": "LIST", + "header": { + "type": "text", + "text": "Header Text" + }, + "body": { + "text": "Body message" + }, + "footer": { + "text": "Footer Text" + } + }, + "to": "121212121212", + "type": "interactive" +} \ No newline at end of file From 78b36b075c809f194352805697d4604878ff84cb Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Tue, 28 Mar 2023 00:37:10 -0300 Subject: [PATCH 4/5] interactive messages example --- .../SendInteractiveMessageExample.java | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/whatsapp/api/examples/SendInteractiveMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendInteractiveMessageExample.java index dc47eb8a6..c746e38db 100644 --- a/src/test/java/com/whatsapp/api/examples/SendInteractiveMessageExample.java +++ b/src/test/java/com/whatsapp/api/examples/SendInteractiveMessageExample.java @@ -1,5 +1,7 @@ package com.whatsapp.api.examples; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.whatsapp.api.TestConstants; import com.whatsapp.api.WhatsappApiFactory; import com.whatsapp.api.domain.messages.Action; @@ -25,7 +27,7 @@ public class SendInteractiveMessageExample { - public static void main(String[] args) { + public static void main(String[] args) throws JsonProcessingException { WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN); @@ -46,7 +48,7 @@ private static void multiProductMessage(WhatsappBusinessCloudApi whatsappBusines .setTo(PHONE_NUMBER_1)// .buildInteractiveMessage(InteractiveMessage.build() // .setAction(new Action() // - .setCatalogId("1") // + .setCatalogId("6019053994849450") // .addSection(new Section() // .setTitle("Title 1") // .addProductItem(new Product() // @@ -74,57 +76,65 @@ private static void multiProductMessage(WhatsappBusinessCloudApi whatsappBusines System.out.println(messageResponse); } - private static void productMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) { + private static void productMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException { var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildInteractiveMessage(InteractiveMessage.build() // .setAction(new Action() // - .setCatalogId("1") // - .setProductRetailerId("ID_TEST_ITEM_1")) // + .setCatalogId("600136185327014") // + .setProductRetailerId("r2d5xse158")) // .setType(InteractiveMessageType.PRODUCT) // .setBody(new Body() // .setText("Body message")) // ); + System.out.println(new ObjectMapper().writeValueAsString(message)); + MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); System.out.println(messageResponse); } - private static void buttonMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) { + private static void buttonMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException { var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildInteractiveMessage(InteractiveMessage.build() // .setAction(new Action() // .addButton(new Button() // - .setType(ButtonType.REPLY) - .setReply(new Reply() // - .setId("UNIQUE_BUTTON_ID_1") // - .setTitle("BUTTON_TITLE_1"))) // + .setType(ButtonType.REPLY).setReply(new Reply() // + .setId("1278454") // + .setTitle("YES"))) // .addButton(new Button() // - .setType(ButtonType.REPLY) - .setReply(new Reply() // - .setId("UNIQUE_BUTTON_ID_2") // - .setTitle("BUTTON_TITLE_2"))) - ) // + .setType(ButtonType.REPLY).setReply(new Reply() // + .setId("1278455") // + .setTitle("NO"))) // + .addButton(new Button() // + .setType(ButtonType.REPLY).setReply(new Reply() // + .setId("1278456") // + .setTitle("CHANGE")))) // .setType(InteractiveMessageType.BUTTON) // - .setBody(new Body() // - .setText("Body message")) // - ); + .setHeader(new Header()// + .setType(HeaderType.TEXT)// + .setText("Appointment confirmation.")// + + ).setBody(new Body() // + .setText("Would you like to confirm your appointment for tomorrow?")) // + .setFooter(new Footer().setText("Choose an option:"))); + System.out.println(new ObjectMapper().writeValueAsString(message)); MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); System.out.println(messageResponse); } - private static void listMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) { + private static void listMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException { var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildInteractiveMessage(InteractiveMessage.build() // .setAction(new Action() // - .setButtonText("BUTTON_TEXT") // + .setButtonText("choose an option") // .addSection(new Section() // - .setTitle("Title 1") // + .setTitle("Section 1") // .addRow(new Row() // .setId("SECTION_1_ROW_1_ID") // .setTitle("Title 1") // @@ -139,7 +149,7 @@ private static void listMessage(WhatsappBusinessCloudApi whatsappBusinessCloudAp .setDescription("SECTION_1_ROW_3_DESCRIPTION")) // ) // .addSection(new Section() // - .setTitle("Title 2") // + .setTitle("Section 2") // .addRow(new Row() // .setId("SECTION_2_ROW_1_ID") // .setTitle("Title 1") // @@ -152,8 +162,7 @@ private static void listMessage(WhatsappBusinessCloudApi whatsappBusinessCloudAp .setId("SECTION_2_ROW_3_ID") // .setTitle("Title 3") // .setDescription("SECTION_2_ROW_3_DESCRIPTION")) // - ) - ) // + )) // .setType(InteractiveMessageType.LIST) // .setHeader(new Header() // .setType(HeaderType.TEXT) // @@ -163,7 +172,7 @@ private static void listMessage(WhatsappBusinessCloudApi whatsappBusinessCloudAp .setFooter(new Footer() // .setText("Footer Text")) // ); - + System.out.println(new ObjectMapper().writeValueAsString(message)); MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); System.out.println(messageResponse); From cffdfedccb6386a5cc516d9344acdf92299a2e6a Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Tue, 28 Mar 2023 01:22:56 -0300 Subject: [PATCH 5/5] todo --- .../api/domain/templates/ButtonComponent.java | 1 + .../WhatsappBusinessManagementApiTest.java | 25 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/whatsapp/api/domain/templates/ButtonComponent.java b/src/main/java/com/whatsapp/api/domain/templates/ButtonComponent.java index e4aeed045..0f118964a 100644 --- a/src/main/java/com/whatsapp/api/domain/templates/ButtonComponent.java +++ b/src/main/java/com/whatsapp/api/domain/templates/ButtonComponent.java @@ -9,6 +9,7 @@ /** * The type Button component. */ +//TODO: review @JsonInclude(JsonInclude.Include.NON_NULL) public class ButtonComponent extends Component { diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java index 6f3f1c872..39d2d2b03 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java @@ -189,7 +189,7 @@ void testCreateMessageTemplate3() throws IOException, URISyntaxException, Interr Assertions.assertNotNull(response); var request = mockWebServer.takeRequest(); - //System.out.println(request.getBody().readUtf8()); + Assertions.assertEquals("POST", request.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + WABA_ID + "/message_templates", request.getPath()); @@ -219,7 +219,8 @@ void testCreateMessageTemplate4() throws IOException, URISyntaxException, Interr .setExample(new Example()// .addBodyTextExamples("Maria", "04/11/2022", "13:30")// ))// - .addComponent(new FooterComponent().setText("Utilize um dos botões abaixo para a confirmação")).addComponent(new ButtonComponent()// + .addComponent(new FooterComponent().setText("Utilize um dos botões abaixo para a confirmação"))// + .addComponent(new ButtonComponent()// .addButton(new QuickReplyButton("SIM"))// .addButton(new QuickReplyButton("NÃO"))// ); @@ -227,7 +228,7 @@ void testCreateMessageTemplate4() throws IOException, URISyntaxException, Interr whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template); var request = mockWebServer.takeRequest(); - // System.out.println(request.getBody().readUtf8()); + Assertions.assertEquals("POST", request.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + WABA_ID + "/message_templates", request.getPath()); @@ -267,7 +268,7 @@ void testCreateMessageTemplateUtility1() throws IOException, URISyntaxException, whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template); var request = mockWebServer.takeRequest(); - //System.out.println(request.getBody().readUtf8()); + Assertions.assertEquals("POST", request.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + WABA_ID + "/message_templates", request.getPath()); @@ -304,7 +305,7 @@ void testCreateMessageTemplateUtility2() throws IOException, URISyntaxException, whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template); var request = mockWebServer.takeRequest(); - //System.out.println(request.getBody().readUtf8()); + Assertions.assertEquals("POST", request.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + WABA_ID + "/message_templates", request.getPath()); @@ -341,7 +342,7 @@ void testCreateMessageTemplateAuthentication() throws IOException, URISyntaxExce whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template); var request = mockWebServer.takeRequest(); - // System.out.println(request.getBody().readUtf8()); + Assertions.assertEquals("POST", request.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + WABA_ID + "/message_templates", request.getPath()); @@ -383,7 +384,7 @@ void testCreateMessageTemplateMarketing2() throws IOException, URISyntaxExceptio whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template); var request = mockWebServer.takeRequest(); - //System.out.println(request.getBody().readUtf8()); + Assertions.assertEquals("POST", request.getMethod()); Assertions.assertEquals("/" + API_VERSION + "/" + WABA_ID + "/message_templates", request.getPath()); @@ -439,14 +440,20 @@ void testDeleteMessageTemplate() throws IOException, URISyntaxException { } @Test - void testRetrieveMessageTemplate1() throws IOException, URISyntaxException { + void testRetrieveMessageTemplate1() throws IOException, URISyntaxException, JSONException { WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN); + var expectedJson = fromResource("/retTemplate1.json"); + WhatsappBusinessManagementApi whatsappBusinessCloudApi = factory.newBusinessManagementApi(); - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/retTemplate1.json"))); + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(expectedJson)); var templates = whatsappBusinessCloudApi.retrieveTemplates(WABA_ID); + //TODO: review button + //var returnedJson = new ObjectMapper().writeValueAsString(templates); + //JSONAssert.assertEquals(expectedJson, returnedJson,JSONCompareMode.STRICT); + // data[1].components[3].buttons[0] Assertions.assertEquals(7, templates.data().size()); Assertions.assertEquals("welcome_template3", templates.data().get(0).name());