diff --git a/src/main/java/com/whatsapp/api/domain/messages/Document.java b/src/main/java/com/whatsapp/api/domain/messages/Document.java new file mode 100644 index 000000000..72efb301d --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/messages/Document.java @@ -0,0 +1,99 @@ +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 Document. + */ +@JsonInclude(Include.NON_NULL) +public class Document { + + @JsonProperty("id") + private String id; + @JsonProperty("link") + private String link; + + @JsonProperty("filename") + private String fileName; + + /** + * Instantiates a new Document. + * + * @param id the id + * @param link the link + * @param fileName the file name + */ + public Document(String id, String link, String fileName) { + this.id = id; + this.link = link; + this.fileName = fileName; + } + + /** + * Instantiates a new Document. + */ + public Document() { + } + + /** + * Gets id. + * + * @return the id + */ + public String getId() { + return id; + } + + /** + * Sets id. + * + * @param id the id + * @return the id + */ + public Document 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 Document setLink(String link) { + this.link = link; + return this; + } + + /** + * Gets file name. + * + * @return the file name + */ + public String getFileName() { + return fileName; + } + + /** + * Sets file name. + * + * @param fileName the file name + * @return the file name + */ + public Document setFileName(String fileName) { + this.fileName = fileName; + return this; + } +} diff --git a/src/main/java/com/whatsapp/api/domain/messages/DocumentParameter.java b/src/main/java/com/whatsapp/api/domain/messages/DocumentParameter.java index 426a92e91..e0e89c1ab 100644 --- a/src/main/java/com/whatsapp/api/domain/messages/DocumentParameter.java +++ b/src/main/java/com/whatsapp/api/domain/messages/DocumentParameter.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.annotation.JsonProperty; import com.whatsapp.api.domain.messages.type.ParameterType; /** @@ -11,10 +10,7 @@ @JsonInclude(Include.NON_NULL) public class DocumentParameter extends Parameter { - @JsonProperty("id") - private String id; - @JsonProperty("link") - private String link; + private Document document; /** @@ -24,55 +20,34 @@ public DocumentParameter() { super(ParameterType.DOCUMENT); } + /** * Instantiates a new Document parameter. * - * @param id the id - * @param link the link + * @param document the document */ - public DocumentParameter(String id, String link) { + public DocumentParameter(Document document) { super(ParameterType.DOCUMENT); - 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 DocumentParameter setId(String id) { - this.id = id; - return this; + this.document = document; } /** - * Gets link. + * Gets document. * - * @return the link + * @return the document */ - public String getLink() { - return link; + public Document getDocument() { + return document; } /** - * Sets link. + * Sets document. * - * @param link the link - * @return the link + * @param document the document + * @return the document */ - public DocumentParameter setLink(String link) { - this.link = link; + public DocumentParameter setDocument(Document document) { + this.document = document; return this; } } diff --git a/src/test/java/com/whatsapp/api/examples/SendTemplateUtilityDocumentMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendTemplateUtilityDocumentMessageExample.java new file mode 100644 index 000000000..af481f8bd --- /dev/null +++ b/src/test/java/com/whatsapp/api/examples/SendTemplateUtilityDocumentMessageExample.java @@ -0,0 +1,54 @@ +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.ButtonComponent; +import com.whatsapp.api.domain.messages.ButtonTextParameter; +import com.whatsapp.api.domain.messages.Document; +import com.whatsapp.api.domain.messages.DocumentParameter; +import com.whatsapp.api.domain.messages.HeaderComponent; +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.messages.type.ButtonSubType; +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 SendTemplateUtilityDocumentMessageExample { + 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.EN_US))// + .setName("new_classes_pdf")// + .addComponent(new HeaderComponent()// + .addParameter(new DocumentParameter()// + .setDocument(new Document()// + .setFileName("Class.pdf").setId("928860901494862")// + ))// + ).addComponent(// + new BodyComponent()// + .addParameter(new TextParameter("Mauricio Binda")))// + .addComponent(new ButtonComponent()// + .setIndex(0)// + .setSubType(ButtonSubType.URL)// + .addParameter(new ButtonTextParameter("career-academy/?trk_ref=globalnav")))// + + ); + System.out.println(new ObjectMapper().writeValueAsString(message)); + + whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + } +} diff --git a/src/test/java/com/whatsapp/api/exception/WhatsappApiExceptionTest.java b/src/test/java/com/whatsapp/api/exception/WhatsappApiExceptionTest.java index dd0643ee1..5579daace 100644 --- a/src/test/java/com/whatsapp/api/exception/WhatsappApiExceptionTest.java +++ b/src/test/java/com/whatsapp/api/exception/WhatsappApiExceptionTest.java @@ -33,8 +33,8 @@ void testConstructor2() { "An error occurred"))); assertNull(actualWhatsappApiException.getCause()); assertEquals(0, actualWhatsappApiException.getSuppressed().length); - assertEquals("Not all who wander are lost | Details", actualWhatsappApiException.getMessage()); - assertEquals("Not all who wander are lost | Details", actualWhatsappApiException.getLocalizedMessage()); + assertEquals("[1] Not all who wander are lost | Details", actualWhatsappApiException.getMessage()); + assertEquals("[1] Not all who wander are lost | Details", actualWhatsappApiException.getLocalizedMessage()); } /** @@ -62,17 +62,17 @@ void testConstructor3() { @Test void testGetMessage() { assertNull((new WhatsappApiException()).getMessage()); - assertEquals("Not all who wander are lost | Details", + assertEquals("[1] Not all who wander are lost | Details", (new WhatsappApiException( new WhatsappApiError(new Error(1, "Details", -1, "42", "Not all who wander are lost", "Messaging Product", new ErrorData("Messaging Product", "Details", "Blame Field Specs"), "Type", true, "Dr", "An error occurred")))).getMessage()); - assertEquals("Not all who wander are lost | An error occurred", + assertEquals("[1] Not all who wander are lost | An error occurred", (new WhatsappApiException( new WhatsappApiError(new Error(1, "Details", -1, "42", "Not all who wander are lost", "Messaging Product", new ErrorData("Messaging Product", null, "Blame Field Specs"), "Type", true, "Dr", "An error occurred")))).getMessage()); - assertEquals("Not all who wander are lost | An error occurred", + assertEquals("[1] Not all who wander are lost | An error occurred", (new WhatsappApiException(new WhatsappApiError(new Error(1, "Details", -1, "42", "Not all who wander are lost", "Messaging Product", null, "Type", true, "Dr", "An error occurred")))) .getMessage()); diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index dd63e4528..4881b0bfb 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -6,11 +6,14 @@ import com.whatsapp.api.domain.messages.BodyComponent; import com.whatsapp.api.domain.messages.ButtonComponent; import com.whatsapp.api.domain.messages.ButtonPayloadParameter; +import com.whatsapp.api.domain.messages.ButtonTextParameter; import com.whatsapp.api.domain.messages.Currency; import com.whatsapp.api.domain.messages.CurrencyParameter; import com.whatsapp.api.domain.messages.DateTime; import com.whatsapp.api.domain.messages.DateTimeParameter; +import com.whatsapp.api.domain.messages.Document; import com.whatsapp.api.domain.messages.DocumentMessage; +import com.whatsapp.api.domain.messages.DocumentParameter; import com.whatsapp.api.domain.messages.HeaderComponent; import com.whatsapp.api.domain.messages.Image; import com.whatsapp.api.domain.messages.ImageMessage; @@ -294,6 +297,44 @@ void testSendTemplateButtonMessageMarketing() throws IOException, URISyntaxExcep } + @Test + void testSendTemplateDocumentPdfMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage6.json"); + + var templateMessage = new TemplateMessage()// + .setLanguage(new Language(LanguageType.EN_US))// + .setName("new_classes_pdf")// + .addComponent(new HeaderComponent()// + .addParameter(new DocumentParameter()// + .setDocument(new Document()// + .setFileName("Class.pdf").setId("928860901494862")// + ))// + ).addComponent(// + new BodyComponent()// + .addParameter(new TextParameter("Mauricio Binda")))// + .addComponent(new ButtonComponent()// + .setIndex(0)// + .setSubType(ButtonSubType.URL)// + .addParameter(new ButtonTextParameter("career-academy/?trk_ref=globalnav"))); + + + 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 { diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java index 562178903..fe21a1c53 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java @@ -621,7 +621,7 @@ void requestCodeError() throws IOException, URISyntaxException, InterruptedExcep Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/request_code", recordedRequest.getPath()); Assertions.assertEquals("{\"code_method\":\"SMS\",\"language\":\"en_US\"}", recordedRequest.getBody().readUtf8()); - Assertions.assertEquals("Request code error | Tente novamente depois de um tempo.", ex.getMessage()); + Assertions.assertEquals("[136024] Request code error | Tente novamente depois de um tempo.", ex.getMessage()); } @@ -665,7 +665,7 @@ void verifyCodeError() throws IOException, URISyntaxException, InterruptedExcept Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/verify_code", recordedRequest.getPath()); Assertions.assertEquals("{\"code\":\"12345678\"}", recordedRequest.getBody().readUtf8()); - Assertions.assertEquals("Verify code error | O código inserido está incorreto.", ex.getMessage()); + Assertions.assertEquals("[136025] Verify code error | O código inserido está incorreto.", ex.getMessage()); } diff --git a/src/test/resources/expected/message/expectedMessage6.json b/src/test/resources/expected/message/expectedMessage6.json new file mode 100644 index 000000000..29d3ea80a --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage6.json @@ -0,0 +1,46 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "template", + "template": { + "components": [ + { + "type": "header", + "parameters": [ + { + "document": { + "id": "928860901494862", + "filename": "Class.pdf" + }, + "type": "document" + } + ] + }, + { + "type": "body", + "parameters": [ + { + "type": "text", + "text": "Mauricio Binda" + } + ] + }, + { + "type": "button", + "parameters": [ + { + "type": "text", + "text": "career-academy/?trk_ref=globalnav" + } + ], + "index": 0, + "sub_type": "url" + } + ], + "name": "new_classes_pdf", + "language": { + "code": "en_US" + } + } +} \ No newline at end of file