[AB-74] changing description and field splitting leading to no fractured texts, decision is based on the last space

removing "safeFieldIteration" as its not necessary anymore
splitting up embeds for the embed size limit
This commit is contained in:
Sheldan
2021-02-21 02:50:57 +01:00
parent 9db5889160
commit 20b6b37151
5 changed files with 308 additions and 256 deletions

View File

@@ -1,139 +0,0 @@
package dev.sheldan.abstracto.core.templating.method;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import freemarker.template.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class SafeFieldIterationsTest {
public static final String TEMPLATE_KEY = "template";
public static final String FIELD_NAME_TEMPLATE = "fieldName";
public static final String FIELD_NAME_VALUE = "fieldName";
public static final String EXPECTED_START_PART = "{ \"name\": \"" + FIELD_NAME_VALUE + "\", \"inline\": \"true\", \"value\": \"";
public static final String INLINE_VALUE = "true";
public static final String FIRST_LIST_ENTRY = "text";
public static final String SIX_HUNDRED_CHARACTERS = RandomStringUtils.randomAlphabetic(600);
@InjectMocks
private SafeFieldIterations safeFieldIterations;
@Mock
private TemplateService templateService;
@Captor
private ArgumentCaptor<String> templateKeyCaptor;
@Before
public void setup() {
when(templateService.renderTemplateWithMap(eq(FIELD_NAME_TEMPLATE), any())).thenReturn(FIELD_NAME_VALUE);
}
@Test
public void testEmptyList() throws TemplateModelException {
String resultingValue = (String) safeFieldIterations.exec(getSimpleParameters());
verify(templateService, times(1)).renderTemplateWithMap(templateKeyCaptor.capture(), any());
List<String> usedTemplateKeys = templateKeyCaptor.getAllValues();
assertEquals(FIELD_NAME_TEMPLATE, usedTemplateKeys.get(0));
assertEquals(EXPECTED_START_PART + "\"}", resultingValue);
}
@Test
public void testOneElement() throws TemplateModelException {
when(templateService.renderTemplateWithMap(eq(TEMPLATE_KEY), any())).thenReturn(FIRST_LIST_ENTRY);
String resultingValue = (String) safeFieldIterations.exec(oneListEntryParameter());
assertEquals(EXPECTED_START_PART + FIRST_LIST_ENTRY + "\"}", resultingValue);
}
@Test
public void testTwoElements() throws TemplateModelException {
when(templateService.renderTemplateWithMap(eq(TEMPLATE_KEY), any())).thenReturn(FIRST_LIST_ENTRY);
String resultingValue = (String) safeFieldIterations.exec(twoListEntryParameter());
assertEquals(EXPECTED_START_PART + FIRST_LIST_ENTRY + FIRST_LIST_ENTRY + "\"}", resultingValue);
}
@Test
public void testElementsStaySolo() throws TemplateModelException {
when(templateService.renderTemplateWithMap(eq(TEMPLATE_KEY), any())).thenReturn(SIX_HUNDRED_CHARACTERS);
String resultingValue = (String) safeFieldIterations.exec(twoListEntryParameter());
assertEquals(EXPECTED_START_PART + SIX_HUNDRED_CHARACTERS + "\"}," + EXPECTED_START_PART + SIX_HUNDRED_CHARACTERS + "\"}", resultingValue);
}
@Test(expected = TemplateModelException.class)
public void testTooLittleParameters() throws TemplateModelException {
safeFieldIterations.exec(Arrays.asList(""));
}
@Test(expected = TemplateModelException.class)
public void testWrongListAdapterType() throws TemplateModelException {
safeFieldIterations.exec(wrongListAdapter());
}
@Test(expected = TemplateModelException.class)
public void testWrongTemplateKeyParameterType() throws TemplateModelException {
safeFieldIterations.exec(wrongTemplateKeyParameterType());
}
@Test(expected = TemplateModelException.class)
public void testWrongFieldNameTemplateKeyParameterType() throws TemplateModelException {
safeFieldIterations.exec(wrongFieldNameTemplateKeyParameterType());
}
@Test(expected = TemplateModelException.class)
public void testWrongInlineParameterType() throws TemplateModelException {
safeFieldIterations.exec(wrongInLineValueParameterType());
}
public List<Object> wrongListAdapter() {
return Arrays.asList(new Object(), new Object(), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
}
public List<Object> wrongTemplateKeyParameterType() {
return Arrays.asList(validEmptyList(), new Object(), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
}
public List<Object> wrongFieldNameTemplateKeyParameterType() {
return Arrays.asList(validEmptyList(), new SimpleScalar(TEMPLATE_KEY), new Object(), new SimpleScalar(INLINE_VALUE));
}
public List<Object> wrongInLineValueParameterType() {
return Arrays.asList(validEmptyList(), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new Object());
}
public List<Object> getSimpleParameters() {
return Arrays.asList(validEmptyList(), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
}
private DefaultListAdapter validEmptyList() {
return DefaultListAdapter.adapt(new ArrayList<Object>(), getWrapper());
}
public List<Object> oneListEntryParameter() {
return Arrays.asList(DefaultListAdapter.adapt(Arrays.asList("testing"), getWrapper()), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
}
public List<Object> twoListEntryParameter() {
return Arrays.asList(DefaultListAdapter.adapt(Arrays.asList("testing", "otherText"), getWrapper()), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
}
private DefaultObjectWrapper getWrapper() {
return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_0).build();
}
}

View File

@@ -17,6 +17,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
@@ -127,7 +128,7 @@ public class TemplateServiceBeanTest {
}
@Test
public void testEmbedWithTooLongDescription() throws IOException, TemplateException {
public void testEmbedWithTooLongDescriptionNoSpace() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
int tooMuchCharacterCount = 1024;
String descriptionText = RandomStringUtils.randomAlphabetic(MessageEmbed.TEXT_MAX_LENGTH + tooMuchCharacterCount);
@@ -158,7 +159,7 @@ public class TemplateServiceBeanTest {
}
@Test
public void testEmbedWithTooLongField() throws IOException, TemplateException {
public void testEmbedWithTooLongFieldNoSpace() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
String fieldValue = RandomStringUtils.randomAlphabetic(1500);
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithTooLongField(fieldValue));
@@ -170,6 +171,119 @@ public class TemplateServiceBeanTest {
Assert.assertEquals(fieldValue.substring(MessageEmbed.VALUE_MAX_LENGTH), firstEmbed.getFields().get(1).getValue());
}
@Test
public void testEmbedWithTooLongFieldWithSpace() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
int partsLength = 750;
String firstPart = RandomStringUtils.randomAlphabetic(partsLength);
String secondPart = RandomStringUtils.randomAlphabetic(partsLength);
String fieldValue = firstPart + " " + secondPart;
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<>());
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
Assert.assertEquals(2, firstEmbed.getFields().size());
Assert.assertEquals(firstPart, firstEmbed.getFields().get(0).getValue());
Assert.assertEquals(secondPart, firstEmbed.getFields().get(1).getValue());
}
@Test
public void testDescriptionWithOneSpace() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
int partLengths = 1024;
String firstPart = RandomStringUtils.randomAlphabetic(partLengths);
String secondPart = RandomStringUtils.randomAlphabetic(partLengths);
String descriptionText = firstPart + " " + secondPart;
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
when(gson.fromJson(embedTemplateWithDescription(descriptionText), EmbedConfiguration.class)).thenReturn(embedConfigWithDescription(descriptionText));
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
Assert.assertEquals(2, messageToSend.getEmbeds().size());
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
Assert.assertEquals(partLengths, firstEmbed.getDescription().length());
Assert.assertEquals(firstPart, firstEmbed.getDescription());
MessageEmbed secondEmbed = messageToSend.getEmbeds().get(1);
Assert.assertEquals(partLengths + 1, secondEmbed.getDescription().length());
Assert.assertEquals(" " + secondPart, secondEmbed.getDescription());
}
@Test
public void testDescriptionWithTwoSpacesAndLongChunks() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
int partLengths = 1024;
String firstPart = RandomStringUtils.randomAlphabetic(partLengths);
String secondPart = RandomStringUtils.randomAlphabetic(partLengths);
String thirdPart = RandomStringUtils.randomAlphabetic(partLengths);
String descriptionText = firstPart + " " + secondPart + " " + thirdPart;
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
when(gson.fromJson(embedTemplateWithDescription(descriptionText), EmbedConfiguration.class)).thenReturn(embedConfigWithDescription(descriptionText));
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
Assert.assertEquals(3, messageToSend.getEmbeds().size());
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
Assert.assertEquals(partLengths, firstEmbed.getDescription().length());
Assert.assertEquals(firstPart, firstEmbed.getDescription());
MessageEmbed secondEmbed = messageToSend.getEmbeds().get(1);
Assert.assertEquals(partLengths + 1, secondEmbed.getDescription().length());
Assert.assertEquals(" " + secondPart, secondEmbed.getDescription());
MessageEmbed thirdEmbed = messageToSend.getEmbeds().get(2);
Assert.assertEquals(partLengths + 1, thirdEmbed.getDescription().length());
Assert.assertEquals(" " + thirdPart, thirdEmbed.getDescription());
}
@Test
public void testDescriptionWithMultipleSpacesSplitIntoTwo() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
int partLengths = 750;
String firstPart = RandomStringUtils.randomAlphabetic(partLengths);
String secondPart = RandomStringUtils.randomAlphabetic(partLengths);
String thirdPart = RandomStringUtils.randomAlphabetic(partLengths);
String descriptionText = firstPart + " " + secondPart + " " + thirdPart;
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
when(gson.fromJson(embedTemplateWithDescription(descriptionText), EmbedConfiguration.class)).thenReturn(embedConfigWithDescription(descriptionText));
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
Assert.assertEquals(2, messageToSend.getEmbeds().size());
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
Assert.assertEquals(partLengths + partLengths + 1, firstEmbed.getDescription().length());
Assert.assertEquals(firstPart + " " + secondPart, firstEmbed.getDescription());
MessageEmbed secondEmbed = messageToSend.getEmbeds().get(1);
Assert.assertEquals(1 + partLengths, secondEmbed.getDescription().length());
Assert.assertEquals(" " + thirdPart, secondEmbed.getDescription());
}
@Test
public void testFieldLengthTooLongForEmbed() throws IOException, TemplateException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
int partLengths = 1000;
String fieldValue = RandomStringUtils.randomAlphabetic(partLengths);
String firstField = fieldValue + "a";
String secondField = fieldValue + "b";
String thirdField = fieldValue + "c";
String fourthField = fieldValue + "d";
String fifthField = fieldValue + "e";
String sixthField = fieldValue + "f";
List<String> fieldValues = Arrays.asList(firstField, secondField, thirdField, fourthField, fifthField, sixthField);
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithFieldValues(fieldValues));
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
when(gson.fromJson(getFields(fieldValues), EmbedConfiguration.class)).thenReturn(getEmbedWithFields(fieldValues));
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
Assert.assertEquals(2, messageToSend.getEmbeds().size());
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
List<MessageEmbed.Field> firstFields = firstEmbed.getFields();
Assert.assertEquals(5, firstFields.size());
Assert.assertEquals(firstField, firstFields.get(0).getValue());
Assert.assertEquals(secondField, firstFields.get(1).getValue());
Assert.assertEquals(thirdField, firstFields.get(2).getValue());
Assert.assertEquals(fourthField, firstFields.get(3).getValue());
Assert.assertEquals(fifthField, firstFields.get(4).getValue());
MessageEmbed secondEmbed = messageToSend.getEmbeds().get(1);
List<MessageEmbed.Field> secondFields = secondEmbed.getFields();
Assert.assertEquals(1, secondFields.size());
Assert.assertEquals(sixthField, secondFields.get(0).getValue());
}
@Test(expected = TemplatingException.class)
public void tryToRenderMissingTemplate() throws IOException {
when(serverContext.getServerId()).thenReturn(SERVER_ID);
@@ -205,6 +319,14 @@ public class TemplateServiceBeanTest {
return EmbedConfiguration.builder().fields(fields).build();
}
private EmbedConfiguration getEmbedWithFields(List<String> fieldValues) {
List<EmbedField> fields = new ArrayList<>();
fieldValues.forEach(s -> {
fields.add(EmbedField.builder().name("name").value(s).build());
});
return EmbedConfiguration.builder().fields(fields).build();
}
private EmbedConfiguration getTooManyFieldsEmbedConfiguration() {
List<EmbedField> fields = new ArrayList<>();
for (int i = 0; i < 30; i++) {
@@ -289,6 +411,10 @@ public class TemplateServiceBeanTest {
return new Template(getEmbedTemplateKey(), embedTemplateWithDescription(description), getNonMockedConfiguration());
}
private Template getEmbedTemplateWithFieldValues(List<String> fieldValues) throws IOException, TemplateException {
return new Template(getEmbedTemplateKey(), getFields(fieldValues), getNonMockedConfiguration());
}
private Template getPageCountTemplate(Integer page) throws IOException, TemplateException {
return new Template(EMBED_PAGE_COUNT_TEMPLATE, getEmbedPageCount(page), getNonMockedConfiguration());
}
@@ -305,12 +431,12 @@ public class TemplateServiceBeanTest {
return new Template(getEmbedTemplateKey(), getSingleFieldWithValue(value), getNonMockedConfiguration());
}
private String getFullEmbedConfigString() throws IOException {
private String getFullEmbedConfigString() {
return IOUtils.toString(this.getClass().getResourceAsStream("/src/test/resources/full_embed.json"), StandardCharsets.UTF_8);
}
private String getFieldsEmbedConfigAsString(Integer count) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();
sb.append("{\"fields\": [");
for (int i = 0; i < count - 1; i++) {
sb.append(FIELD_TEMPLATE + ",");
@@ -321,6 +447,17 @@ public class TemplateServiceBeanTest {
return sb.toString();
}
private String getFields(List<String> fieldValues) {
StringBuilder sb = new StringBuilder();
sb.append("{\"fields\": [");
for (String fieldValue: fieldValues) {
sb.append(getSingleFieldWithValue(fieldValue));
}
sb.append("]\n" +
"}");
return sb.toString();
}
private String getSingleFieldWithValue(String value) {
return String.format("{\"fields\": [" + FIELD_TEMPLATE_WITH_VALUE + "]\n}",value);
}