Logic for FoundCommandResult.Status / Add tests for CommandHandler

- Fix other tests
- Add logic for smaller pending FIXME's
This commit is contained in:
ljacqu
2015-12-17 22:23:43 +01:00
parent 3e928cddf0
commit cbf0996197
9 changed files with 154 additions and 133 deletions
@@ -3,14 +3,10 @@ package fr.xephi.authme.command;
import fr.xephi.authme.permission.DefaultPermission;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@@ -19,15 +15,11 @@ import java.util.Set;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* Test for {@link CommandHandler}.
@@ -40,8 +32,6 @@ public class CommandHandlerTest {
@BeforeClass
public static void setUpCommandHandler() {
WrapperMock.createInstance();
CommandDescription authMeBase = createCommand(null, null, singletonList("authme"));
createCommand(PlayerPermission.LOGIN, authMeBase, singletonList("login"), newArgument("password", false));
createCommand(PlayerPermission.LOGIN, authMeBase, asList("register", "reg"),
@@ -54,48 +44,89 @@ public class CommandHandlerTest {
}
@Test
@Ignore
public void shouldForwardCommandToExecutable() {
public void shouldMapPartsToLoginChildCommand() {
// given
CommandSender sender = Mockito.mock(CommandSender.class);
given(sender.isOp()).willReturn(true);
String bukkitLabel = "authme";
String[] args = {"login", "password"};
List<String> parts = Arrays.asList("authme", "login", "test1");
// when
handler.processCommand(sender, bukkitLabel, args);
FoundCommandResult result = handler.mapPartsToCommand(parts);
// then
final CommandDescription loginCmd = getChildWithLabel("login", getCommandWithLabel("authme", commands));
verify(sender, never()).sendMessage(anyString());
verify(loginCmd.getExecutableCommand()).executeCommand(eq(sender), anyListOf(String.class));
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("login", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.SUCCESS));
assertThat(result.getArguments(), contains("test1"));
assertThat(result.getDifference(), equalTo(0.0));
}
@Test
@Ignore // TODO ljacqu Fix test --> command classes too tightly coupled at the moment
public void shouldRejectCommandWithTooManyArguments() {
public void shouldMapPartsToCommandWithNoCaseSensitivity() {
// given
CommandSender sender = Mockito.mock(CommandSender.class);
given(sender.isOp()).willReturn(true);
String bukkitLabel = "authme";
String[] args = {"login", "password", "__unneededArgument__"};
List<String> parts = Arrays.asList("Authme", "REG", "arg1", "arg2");
// when
boolean result = handler.processCommand(sender, bukkitLabel, args);
FoundCommandResult result = handler.mapPartsToCommand(parts);
// then
assertThat(result, equalTo(true));
assertSenderGotMessageContaining("help", sender);
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.SUCCESS));
assertThat(result.getArguments(), contains("arg1", "arg2"));
assertThat(result.getDifference(), equalTo(0.0));
}
@Test
public void shouldRejectCommandWithTooManyArguments() {
// given
List<String> parts = Arrays.asList("authme", "register", "pass123", "pass123", "pass123");
// when
FoundCommandResult result = handler.mapPartsToCommand(parts);
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
}
@Test
public void shouldSuggestCommandWithSimilarLabel() {
// given
List<String> parts = Arrays.asList("authme", "reh", "pass123", "pass123");
// when
FoundCommandResult result = handler.mapPartsToCommand(parts);
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() < 0.75, equalTo(true));
}
/** In contrast to the previous test, we test a command request with a very apart label. */
@Test
public void shouldSuggestMostSimilarCommand() {
// given
List<String> parts = Arrays.asList("authme", "asdfawetawty4asdca");
// when
FoundCommandResult result = handler.mapPartsToCommand(parts);
// then
assertThat(result.getCommandDescription(), not(nullValue()));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() > 0.75, equalTo(true));
}
// ----------
// Helper methods
// ----------
private static CommandDescription createCommand(PlayerPermission permission, CommandDescription parent,
List<String> labels, CommandArgumentDescription... arguments) {
CommandDescription.CommandBuilder command = CommandDescription.builder()
.labels(labels)
.parent(parent)
.permissions(DefaultPermission.OP_ONLY, permission)
.description("Test")
.detailedDescription("Test command")
.description(labels.get(0))
.detailedDescription("'" + labels.get(0) + "' test command")
.executableCommand(mock(ExecutableCommand.class));
if (arguments != null && arguments.length > 0) {
@@ -111,27 +142,17 @@ public class CommandHandlerTest {
return new CommandArgumentDescription(label, "Test description", isOptional);
}
private void assertSenderGotMessageContaining(String text, CommandSender sender) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(captor.capture());
assertThat(captor.getValue(), stringContainsInOrder(text));
private static CommandDescription getChildWithLabel(String childLabel, String parentLabel) {
CommandDescription parent = getCommandWithLabel(parentLabel, commands);
return getCommandWithLabel(childLabel, parent.getChildren());
}
private static CommandDescription getCommandWithLabel(String label, Collection<CommandDescription> commands) {
for (CommandDescription command : commands) {
if (command.getLabels().contains(label)) {
return command;
}
}
return null;
}
private static CommandDescription getChildWithLabel(String label, CommandDescription command) {
for (CommandDescription child : command.getChildren()) {
for (CommandDescription child : commands) {
if (child.getLabels().contains(label)) {
return child;
}
}
return null;
throw new RuntimeException("Could not find command with label '" + label + "'");
}
}
@@ -72,7 +72,7 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, Collections.singletonList("pass"));
command.executeCommand(sender, Arrays.asList("pass", "pass"));
// then
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
@@ -11,6 +11,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Arrays;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -52,7 +53,7 @@ public class AddEmailCommandTest {
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, new ArrayList<String>());
command.executeCommand(sender, Arrays.asList("mail@example", "other_example"));
// then
verify(authMeMock).getManagement();
@@ -59,19 +59,4 @@ public class LoginCommandTest {
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
}
@Test
public void shouldHandleMissingPassword() {
// given
Player sender = mock(Player.class);
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, new ArrayList<String>());
// then
// TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call
String password = null;
Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false));
}
}