Skip to content

Commit

Permalink
fixes #59
Browse files Browse the repository at this point in the history
- fix Document objetct in message templates;
- fix tests;
- add new tests.
  • Loading branch information
Bindambc committed Mar 25, 2023
1 parent 181ca9c commit d60a409
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 46 deletions.
99 changes: 99 additions & 0 deletions src/main/java/com/whatsapp/api/domain/messages/Document.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;


/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}

Expand Down Expand Up @@ -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());

}

Expand Down
Loading

0 comments on commit d60a409

Please sign in to comment.