mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-25 05:57:06 +00:00
[AB-218] adding ability to split the normal message as well according to custom configuration
adding ability to define the max amount of messages resulting from a template on a server and template base fixing not always considering the server when rendering templates in various places
This commit is contained in:
@@ -163,7 +163,7 @@ public class Help implements Command {
|
||||
List<ModuleDefinition> subModules = moduleService.getSubModules(moduleDefinition);
|
||||
HelpModuleOverviewModel model = (HelpModuleOverviewModel) ContextConverter.fromCommandContext(commandContext, HelpModuleOverviewModel.class);
|
||||
model.setModules(subModules);
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_module_overview_response", model);
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_module_overview_response", model, commandContext.getGuild().getIdLong());
|
||||
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()))
|
||||
.thenApply(aVoid -> CommandResult.fromIgnored());
|
||||
}
|
||||
|
||||
@@ -200,33 +200,38 @@ public class ChannelServiceBean implements ChannelService {
|
||||
|
||||
@Override
|
||||
public List<CompletableFuture<Message>> sendMessageToSendToChannel(MessageToSend messageToSend, MessageChannel textChannel) {
|
||||
String messageText = messageToSend.getMessage();
|
||||
List<CompletableFuture<Message>> futures = new ArrayList<>();
|
||||
MessageAction firstMessageAction = null;
|
||||
List<MessageAction> allMessageActions = new ArrayList<>();
|
||||
if(!StringUtils.isBlank(messageText)) {
|
||||
int iterations = Math.min(messageToSend.getMessages().size(), messageToSend.getEmbeds().size());
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
firstMessageAction = textChannel.sendMessage(messageText);
|
||||
String text = messageToSend.getMessages().get(i);
|
||||
MessageEmbed embed = messageToSend.getEmbeds().get(i);
|
||||
MessageAction messageAction = textChannel.sendMessage(text).embed(embed);
|
||||
allMessageActions.add(messageAction);
|
||||
}
|
||||
if(!messageToSend.getEmbeds().isEmpty()) {
|
||||
if(firstMessageAction != null) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
firstMessageAction.embed(messageToSend.getEmbeds().get(0));
|
||||
} else {
|
||||
firstMessageAction = textChannel.sendMessage(messageToSend.getEmbeds().get(0));
|
||||
}
|
||||
messageToSend.getEmbeds().stream().skip(1).forEach(embed -> allMessageActions.add(sendEmbedToChannelInComplete(embed, textChannel)));
|
||||
// one of these loops will get additional iterations, if the number is different, not both
|
||||
for (int i = iterations; i < messageToSend.getMessages().size(); i++) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
String text = messageToSend.getMessages().get(i);
|
||||
MessageAction messageAction = textChannel.sendMessage(text);
|
||||
allMessageActions.add(messageAction);
|
||||
}
|
||||
for (int i = iterations; i < messageToSend.getEmbeds().size(); i++) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
MessageEmbed embed = messageToSend.getEmbeds().get(i);
|
||||
MessageAction messageAction = textChannel.sendMessage(embed);
|
||||
allMessageActions.add(messageAction);
|
||||
}
|
||||
if(messageToSend.hasFileToSend()) {
|
||||
if(firstMessageAction != null) {
|
||||
if(!allMessageActions.isEmpty()) {
|
||||
// in case there has not been a message, we need to increment it
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
firstMessageAction.addFile(messageToSend.getFileToSend());
|
||||
allMessageActions.set(0, allMessageActions.get(0).addFile(messageToSend.getFileToSend()));
|
||||
} else {
|
||||
firstMessageAction = textChannel.sendFile(messageToSend.getFileToSend());
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
allMessageActions.add(textChannel.sendFile(messageToSend.getFileToSend()));
|
||||
}
|
||||
}
|
||||
allMessageActions.add(0, firstMessageAction);
|
||||
List<Message.MentionType> allowedMentions = getAllowedMentionsFor(textChannel);
|
||||
allMessageActions.forEach(messageAction ->
|
||||
futures.add(messageAction.allowedMentions(allowedMentions).submit())
|
||||
@@ -253,9 +258,9 @@ public class ChannelServiceBean implements ChannelService {
|
||||
@Override
|
||||
public CompletableFuture<Message> editMessageInAChannelFuture(MessageToSend messageToSend, MessageChannel channel, Long messageId) {
|
||||
MessageAction messageAction;
|
||||
if(!StringUtils.isBlank(messageToSend.getMessage())) {
|
||||
if(!StringUtils.isBlank(messageToSend.getMessages().get(0))) {
|
||||
log.trace("Editing message {} with new text content.", messageId);
|
||||
messageAction = channel.editMessageById(messageId, messageToSend.getMessage());
|
||||
messageAction = channel.editMessageById(messageId, messageToSend.getMessages().get(0));
|
||||
if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) {
|
||||
log.trace("Also editing the embed for message {}.", messageId);
|
||||
messageAction = messageAction.embed(messageToSend.getEmbeds().get(0));
|
||||
@@ -459,7 +464,7 @@ public class ChannelServiceBean implements ChannelService {
|
||||
if(tempFile.length() > maxFileSize) {
|
||||
throw new UploadFileTooLargeException(tempFile.length(), maxFileSize);
|
||||
}
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(messageTemplate, model);
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(messageTemplate, model, channel.getGuild().getIdLong());
|
||||
messageToSend.setFileToSend(tempFile);
|
||||
return sendMessageToSendToChannel(messageToSend, channel);
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -130,7 +130,8 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
@Override
|
||||
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTarget target) {
|
||||
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
|
||||
String messageText = message.getMessage();
|
||||
// always takes the first one, only applicable for this scenario
|
||||
String messageText = message.getMessages().get(0);
|
||||
if(StringUtils.isBlank(messageText)) {
|
||||
log.trace("Editing embeds of message {} in post target {}.", messageId, target.getName());
|
||||
return Arrays.asList(channelService.editEmbedMessageInAChannel(message.getEmbeds().get(0), textChannelForPostTarget, messageId));
|
||||
@@ -146,7 +147,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
|
||||
CompletableFuture<Message> messageEditFuture = new CompletableFuture<>();
|
||||
futures.add(messageEditFuture);
|
||||
if(StringUtils.isBlank(messageToSend.getMessage().trim())) {
|
||||
if(StringUtils.isBlank(messageToSend.getMessages().get(0).trim())) {
|
||||
channelService.retrieveMessageInChannel(textChannelForPostTarget, messageId).thenAccept(message -> {
|
||||
log.trace("Editing existing message {} when upserting message embeds in channel {} in server {}.",
|
||||
messageId, textChannelForPostTarget.getIdLong(), textChannelForPostTarget.getGuild().getId());
|
||||
@@ -167,7 +168,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
channelService.retrieveMessageInChannel(textChannelForPostTarget, messageId).thenAccept(message -> {
|
||||
log.trace("Editing existing message {} when upserting message in channel {} in server {}.",
|
||||
messageId, textChannelForPostTarget.getIdLong(), textChannelForPostTarget.getGuild().getId());
|
||||
messageService.editMessage(message, messageToSend.getMessage(), messageToSend.getEmbeds().get(0))
|
||||
messageService.editMessage(message, messageToSend.getMessages().get(0), messageToSend.getEmbeds().get(0))
|
||||
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
||||
}).exceptionally(throwable -> {
|
||||
log.trace("Creating new message when trying to upsert a message {} in channel {} in server {}.",
|
||||
|
||||
@@ -55,6 +55,8 @@ public class EmbedConfiguration {
|
||||
* The message which is posted along the {@link net.dv8tion.jda.api.entities.MessageEmbed} as a normal message.
|
||||
*/
|
||||
private String additionalMessage;
|
||||
private Long additionalMessageLengthLimit;
|
||||
private Long messageLimit;
|
||||
|
||||
private boolean preventEmptyEmbed = false;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package dev.sheldan.abstracto.core.templating.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import dev.sheldan.abstracto.core.command.config.features.CoreFeatureConfig;
|
||||
import dev.sheldan.abstracto.core.config.ServerContext;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.templating.Templatable;
|
||||
import dev.sheldan.abstracto.core.templating.exception.TemplatingException;
|
||||
import dev.sheldan.abstracto.core.templating.model.*;
|
||||
@@ -13,6 +15,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -41,6 +44,9 @@ public class TemplateServiceBean implements TemplateService {
|
||||
@Autowired
|
||||
private ServerContext serverContext;
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
||||
/**
|
||||
* Formats the passed passed count with the embed used for formatting pages.
|
||||
*
|
||||
@@ -114,9 +120,51 @@ public class TemplateServiceBean implements TemplateService {
|
||||
embeds = embedBuilders.stream().map(EmbedBuilder::build).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
List<String> messages = new ArrayList<>();
|
||||
String additionalMessage = embedConfiguration.getAdditionalMessage();
|
||||
if(additionalMessage != null) {
|
||||
Long segmentLimit = embedConfiguration.getAdditionalMessageLengthLimit() != null ? embedConfiguration.getAdditionalMessageLengthLimit() : Long.valueOf(Message.MAX_CONTENT_LENGTH);
|
||||
if(additionalMessage.length() > segmentLimit) {
|
||||
int segmentStart = 0;
|
||||
int segmentEnd = segmentLimit.intValue();
|
||||
while(segmentStart < additionalMessage.length()) {
|
||||
int segmentLength = additionalMessage.length() - segmentStart;
|
||||
if(segmentLength > segmentLimit) {
|
||||
int lastSpace = additionalMessage.substring(segmentStart, segmentEnd).lastIndexOf(" ");
|
||||
if(lastSpace != -1) {
|
||||
segmentEnd = segmentStart + lastSpace;
|
||||
}
|
||||
} else {
|
||||
segmentEnd = additionalMessage.length();
|
||||
}
|
||||
String messageText = additionalMessage.substring(segmentStart, segmentEnd);
|
||||
messages.add(messageText);
|
||||
segmentStart = segmentEnd;
|
||||
segmentEnd += segmentLimit;
|
||||
}
|
||||
} else {
|
||||
messages.add(additionalMessage);
|
||||
}
|
||||
}
|
||||
Long messageLimit = 100L;
|
||||
if(serverContext.getServerId() != null) {
|
||||
messageLimit = Math.min(messageLimit, configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, serverContext.getServerId()));
|
||||
}
|
||||
if(embedConfiguration.getMessageLimit() != null) {
|
||||
messageLimit = Math.min(messageLimit, embedConfiguration.getMessageLimit());
|
||||
}
|
||||
if(embeds.size() > messageLimit) {
|
||||
log.info("Limiting size of embeds. Max allowed: {}, currently: {}.", messageLimit, embeds.size());
|
||||
embeds.subList(messageLimit.intValue(), embeds.size()).clear();
|
||||
}
|
||||
if(messages.size() > messageLimit) {
|
||||
log.info("Limiting size of messages. Max allowed: {}, currently: {}.", messageLimit, messages.size());
|
||||
messages.subList(messageLimit.intValue(), messages.size()).clear();
|
||||
}
|
||||
|
||||
return MessageToSend.builder()
|
||||
.embeds(embeds)
|
||||
.message(embedConfiguration.getAdditionalMessage())
|
||||
.messages(messages)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -171,7 +219,7 @@ public class TemplateServiceBean implements TemplateService {
|
||||
|
||||
@Override
|
||||
public MessageToSend renderTemplateToMessageToSend(String key, Object model) {
|
||||
return MessageToSend.builder().message(renderTemplate(key, model)).build();
|
||||
return MessageToSend.builder().messages(Arrays.asList(renderTemplate(key, model))).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,5 +13,8 @@ abstracto.systemConfigs.prefix.stringValue=!
|
||||
abstracto.systemConfigs.noCommandFoundReporting.name=noCommandFoundReporting
|
||||
abstracto.systemConfigs.noCommandFoundReporting.stringValue=true
|
||||
|
||||
abstracto.systemConfigs.maxMessages.name=maxMessages
|
||||
abstracto.systemConfigs.maxMessages.longValue=3
|
||||
|
||||
abstracto.featureFlags.core.featureName=core
|
||||
abstracto.featureFlags.core.enabled=true
|
||||
@@ -1,7 +1,9 @@
|
||||
package dev.sheldan.abstracto.core.templating.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import dev.sheldan.abstracto.core.command.config.features.CoreFeatureConfig;
|
||||
import dev.sheldan.abstracto.core.config.ServerContext;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.templating.Templatable;
|
||||
import dev.sheldan.abstracto.core.templating.exception.TemplatingException;
|
||||
import dev.sheldan.abstracto.core.templating.model.*;
|
||||
@@ -54,6 +56,9 @@ public class TemplateServiceBeanTest {
|
||||
@Mock
|
||||
private Configuration configuration;
|
||||
|
||||
@Mock
|
||||
private ConfigService configService;
|
||||
|
||||
@Mock
|
||||
private Gson gson;
|
||||
|
||||
@@ -66,15 +71,20 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testSimpleTemplate() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenReturn(getSimpleTemplate());
|
||||
String rendered = templateServiceBean.renderSimpleTemplate(TEMPLATE_KEY);
|
||||
Assert.assertEquals(SIMPLE_TEMPLATE_SOURCE, rendered);
|
||||
}
|
||||
|
||||
private void setupServerAware() {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplatable() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenReturn(getSimpleTemplate());
|
||||
Templatable templatable = getTemplatableWithSimpleTemplate();
|
||||
String rendered = templateServiceBean.renderTemplatable(templatable);
|
||||
@@ -82,8 +92,62 @@ public class TemplateServiceBeanTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemplateWithMapParameter() throws IOException, TemplateException {
|
||||
public void testRenderTooLongAdditionalMessage() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
String additionalMessage = RandomStringUtils.randomAlphabetic(3500);
|
||||
when(configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
String templateContent = String.format("{ \"additionalMessage\": \"%s\"}", additionalMessage);
|
||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||
when(config.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
Assert.assertEquals(2, messageToSend.getMessages().size());
|
||||
Assert.assertEquals(additionalMessage.substring(0, 2000), messageToSend.getMessages().get(0));
|
||||
Assert.assertEquals(additionalMessage.substring(2000, 3500), messageToSend.getMessages().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderEmbedWithMessageLimit() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
String additionalMessage = RandomStringUtils.randomAlphabetic(3500);
|
||||
when(configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
String templateContent = String.format("{ \"additionalMessage\": \"%s\", \"messageLimit\": 1}", additionalMessage);
|
||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||
when(config.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||
when(config.getMessageLimit()).thenReturn(1L);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
Assert.assertEquals(1, messageToSend.getMessages().size());
|
||||
Assert.assertEquals(additionalMessage.substring(0, 2000), messageToSend.getMessages().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderTooLongMultipleAdditionalMessages() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
String additionalMessage = RandomStringUtils.randomAlphabetic(3500);
|
||||
when(configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
String templateContent = String.format("{ \"additionalMessage\": \"%s\"}", additionalMessage);
|
||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||
when(config.getAdditionalMessageLengthLimit()).thenReturn(500L);
|
||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
Assert.assertEquals(5, messageToSend.getMessages().size());
|
||||
Assert.assertEquals(additionalMessage.substring(0, 500), messageToSend.getMessages().get(0));
|
||||
Assert.assertEquals(additionalMessage.substring(500, 1000), messageToSend.getMessages().get(1));
|
||||
Assert.assertEquals(additionalMessage.substring(1000, 1500), messageToSend.getMessages().get(2));
|
||||
Assert.assertEquals(additionalMessage.substring(1500, 2000), messageToSend.getMessages().get(3));
|
||||
Assert.assertEquals(additionalMessage.substring(2000, 2500), messageToSend.getMessages().get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemplateWithMapParameter() throws IOException, TemplateException {
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenReturn(getSimpleTemplate());
|
||||
String rendered = templateServiceBean.renderTemplateWithMap(TEMPLATE_KEY, new HashMap<>());
|
||||
Assert.assertEquals(SIMPLE_TEMPLATE_SOURCE, rendered);
|
||||
@@ -91,7 +155,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testEmbedWithDescription() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
String descriptionText = "test";
|
||||
String fullEmbedTemplateKey = getEmbedTemplateKey();
|
||||
when(configuration.getTemplate(fullEmbedTemplateKey, null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
|
||||
@@ -102,11 +166,11 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testEmbedWithAllUsableAttributes() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithFallFieldsUsedOnce());
|
||||
when(gson.fromJson(getFullEmbedConfigString(), EmbedConfiguration.class)).thenReturn(getFullEmbedConfiguration());
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
Assert.assertEquals("additionalMessage", messageToSend.getMessage());
|
||||
Assert.assertEquals("additionalMessage", messageToSend.getMessages().get(0));
|
||||
MessageEmbed onlyEmbed = messageToSend.getEmbeds().get(0);
|
||||
Assert.assertEquals(EXAMPLE_URL, onlyEmbed.getAuthor().getIconUrl());
|
||||
Assert.assertEquals("name", onlyEmbed.getAuthor().getName());
|
||||
@@ -129,7 +193,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testEmbedWithTooLongDescriptionNoSpace() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int tooMuchCharacterCount = 1024;
|
||||
String descriptionText = RandomStringUtils.randomAlphabetic(MessageEmbed.TEXT_MAX_LENGTH + tooMuchCharacterCount);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
|
||||
@@ -146,7 +210,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testEmbedWithTooManyFields() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int totalFieldCount = 30;
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithFieldCount(totalFieldCount));
|
||||
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
|
||||
@@ -162,6 +226,7 @@ public class TemplateServiceBeanTest {
|
||||
public void testEmbedWithTooLongFieldNoSpace() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
String fieldValue = RandomStringUtils.randomAlphabetic(1500);
|
||||
when(configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithTooLongField(fieldValue));
|
||||
when(gson.fromJson(getSingleFieldWithValue(fieldValue), EmbedConfiguration.class)).thenReturn(getEmbedWithSingleFieldOfValue(fieldValue));
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
@@ -173,7 +238,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testEmbedWithTooLongFieldWithSpace() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int partsLength = 750;
|
||||
String firstPart = RandomStringUtils.randomAlphabetic(partsLength);
|
||||
String secondPart = RandomStringUtils.randomAlphabetic(partsLength);
|
||||
@@ -189,7 +254,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testDescriptionWithOneSpace() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int partLengths = 1024;
|
||||
String firstPart = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
String secondPart = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
@@ -209,7 +274,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testDescriptionWithTwoSpacesAndLongChunks() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int partLengths = 1024;
|
||||
String firstPart = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
String secondPart = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
@@ -233,7 +298,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testDescriptionWithMultipleSpacesSplitIntoTwo() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int partLengths = 750;
|
||||
String firstPart = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
String secondPart = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
@@ -254,7 +319,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testFieldLengthTooLongForEmbed() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
int partLengths = 1000;
|
||||
String fieldValue = RandomStringUtils.randomAlphabetic(partLengths);
|
||||
String firstField = fieldValue + "a";
|
||||
@@ -286,25 +351,21 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
@Test(expected = TemplatingException.class)
|
||||
public void tryToRenderMissingTemplate() throws IOException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenThrow(new TemplateNotFoundException(TEMPLATE_KEY, new Object(), ""));
|
||||
templateServiceBean.renderSimpleTemplate(TEMPLATE_KEY);
|
||||
}
|
||||
|
||||
@Test(expected = TemplatingException.class)
|
||||
public void tryToRenderMissingEmbedTemplate() throws IOException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenThrow(new TemplateNotFoundException(TEMPLATE_KEY, new Object(), ""));
|
||||
templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
}
|
||||
|
||||
private String getEmbedTemplateKey() {
|
||||
return TEMPLATE_KEY + "_embed";
|
||||
}
|
||||
|
||||
@Test(expected = TemplatingException.class)
|
||||
public void tryToRenderMissingTemplateWithMap() throws IOException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
setupServerAware();
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenThrow(new TemplateNotFoundException(TEMPLATE_KEY, new Object(), ""));
|
||||
templateServiceBean.renderTemplateWithMap(TEMPLATE_KEY, new HashMap<>());
|
||||
}
|
||||
@@ -319,6 +380,10 @@ public class TemplateServiceBeanTest {
|
||||
return EmbedConfiguration.builder().fields(fields).build();
|
||||
}
|
||||
|
||||
private String getEmbedTemplateKey() {
|
||||
return TEMPLATE_KEY + "_embed";
|
||||
}
|
||||
|
||||
private EmbedConfiguration getEmbedWithFields(List<String> fieldValues) {
|
||||
List<EmbedField> fields = new ArrayList<>();
|
||||
fieldValues.forEach(s -> {
|
||||
|
||||
Reference in New Issue
Block a user