Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for updating whatsapp template #244

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions api/src/main/java/com/messagebird/MessageBirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,31 @@ public TemplateResponse createWhatsAppTemplate(final Template template)
return messageBirdService.sendPayLoad(url, template, TemplateResponse.class);
}

/**
* Update a WhatsApp message template through MessageBird.
*
* @param template {@link Template} object to be created
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
* @return {@link TemplateResponse} response object
* @throws UnauthorizedException if client is unauthorized
* @throws GeneralException general exception
* @throws IllegalArgumentException invalid template format
*/
public TemplateResponse updateWhatsAppTemplate(final Template template, final String templateName, final String language)
throws UnauthorizedException, GeneralException, IllegalArgumentException {
template.validate();

String url = String.format(
"%s%s%s/%s/%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH,
templateName,
language);

return messageBirdService.sendPayLoad("PUT",url, template, TemplateResponse.class);
}
/**
* Gets a WhatsAppTemplate listing with specified pagination options.
*
Expand Down
38 changes: 38 additions & 0 deletions api/src/test/java/com/messagebird/MessageBirdClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,44 @@ public void testCreateWhatsAppTemplate() throws UnauthorizedException, GeneralEx
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
}
}
@Test
public void testUpdateWhatsAppTemplate() throws UnauthorizedException, GeneralException {
final TemplateResponse templateResponse = TestUtil.createWhatsAppTemplateResponse("sample_template_name", "ko");
final Template template = TestUtil.createWhatsAppTemplate("sample_template_name", "ko");

MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock);

String url = String.format(
"%s%s%s/%s/%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH,
"sample_template_name",
"ko"
);

when(messageBirdServiceMock.sendPayLoad("PUT",url, template, TemplateResponse.class))
.thenReturn(templateResponse);

final TemplateResponse response = messageBirdClientInjectMock.updateWhatsAppTemplate(template,"sample_template_name","ko");
verify(messageBirdServiceMock, times(1)).sendPayLoad("PUT",url, template, TemplateResponse.class);
assertNotNull(response);
assertEquals(response.getName(), templateResponse.getName());
assertEquals(response.getLanguage(), templateResponse.getLanguage());
assertEquals(response.getCategory(), templateResponse.getCategory());
assertEquals(response.getStatus(), templateResponse.getStatus());
assertEquals(response.getWabaID(), templateResponse.getWabaID());
assertEquals(response.getCreatedAt(), templateResponse.getCreatedAt());
assertEquals(response.getUpdatedAt(), templateResponse.getUpdatedAt());

/* verify components */
for (int i = 0; i < response.getComponents().size(); i++) {
assertEquals(response.getComponents().get(i).getType(), templateResponse.getComponents().get(i).getType());
assertEquals(response.getComponents().get(i).getFormat(), templateResponse.getComponents().get(i).getFormat());
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
}
}

@Test
public void testListWhatsAppTemplates() throws UnauthorizedException, GeneralException {
Expand Down