mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-23 22:25:22 +00:00
[AB-215] adding create alias and delete alias commands to create service specific aliases for commands
renaming package in commands package defaulting to latest in docker compose build for deployment container fixing jacoco configuration changing that if there are no parameters required, parameters are accepted
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.models.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.service.management.ChannelGroupCommandManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannelGroup;
|
||||
@@ -11,9 +11,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.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package dev.sheldan.abstracto.core.command.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.Command;
|
||||
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.core.command.exception.CommandAliasAlreadyExistsException;
|
||||
import dev.sheldan.abstracto.core.command.exception.CommandAliasDoesNotExistsException;
|
||||
import dev.sheldan.abstracto.core.command.exception.CommandAliasHidesCommandException;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInAServer;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInServerAlias;
|
||||
import dev.sheldan.abstracto.core.command.model.database.CommandInServerAliasId;
|
||||
import dev.sheldan.abstracto.core.command.service.management.CommandInServerAliasManagementServiceBean;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
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 java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandInServerAliasServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private CommandInServerAliasServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private CommandInServerAliasManagementServiceBean commandInServerAliasManagementServiceBean;
|
||||
|
||||
@Mock
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Mock
|
||||
private CommandInServerService commandInServerService;
|
||||
|
||||
@Mock
|
||||
private CommandRegistry commandRegistry;
|
||||
|
||||
@Mock
|
||||
private AServer server;
|
||||
|
||||
@Mock
|
||||
private ACommandInServerAlias alias;
|
||||
|
||||
@Mock
|
||||
private ACommandInAServer aCommandInAServer;
|
||||
|
||||
@Mock
|
||||
private Command command;
|
||||
|
||||
private static final Long SERVER_ID = 1L;
|
||||
private static final String COMMAND_NAME = "command";
|
||||
private static final String ALIAS_NAME = "alias";
|
||||
|
||||
@Test
|
||||
public void testCreateAlias() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.empty());
|
||||
when(commandRegistry.getCommandByNameOptional(ALIAS_NAME, false, SERVER_ID)).thenReturn(Optional.empty());
|
||||
when(commandInServerService.getCommandInAServer(server, COMMAND_NAME)).thenReturn(aCommandInAServer);
|
||||
when(commandInServerAliasManagementServiceBean.createAliasForCommand(aCommandInAServer, ALIAS_NAME)).thenReturn(alias);
|
||||
ACommandInServerAlias createdAlias = testUnit.createAliasForCommandInServer(SERVER_ID, COMMAND_NAME, ALIAS_NAME);
|
||||
Assert.assertEquals(alias, createdAlias);
|
||||
}
|
||||
|
||||
@Test(expected = CommandAliasAlreadyExistsException.class)
|
||||
public void testCreateAliasAlreadyExists() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.of(alias));
|
||||
ACommandInAServer commandInAServer = Mockito.mock(ACommandInAServer.class);
|
||||
when(alias.getCommandInAServer()).thenReturn(commandInAServer);
|
||||
ACommand command = Mockito.mock(ACommand.class);
|
||||
when(commandInAServer.getCommandReference()).thenReturn(command);
|
||||
when(command.getName()).thenReturn(COMMAND_NAME);
|
||||
testUnit.createAliasForCommandInServer(SERVER_ID, COMMAND_NAME, ALIAS_NAME);
|
||||
}
|
||||
|
||||
@Test(expected = CommandAliasHidesCommandException.class)
|
||||
public void testCreateAliasForCommandName() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.empty());
|
||||
when(commandRegistry.getCommandByNameOptional(ALIAS_NAME, false, SERVER_ID)).thenReturn(Optional.of(command));
|
||||
CommandConfiguration config = Mockito.mock(CommandConfiguration.class);
|
||||
when(command.getConfiguration()).thenReturn(config);
|
||||
when(config.getName()).thenReturn(COMMAND_NAME);
|
||||
testUnit.createAliasForCommandInServer(SERVER_ID, COMMAND_NAME, ALIAS_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandInServerAliasExisting() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.of(alias));
|
||||
Optional<ACommandInServerAlias> optional = testUnit.getCommandInServerAlias(SERVER_ID, ALIAS_NAME);
|
||||
Assert.assertTrue(optional.isPresent());
|
||||
optional.ifPresent(returnedAlias ->
|
||||
Assert.assertEquals(alias, returnedAlias)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandInServerAliasNotExisting() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.empty());
|
||||
Optional<ACommandInServerAlias> optional = testUnit.getCommandInServerAlias(SERVER_ID, ALIAS_NAME);
|
||||
Assert.assertFalse(optional.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteCommandInServerAlias() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.of(alias));
|
||||
testUnit.deleteCommandInServerAlias(SERVER_ID, ALIAS_NAME);
|
||||
verify(commandInServerAliasManagementServiceBean, times(1)).deleteCommandInServerAlias(alias);
|
||||
}
|
||||
|
||||
@Test(expected = CommandAliasDoesNotExistsException.class)
|
||||
public void testDeleteCommandInServerAliasNotExisting() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getCommandInServerAlias(server, ALIAS_NAME)).thenReturn(Optional.empty());
|
||||
testUnit.deleteCommandInServerAlias(SERVER_ID, ALIAS_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAliasesForCommandNoResults() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getAliasesForCommandInServer(server, COMMAND_NAME)).thenReturn(new ArrayList<>());
|
||||
List<String> aliases = testUnit.getAliasesForCommand(SERVER_ID, COMMAND_NAME);
|
||||
Assert.assertEquals(0, aliases.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAliasesForCommandWithResults() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandInServerAliasManagementServiceBean.getAliasesForCommandInServer(server, COMMAND_NAME)).thenReturn(Arrays.asList(alias));
|
||||
CommandInServerAliasId id = Mockito.mock(CommandInServerAliasId.class);
|
||||
when(alias.getAliasId()).thenReturn(id);
|
||||
when(id.getName()).thenReturn(ALIAS_NAME);
|
||||
List<String> aliases = testUnit.getAliasesForCommand(SERVER_ID, COMMAND_NAME);
|
||||
Assert.assertEquals(1, aliases.size());
|
||||
Assert.assertEquals(ALIAS_NAME, aliases.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package dev.sheldan.abstracto.core.command.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInAServer;
|
||||
import dev.sheldan.abstracto.core.command.service.management.CommandInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.command.service.management.CommandManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
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 static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandInServerServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private CommandInServerServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private CommandManagementService commandManagementService;
|
||||
|
||||
@Mock
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Mock
|
||||
private CommandInServerManagementService commandInServerManagementService;
|
||||
|
||||
@Mock
|
||||
private AServer server;
|
||||
|
||||
@Mock
|
||||
private ACommand command;
|
||||
|
||||
@Mock
|
||||
private ACommandInAServer commandInAServer;
|
||||
|
||||
private static final Long SERVER_ID = 4L;
|
||||
private static final String COMMAND_NAME = "command";
|
||||
|
||||
@Test
|
||||
public void testGetCommandInAServerWithId() {
|
||||
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
|
||||
when(commandManagementService.findCommandByName(COMMAND_NAME)).thenReturn(command);
|
||||
when(commandInServerManagementService.getCommandForServer(command, server)).thenReturn(commandInAServer);
|
||||
ACommandInAServer foundCommandInServer = testUnit.getCommandInAServer(SERVER_ID, COMMAND_NAME);
|
||||
Assert.assertEquals(commandInAServer, foundCommandInServer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandInAServer() {
|
||||
when(commandManagementService.findCommandByName(COMMAND_NAME)).thenReturn(command);
|
||||
when(commandInServerManagementService.getCommandForServer(command, server)).thenReturn(commandInAServer);
|
||||
ACommandInAServer foundCommandInServer = testUnit.getCommandInAServer(server, COMMAND_NAME);
|
||||
Assert.assertEquals(commandInAServer, foundCommandInServer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
package dev.sheldan.abstracto.core.command.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.Command;
|
||||
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.core.command.config.ModuleDefinition;
|
||||
import dev.sheldan.abstracto.core.command.config.ModuleInfo;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.exception.CommandNotFoundException;
|
||||
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
|
||||
import dev.sheldan.abstracto.core.command.execution.UnParsedCommandParameter;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInAServer;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInServerAlias;
|
||||
import dev.sheldan.abstracto.core.metric.service.MetricService;
|
||||
import dev.sheldan.abstracto.core.models.property.SystemConfigProperty;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static dev.sheldan.abstracto.core.command.service.CommandManager.PREFIX;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandManagerTest {
|
||||
|
||||
public static final String DEFAULT_PREFIX = "!";
|
||||
@InjectMocks
|
||||
private CommandManager testUnit;
|
||||
|
||||
@Mock
|
||||
private ConfigService configService;
|
||||
|
||||
@Mock
|
||||
private DefaultConfigManagementService defaultConfigManagementService;
|
||||
|
||||
@Mock
|
||||
private MetricService metricService;
|
||||
|
||||
@Mock
|
||||
private CommandInServerAliasService commandInServerAliasService;
|
||||
|
||||
@Mock
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Spy
|
||||
private List<Command> commands = new ArrayList<>();
|
||||
|
||||
@Mock
|
||||
private Command firstCommand;
|
||||
|
||||
@Mock
|
||||
private CommandConfiguration commandConfiguration;
|
||||
|
||||
@Mock
|
||||
private UnParsedCommandParameter parsedCommandParameter;
|
||||
|
||||
@Mock
|
||||
private Parameter parameter;
|
||||
|
||||
@Mock
|
||||
private ACommandInServerAlias alias;
|
||||
|
||||
@Mock
|
||||
private ACommandInAServer commandInAServer;
|
||||
|
||||
@Mock
|
||||
private ACommand aCommand;
|
||||
|
||||
private static final String COMMAND_NAME = "name";
|
||||
private static final String COMMAND_NAME_2 = COMMAND_NAME + "suffix";
|
||||
private static final String ALIAS_NAME = "name2";
|
||||
private static final Long SERVER_ID = 1L;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test(expected = CommandNotFoundException.class)
|
||||
public void testFindByParametersNoCommands(){
|
||||
when(commandInServerAliasService.getCommandInServerAlias(SERVER_ID, COMMAND_NAME)).thenReturn(Optional.empty());
|
||||
testUnit.findCommandByParameters(COMMAND_NAME, parsedCommandParameter, SERVER_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByParametersViaCommandNameNecessaryParameter() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getNecessaryParameterCount()).thenReturn(0);
|
||||
when(parsedCommandParameter.getParameters()).thenReturn(new ArrayList<>());
|
||||
Command foundCommand = testUnit.findCommandByParameters(COMMAND_NAME, parsedCommandParameter, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByParametersViaCommandNameNoParameters() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getParameters()).thenReturn(null);
|
||||
Command foundCommand = testUnit.findCommandByParameters(COMMAND_NAME, parsedCommandParameter, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByParametersViaCommandNameNoParametersButParametersGiven() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getParameters()).thenReturn(null);
|
||||
Command foundCommand = testUnit.findCommandByParameters(COMMAND_NAME, parsedCommandParameter, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByParametersViaCommandAliasAdequateParameter() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME_2);
|
||||
when(commandConfiguration.getAliases()).thenReturn(Arrays.asList(COMMAND_NAME));
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getNecessaryParameterCount()).thenReturn(0);
|
||||
when(parsedCommandParameter.getParameters()).thenReturn(new ArrayList<>());
|
||||
Command foundCommand = testUnit.findCommandByParameters(COMMAND_NAME, parsedCommandParameter, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test(expected = InsufficientParametersException.class)
|
||||
public void testFindByParametersViaCommandNameInsufficientParameters() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getParameters()).thenReturn(Arrays.asList(parameter));
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(commandConfiguration.getNecessaryParameterCount()).thenReturn(1);
|
||||
when(parsedCommandParameter.getParameters()).thenReturn(new ArrayList<>());
|
||||
testUnit.findCommandByParameters(COMMAND_NAME, parsedCommandParameter, SERVER_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByParametersViaServerAliasAdequateParameter() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getNecessaryParameterCount()).thenReturn(0);
|
||||
when(parsedCommandParameter.getParameters()).thenReturn(new ArrayList<>());
|
||||
setupAliasTest();
|
||||
Command foundCommand = testUnit.findCommandByParameters(ALIAS_NAME, parsedCommandParameter, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindCommandViaName() {
|
||||
commands.add(firstCommand);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
Command foundName = testUnit.findCommandViaName(COMMAND_NAME);
|
||||
Assert.assertEquals(firstCommand, foundName);
|
||||
}
|
||||
|
||||
@Test(expected = CommandNotFoundException.class)
|
||||
public void testFindCommandViaNameNotFound() {
|
||||
commands.add(firstCommand);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME_2);
|
||||
testUnit.findCommandViaName(COMMAND_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsCommand() {
|
||||
Message message = setupIsCommandScenario();
|
||||
when(message.getContentRaw()).thenReturn("!com");
|
||||
Assert.assertTrue(testUnit.isCommand(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsCommandNoneFound() {
|
||||
Message message = setupIsCommandScenario();
|
||||
when(message.getContentRaw()).thenReturn("com");
|
||||
Assert.assertFalse(testUnit.isCommand(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommand() {
|
||||
setupPrefix();
|
||||
String foundCommandName = testUnit.getCommandName("!com", SERVER_ID);
|
||||
Assert.assertEquals("com", foundCommandName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandByNameWithoutAlias() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
Command foundCommand = testUnit.getCommandByName(COMMAND_NAME, false, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandByNameOptionalWithoutAlias() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
Optional<Command> foundCommandOptional = testUnit.getCommandByNameOptional(COMMAND_NAME, false, SERVER_ID);
|
||||
Assert.assertTrue(foundCommandOptional.isPresent());
|
||||
foundCommandOptional.ifPresent(command -> {
|
||||
Assert.assertEquals(firstCommand, foundCommandOptional.get());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandByNameNotFoundWithoutAlias() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME_2);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
Command foundCommand = testUnit.getCommandByName(COMMAND_NAME, false, SERVER_ID);
|
||||
Assert.assertNull(foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandByNameViaAlias() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
setupAliasTest();
|
||||
Command foundCommand = testUnit.getCommandByName(ALIAS_NAME, true, SERVER_ID);
|
||||
Assert.assertEquals(firstCommand, foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandByNameViaAliasNotFound() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
Command foundCommand = testUnit.getCommandByName(ALIAS_NAME, true, SERVER_ID);
|
||||
Assert.assertNull(foundCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandExistsWithoutAlias() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
Assert.assertTrue(testUnit.commandExists(COMMAND_NAME, false, SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandExistsWithoutAliasNotFound() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME_2);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
Assert.assertFalse(testUnit.commandExists(COMMAND_NAME, false, SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandExistsWithAlias() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
setupAliasTest();
|
||||
Assert.assertTrue(testUnit.commandExists(COMMAND_NAME, true, SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandExistsWithAliasNotFound() {
|
||||
commands.add(firstCommand);
|
||||
when(commandConfiguration.getName()).thenReturn(COMMAND_NAME_2);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
setupAliasTest();
|
||||
Assert.assertFalse(testUnit.commandExists(COMMAND_NAME, true, SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllCommandsFromModuleOneCommandFound() {
|
||||
ModuleDefinition moduleDefinition = Mockito.mock(ModuleDefinition.class);
|
||||
ModuleInfo info = Mockito.mock(ModuleInfo.class);
|
||||
String moduleName = "module";
|
||||
when(info.getName()).thenReturn(moduleName);
|
||||
when(moduleDefinition.getInfo()).thenReturn(info);
|
||||
when(commandConfiguration.getModule()).thenReturn(moduleName);
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
commands.add(firstCommand);
|
||||
List<Command> foundCommands = testUnit.getAllCommandsFromModule(moduleDefinition);
|
||||
Assert.assertEquals(1, foundCommands.size());
|
||||
Assert.assertEquals(firstCommand, foundCommands.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllCommandsFromModuleNoCommandFound() {
|
||||
ModuleDefinition moduleDefinition = Mockito.mock(ModuleDefinition.class);
|
||||
ModuleInfo info = Mockito.mock(ModuleInfo.class);
|
||||
String moduleName = "module";
|
||||
when(info.getName()).thenReturn(moduleName);
|
||||
when(moduleDefinition.getInfo()).thenReturn(info);
|
||||
when(commandConfiguration.getModule()).thenReturn(moduleName + "2");
|
||||
when(firstCommand.getConfiguration()).thenReturn(commandConfiguration);
|
||||
commands.add(firstCommand);
|
||||
List<Command> foundCommands = testUnit.getAllCommandsFromModule(moduleDefinition);
|
||||
Assert.assertEquals(0, foundCommands.size());
|
||||
}
|
||||
|
||||
|
||||
private void setupAliasTest() {
|
||||
when(commandInServerAliasService.getCommandInServerAlias(SERVER_ID, ALIAS_NAME)).thenReturn(Optional.of(alias));
|
||||
when(alias.getCommandInAServer()).thenReturn(commandInAServer);
|
||||
when(commandInAServer.getCommandReference()).thenReturn(aCommand);
|
||||
when(aCommand.getName()).thenReturn(COMMAND_NAME);
|
||||
}
|
||||
|
||||
private Message setupIsCommandScenario() {
|
||||
Message message = Mockito.mock(Message.class);
|
||||
setupPrefix();
|
||||
Guild guild = Mockito.mock(Guild.class);
|
||||
when(message.getGuild()).thenReturn(guild);
|
||||
when(guild.getIdLong()).thenReturn(SERVER_ID);
|
||||
return message;
|
||||
}
|
||||
|
||||
private void setupPrefix() {
|
||||
SystemConfigProperty defaultConfig = Mockito.mock(SystemConfigProperty.class);
|
||||
when(defaultConfig.getStringValue()).thenReturn(DEFAULT_PREFIX);
|
||||
when(defaultConfigManagementService.getDefaultConfig(PREFIX)).thenReturn(defaultConfig);
|
||||
when(configService.getStringValue(PREFIX, SERVER_ID, DEFAULT_PREFIX)).thenReturn(DEFAULT_PREFIX);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package dev.sheldan.abstracto.core.command.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInAServer;
|
||||
import dev.sheldan.abstracto.core.command.model.database.ACommandInServerAlias;
|
||||
import dev.sheldan.abstracto.core.command.repository.CommandInServerAliasRepository;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandInServerAliasManagementServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private CommandInServerAliasManagementServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private CommandInServerAliasRepository repository;
|
||||
|
||||
@Mock
|
||||
private ACommandInServerAlias alias;
|
||||
|
||||
@Mock
|
||||
private AServer server;
|
||||
|
||||
private static final String ALIAS_NAME = "alias";
|
||||
private static final String COMMAND_NAME = "command";
|
||||
|
||||
@Test
|
||||
public void testGetAliasesInServer() {
|
||||
when(repository.findByCommandInAServer_ServerReference(server)).thenReturn(Arrays.asList(alias));
|
||||
List<ACommandInServerAlias> foundAliases = testUnit.getAliasesInServer(server);
|
||||
Assert.assertEquals(1, foundAliases.size());
|
||||
Assert.assertEquals(alias, foundAliases.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesCommandInServerAliasExist() {
|
||||
when(repository.existsByCommandInAServer_ServerReferenceAndAliasId_NameEqualsIgnoreCase(server, ALIAS_NAME)).thenReturn(true);
|
||||
Assert.assertTrue(testUnit.doesCommandInServerAliasExist(server, ALIAS_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesCommandInServerAliasNotExist() {
|
||||
when(repository.existsByCommandInAServer_ServerReferenceAndAliasId_NameEqualsIgnoreCase(server, ALIAS_NAME)).thenReturn(false);
|
||||
Assert.assertFalse(testUnit.doesCommandInServerAliasExist(server, ALIAS_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandInServerAlias() {
|
||||
when(repository.findByCommandInAServer_ServerReferenceAndAliasId_NameEqualsIgnoreCase(server, ALIAS_NAME)).thenReturn(Optional.of(alias));
|
||||
Optional<ACommandInServerAlias> commandOptional = testUnit.getCommandInServerAlias(server, ALIAS_NAME);
|
||||
Assert.assertTrue(commandOptional.isPresent());
|
||||
commandOptional.ifPresent(existingAlias ->
|
||||
Assert.assertEquals(alias, existingAlias)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCommandInServerAliasNotExist() {
|
||||
when(repository.findByCommandInAServer_ServerReferenceAndAliasId_NameEqualsIgnoreCase(server, ALIAS_NAME)).thenReturn(Optional.empty());
|
||||
Optional<ACommandInServerAlias> commandOptional = testUnit.getCommandInServerAlias(server, ALIAS_NAME);
|
||||
Assert.assertFalse(commandOptional.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAliasForCommand() {
|
||||
ACommandInAServer commandInAServer = Mockito.mock(ACommandInAServer.class);
|
||||
ArgumentCaptor<ACommandInServerAlias> aliasArgumentCaptor = ArgumentCaptor.forClass(ACommandInServerAlias.class);
|
||||
ACommandInServerAlias savedAlias = Mockito.mock(ACommandInServerAlias.class);
|
||||
when(repository.save(aliasArgumentCaptor.capture())).thenReturn(savedAlias);
|
||||
ACommandInServerAlias createdAlias = testUnit.createAliasForCommand(commandInAServer, ALIAS_NAME);
|
||||
Assert.assertEquals(savedAlias, createdAlias);
|
||||
ACommandInServerAlias capturedAlias = aliasArgumentCaptor.getValue();
|
||||
Assert.assertEquals(commandInAServer, capturedAlias.getCommandInAServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteCommandInServerAlias() {
|
||||
testUnit.deleteCommandInServerAlias(alias);
|
||||
verify(repository, times(1)).delete(alias);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAliasesForCommandInServer() {
|
||||
when(repository.findByCommandInAServer_ServerReferenceAndCommandInAServer_CommandReference_NameEqualsIgnoreCase(server, COMMAND_NAME)).thenReturn(Arrays.asList(alias));
|
||||
List<ACommandInServerAlias> foundAliases = testUnit.getAliasesForCommandInServer(server, COMMAND_NAME);
|
||||
Assert.assertEquals(1, foundAliases.size());
|
||||
Assert.assertEquals(alias, foundAliases.get(0));
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
@@ -67,11 +68,13 @@ public class AllowedMentionManagementServiceBeanTest {
|
||||
@Test
|
||||
public void createCustomAllowedMention() {
|
||||
when(serverManagementService.loadOrCreate(SERVER_ID)).thenReturn(server);
|
||||
AllowedMention createdMention = testUnit.createCustomAllowedMention(SERVER_ID, allowedMention);
|
||||
ArgumentCaptor<AllowedMention> mentionCaptor = ArgumentCaptor.forClass(AllowedMention.class);
|
||||
verify(allowedMentionRepository, times(1)).save(mentionCaptor.capture());
|
||||
Assert.assertEquals(createdMention, mentionCaptor.getValue());
|
||||
Assert.assertEquals(server, createdMention.getServer());
|
||||
AllowedMention toReturn = Mockito.mock(AllowedMention.class);
|
||||
when(allowedMentionRepository.save(mentionCaptor.capture())).thenReturn(toReturn);
|
||||
AllowedMention createdMention = testUnit.createCustomAllowedMention(SERVER_ID, allowedMention);
|
||||
Assert.assertEquals(toReturn, createdMention);
|
||||
AllowedMention usedValue = mentionCaptor.getValue();
|
||||
Assert.assertEquals(server, usedValue.getServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -6,6 +6,8 @@ import dev.sheldan.abstracto.core.repository.FeatureModeRepository;
|
||||
import org.junit.Assert;
|
||||
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;
|
||||
@@ -43,29 +45,36 @@ public class FeatureModeManagementServiceBeanTest {
|
||||
@Mock
|
||||
private AFeatureMode aFeatureMode;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<AFeatureMode> modeCaptor;
|
||||
|
||||
private static final String FEATURE_MODE = "featureMode";
|
||||
|
||||
@Test
|
||||
public void createModeWithModeAsString() {
|
||||
when(featureFlag.getServer()).thenReturn(server);
|
||||
when(featureModeRepository.save(modeCaptor.capture())).thenReturn(aFeatureMode);
|
||||
AFeatureMode createdMode = testUnit.createMode(featureFlag, FEATURE_MODE, true);
|
||||
Assert.assertEquals(true, createdMode.getEnabled());
|
||||
Assert.assertEquals(featureFlag, createdMode.getFeatureFlag());
|
||||
Assert.assertEquals(FEATURE_MODE, createdMode.getFeatureMode());
|
||||
Assert.assertEquals(server, createdMode.getServer());
|
||||
verify(featureModeRepository, times(1)).save(createdMode);
|
||||
AFeatureMode createdValue = modeCaptor.getValue();
|
||||
Assert.assertEquals(aFeatureMode, createdMode);
|
||||
Assert.assertEquals(true, createdValue.getEnabled());
|
||||
Assert.assertEquals(featureFlag, createdValue.getFeatureFlag());
|
||||
Assert.assertEquals(FEATURE_MODE, createdValue.getFeatureMode());
|
||||
Assert.assertEquals(server, createdValue.getServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateMode() {
|
||||
when(featureFlag.getServer()).thenReturn(server);
|
||||
when(featureMode.getKey()).thenReturn(FEATURE_MODE);
|
||||
when(featureModeRepository.save(modeCaptor.capture())).thenReturn(aFeatureMode);
|
||||
AFeatureMode createdMode = testUnit.createMode(featureFlag, featureMode, true);
|
||||
Assert.assertEquals(true, createdMode.getEnabled());
|
||||
Assert.assertEquals(featureFlag, createdMode.getFeatureFlag());
|
||||
Assert.assertEquals(FEATURE_MODE, createdMode.getFeatureMode());
|
||||
Assert.assertEquals(server, createdMode.getServer());
|
||||
verify(featureModeRepository, times(1)).save(createdMode);
|
||||
AFeatureMode createdValue = modeCaptor.getValue();
|
||||
Assert.assertEquals(aFeatureMode, createdMode);
|
||||
Assert.assertEquals(true, createdValue.getEnabled());
|
||||
Assert.assertEquals(featureFlag, createdValue.getFeatureFlag());
|
||||
Assert.assertEquals(FEATURE_MODE, createdValue.getFeatureMode());
|
||||
Assert.assertEquals(server, createdValue.getServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -5,6 +5,8 @@ import dev.sheldan.abstracto.core.templating.repository.TemplateRepository;
|
||||
import org.junit.Assert;
|
||||
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;
|
||||
@@ -24,6 +26,12 @@ public class TemplateManagementServiceBeanTest {
|
||||
@Mock
|
||||
private TemplateRepository repository;
|
||||
|
||||
@Mock
|
||||
private Template template;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Template> templateArgumentCaptor;
|
||||
|
||||
private static final String TEMPLATE_KEY = "templateKey";
|
||||
private static final String TEMPLATE_SOURCE = "source";
|
||||
|
||||
@@ -53,11 +61,14 @@ public class TemplateManagementServiceBeanTest {
|
||||
|
||||
@Test
|
||||
public void testCreateTemplate() {
|
||||
Template template = templateManagementServiceBean.createTemplate(TEMPLATE_KEY, TEMPLATE_SOURCE);
|
||||
Assert.assertEquals(template.getContent(), TEMPLATE_SOURCE);
|
||||
Assert.assertEquals(template.getKey(), TEMPLATE_KEY);
|
||||
when(repository.save(templateArgumentCaptor.capture())).thenReturn(template);
|
||||
Template createdTemplate = templateManagementServiceBean.createTemplate(TEMPLATE_KEY, TEMPLATE_SOURCE);
|
||||
Template savedTemplate = templateArgumentCaptor.getValue();
|
||||
Assert.assertEquals(template, createdTemplate);
|
||||
Assert.assertEquals(TEMPLATE_SOURCE, savedTemplate.getContent());
|
||||
Assert.assertEquals(TEMPLATE_KEY, savedTemplate.getKey());
|
||||
verify(repository, times(1)).save(any(Template.class));
|
||||
Assert.assertTrue(Duration.between(template.getLastModified(), Instant.now()).getSeconds() < 1);
|
||||
Assert.assertTrue(Duration.between(savedTemplate.getLastModified(), Instant.now()).getSeconds() < 1);
|
||||
}
|
||||
|
||||
private Template getTemplate() {
|
||||
|
||||
Reference in New Issue
Block a user