Skip to content

Commit 1c740ff

Browse files
authored
Merge pull request #244 from messagebird/Added_update_whatsapp_template
Added support for updating whatsapp template
2 parents 429248b + 989b090 commit 1c740ff

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

api/src/main/java/com/messagebird/MessageBirdClient.java

+25
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,31 @@ public TemplateResponse createWhatsAppTemplate(final Template template)
18761876
return messageBirdService.sendPayLoad(url, template, TemplateResponse.class);
18771877
}
18781878

1879+
/**
1880+
* Update a WhatsApp message template through MessageBird.
1881+
*
1882+
* @param template {@link Template} object to be created
1883+
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
1884+
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
1885+
* @return {@link TemplateResponse} response object
1886+
* @throws UnauthorizedException if client is unauthorized
1887+
* @throws GeneralException general exception
1888+
* @throws IllegalArgumentException invalid template format
1889+
*/
1890+
public TemplateResponse updateWhatsAppTemplate(final Template template, final String templateName, final String language)
1891+
throws UnauthorizedException, GeneralException, IllegalArgumentException {
1892+
template.validate();
1893+
1894+
String url = String.format(
1895+
"%s%s%s/%s/%s",
1896+
INTEGRATIONS_BASE_URL_V2,
1897+
INTEGRATIONS_WHATSAPP_PATH,
1898+
TEMPLATES_PATH,
1899+
templateName,
1900+
language);
1901+
1902+
return messageBirdService.sendPayLoad("PUT",url, template, TemplateResponse.class);
1903+
}
18791904
/**
18801905
* Gets a WhatsAppTemplate listing with specified pagination options.
18811906
*

api/src/test/java/com/messagebird/MessageBirdClientTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,44 @@ public void testCreateWhatsAppTemplate() throws UnauthorizedException, GeneralEx
10771077
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
10781078
}
10791079
}
1080+
@Test
1081+
public void testUpdateWhatsAppTemplate() throws UnauthorizedException, GeneralException {
1082+
final TemplateResponse templateResponse = TestUtil.createWhatsAppTemplateResponse("sample_template_name", "ko");
1083+
final Template template = TestUtil.createWhatsAppTemplate("sample_template_name", "ko");
1084+
1085+
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
1086+
MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock);
1087+
1088+
String url = String.format(
1089+
"%s%s%s/%s/%s",
1090+
INTEGRATIONS_BASE_URL_V2,
1091+
INTEGRATIONS_WHATSAPP_PATH,
1092+
TEMPLATES_PATH,
1093+
"sample_template_name",
1094+
"ko"
1095+
);
1096+
1097+
when(messageBirdServiceMock.sendPayLoad("PUT",url, template, TemplateResponse.class))
1098+
.thenReturn(templateResponse);
1099+
1100+
final TemplateResponse response = messageBirdClientInjectMock.updateWhatsAppTemplate(template,"sample_template_name","ko");
1101+
verify(messageBirdServiceMock, times(1)).sendPayLoad("PUT",url, template, TemplateResponse.class);
1102+
assertNotNull(response);
1103+
assertEquals(response.getName(), templateResponse.getName());
1104+
assertEquals(response.getLanguage(), templateResponse.getLanguage());
1105+
assertEquals(response.getCategory(), templateResponse.getCategory());
1106+
assertEquals(response.getStatus(), templateResponse.getStatus());
1107+
assertEquals(response.getWabaID(), templateResponse.getWabaID());
1108+
assertEquals(response.getCreatedAt(), templateResponse.getCreatedAt());
1109+
assertEquals(response.getUpdatedAt(), templateResponse.getUpdatedAt());
1110+
1111+
/* verify components */
1112+
for (int i = 0; i < response.getComponents().size(); i++) {
1113+
assertEquals(response.getComponents().get(i).getType(), templateResponse.getComponents().get(i).getType());
1114+
assertEquals(response.getComponents().get(i).getFormat(), templateResponse.getComponents().get(i).getFormat());
1115+
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
1116+
}
1117+
}
10801118

10811119
@Test
10821120
public void testListWhatsAppTemplates() throws UnauthorizedException, GeneralException {

0 commit comments

Comments
 (0)