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

APP-3073: Enhancements on MessageService #277

Merged
merged 18 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
214d830
Add internal dependencies version to bom module
symphony-hong Oct 1, 2020
4220b9c
Update missing dependencies
symphony-hong Oct 2, 2020
a9c2f47
Merge branch 'master' of https://github.com/SymphonyPlatformSolutions…
symphony-hong Oct 5, 2020
b3ad26d
Adding plugin to http and template submodule
symphony-hong Oct 5, 2020
72bb76c
Ability to directly set privateKey and certificate content
symphony-hong Oct 6, 2020
ca67543
Merge branch 'master' of https://github.com/SymphonyPlatformSolutions…
symphony-hong Oct 6, 2020
8991945
Fix typo and add documentation
symphony-hong Oct 6, 2020
ba7cef6
Update documentation
symphony-hong Oct 6, 2020
ca0035c
Enhancements on message service
symphony-hong Oct 7, 2020
8503e8f
Merge branch 'master' of https://github.com/SymphonyPlatformSolutions…
symphony-hong Oct 7, 2020
98c3b3d
Merge branch 'master' into APP-3073
symphony-hong Oct 7, 2020
fbe6676
Create message and attachment model to use in MessageService
symphony-hong Oct 8, 2020
edd4225
Merge branch 'master' of https://github.com/SymphonyPlatformSolutions…
symphony-hong Oct 8, 2020
b99a085
Merge branch 'APP-3073' of github.com:symphony-hong/symphony-api-clie…
symphony-hong Oct 8, 2020
09b9aef
Refactor MessageService and update unittest
symphony-hong Oct 8, 2020
865bf89
Provide MessageService#builder to build a message instance from Messa…
symphony-hong Oct 9, 2020
5fba2fa
Merge branch 'master' into APP-3073
symphony-hong Oct 9, 2020
d837eda
Update handling attachments
symphony-hong Oct 9, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.symphony.bdk.core.client.ApiClientFactory;
import com.symphony.bdk.core.config.model.BdkConfig;
import com.symphony.bdk.core.retry.RetryWithRecoveryBuilder;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.message.MessageService;
thibauult marked this conversation as resolved.
Show resolved Hide resolved
import com.symphony.bdk.core.service.SessionService;
import com.symphony.bdk.core.service.datafeed.DatafeedService;
import com.symphony.bdk.core.service.datafeed.DatafeedVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.symphony.bdk.core.auth.exception.AuthUnauthorizedException;
import com.symphony.bdk.core.client.ApiClientFactory;
import com.symphony.bdk.core.config.model.BdkConfig;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.message.MessageService;
import com.symphony.bdk.core.service.SessionService;
import com.symphony.bdk.core.service.datafeed.DatafeedService;
import com.symphony.bdk.core.service.stream.StreamService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.symphony.bdk.core.service;
package com.symphony.bdk.core.service.message;


import static java.util.Collections.emptyMap;

import com.symphony.bdk.core.auth.AuthSession;
import com.symphony.bdk.core.retry.RetryWithRecovery;
import com.symphony.bdk.core.retry.RetryWithRecoveryBuilder;
Expand Down Expand Up @@ -33,6 +35,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apiguardian.api.API;

