SystemCommandLocator { public Optional<SystemCommand> forName(final String commandName) { return systemCommandScanner.findCommands().stream() .filter(systemCommand -> systemCommand.getName().equals(commandName)) .findFirst(); }  Optional<SystemCommand> forName(final String commandName);  }
@Test public void shouldFindCommandByName() throws Exception { final SystemCommand systemCommand_1 = mock(SystemCommand.class); final SystemCommand systemCommand_2 = mock(SystemCommand.class); final SystemCommand systemCommand_3 = mock(SystemCommand.class); when(systemCommand_1.getName()).thenReturn("command_1"); when(systemCommand_2.getName()).thenReturn("command_2"); when(systemCommand_3.getName()).thenReturn("command_3"); when(systemCommandScanner.findCommands()).thenReturn(asList( systemCommand_1, systemCommand_2, systemCommand_3)); assertThat(systemCommandLocator.forName("command_2"), is(of(systemCommand_2))); }
@Test public void shouldReturnEmptyIfNoCommandFound() throws Exception { final SystemCommand systemCommand_1 = mock(SystemCommand.class); final SystemCommand systemCommand_2 = mock(SystemCommand.class); final SystemCommand systemCommand_3 = mock(SystemCommand.class); when(systemCommand_1.getName()).thenReturn("command_1"); when(systemCommand_2.getName()).thenReturn("command_2"); when(systemCommand_3.getName()).thenReturn("command_3"); when(systemCommandScanner.findCommands()).thenReturn(asList( systemCommand_1, systemCommand_2, systemCommand_3)); assertThat(systemCommandLocator.forName("other_command"), is(empty())); }