@Test void testCreateUserMethodSuccessDoesNotExist2() throws IOException { final String user = "user"; final String group = "group"; Mockito.when(this.executor.execute(Mockito.any(CommandLine.class))).thenThrow(new IOException()); Assertions.assertThatIOException().isThrownBy(() -> UNIXUtils.createUser(user, group, executor)); }
public static synchronized void createUser( final String user, @Nullable final String group, final Executor executor ) throws IOException { final CommandLine idCheckCommandLine = new CommandLine("id") .addArgument("-u") .addArgument(user); try { executor.execute(idCheckCommandLine); log.debug("User already exists"); } catch (final IOException ioe) { log.debug("User does not exist. Creating it now."); final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user); if (isGroupValid) { log.debug("Group and User are different so creating group now."); final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd") .addArgument(group); try { log.debug("Running command to create group: [{}]", groupCreateCommandLine); executor.execute(groupCreateCommandLine); } catch (IOException ioexception) { log.debug("Group creation threw an error as it might already exist", ioexception); } } final CommandLine userCreateCommandLine = new CommandLine(SUDO) .addArgument("useradd") .addArgument(user); if (isGroupValid) { userCreateCommandLine .addArgument("-G") .addArgument(group); } userCreateCommandLine .addArgument("-M"); log.debug("Running command to create user: [{}]", userCreateCommandLine); executor.execute(userCreateCommandLine); } }
UNIXUtils { public static synchronized void createUser( final String user, @Nullable final String group, final Executor executor ) throws IOException { final CommandLine idCheckCommandLine = new CommandLine("id") .addArgument("-u") .addArgument(user); try { executor.execute(idCheckCommandLine); log.debug("User already exists"); } catch (final IOException ioe) { log.debug("User does not exist. Creating it now."); final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user); if (isGroupValid) { log.debug("Group and User are different so creating group now."); final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd") .addArgument(group); try { log.debug("Running command to create group: [{}]", groupCreateCommandLine); executor.execute(groupCreateCommandLine); } catch (IOException ioexception) { log.debug("Group creation threw an error as it might already exist", ioexception); } } final CommandLine userCreateCommandLine = new CommandLine(SUDO) .addArgument("useradd") .addArgument(user); if (isGroupValid) { userCreateCommandLine .addArgument("-G") .addArgument(group); } userCreateCommandLine .addArgument("-M"); log.debug("Running command to create user: [{}]", userCreateCommandLine); executor.execute(userCreateCommandLine); } } }
UNIXUtils { public static synchronized void createUser( final String user, @Nullable final String group, final Executor executor ) throws IOException { final CommandLine idCheckCommandLine = new CommandLine("id") .addArgument("-u") .addArgument(user); try { executor.execute(idCheckCommandLine); log.debug("User already exists"); } catch (final IOException ioe) { log.debug("User does not exist. Creating it now."); final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user); if (isGroupValid) { log.debug("Group and User are different so creating group now."); final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd") .addArgument(group); try { log.debug("Running command to create group: [{}]", groupCreateCommandLine); executor.execute(groupCreateCommandLine); } catch (IOException ioexception) { log.debug("Group creation threw an error as it might already exist", ioexception); } } final CommandLine userCreateCommandLine = new CommandLine(SUDO) .addArgument("useradd") .addArgument(user); if (isGroupValid) { userCreateCommandLine .addArgument("-G") .addArgument(group); } userCreateCommandLine .addArgument("-M"); log.debug("Running command to create user: [{}]", userCreateCommandLine); executor.execute(userCreateCommandLine); } } private  UNIXUtils(); }
UNIXUtils { public static synchronized void createUser( final String user, @Nullable final String group, final Executor executor ) throws IOException { final CommandLine idCheckCommandLine = new CommandLine("id") .addArgument("-u") .addArgument(user); try { executor.execute(idCheckCommandLine); log.debug("User already exists"); } catch (final IOException ioe) { log.debug("User does not exist. Creating it now."); final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user); if (isGroupValid) { log.debug("Group and User are different so creating group now."); final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd") .addArgument(group); try { log.debug("Running command to create group: [{}]", groupCreateCommandLine); executor.execute(groupCreateCommandLine); } catch (IOException ioexception) { log.debug("Group creation threw an error as it might already exist", ioexception); } } final CommandLine userCreateCommandLine = new CommandLine(SUDO) .addArgument("useradd") .addArgument(user); if (isGroupValid) { userCreateCommandLine .addArgument("-G") .addArgument(group); } userCreateCommandLine .addArgument("-M"); log.debug("Running command to create user: [{}]", userCreateCommandLine); executor.execute(userCreateCommandLine); } } private  UNIXUtils(); static synchronized void createUser(
        final String user,
        @Nullable final String group,
        final Executor executor
    ); static void changeOwnershipOfDirectory(
        final String dir,
        final String user,
        final Executor executor
    ); }
UNIXUtils { public static synchronized void createUser( final String user, @Nullable final String group, final Executor executor ) throws IOException { final CommandLine idCheckCommandLine = new CommandLine("id") .addArgument("-u") .addArgument(user); try { executor.execute(idCheckCommandLine); log.debug("User already exists"); } catch (final IOException ioe) { log.debug("User does not exist. Creating it now."); final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user); if (isGroupValid) { log.debug("Group and User are different so creating group now."); final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd") .addArgument(group); try { log.debug("Running command to create group: [{}]", groupCreateCommandLine); executor.execute(groupCreateCommandLine); } catch (IOException ioexception) { log.debug("Group creation threw an error as it might already exist", ioexception); } } final CommandLine userCreateCommandLine = new CommandLine(SUDO) .addArgument("useradd") .addArgument(user); if (isGroupValid) { userCreateCommandLine .addArgument("-G") .addArgument(group); } userCreateCommandLine .addArgument("-M"); log.debug("Running command to create user: [{}]", userCreateCommandLine); executor.execute(userCreateCommandLine); } } private  UNIXUtils(); static synchronized void createUser(
        final String user,
        @Nullable final String group,
        final Executor executor
    ); static void changeOwnershipOfDirectory(
        final String dir,
        final String user,
        final Executor executor
    );  }