[AB-165] removing mocking utils and general test improvements

This commit is contained in:
Sheldan
2021-02-28 21:46:45 +01:00
parent 5f6746d742
commit 821971523e
55 changed files with 834 additions and 842 deletions

View File

@@ -50,7 +50,7 @@ public class AChannelParameterHandlerImplTest extends AbstractParameterHandlerTe
@Test
public void testProperChannelMention() {
UnparsedCommandParameterPiece piece = getPieceWithValue("input");
UnparsedCommandParameterPiece piece = getPiece();
when(textChannelParameterHandler.handle(piece, iterators, TextChannel.class, message)).thenReturn(channel);
when(channelService.getFakeChannelFromTextChannel(channel)).thenReturn(aChannel);
AChannel parsed = (AChannel) testUnit.handle(piece, iterators, TextChannel.class, message);

View File

@@ -51,7 +51,7 @@ public class ARoleParameterHandlerImplImplTest extends AbstractParameterHandlerT
@Test
public void testProperRoleMention() {
UnparsedCommandParameterPiece piece = getPieceWithValue("test");
UnparsedCommandParameterPiece piece = getPiece();
when(roleParameterHandler.handle(piece, iterators, Role.class, message)).thenReturn(role);
when(roleService.getFakeRoleFromRole(role)).thenReturn(aRole);
ARole parsed = (ARole) testUnit.handle(piece, iterators, AEmote.class, message);

View File

@@ -1,9 +1,18 @@
package dev.sheldan.abstracto.core.command.handler;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
public abstract class AbstractParameterHandlerTest {
protected UnparsedCommandParameterPiece getPieceWithValue(String value) {
return UnparsedCommandParameterPiece.builder().value(value).build();
UnparsedCommandParameterPiece mock = Mockito.mock(UnparsedCommandParameterPiece.class);
when(mock.getValue()).thenReturn(value);
return mock;
}
protected UnparsedCommandParameterPiece getPiece() {
return Mockito.mock(UnparsedCommandParameterPiece.class);
}
}

View File

@@ -51,8 +51,7 @@ public class FullRoleParameterHandlerImplImplTest extends AbstractParameterHandl
@Test
public void testProperEmoteMention() {
String input = "test";
UnparsedCommandParameterPiece piece = getPieceWithValue(input);
UnparsedCommandParameterPiece piece = getPiece();
when(roleParameterHandler.handle(piece, iterators, Role.class, message)).thenReturn(role);
when(roleService.getFakeRoleFromRole(role)).thenReturn(aRole);
FullRole parsed = (FullRole) testUnit.handle(piece, iterators, FullRole.class, message);

View File

@@ -11,6 +11,7 @@ import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.List;
import static org.mockito.Mockito.when;
@@ -26,22 +27,26 @@ public class CommandServiceBeanTest {
@Test
public void testUsageWithoutParameters() {
executeTest("test", commandWithConfig(getNoParameters()));
CommandConfiguration parameters = getNoParameters();
executeTest("test", commandWithConfig(parameters));
}
@Test
public void testUsageWithOptionalParameter() {
executeTest("test [param1]", commandWithConfig(getOptionalParameterConfig()));
CommandConfiguration parameters = getOptionalParameterConfig();
executeTest("test [param1]", commandWithConfig(parameters));
}
@Test
public void testUsageWithMandatoryParameter() {
executeTest("test <param1>", commandWithConfig(getMandatoryParameterConfig()));
CommandConfiguration getparameters = getMandatoryParameterConfig();
executeTest("test <param1>", commandWithConfig(getparameters));
}
@Test
public void testUsageWithMixedParameters() {
executeTest("test <param1> [param2]", commandWithConfig(getMixedParameterConfig()));
CommandConfiguration parameters = getMixedParameterConfig();
executeTest("test <param1> [param2]", commandWithConfig(parameters));
}
private void executeTest(String expectedUsage, Command commandToExecute) {
@@ -56,40 +61,46 @@ public class CommandServiceBeanTest {
}
private CommandConfiguration getNoParameters() {
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.build();
CommandConfiguration configuration = Mockito.mock(CommandConfiguration.class);
when(configuration.getName()).thenReturn(COMMAND_NAME);
return configuration;
}
private CommandConfiguration getOptionalParameterConfig() {
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.parameters(Arrays.asList(getOptionalParameter(true)))
.build();
CommandConfiguration configuration = Mockito.mock(CommandConfiguration.class);
when(configuration.getName()).thenReturn(COMMAND_NAME);
List<Parameter> parameters = Arrays.asList(getParameter(true));
when(configuration.getParameters()).thenReturn(parameters);
return configuration;
}
private CommandConfiguration getMandatoryParameterConfig() {
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.parameters(Arrays.asList(getOptionalParameter(false)))
.build();
CommandConfiguration configuration = Mockito.mock(CommandConfiguration.class);
when(configuration.getName()).thenReturn(COMMAND_NAME);
List<Parameter> parameters = Arrays.asList(getParameter(false));
when(configuration.getParameters()).thenReturn(parameters);
return configuration;
}
private CommandConfiguration getMixedParameterConfig() {
Parameter param1 = Parameter.builder().name(PARAMETER_1_NAME).type(Object.class).optional(false).build();
Parameter param2 = Parameter.builder().name(PARAMETER_2_NAME).type(Object.class).optional(true).build();
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.parameters(Arrays.asList(param1, param2))
.build();
Parameter param1 = getParameter(false);
Parameter param2 = Mockito.mock(Parameter.class);
when(param2.getName()).thenReturn(PARAMETER_2_NAME);
when(param2.getType()).thenReturn(Object.class);
when(param2.isOptional()).thenReturn(true);
CommandConfiguration configuration = Mockito.mock(CommandConfiguration.class);
when(configuration.getName()).thenReturn(COMMAND_NAME);
List<Parameter> parameters = Arrays.asList(param1, param2);
when(configuration.getParameters()).thenReturn(parameters);
return configuration;
}
private Parameter getOptionalParameter(boolean b) {
return Parameter.builder().name(PARAMETER_1_NAME).type(Object.class).optional(b).build();
private Parameter getParameter(boolean optional) {
Parameter parameter = Mockito.mock(Parameter.class);
when(parameter.getType()).thenReturn(Object.class);
when(parameter.getName()).thenReturn(PARAMETER_1_NAME);
when(parameter.isOptional()).thenReturn(optional);
return parameter;
}
}

View File

@@ -1,61 +0,0 @@
package dev.sheldan.abstracto.core.test;
import dev.sheldan.abstracto.core.models.database.*;
import net.dv8tion.jda.api.requests.RestAction;
import java.util.function.Consumer;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
public class MockUtils {
private MockUtils() {
}
public static AUserInAServer getUserObject(Long id, AServer server) {
AUser user = AUser.builder().id(id).build();
AUserInAServer createdUser = AUserInAServer.builder().userReference(user).serverReference(server).userInServerId(id).build();
server.getUsers().add(createdUser);
return createdUser;
}
public static AServer getServer(Long id){
return AServer.builder().id(id).build();
}
public static AServer getServer() {
return getServer(2L);
}
public static AChannel getTextChannel(AServer server, Long id) {
return AChannel.builder().id(id).server(server).deleted(false).type(AChannelType.TEXT).build();
}
public static ARole getRole(Long id, AServer server) {
return ARole.builder().server(server).id(id).build();
}
public static void mockQueueDoubleVoidConsumer(RestAction action) {
doAnswer(invocationOnMock -> {
Object consumerObj = invocationOnMock.getArguments()[0];
if(consumerObj instanceof Consumer) {
Consumer<Void> consumer = (Consumer) consumerObj;
consumer.accept(null);
}
return null;
}).when(action).queue(any(Consumer.class), any(Consumer.class));
}
public static void mockQueueVoidConsumer(RestAction action) {
doAnswer(invocationOnMock -> {
Object consumerObj = invocationOnMock.getArguments()[0];
if(consumerObj instanceof Consumer) {
Consumer<Void> consumer = (Consumer) consumerObj;
consumer.accept(null);
}
return null;
}).when(action).queue(any(Consumer.class));
}
}