Skip to content

Commit

Permalink
[#5985] improvement(CLI): Fix role command that supports handling mul…
Browse files Browse the repository at this point in the history
…tiple values

Add test case.
  • Loading branch information
Abyss-lord committed Dec 25, 2024
1 parent 3948e02 commit b96f648
Showing 1 changed file with 110 additions and 10 deletions.
120 changes: 110 additions & 10 deletions clients/cli/src/test/java/org/apache/gravitino/cli/TestRoleCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -144,15 +224,16 @@ 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(
new GravitinoCommandLine(
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();
}
Expand All @@ -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 =
Expand Down Expand Up @@ -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 =
Expand All @@ -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);
}
}

0 comments on commit b96f648

Please sign in to comment.