### Question:
DebugCommand implements ExecutableCommand { @Override public void executeCommand(CommandSender sender, List<String> arguments) { DebugSection debugSection = findDebugSection(arguments); if (debugSection == null) { sendAvailableSections(sender); } else { executeSection(debugSection, sender, arguments); } }  @Override void executeCommand(CommandSender sender, List<String> arguments);  }### Answer:
@Test public void shouldListAllAvailableDebugSections() { CommandSender sender = mock(CommandSender.class); given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(false); given(permissionsManager.hasPermission(sender, DebugSectionPermissions.INPUT_VALIDATOR)).willReturn(true); given(permissionsManager.hasPermission(sender, DebugSectionPermissions.DATA_STATISTICS)).willReturn(true); command.executeCommand(sender, emptyList()); verify(debugSectionFactory, atLeast(MIN_DEBUG_SECTIONS)).newInstance(any(Class.class)); verify(permissionsManager, atLeast(MIN_DEBUG_SECTIONS)).hasPermission(eq(sender), any(DebugSectionPermissions.class)); ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class); verify(sender, times(4)).sendMessage(strCaptor.capture()); assertThat(strCaptor.getAllValues(), contains( equalTo(ChatColor.BLUE + "AuthMe debug utils"), equalTo("Sections available to you:"), containsString("stats: Outputs general data statistics"), containsString("valid: Checks if your config.yml allows a password / email"))); }
@Test public void shouldNotListAnyDebugSection() { CommandSender sender = mock(CommandSender.class); given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(false); command.executeCommand(sender, emptyList()); verify(debugSectionFactory, atLeast(MIN_DEBUG_SECTIONS)).newInstance(any(Class.class)); verify(permissionsManager, atLeast(MIN_DEBUG_SECTIONS)).hasPermission(eq(sender), any(DebugSectionPermissions.class)); ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class); verify(sender, times(3)).sendMessage(strCaptor.capture()); assertThat(strCaptor.getAllValues(), contains( equalTo(ChatColor.BLUE + "AuthMe debug utils"), equalTo("Sections available to you:"), containsString("You don't have permission to view any debug section"))); }
@Test public void shouldRunSection() { DebugSection section = spy(InputValidator.class); doNothing().when(section).execute(any(CommandSender.class), anyList()); doReturn(section).when(debugSectionFactory).newInstance(InputValidator.class); CommandSender sender = mock(CommandSender.class); given(permissionsManager.hasPermission(sender, section.getRequiredPermission())).willReturn(true); List<String> arguments = Arrays.asList(section.getName().toUpperCase(), "test", "toast"); command.executeCommand(sender, arguments); verify(permissionsManager).hasPermission(sender, section.getRequiredPermission()); verify(section).execute(sender, Arrays.asList("test", "toast")); }
@Test public void shouldNotRunSectionForMissingPermission() { DebugSection section = spy(InputValidator.class); doReturn(section).when(debugSectionFactory).newInstance(InputValidator.class); CommandSender sender = mock(CommandSender.class); given(permissionsManager.hasPermission(sender, section.getRequiredPermission())).willReturn(false); List<String> arguments = Arrays.asList(section.getName().toUpperCase(), "test"); command.executeCommand(sender, arguments); verify(permissionsManager).hasPermission(sender, section.getRequiredPermission()); verify(section, never()).execute(any(CommandSender.class), anyList()); verify(sender).sendMessage(argThat(containsString("You don't have permission"))); }