import java.io.File;
import java.time.Instant;
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -178,11 +181,22 @@ public V4Message send(@Nonnull V4Stream stream, @Nonnull String message) {
* @return a {@link V4Message} object containing the details of the sent message
* @see <a href="https://developers.symphony.com/restapi/reference#create-message-v4">Create Message v4</a>
*/
public V4Message send(@Nonnull V4Stream stream, @Nonnull String template, Object parameters)
throws TemplateException {
public V4Message sendWithTemplate(@Nonnull V4Stream stream, @Nonnull String template, Object parameters) {
return send(stream.getStreamId(), templateResolver.resolve(template).process(parameters));
}

/**
* Sends a templated to the stream ID of the passed {@link V4Stream} object.
*
* @param stream the stream to send the message to
* @param template the template name to be used to produce the message
* @return a {@link V4Message} object containing the details of the sent message
* @throws TemplateException
*/
public V4Message sendWithTemplate(@Nonnull V4Stream stream, @Nonnull String template) {
symphony-hong marked this conversation as resolved.
Show resolved Hide resolved
return sendWithTemplate(stream, template, emptyMap());
}

/**
* Sends a templated to the stream ID passed in parameter.
*
Expand All @@ -192,11 +206,22 @@ public V4Message send(@Nonnull V4Stream stream, @Nonnull String template, Object
* @return a {@link V4Message} object containing the details of the sent message
* @throws TemplateException
*/
public V4Message send(@Nonnull String streamId, @Nonnull String template, Object parameters)
throws TemplateException {
public V4Message sendWithTemplate(@Nonnull String streamId, @Nonnull String template, Object parameters) {
return send(streamId, templateResolver.resolve(template).process(parameters));
}

/**
* Sends a templated to the stream ID passed in parameter.
*
* @param streamId the ID of the stream to send the message to
* @param template the template name to be used to produce the message
* @return a {@link V4Message} object containing the details of the sent message
* @throws TemplateException
*/
public V4Message sendWithTemplate(@Nonnull String streamId, @Nonnull String template) {
return sendWithTemplate(streamId, template, emptyMap());
}

/**
* Sends a message to the stream ID passed in parameter.
*
Expand All @@ -206,18 +231,46 @@ public V4Message send(@Nonnull String streamId, @Nonnull String template, Object
* @see <a href="https://developers.symphony.com/restapi/reference#create-message-v4">Create Message v4</a>
*/
public V4Message send(@Nonnull String streamId, @Nonnull String message) {
return send(streamId, message, null, null);
}

/**
* Sends a message to the stream ID passed in parameter.
*
* @param streamId the ID of the stream to send the message to
* @param message the message payload in MessageML
* @param data the data to be send with the message
* @param attachment the attachment of the message
* @return a {@link V4Message} object containing the details of the sent message
* @see <a href="https://developers.symphony.com/restapi/reference#create-message-v4">Create Message v4</a>
*/
public V4Message send(@Nonnull String streamId, @Nonnull String message, String data, File attachment) {
symphony-hong marked this conversation as resolved.
Show resolved Hide resolved
return executeAndRetry("send", () -> messagesApi.v4StreamSidMessageCreatePost(
streamId,
authSession.getSessionToken(),
authSession.getKeyManagerToken(),
message,
data,
null,
null,
null,
attachment,
null
));
}

/**
* Sends a message to the stream ID passed in parameter.
*
* @param stream the stream to send the message to
* @param message the message payload in MessageML
* @param data the data to be send with the message
* @param attachment the attachment of the message
* @return a {@link V4Message} object containing the details of the sent message
* @see <a href="https://developers.symphony.com/restapi/reference#create-message-v4">Create Message v4</a>
*/
public V4Message send(@Nonnull V4Stream stream, @Nonnull String message, String data, File attachment) {
return send(stream.getStreamId(), message, data, attachment);
}

/**
* Downloads the attachment body by the stream ID, message ID and attachment ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.symphony.bdk.core.config.model.BdkConfig;

import com.symphony.bdk.core.config.model.BdkDatafeedConfig;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.message.MessageService;
import com.symphony.bdk.core.service.SessionService;
import com.symphony.bdk.core.service.datafeed.DatafeedService;
import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.symphony.bdk.core.config.BdkConfigLoader;
import com.symphony.bdk.core.config.exception.BdkConfigException;
import com.symphony.bdk.core.config.model.BdkConfig;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.message.MessageService;
import com.symphony.bdk.core.service.datafeed.DatafeedService;
import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV1;
import com.symphony.bdk.core.service.stream.StreamService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.symphony.bdk.core.auth.AuthSession;
import com.symphony.bdk.core.retry.RetryWithRecoveryBuilder;
import com.symphony.bdk.core.service.message.MessageService;
import com.symphony.bdk.core.service.stream.constant.AttachmentSort;
import com.symphony.bdk.core.test.JsonHelper;
import com.symphony.bdk.core.test.MockApiClient;
Expand All @@ -42,10 +43,14 @@
import com.symphony.bdk.template.api.TemplateEngine;
import com.symphony.bdk.template.api.TemplateException;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -167,34 +172,84 @@ void testSendWithStreamObjectCallsSendWithStreamId() {
}

@Test
void testSendWithTemplateAndStreamObjectCallsSendWithCorrectStreamIdAndMessage() throws TemplateException {
void testSendWithTemplateAndStreamObjectCallsSendWithCorrectStreamIdAndMessage() {
when(templateEngine.newTemplateFromClasspath(eq(TEMPLATE_NAME))).thenReturn(parameters -> MESSAGE);

MessageService service = spy(messageService);
doReturn(new V4Message()).when(service).send(anyString(), anyString());

final V4Stream v4Stream = new V4Stream().streamId(STREAM_ID);

assertNotNull(service.send(v4Stream, TEMPLATE_NAME, Collections.emptyMap()));
assertNotNull(service.sendWithTemplate(v4Stream, TEMPLATE_NAME, Collections.emptyMap()));
verify(service).send(eq(STREAM_ID), eq(MESSAGE));
}

@Test
void testSendWithTemplateCallsSendWithCorrectMessage() throws TemplateException {
void testSendWithTemplateCallsSendWithCorrectMessage() {
when(templateEngine.newTemplateFromClasspath(eq(TEMPLATE_NAME))).thenReturn(parameters -> MESSAGE);

MessageService service = spy(messageService);
doReturn(new V4Message()).when(service).send(anyString(), anyString());

assertNotNull(service.send(STREAM_ID, TEMPLATE_NAME, Collections.emptyMap()));
assertNotNull(service.sendWithTemplate(STREAM_ID, TEMPLATE_NAME, Collections.emptyMap()));
verify(service).send(eq(STREAM_ID), eq(MESSAGE));
}

@Test
void testSendWithTemplateThrowingTemplateException() throws TemplateException {
void testSendWithTemplateThrowingTemplateException() {
when(templateEngine.newTemplateFromClasspath(eq(TEMPLATE_NAME))).thenThrow(new TemplateException("error"));

assertThrows(TemplateException.class, () -> messageService.send(STREAM_ID, TEMPLATE_NAME, Collections.emptyMap()));
assertThrows(TemplateException.class,
() -> messageService.sendWithTemplate(STREAM_ID, TEMPLATE_NAME, Collections.emptyMap()));
}

@Test
void testSendWithTemplateObjectEmptyToStream() {
when(templateEngine.newTemplateFromClasspath(eq(TEMPLATE_NAME))).thenReturn(parameters -> MESSAGE);
MessageService service = spy(messageService);
doReturn(new V4Message()).when(service).send(anyString(), anyString());

final V4Stream v4Stream = new V4Stream().streamId(STREAM_ID);

assertNotNull(service.sendWithTemplate(v4Stream, TEMPLATE_NAME));
verify(service).send(eq(STREAM_ID), eq(MESSAGE));
}

@Test
void testSendWithTemplateObjectEmptyToStreamId() {
when(templateEngine.newTemplateFromClasspath(eq(TEMPLATE_NAME))).thenReturn(parameters -> MESSAGE);
MessageService service = spy(messageService);
doReturn(new V4Message()).when(service).send(anyString(), anyString());

assertNotNull(service.sendWithTemplate(STREAM_ID, TEMPLATE_NAME));
verify(service).send(eq(STREAM_ID), eq(MESSAGE));
}

@Test
void testSendWithAttachmentToStream(@TempDir Path tmpDir) throws IOException {
Path tempFilePath = tmpDir.resolve("tempFile");
IOUtils.write("test", new FileOutputStream(tempFilePath.toFile()), "utf-8");
mockApiClient.onPost(V4_STREAM_MESSAGE_CREATE.replace("{sid}", STREAM_ID),
JsonHelper.readFromClasspath("/message/send_message.json"));

final V4Message sentMessage =
messageService.send(new V4Stream().streamId(STREAM_ID), MESSAGE, null, tempFilePath.toFile());

assertEquals(MESSAGE_ID, sentMessage.getMessageId());
assertEquals("gXFV8vN37dNqjojYS_y2wX___o2KxfmUdA", sentMessage.getStream().getStreamId());
}

@Test
void testSendWithAttachmentToStreamId(@TempDir Path tmpDir) throws IOException {
Path tempFilePath = tmpDir.resolve("tempFile");
IOUtils.write("test", new FileOutputStream(tempFilePath.toFile()), "utf-8");
mockApiClient.onPost(V4_STREAM_MESSAGE_CREATE.replace("{sid}", STREAM_ID),
JsonHelper.readFromClasspath("/message/send_message.json"));

final V4Message sentMessage = messageService.send(STREAM_ID, MESSAGE, null, tempFilePath.toFile());

assertEquals(MESSAGE_ID, sentMessage.getMessageId());
assertEquals("gXFV8vN37dNqjojYS_y2wX___o2KxfmUdA", sentMessage.getStream().getStreamId());
}

@Test
Expand All @@ -204,12 +259,14 @@ void testGetAttachment() throws ApiException {
doReturn(new byte[0]).when(attachmentsApi).v1StreamSidAttachmentGet(any(), any(), any(), any(), any());

assertNotNull(messageService.getAttachment(STREAM_ID, MESSAGE_ID, attachmentId));
verify(attachmentsApi).v1StreamSidAttachmentGet(eq(STREAM_ID), eq(attachmentId), eq(MESSAGE_ID), anyString(), anyString());
verify(attachmentsApi).v1StreamSidAttachmentGet(eq(STREAM_ID), eq(attachmentId), eq(MESSAGE_ID), anyString(),
anyString());
}

@Test
void testGetAttachmentThrowingApiException() throws ApiException {
doThrow(new ApiException(500, "error")).when(attachmentsApi).v1StreamSidAttachmentGet(any(), any(), any(), any(), any());
doThrow(new ApiException(500, "error")).when(attachmentsApi)
.v1StreamSidAttachmentGet(any(), any(), any(), any(), any());

assertThrows(ApiRuntimeException.class, () -> messageService.getAttachment(STREAM_ID, MESSAGE_ID, "attachmentId"));
}
Expand Down Expand Up @@ -271,30 +328,34 @@ void testListAttachments() throws IOException {
mockApiClient.onGet(V1_STREAM_ATTACHMENTS.replace("{sid}", STREAM_ID),
JsonHelper.readFromClasspath("/stream/list_attachments.json"));

List<StreamAttachmentItem> attachments = messageService.listAttachments(STREAM_ID, null, null, null, AttachmentSort.ASC);
List<StreamAttachmentItem> attachments =
messageService.listAttachments(STREAM_ID, null, null, null, AttachmentSort.ASC);

assertEquals(attachments.size(), 2);
}

@Test
void testListAttachmentWithSortDirAsc() throws ApiException {
doReturn(Collections.emptyList()).when(streamsApi).v1StreamsSidAttachmentsGet(any(), any(), any(), any(), any(), any());
doReturn(Collections.emptyList()).when(streamsApi)
.v1StreamsSidAttachmentsGet(any(), any(), any(), any(), any(), any());

assertNotNull(messageService.listAttachments(STREAM_ID, null, null, null, AttachmentSort.ASC));
verify(streamsApi).v1StreamsSidAttachmentsGet(eq(STREAM_ID), any(), any(), any(), any(), eq("ASC"));
}

@Test
void testListAttachmentWithSortDirDesc() throws ApiException {
doReturn(Collections.emptyList()).when(streamsApi).v1StreamsSidAttachmentsGet(any(), any(), any(), any(), any(), any());
doReturn(Collections.emptyList()).when(streamsApi)
.v1StreamsSidAttachmentsGet(any(), any(), any(), any(), any(), any());

assertNotNull(messageService.listAttachments(STREAM_ID, null, null, null, AttachmentSort.DESC));
verify(streamsApi).v1StreamsSidAttachmentsGet(eq(STREAM_ID), any(), any(), any(), any(), eq("DESC"));
}

@Test
void testListAttachmentWithSortDirNull() throws ApiException {
doReturn(Collections.emptyList()).when(streamsApi).v1StreamsSidAttachmentsGet(any(), any(), any(), any(), any(), any());
doReturn(Collections.emptyList()).when(streamsApi)
.v1StreamsSidAttachmentsGet(any(), any(), any(), any(), any(), any());

assertNotNull(messageService.listAttachments(STREAM_ID, null, null, null, null));
verify(streamsApi).v1StreamsSidAttachmentsGet(eq(STREAM_ID), any(), any(), any(), any(), eq("ASC"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void main(String[] args) throws Exception {
//use templates

final V4Message sendWithCustomTemplate = bdk.messages()
.send(STREAM_ID, "/complex-message.ftl", Collections.singletonMap("name", "Freemarker"));
.sendWithTemplate(STREAM_ID, "/complex-message.ftl", Collections.singletonMap("name", "Freemarker"));

//retrieve the details of existing messages
final V4Message message = bdk.messages().getMessage(regularMessage.getMessageId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.symphony.bdk.core.activity.form.FormReplyContext;
import com.symphony.bdk.core.activity.model.ActivityInfo;
import com.symphony.bdk.core.activity.model.ActivityType;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.message.MessageService;

import lombok.extern.slf4j.Slf4j;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import com.symphony.bdk.core.activity.form.FormReplyContext;
import com.symphony.bdk.core.activity.model.ActivityInfo;
import com.symphony.bdk.core.activity.model.ActivityType;
import com.symphony.bdk.core.service.MessageService;
import com.symphony.bdk.core.service.message.MessageService;
import com.symphony.bdk.spring.annotation.Slash;
import com.symphony.bdk.template.api.TemplateException;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -32,8 +31,8 @@ public class GifFormActivity extends FormReplyActivity<FormReplyContext> {
private MessageService messageService;

@Slash("/gif")
public void displayGifForm(CommandContext context) throws TemplateException {
this.messageService.send(context.getStreamId(), "/templates/gif.ftl", emptyMap());
public void displayGifForm(CommandContext context) {
this.messageService.sendWithTemplate(context.getStreamId(), "/templates/gif.ftl", emptyMap());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.apiguardian.api.API;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -118,7 +119,7 @@ public <T> ApiResponse<T> invokeAPI(
for (Map.Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
formValueMap.add(param.getKey(), file);
formValueMap.add(param.getKey(), new FileSystemResource(file));
} else {
formValueMap.add(param.getKey(), parameterToString(param.getValue()));
}
Expand Down
Loading