diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestRoleCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestRoleCommands.java index 88b380d63ee..42a4084049f 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestRoleCommands.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestRoleCommands.java @@ -19,14 +19,20 @@ package org.apache.gravitino.cli; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Options; import org.apache.gravitino.cli.commands.CreateRole; @@ -36,17 +42,35 @@ import org.apache.gravitino.cli.commands.RevokePrivilegesFromRole; import org.apache.gravitino.cli.commands.RoleAudit; import org.apache.gravitino.cli.commands.RoleDetails; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class TestRoleCommands { private CommandLine mockCommandLine; private Options mockOptions; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; @BeforeEach void setUp() { mockCommandLine = mock(CommandLine.class); mockOptions = mock(Options.class); + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @AfterEach + void restoreExitFlg() { + Main.useExit = true; + } + + @AfterEach + public void restoreStreams() { + System.setOut(originalOut); + System.setErr(originalErr); } @Test @@ -71,7 +95,7 @@ void testRoleDetailsCommand() { when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("admin"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"admin"}); GravitinoCommandLine commandLine = spy( new GravitinoCommandLine( @@ -89,7 +113,7 @@ void testRoleAuditCommand() { when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("group"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"group"}); when(mockCommandLine.hasOption(GravitinoOptions.AUDIT)).thenReturn(true); GravitinoCommandLine commandLine = spy( @@ -108,14 +132,39 @@ void testCreateRoleCommand() { when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("admin"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"admin"}); GravitinoCommandLine commandLine = spy( new GravitinoCommandLine( mockCommandLine, mockOptions, CommandEntities.ROLE, CommandActions.CREATE)); doReturn(mockCreate) .when(commandLine) - .newCreateRole(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "admin"); + .newCreateRole( + GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", new String[] {"admin"}); + commandLine.handleCommandLine(); + verify(mockCreate).handle(); + } + + @Test + void testCreateRolesCommand() { + CreateRole mockCreate = mock(CreateRole.class); + when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); + when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); + when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)) + .thenReturn(new String[] {"admin", "engineer", "scientist"}); + GravitinoCommandLine commandLine = + spy( + new GravitinoCommandLine( + mockCommandLine, mockOptions, CommandEntities.ROLE, CommandActions.CREATE)); + + doReturn(mockCreate) + .when(commandLine) + .newCreateRole( + eq(GravitinoCommandLine.DEFAULT_URL), + eq(false), + eq("metalake_demo"), + eq(new String[] {"admin", "engineer", "scientist"})); commandLine.handleCommandLine(); verify(mockCreate).handle(); } @@ -126,14 +175,45 @@ void testDeleteRoleCommand() { when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("admin"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"admin"}); GravitinoCommandLine commandLine = spy( new GravitinoCommandLine( mockCommandLine, mockOptions, CommandEntities.ROLE, CommandActions.DELETE)); doReturn(mockDelete) .when(commandLine) - .newDeleteRole(GravitinoCommandLine.DEFAULT_URL, false, false, "metalake_demo", "admin"); + .newDeleteRole( + GravitinoCommandLine.DEFAULT_URL, + false, + false, + "metalake_demo", + new String[] {"admin"}); + commandLine.handleCommandLine(); + verify(mockDelete).handle(); + } + + @Test + void testDeleteRolesCommand() { + DeleteRole mockDelete = mock(DeleteRole.class); + when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); + when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); + when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)) + .thenReturn(new String[] {"admin", "engineer", "scientist"}); + when(mockCommandLine.hasOption(GravitinoOptions.FORCE)).thenReturn(false); + GravitinoCommandLine commandLine = + spy( + new GravitinoCommandLine( + mockCommandLine, mockOptions, CommandEntities.ROLE, CommandActions.DELETE)); + + doReturn(mockDelete) + .when(commandLine) + .newDeleteRole( + eq(GravitinoCommandLine.DEFAULT_URL), + eq(false), + eq(false), + eq("metalake_demo"), + eq(new String[] {"admin", "engineer", "scientist"})); commandLine.handleCommandLine(); verify(mockDelete).handle(); } @@ -144,7 +224,7 @@ void testDeleteRoleForceCommand() { when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("admin"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"admin"}); when(mockCommandLine.hasOption(GravitinoOptions.FORCE)).thenReturn(true); GravitinoCommandLine commandLine = spy( @@ -152,7 +232,8 @@ void testDeleteRoleForceCommand() { mockCommandLine, mockOptions, CommandEntities.ROLE, CommandActions.DELETE)); doReturn(mockDelete) .when(commandLine) - .newDeleteRole(GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo", "admin"); + .newDeleteRole( + GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo", new String[] {"admin"}); commandLine.handleCommandLine(); verify(mockDelete).handle(); } @@ -166,7 +247,7 @@ void testGrantPrivilegesToRole() { when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("admin"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"admin"}); when(mockCommandLine.hasOption(GravitinoOptions.PRIVILEGE)).thenReturn(true); when(mockCommandLine.getOptionValues(GravitinoOptions.PRIVILEGE)).thenReturn(privileges); GravitinoCommandLine commandLine = @@ -195,7 +276,7 @@ void testRevokePrivilegesFromRole() { when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog"); when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(true); - when(mockCommandLine.getOptionValue(GravitinoOptions.ROLE)).thenReturn("admin"); + when(mockCommandLine.getOptionValues(GravitinoOptions.ROLE)).thenReturn(new String[] {"admin"}); when(mockCommandLine.hasOption(GravitinoOptions.PRIVILEGE)).thenReturn(true); when(mockCommandLine.getOptionValues(GravitinoOptions.PRIVILEGE)).thenReturn(privileges); GravitinoCommandLine commandLine = @@ -214,4 +295,23 @@ void testRevokePrivilegesFromRole() { commandLine.handleCommandLine(); verify(mockRevoke).handle(); } + + @Test + void testDeleteRoleCommandWithoutRole() { + Main.useExit = false; + when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); + when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); + when(mockCommandLine.hasOption(GravitinoOptions.ROLE)).thenReturn(false); + GravitinoCommandLine commandLine = + spy( + new GravitinoCommandLine( + mockCommandLine, mockOptions, CommandEntities.ROLE, CommandActions.REVOKE)); + + assertThrows(RuntimeException.class, commandLine::handleCommandLine); + verify(commandLine, never()) + .newDeleteRole( + eq(GravitinoCommandLine.DEFAULT_URL), eq(false), eq(false), eq("metalake_demo"), any()); + String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); + assertEquals(output, ErrorMessages.MISSING_ROLE); + } }