### Question:
CommandOptionsValidator { public Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender) { int argumentsLength = arguments.length - 1; if (argumentsLength < options.getMinimumNumberOfArguments() || argumentsLength >= options.getMaximumNumberOfArguments()) { return Optional.of(new Message("command.arguments.insufficient")); } Optional<String> permission = options.getPermission(); if (permission.isPresent() && !sender.hasPermission(permission.get())) { return Optional.of(new Message("permissions.no.permission")); } if (sender.getKind().equals(SenderKind.CONSOLE) && options.forPlayersOnly()) { throw new PlayerOnlyException(); } return Optional.empty(); }  Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender);  }### Answer:
@Test public void whenInvalidArgumentLength() { CommandOptionsValidator commandOptionsValidator = new CommandOptionsValidator(); assertTrue( commandOptionsValidator.validate(commandOptions(), new String[]{"a"}, mock(Sender.class)).isPresent()); assertTrue( commandOptionsValidator.validate(commandOptions(), new String[]{"a", "b", "c", "d", "e"}, mock(Sender.class)).isPresent()); Sender sender = mock(Sender.class); when(sender.getKind()).thenReturn(SenderKind.PLAYER); when(sender.hasPermission(any())).thenReturn(true); assertFalse( commandOptionsValidator.validate(commandOptions(), new String[]{"a", "b", "c"}, sender).isPresent() ); }
@Test(expected = PlayerOnlyException.class) public void whenConsoleSendsPlayer() { CommandOptionsValidator commandOptionsValidator = new CommandOptionsValidator(); Sender sender = mock(Sender.class); when(sender.getKind()).thenReturn(SenderKind.CONSOLE); when(sender.hasPermission(any())).thenReturn(true); commandOptionsValidator.validate(commandOptions(), new String[]{"a", "b", "c"}, sender); }
@Test public void whenDoesNotHavePermission() { CommandOptionsValidator optionsValidator = new CommandOptionsValidator(); Sender sender = mock(Sender.class); assertTrue(optionsValidator.validate(commandOptions(), new String[]{"a", "b", "c"}, sender).isPresent()); }