AddUserTask extends Task { @Override public void execute(final ImmutableMultimap<String, String> args, final PrintWriter printer) throws AuthenticationException { printer.println("Adding/update credentials ..."); final DbCredential credential = new DbCredential(); credential.setUser(args.get(ServerAPI.USER_PARAM).asList().get(0)); credential.setPassword(GrapesAuthenticator.encrypt(args.get(ServerAPI.PASSWORD_PARAM).asList().get(0))); if(!credential.isHealthy()){ printer.println("ERROR: Bad request! The provided credential are unhealthy or incomplete."); return; } repoHandler.store(credential); printer.println("Task performed successfully."); } AddUserTask(final RepositoryHandler repoHandler); @Override void execute(final ImmutableMultimap<String, String> args, final PrintWriter printer);  }
@Test public void testAddUser() throws UnknownHostException, AuthenticationException { final TestingRepositoryHandler repoHandler = new TestingRepositoryHandler(); final AddUserTask addUser = new AddUserTask(repoHandler); final ImmutableMultimap.Builder<String, String> builder = new Builder<String, String>(); builder.put(ServerAPI.USER_PARAM, "user"); builder.put(ServerAPI.PASSWORD_PARAM, "password"); Exception exception = null; try { addUser.execute(builder.build(), mock(PrintWriter.class)); } catch (Exception e) { exception = e; } assertNull(exception); final DbCredential credential = repoHandler.getCredential("user"); assertEquals("user", credential.getUser()); assertEquals(GrapesAuthenticator.encrypt("password"), credential.getPassword()); }