diff --git a/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java b/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java index 559314fb680..7735040a0d9 100644 --- a/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java +++ b/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java @@ -6,8 +6,8 @@ package org.elasticsearch.shield.authc.esusers.tool; import com.google.common.io.Files; -import org.elasticsearch.common.base.Charsets; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.base.Charsets; import org.elasticsearch.common.cli.CliTool; import org.elasticsearch.common.cli.CliToolTestCase; import org.elasticsearch.common.cli.Terminal; @@ -22,7 +22,6 @@ import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; @@ -60,7 +59,7 @@ public class ESUsersToolTests extends CliToolTestCase { @Test public void testUseradd_Parse_NoPassword() throws Exception { - ESUsersTool tool = new ESUsersTool(new TerminalMock() { + ESUsersTool tool = new ESUsersTool(new MockTerminal() { @Override public char[] readSecret(String text, Object... args) { return "changeme".toCharArray(); @@ -85,7 +84,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", userRolesFile) .build(); - ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(new TerminalMock(), "user1", "changeme".toCharArray(), "r1", "r2"); + ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(new MockTerminal(), "user1", "changeme".toCharArray(), "r1", "r2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -116,7 +115,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", userRolesFile) .build(); - ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(new TerminalMock(), "user1", "changeme".toCharArray(), "r1", "r2"); + ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(new MockTerminal(), "user1", "changeme".toCharArray(), "r1", "r2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -149,7 +148,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", userRolesFile) .build(); - ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(new TerminalMock(), "user1", "changeme".toCharArray(), "r1", "r2"); + ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(new MockTerminal(), "user1", "changeme".toCharArray(), "r1", "r2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.CODE_ERROR)); @@ -182,7 +181,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", userRolesFile) .build(); - ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(new TerminalMock(), "user1"); + ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(new MockTerminal(), "user1"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -205,7 +204,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", userRolesFile) .build(); - ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(new TerminalMock(), "user2"); + ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(new MockTerminal(), "user2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -229,7 +228,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", userRolesFile) .build(); - ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(new TerminalMock(), "user2"); + ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(new MockTerminal(), "user2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -260,7 +259,7 @@ public class ESUsersToolTests extends CliToolTestCase { @Test public void testPasswd_Parse_MissingPassword() throws Exception { final AtomicReference secretRequested = new AtomicReference<>(false); - Terminal terminal = new TerminalMock() { + Terminal terminal = new MockTerminal() { @Override public char[] readSecret(String text, Object... args) { secretRequested.set(true); @@ -283,7 +282,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users", userFile) .build(); - ESUsersTool.Passwd cmd = new ESUsersTool.Passwd(new TerminalMock(), "user1", "changeme".toCharArray()); + ESUsersTool.Passwd cmd = new ESUsersTool.Passwd(new MockTerminal(), "user1", "changeme".toCharArray()); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -304,7 +303,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users", userFile) .build(); - ESUsersTool.Passwd cmd = new ESUsersTool.Passwd(new TerminalMock(), "user2", "changeme".toCharArray()); + ESUsersTool.Passwd cmd = new ESUsersTool.Passwd(new MockTerminal(), "user2", "changeme".toCharArray()); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.NO_USER)); } @@ -316,7 +315,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users", userFile) .build(); - ESUsersTool.Passwd cmd = new ESUsersTool.Passwd(new TerminalMock(), "user2", "changeme".toCharArray()); + ESUsersTool.Passwd cmd = new ESUsersTool.Passwd(new MockTerminal(), "user2", "changeme".toCharArray()); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.NO_USER)); } @@ -364,7 +363,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - ESUsersTool.Roles cmd = new ESUsersTool.Roles(new TerminalMock(), "user", new String[]{"foo"}, Strings.EMPTY_ARRAY); + ESUsersTool.Roles cmd = new ESUsersTool.Roles(new MockTerminal(), "user", new String[]{"foo"}, Strings.EMPTY_ARRAY); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -383,7 +382,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - ESUsersTool.Roles cmd = new ESUsersTool.Roles(new TerminalMock(), "user", Strings.EMPTY_ARRAY, new String[]{"foo"}); + ESUsersTool.Roles cmd = new ESUsersTool.Roles(new MockTerminal(), "user", Strings.EMPTY_ARRAY, new String[]{"foo"}); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -402,7 +401,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - ESUsersTool.Roles cmd = new ESUsersTool.Roles(new TerminalMock(), "user", new String[]{"newrole"}, new String[]{"foo"}); + ESUsersTool.Roles cmd = new ESUsersTool.Roles(new MockTerminal(), "user", new String[]{"newrole"}, new String[]{"foo"}); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); @@ -421,7 +420,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - ESUsersTool.Roles cmd = new ESUsersTool.Roles(new TerminalMock(), "does-not-exist", Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY); + ESUsersTool.Roles cmd = new ESUsersTool.Roles(new MockTerminal(), "does-not-exist", Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.NO_USER)); @@ -436,7 +435,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - LoggingTerminal catchTerminalOutput = new LoggingTerminal(); + CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); ESUsersTool.Roles cmd = new ESUsersTool.Roles(catchTerminalOutput, "user", Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY); CliTool.ExitStatus status = execute(cmd, settings); @@ -460,7 +459,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - LoggingTerminal catchTerminalOutput = new LoggingTerminal(); + CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, null); CliTool.ExitStatus status = execute(cmd, settings); @@ -479,7 +478,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users", usersFile) .build(); - LoggingTerminal catchTerminalOutput = new LoggingTerminal(); + CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, "admin"); CliTool.ExitStatus status = execute(cmd, settings); @@ -496,7 +495,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users_roles", usersRoleFile) .build(); - LoggingTerminal catchTerminalOutput = new LoggingTerminal(); + CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, "does-not-exist"); CliTool.ExitStatus status = execute(cmd, settings); @@ -512,7 +511,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users", usersFile) .build(); - LoggingTerminal catchTerminalOutput = new LoggingTerminal(); + CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, null); CliTool.ExitStatus status = execute(cmd, settings); @@ -532,7 +531,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.esusers.files.users", usersFile) .build(); - LoggingTerminal loggingTerminal = new LoggingTerminal(); + CaptureOutputTerminal loggingTerminal = new CaptureOutputTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(loggingTerminal, "admin"); CliTool.ExitStatus status = execute(cmd, settings); @@ -541,33 +540,6 @@ public class ESUsersToolTests extends CliToolTestCase { assertThat(loggingTerminal.getTerminalOutput(), hasItem(allOf(containsString("admin"), containsString("-")))); } - public static class LoggingTerminal extends TerminalMock { - - List terminalOutput = new ArrayList(); - - public LoggingTerminal() { - super(Verbosity.NORMAL); - } - - public LoggingTerminal(Verbosity verbosity) { - super(verbosity); - } - - @Override - protected void doPrint(String msg, Object... args) { - terminalOutput.add(String.format(Locale.ROOT, msg, args)); - } - - @Override - public void print(String msg, Object... args) { - doPrint(msg, args); - } - - public List getTerminalOutput() { - return terminalOutput; - } - } - private CliTool.ExitStatus execute(CliTool.Command cmd, Settings settings) throws Exception { Environment env = new Environment(settings); return cmd.execute(settings, env);