Skip to content

Commit

Permalink
[apache#5831] fix(CLI): Fix CLi gives unexpected output when setting …
Browse files Browse the repository at this point in the history
…a tag

Add some test case.
  • Loading branch information
Abyss-lord committed Dec 29, 2024
1 parent cfe6691 commit 79ac312
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ public void missingArgs() throws Exception {
assertNull(namePart);
}

@Test
public void hasPartName() throws ParseException {
String[] argsWithoutName = {"catalog", "details", "--metalake", "metalake"};
CommandLine commandLineWithoutName = new DefaultParser().parse(options, argsWithoutName);
FullName fullNameWithoutName = new FullName(commandLineWithoutName);
assertFalse(fullNameWithoutName.hasName());

String[] argsWithName = {
"catalog", "details", "--metalake", "metalake", "--name", "Hive_catalog"
};
CommandLine commandLineWithName = new DefaultParser().parse(options, argsWithName);
FullName fullNameWithName = new FullName(commandLineWithName);
assertTrue(fullNameWithName.hasName());
}

@Test
public void hasPartNameMetalake() throws Exception {
String[] args = {"metalake", "details", "--metalake", "metalake"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
Expand Down Expand Up @@ -141,6 +142,30 @@ void testCreateTagCommand() {
verify(mockCreate).handle();
}

@Test
void testCreateCommandWithoutTagOption() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.TAG)).thenReturn(false);

GravitinoCommandLine commandLine =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.TAG, CommandActions.CREATE));

assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newCreateTags(
eq(GravitinoCommandLine.DEFAULT_URL),
eq(false),
eq("metalake_demo"),
isNull(),
isNull());
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(output, ErrorMessages.MISSING_TAG);
}

@Test
void testCreateTagsCommand() {
CreateTag mockCreate = mock(CreateTag.class);
Expand Down Expand Up @@ -272,6 +297,68 @@ void testSetTagPropertyCommand() {
verify(mockSetProperty).handle();
}

@Test
void testSetTagPropertyCommandWithoutPropertyOption() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.TAG)).thenReturn(true);
when(mockCommandLine.getOptionValues(GravitinoOptions.TAG)).thenReturn(new String[] {"tagA"});
when(mockCommandLine.hasOption(GravitinoOptions.PROPERTY)).thenReturn(false);
when(mockCommandLine.hasOption(GravitinoOptions.VALUE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.VALUE)).thenReturn("value");
GravitinoCommandLine commandLine =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.TAG, CommandActions.SET));

assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newSetTagProperty(
eq(GravitinoCommandLine.DEFAULT_URL),
eq(false),
eq("metalake_demo"),
eq("tagA"),
isNull(),
eq("value"));
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
output,
"Command cannot be executed. The set command only supports configuring tag properties or attaching "
+ "tags to entity.");
}

@Test
void testSetTagPropertyCommandWithoutValueOption() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.TAG)).thenReturn(true);
when(mockCommandLine.getOptionValues(GravitinoOptions.TAG)).thenReturn(new String[] {"tagA"});
when(mockCommandLine.hasOption(GravitinoOptions.PROPERTY)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.PROPERTY)).thenReturn("property");
when(mockCommandLine.hasOption(GravitinoOptions.VALUE)).thenReturn(false);
GravitinoCommandLine commandLine =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.TAG, CommandActions.SET));

assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newSetTagProperty(
eq(GravitinoCommandLine.DEFAULT_URL),
eq(false),
eq("metalake_demo"),
eq("tagA"),
eq("property"),
isNull());
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
output,
"Command cannot be executed. The set command only supports configuring tag properties or attaching "
+ "tags to entity.");
}

@Test
void testSetMultipleTagPropertyCommandError() {
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
Expand Down Expand Up @@ -448,6 +535,32 @@ public boolean matches(String[] argument) {
verify(mockTagEntity).handle();
}

@Test
void testTagEntityCommandWithoutName() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(false);
when(mockCommandLine.hasOption(GravitinoOptions.TAG)).thenReturn(true);
when(mockCommandLine.getOptionValues(GravitinoOptions.TAG)).thenReturn(new String[] {"tagA"});
GravitinoCommandLine commandLine =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.TAG, CommandActions.SET));

assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newTagEntity(
eq(GravitinoCommandLine.DEFAULT_URL),
eq(false),
eq("metalake_demo"),
isNull(),
argThat(
argument -> argument != null && argument.length > 0 && "tagA".equals(argument[0])));
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(output, ErrorMessages.MISSING_NAME);
}

@Test
void testTagsEntityCommand() {
TagEntity mockTagEntity = mock(TagEntity.class);
Expand Down Expand Up @@ -517,6 +630,37 @@ public boolean matches(String[] argument) {
verify(mockUntagEntity).handle();
}

@Test
void testUntagEntityCommandWithoutName() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(false);
when(mockCommandLine.hasOption(GravitinoOptions.TAG)).thenReturn(true);
when(mockCommandLine.getOptionValues(GravitinoOptions.TAG))
.thenReturn(new String[] {"tagA", "tagB"});
GravitinoCommandLine commandLine =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.TAG, CommandActions.REMOVE));

assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newUntagEntity(
eq(GravitinoCommandLine.DEFAULT_URL),
eq(false),
eq("metalake_demo"),
isNull(),
argThat(
argument ->
argument != null
&& argument.length > 0
&& "tagA".equals(argument[0])
&& "tagB".equals(argument[1])));
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(output, ErrorMessages.MISSING_NAME);
}

@Test
void testUntagsEntityCommand() {
UntagEntity mockUntagEntity = mock(UntagEntity.class);
Expand Down

0 comments on commit 79ac312

Please sign in to comment.