LdapHelper { public void register(String remoteUserName) throws IOException { String ldapServerName = PropertiesWrapper.get("ldap.server.name"); String ldapSshKeyFile = PropertiesWrapper.get("ldap.ssh.key"); CommandLineWrapper cmd = CommandLineWrapperBuilder.create() .withWorkingDirectoryPath(".") .withCommand("ssh") .withCommandArgs("-t", "-t", "-i", ldapSshKeyFile, ldapServerName, "useradd", remoteUserName) .withDefaultAsyncCommandLineIO() .build(); CommandLineIO commandLineIO = cmd.executeAsync(); CommandLineResultHandler resultHandler = commandLineIO.getResultHandler(); try { resultHandler.waitFor(DEFAULT_TIMEOUT); } catch (InterruptedException exception) { throw new CommandLineException("Adding user '" + remoteUserName + "' to LDAP took too long. The operation was aborted."); } if (resultHandler.getExitValue() != 0) { for (String outputString : commandLineIO.getOutputStringList()) { logger.log(Level.SEVERE, outputString); } for (String errorString : commandLineIO.getErrorStringList()) { logger.log(Level.SEVERE, errorString); } throw new CommandLineException("Unable to add user '" + remoteUserName + "' to LDAP : " + commandLineIO.getOutputStream().toString()); } for (String outputString : commandLineIO.getOutputStringList()) { logger.log(Level.INFO, outputString); } }  boolean isRegistered(String remoteUserName); void register(String remoteUserName);  }
@Test public void canRegisterRemoteUser() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); when(mockResultHandler.getExitValue()).thenReturn(0); when(mockResultHandler.hasResult()).thenReturn(true); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); ldapHelper.register(MOCK_REMOTE_USER_NAME); }
@Test(expected = CommandLineException.class) public void canThrowExceptionWhenRegisteringProcessTimesOut() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); doThrow(new InterruptedException()).when(mockResultHandler).waitFor(anyLong()); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); ldapHelper.register(MOCK_REMOTE_USER_NAME); }
@Test(expected = CommandLineException.class) public void canThrowExceptionWhenRegisteringProcessFails() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); when(mockResultHandler.getExitValue()).thenReturn(1); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); ByteArrayOutputStream mockOutputStream = mock(ByteArrayOutputStream.class); when(mockOutputStream.toString()).thenReturn("ERROR: user \"test111\" already exists\n" + "Connection to auth closed."); when(mockCmdIO.getOutputStream()).thenReturn(mockOutputStream); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); ldapHelper.register(MOCK_REMOTE_USER_NAME); }