mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-07 09:38:04 +00:00
[AB-217] adding profanity service
using profanity service to filter profanities in urban define adding cache clearing listeners fixing using 0 as default for maxMessages if not defined
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.ProfanityGroup;
|
||||
import dev.sheldan.abstracto.core.models.database.ProfanityRegex;
|
||||
import dev.sheldan.abstracto.core.service.management.ProfanityGroupManagementService;
|
||||
import org.junit.Assert;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ProfanityServiceTest {
|
||||
@InjectMocks
|
||||
private ProfanityServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private ProfanityGroupManagementService profanityGroupManagementService;
|
||||
|
||||
@Mock
|
||||
private ProfanityGroup group;
|
||||
|
||||
@Mock
|
||||
private ProfanityRegex regex;
|
||||
|
||||
@Mock
|
||||
private AServer server;
|
||||
|
||||
private final static String INPUT = "input";
|
||||
private final static String REPLACEMENT = "repl";
|
||||
|
||||
private static final Long SERVER_ID = 4L;
|
||||
|
||||
@Test
|
||||
public void testEmptyConfig() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(new ArrayList<>());
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, REPLACEMENT);
|
||||
Assert.assertEquals(INPUT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupNoRegex() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, REPLACEMENT);
|
||||
Assert.assertEquals(INPUT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithOneRegexNotMatching() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex));
|
||||
when(regex.getRegex()).thenReturn("a");
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, REPLACEMENT);
|
||||
Assert.assertEquals(INPUT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithOneRegexMatching() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, REPLACEMENT);
|
||||
Assert.assertEquals(REPLACEMENT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithOneRegexMatchingDefinedReplacement() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
when(regex.getReplacement()).thenReturn(REPLACEMENT);
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, "");
|
||||
Assert.assertEquals(REPLACEMENT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithTwoRegexOneMatching() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
ProfanityRegex regex2 = Mockito.mock(ProfanityRegex.class);
|
||||
when(regex2.getRegex()).thenReturn("asdf");
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex, regex2));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
when(regex.getReplacement()).thenReturn(REPLACEMENT);
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, "");
|
||||
Assert.assertEquals(REPLACEMENT, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* This scenario is not desired, generally, the outputs should be independent, because the user cannot define the order in which
|
||||
* the regexes are applied
|
||||
*/
|
||||
@Test
|
||||
public void testOneGroupWithTwoRegexBothMatching() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
ProfanityRegex regex2 = Mockito.mock(ProfanityRegex.class);
|
||||
when(regex2.getRegex()).thenReturn(REPLACEMENT);
|
||||
String finalString = "FINAL";
|
||||
when(regex2.getReplacement()).thenReturn(finalString);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex, regex2));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
when(regex.getReplacement()).thenReturn(REPLACEMENT);
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanitiesWithDefault(INPUT, SERVER_ID, "");
|
||||
Assert.assertEquals(finalString, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithOneRegexMatchingAndFixReplacement() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanities(INPUT, SERVER_ID, REPLACEMENT);
|
||||
Assert.assertEquals(REPLACEMENT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithOneRegexMatchingNoProvidedReplacement() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
when(regex.getReplacement()).thenReturn(REPLACEMENT);
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanities(INPUT, SERVER_ID);
|
||||
Assert.assertEquals(REPLACEMENT, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneGroupWithOneRegexMatchingUsingProvidedReplacement() {
|
||||
when(profanityGroupManagementService.getAllGroups()).thenReturn(Arrays.asList(group));
|
||||
when(group.getServer()).thenReturn(server);
|
||||
when(server.getId()).thenReturn(SERVER_ID);
|
||||
when(group.getProfanities()).thenReturn(Arrays.asList(regex));
|
||||
when(regex.getRegex()).thenReturn("input");
|
||||
testUnit.reloadRegex();
|
||||
String result = testUnit.replaceProfanities(INPUT, SERVER_ID);
|
||||
Assert.assertEquals("", result);
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ public class TemplateServiceBeanTest {
|
||||
|
||||
private void setupServerAware() {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configService.getLongValue(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
when(configService.getLongValueOrConfigDefault(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -95,7 +95,7 @@ public class TemplateServiceBeanTest {
|
||||
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);
|
||||
when(configService.getLongValueOrConfigDefault(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);
|
||||
@@ -113,7 +113,7 @@ public class TemplateServiceBeanTest {
|
||||
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);
|
||||
when(configService.getLongValueOrConfigDefault(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);
|
||||
@@ -130,7 +130,7 @@ public class TemplateServiceBeanTest {
|
||||
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);
|
||||
when(configService.getLongValueOrConfigDefault(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);
|
||||
@@ -228,7 +228,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(configService.getLongValueOrConfigDefault(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<>());
|
||||
|
||||
Reference in New Issue
Block a user