From 10bbfeee61e1a0e55a4b6c8bdc1d8b9815308868 Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Sun, 26 Mar 2023 12:01:50 -0300 Subject: [PATCH] fix video object in message template, new tests --- .../whatsapp/api/domain/messages/Video.java | 74 ++++++++++++ .../api/domain/messages/VideoParameter.java | 58 +++------ .../CreateMessageTemplate8Example.java | 19 +-- .../SendTemplateAuthMessageExample.java | 47 ++++++++ .../impl/WhatsappBusinessCloudApiTest.java | 112 ++++++++++++++++-- .../expected/message/expectedMessage7.json | 9 ++ .../expected/message/expectedMessage8.json | 34 ++++++ .../expected/message/expectedMessage9.json | 34 ++++++ 8 files changed, 324 insertions(+), 63 deletions(-) create mode 100644 src/main/java/com/whatsapp/api/domain/messages/Video.java create mode 100644 src/test/java/com/whatsapp/api/examples/SendTemplateAuthMessageExample.java create mode 100644 src/test/resources/expected/message/expectedMessage7.json create mode 100644 src/test/resources/expected/message/expectedMessage8.json create mode 100644 src/test/resources/expected/message/expectedMessage9.json diff --git a/src/main/java/com/whatsapp/api/domain/messages/Video.java b/src/main/java/com/whatsapp/api/domain/messages/Video.java new file mode 100644 index 000000000..fa35b83af --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/messages/Video.java @@ -0,0 +1,74 @@ +package com.whatsapp.api.domain.messages; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * The type Video. + */ +@JsonInclude(Include.NON_NULL) +public class Video { + + @JsonProperty("id") + private String id; + @JsonProperty("link") + private String link; + + /** + * Instantiates a new Video. + */ + public Video() { + } + + /** + * Instantiates a new Video. + * + * @param id the id + * @param link the link + */ + public Video(String id, String link) { + this.id = id; + this.link = link; + } + + /** + * Gets id. + * + * @return the id + */ + public String getId() { + return id; + } + + /** + * Sets id. + * + * @param id the id + * @return the id + */ + public Video setId(String id) { + this.id = id; + return this; + } + + /** + * Gets link. + * + * @return the link + */ + public String getLink() { + return link; + } + + /** + * Sets link. + * + * @param link the link + * @return the link + */ + public Video setLink(String link) { + this.link = link; + return this; + } +} diff --git a/src/main/java/com/whatsapp/api/domain/messages/VideoParameter.java b/src/main/java/com/whatsapp/api/domain/messages/VideoParameter.java index 23eeaf471..a373ac6cd 100644 --- a/src/main/java/com/whatsapp/api/domain/messages/VideoParameter.java +++ b/src/main/java/com/whatsapp/api/domain/messages/VideoParameter.java @@ -7,68 +7,46 @@ * The type Video parameter. */ public class VideoParameter extends Parameter { + @JsonProperty("video") + private Video video; - @JsonProperty("id") - private String id; - @JsonProperty("link") - private String link; /** * Instantiates a new Parameter. */ - protected VideoParameter() { + public VideoParameter() { super(ParameterType.VIDEO); } - /** - * Instantiates a new Video parameter. - * - * @param id the id - * @param link the link - */ - public VideoParameter(String id, String link) { - super(ParameterType.VIDEO); - this.id = id; - this.link = link; - } /** - * Gets id. - * - * @return the id - */ - public String getId() { - return id; - } - - /** - * Sets id. + * Instantiates a new Video parameter. * - * @param id the id - * @return the id + * @param type the type + * @param video the video */ - public VideoParameter setId(String id) { - this.id = id; - return this; + public VideoParameter(ParameterType type, Video video) { + super(type); + this.video = video; } /** - * Gets link. + * Gets video. * - * @return the link + * @return the video */ - public String getLink() { - return link; + public Video getVideo() { + return video; } /** - * Sets link. + * Sets video. * - * @param link the link - * @return the link + * @param video the video + * @return the video */ - public VideoParameter setLink(String link) { - this.link = link; + public VideoParameter setVideo(Video video) { + this.video = video; return this; } } diff --git a/src/test/java/com/whatsapp/api/examples/CreateMessageTemplate8Example.java b/src/test/java/com/whatsapp/api/examples/CreateMessageTemplate8Example.java index fcd51f3a9..4cf37b0c4 100644 --- a/src/test/java/com/whatsapp/api/examples/CreateMessageTemplate8Example.java +++ b/src/test/java/com/whatsapp/api/examples/CreateMessageTemplate8Example.java @@ -4,12 +4,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.whatsapp.api.WhatsappApiFactory; import com.whatsapp.api.domain.templates.BodyComponent; -import com.whatsapp.api.domain.templates.ButtonComponent; import com.whatsapp.api.domain.templates.Example; import com.whatsapp.api.domain.templates.FooterComponent; import com.whatsapp.api.domain.templates.HeaderComponent; import com.whatsapp.api.domain.templates.MessageTemplate; -import com.whatsapp.api.domain.templates.QuickReplyButton; import com.whatsapp.api.domain.templates.type.Category; import com.whatsapp.api.domain.templates.type.HeaderFormat; import com.whatsapp.api.domain.templates.type.LanguageType; @@ -27,25 +25,18 @@ public static void main(String[] args) throws JsonProcessingException { var template = new MessageTemplate(); - template.setName("auth_code_2")// + template.setName("auth_app")// .setCategory(Category.AUTHENTICATION)// .setLanguage(LanguageType.EN_US)// .addComponent(new HeaderComponent()// - .setFormat(HeaderFormat.TEXT).setText("Your authentication code for {{1}}")// - .setExample(new Example().addHeaderTextExamples("App X")))// + .setFormat(HeaderFormat.IMAGE).setExample(new Example().addHeaderHandleExamples("4::aW1hZ2UvanBlZw==:ARbzAaeBdkTpPbPQcqoRsHvmI1iJnyxkjZBXVACmVZBGfIOSNOqujojIUCIciq0OttnlTqKZNfbTV81PmOJ2t-eIrJ0MCQFNP5pfMJvAdd-PZQ:e:1680186259:3449824985304722:100002914375136:ARYJQLBIIVIUS1MCM1w")) + )// .addComponent(new BodyComponent()// - .setText("Please use the code {{1}} to sign in to your account. Do not provide this code to third parties.")// + .setText("Hello, scan the QR code or use the code {{1}} to authenticate on the website.")// .setExample(new Example()// .addBodyTextExamples("784-H45-7R4")))// - .addComponent(new FooterComponent().setText("Did you not request the code? Click on 'Not me'"))// - .addComponent(new ButtonComponent()// - .addButton(new QuickReplyButton("Not me"))// - - - )// - + .addComponent(new FooterComponent().setText("Do not share this message with anyone.'"));// - ; System.out.println(new ObjectMapper().writeValueAsString(template)); var response = whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template); diff --git a/src/test/java/com/whatsapp/api/examples/SendTemplateAuthMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendTemplateAuthMessageExample.java new file mode 100644 index 000000000..bf8c1fd7a --- /dev/null +++ b/src/test/java/com/whatsapp/api/examples/SendTemplateAuthMessageExample.java @@ -0,0 +1,47 @@ +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.BodyComponent; +import com.whatsapp.api.domain.messages.HeaderComponent; +import com.whatsapp.api.domain.messages.Image; +import com.whatsapp.api.domain.messages.ImageParameter; +import com.whatsapp.api.domain.messages.Language; +import com.whatsapp.api.domain.messages.Message.MessageBuilder; +import com.whatsapp.api.domain.messages.TemplateMessage; +import com.whatsapp.api.domain.messages.TextParameter; +import com.whatsapp.api.domain.templates.type.LanguageType; +import com.whatsapp.api.impl.WhatsappBusinessCloudApi; + +import static com.whatsapp.api.TestConstants.PHONE_NUMBER_1; +import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID; + +public class SendTemplateAuthMessageExample { + public static void main(String[] args) throws JsonProcessingException { + WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN); + + WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi(); + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildTemplateMessage(// + new TemplateMessage()// + .setLanguage(new Language(LanguageType.PT_BR))// + .setName("auth_app")// + .addComponent(new HeaderComponent()// + .addParameter(new ImageParameter()// + .setImage(new Image("554066036582230", null)) + ) + + ).addComponent(// + new BodyComponent()// + .addParameter(new TextParameter("T87-G74-876")))// + + ); + System.out.println(new ObjectMapper().writeValueAsString(message)); + + whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + } +} diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index 8e38dd5cd..03799fa6b 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -24,7 +24,9 @@ import com.whatsapp.api.domain.messages.TemplateMessage; import com.whatsapp.api.domain.messages.TextMessage; import com.whatsapp.api.domain.messages.TextParameter; +import com.whatsapp.api.domain.messages.Video; import com.whatsapp.api.domain.messages.VideoMessage; +import com.whatsapp.api.domain.messages.VideoParameter; import com.whatsapp.api.domain.messages.type.ButtonSubType; import com.whatsapp.api.domain.messages.type.CalendarType; import com.whatsapp.api.domain.templates.type.LanguageType; @@ -335,6 +337,73 @@ void testSendTemplateDocumentPdfMessage() throws IOException, URISyntaxException } + @Test + void testSendTemplateVideoMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage8.json"); + + var templateMessage = new TemplateMessage()// + .setLanguage(new Language(LanguageType.EN_US))// + .setName("video_tm")// + .addComponent(new HeaderComponent()// + .addParameter(new VideoParameter()// + .setVideo(new Video()// + .setId("4548775454857854")))// + ).addComponent(// + new BodyComponent()// + .addParameter(new TextParameter("Mauricio Binda")))// + ; + + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildTemplateMessage(templateMessage); + + 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()); + //System.out.println(recordedRequest.getBody().readUtf8()); + + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); + + } + + @Test + void testSendTemplateAuthMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage9.json"); + + var templateMessage = new TemplateMessage()// + .setLanguage(new Language(LanguageType.PT_BR))// + .setName("auth_app")// + .addComponent(new HeaderComponent()// + .addParameter(new ImageParameter()// + .setImage(new Image("554066036582230", null))) + + ).addComponent(// + new BodyComponent()// + .addParameter(new TextParameter("T87-G74-876")));// + + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildTemplateMessage(templateMessage); + + 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()); + //System.out.println(recordedRequest.getBody().readUtf8()); + + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); + + } + @Test void testSendAudioMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { @@ -362,14 +431,25 @@ void testSendAudioMessage() throws IOException, URISyntaxException, InterruptedE } @Test - void testSendAudioLinkMessage() throws IOException, URISyntaxException, InterruptedException { + void testSendAudioLinkMessage() throws InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); - var link = "https://testeteste778787878.com/audio.mp3"; + var expectedJson = """ + { + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "audio", + "audio": { + "link": "https://testeteste778787878.com/audio.mp3" + } + } + """; + var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildAudioMessage(new AudioMessage()// - .setLink(link)); + .setLink("https://testeteste778787878.com/audio.mp3")); var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); @@ -377,16 +457,31 @@ void testSendAudioLinkMessage() throws IOException, URISyntaxException, Interrup 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\":\"audio\",\"audio\":{\"link\":\"%s\"}}", PHONE_NUMBER_1, link), recordedRequest.getBody().readUtf8()); Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); } @Test - void testSendVideoMessage() throws IOException, URISyntaxException, InterruptedException { + void testSendVideoMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + var expectedJson = """ + { + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "video", + "video": { + "id": "78795489879879554", + "caption": "See the video" + } + } + """; + var message = MessageBuilder.builder()// .setTo(PHONE_NUMBER_1)// .buildVideoMessage(new VideoMessage()// @@ -394,16 +489,15 @@ void testSendVideoMessage() throws IOException, URISyntaxException, InterruptedE .setCaption("See the video")); - 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()); + // System.out.println(recordedRequest.getBody().readUtf8()); + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); - Assertions.assertEquals(String.format("{\"messaging_product\":\"whatsapp\",\"recipient_type\":\"individual\",\"to\":\"%s\",\"type\":\"video\",\"video\":{\"id\":\"78795489879879554\",\"caption\":\"See the video\"}}", PHONE_NUMBER_1), recordedRequest.getBody().readUtf8()); - - Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); } @Test diff --git a/src/test/resources/expected/message/expectedMessage7.json b/src/test/resources/expected/message/expectedMessage7.json new file mode 100644 index 000000000..892ae1e02 --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage7.json @@ -0,0 +1,9 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "audio", + "audio": { + "id": "4545454545454" + } +} \ No newline at end of file diff --git a/src/test/resources/expected/message/expectedMessage8.json b/src/test/resources/expected/message/expectedMessage8.json new file mode 100644 index 000000000..b3e0a94f9 --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage8.json @@ -0,0 +1,34 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "template", + "template": { + "components": [ + { + "type": "header", + "parameters": [ + { + "type": "video", + "video": { + "id": "4548775454857854" + } + } + ] + }, + { + "type": "body", + "parameters": [ + { + "type": "text", + "text": "Mauricio Binda" + } + ] + } + ], + "name": "video_tm", + "language": { + "code": "en_US" + } + } +} \ No newline at end of file diff --git a/src/test/resources/expected/message/expectedMessage9.json b/src/test/resources/expected/message/expectedMessage9.json new file mode 100644 index 000000000..39bed325e --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage9.json @@ -0,0 +1,34 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "template", + "template": { + "components": [ + { + "type": "header", + "parameters": [ + { + "type": "image", + "image": { + "id": "554066036582230" + } + } + ] + }, + { + "type": "body", + "parameters": [ + { + "type": "text", + "text": "T87-G74-876" + } + ] + } + ], + "name": "auth_app", + "language": { + "code": "pt_BR" + } + } +} \ No newline at end of file