Skip to content

Commit

Permalink
[apache#6086] fix(CLI): Refactor the validation logic of Metalake
Browse files Browse the repository at this point in the history
add test case.
  • Loading branch information
Abyss-lord committed Jan 3, 2025
1 parent 6ddea93 commit 1c01f97
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,5 @@ public void testGetMetalakeWithoutMetalakeOption() throws ParseException {
FullName fullName = new FullName(commandLine);
String metalakeName = fullName.getMetalakeName();
assertNull(metalakeName);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(errOutput, ErrorMessages.MISSING_METALAKE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.gravitino.cli;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -29,6 +31,7 @@

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.CreateMetalake;
Expand Down Expand Up @@ -87,6 +90,7 @@ void testListMetalakesCommand() {
doReturn(mockList)
.when(commandLine)
.newListMetalakes(GravitinoCommandLine.DEFAULT_URL, false, null);
doReturn(mockList).when(mockList).verify();
commandLine.handleCommandLine();
verify(mockList).handle();
}
Expand All @@ -104,10 +108,23 @@ void testMetalakeDetailsCommand() {
doReturn(mockDetails)
.when(commandLine)
.newMetalakeDetails(GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo");
doReturn(mockDetails).when(mockDetails).verify();
commandLine.handleCommandLine();
verify(mockDetails).handle();
}

@Test
void testMetalakeDetailsCommandWithoutMetalake() {
Main.useExit = false;
MetalakeDetails metalakeDetails =
spy(new MetalakeDetails(GravitinoCommandLine.DEFAULT_URL, false, "table", null));

assertThrows(RuntimeException.class, metalakeDetails::verify);
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
verify(metalakeDetails, never()).handle();
assertEquals(ErrorMessages.MISSING_METALAKE, output);
}

@Test
void testMetalakeAuditCommand() {
MetalakeAudit mockAudit = mock(MetalakeAudit.class);
Expand All @@ -121,10 +138,22 @@ void testMetalakeAuditCommand() {
doReturn(mockAudit)
.when(commandLine)
.newMetalakeAudit(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo");
doReturn(mockAudit).when(mockAudit).verify();
commandLine.handleCommandLine();
verify(mockAudit).handle();
}

@Test
void testMetalakeAuditCommandWithoutMetalake() {
Main.useExit = false;
MetalakeAudit metalakeAudit =
spy(new MetalakeAudit(GravitinoCommandLine.DEFAULT_URL, false, null));

assertThrows(RuntimeException.class, metalakeAudit::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testCreateMetalakeCommand() {
CreateMetalake mockCreate = mock(CreateMetalake.class);
Expand All @@ -139,10 +168,22 @@ void testCreateMetalakeCommand() {
doReturn(mockCreate)
.when(commandLine)
.newCreateMetalake(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "comment");
doReturn(mockCreate).when(mockCreate).verify();
commandLine.handleCommandLine();
verify(mockCreate).handle();
}

@Test
void testCreateMetalakeCommandWithoutMetalake() {
Main.useExit = false;
CreateMetalake metalakeCreate =
spy(new CreateMetalake(GravitinoCommandLine.DEFAULT_URL, false, null, null));

assertThrows(RuntimeException.class, metalakeCreate::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testCreateMetalakeCommandNoComment() {
CreateMetalake mockCreate = mock(CreateMetalake.class);
Expand All @@ -155,6 +196,7 @@ void testCreateMetalakeCommandNoComment() {
doReturn(mockCreate)
.when(commandLine)
.newCreateMetalake(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", null);
doReturn(mockCreate).when(mockCreate).verify();
commandLine.handleCommandLine();
verify(mockCreate).handle();
}
Expand All @@ -171,10 +213,22 @@ void testDeleteMetalakeCommand() {
doReturn(mockDelete)
.when(commandLine)
.newDeleteMetalake(GravitinoCommandLine.DEFAULT_URL, false, false, "metalake_demo");
doReturn(mockDelete).when(mockDelete).verify();
commandLine.handleCommandLine();
verify(mockDelete).handle();
}

@Test
void testDeleteMetalakeCommandWithoutMetalake() {
Main.useExit = false;
DeleteMetalake metalakeDelete =
spy(new DeleteMetalake(GravitinoCommandLine.DEFAULT_URL, false, true, null));

assertThrows(RuntimeException.class, metalakeDelete::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testDeleteMetalakeForceCommand() {
DeleteMetalake mockDelete = mock(DeleteMetalake.class);
Expand All @@ -188,6 +242,7 @@ void testDeleteMetalakeForceCommand() {
doReturn(mockDelete)
.when(commandLine)
.newDeleteMetalake(GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo");
doReturn(mockDelete).when(mockDelete).verify();
commandLine.handleCommandLine();
verify(mockDelete).handle();
}
Expand All @@ -209,10 +264,61 @@ void testSetMetalakePropertyCommand() {
.when(commandLine)
.newSetMetalakeProperty(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "property", "value");
doReturn(mockSetProperty).when(mockSetProperty).verify();
commandLine.handleCommandLine();
verify(mockSetProperty).handle();
}

@Test
void testSetMetalakePropertyCommandWithoutMetalake() {
Main.useExit = false;
SetMetalakeProperty metalakeProperty =
spy(new SetMetalakeProperty(GravitinoCommandLine.DEFAULT_URL, false, null, null, null));

assertThrows(RuntimeException.class, metalakeProperty::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testSetMetalakePropertyCommandWithoutPropertyAndValue() {
Main.useExit = false;
SetMetalakeProperty metalakeProperty =
spy(
new SetMetalakeProperty(
GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null, null));

assertThrows(RuntimeException.class, metalakeProperty::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals("Missing --property and --value options.", errOutput);
}

@Test
void testSetMetalakePropertyCommandWithoutProperty() {
Main.useExit = false;
SetMetalakeProperty metalakeProperty =
spy(
new SetMetalakeProperty(
GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null, "val1"));

assertThrows(RuntimeException.class, metalakeProperty::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_PROPERTY, errOutput);
}

@Test
void testSetMetalakePropertyCommandWithoutValue() {
Main.useExit = false;
SetMetalakeProperty metalakeProperty =
spy(
new SetMetalakeProperty(
GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", "property1", null));

assertThrows(RuntimeException.class, metalakeProperty::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_VALUE, errOutput);
}

@Test
void testRemoveMetalakePropertyCommand() {
RemoveMetalakeProperty mockRemoveProperty = mock(RemoveMetalakeProperty.class);
Expand All @@ -228,10 +334,35 @@ void testRemoveMetalakePropertyCommand() {
.when(commandLine)
.newRemoveMetalakeProperty(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "property");
doReturn(mockRemoveProperty).when(mockRemoveProperty).verify();
commandLine.handleCommandLine();
verify(mockRemoveProperty).handle();
}

@Test
void testRemoveMetalakePropertyCommandWithoutMetalake() {
Main.useExit = false;
RemoveMetalakeProperty mockRemoveProperty =
spy(new RemoveMetalakeProperty(GravitinoCommandLine.DEFAULT_URL, false, null, "property1"));

assertThrows(RuntimeException.class, mockRemoveProperty::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testRemoveMetalakePropertyCommandWithoutProperty() {
Main.useExit = false;
RemoveMetalakeProperty mockRemoveProperty =
spy(
new RemoveMetalakeProperty(
GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null));

assertThrows(RuntimeException.class, mockRemoveProperty::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_PROPERTY, errOutput);
}

@Test
void testListMetalakePropertiesCommand() {
ListMetalakeProperties mockListProperties = mock(ListMetalakeProperties.class);
Expand All @@ -244,10 +375,22 @@ void testListMetalakePropertiesCommand() {
doReturn(mockListProperties)
.when(commandLine)
.newListMetalakeProperties(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo");
doReturn(mockListProperties).when(mockListProperties).verify();
commandLine.handleCommandLine();
verify(mockListProperties).handle();
}

@Test
void testListMetalakePropertiesCommandWithoutMetalake() {
Main.useExit = false;
ListMetalakeProperties mockListProperties =
spy(new ListMetalakeProperties(GravitinoCommandLine.DEFAULT_URL, false, null));

assertThrows(RuntimeException.class, mockListProperties::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testUpdateMetalakeCommentCommand() {
UpdateMetalakeComment mockUpdateComment = mock(UpdateMetalakeComment.class);
Expand All @@ -263,10 +406,35 @@ void testUpdateMetalakeCommentCommand() {
.when(commandLine)
.newUpdateMetalakeComment(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "new comment");
doReturn(mockUpdateComment).when(mockUpdateComment).verify();
commandLine.handleCommandLine();
verify(mockUpdateComment).handle();
}

@Test
void testUpdateMetalakeCommentCommandWithoutMetalake() {
Main.useExit = false;
UpdateMetalakeComment mockUpdateComment =
spy(new UpdateMetalakeComment(GravitinoCommandLine.DEFAULT_URL, false, null, "comment"));

assertThrows(RuntimeException.class, mockUpdateComment::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testUpdateMetalakeCommentCommandWithoutComment() {
Main.useExit = false;
UpdateMetalakeComment mockUpdateComment =
spy(
new UpdateMetalakeComment(
GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null));

assertThrows(RuntimeException.class, mockUpdateComment::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_COMMENT, errOutput);
}

@Test
void testUpdateMetalakeNameCommand() {
UpdateMetalakeName mockUpdateName = mock(UpdateMetalakeName.class);
Expand All @@ -282,10 +450,24 @@ void testUpdateMetalakeNameCommand() {
.when(commandLine)
.newUpdateMetalakeName(
GravitinoCommandLine.DEFAULT_URL, false, false, "metalake_demo", "new_name");
doReturn(mockUpdateName).when(mockUpdateName).verify();
commandLine.handleCommandLine();
verify(mockUpdateName).handle();
}

@Test
void testUpdateMetalakeNameCommandWithoutName() {
Main.useExit = false;
UpdateMetalakeName mockUpdateName =
spy(
new UpdateMetalakeName(
GravitinoCommandLine.DEFAULT_URL, false, false, "demo_metalake", null));

assertThrows(RuntimeException.class, mockUpdateName::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_NAME, errOutput);
}

@Test
void testUpdateMetalakeNameForceCommand() {
UpdateMetalakeName mockUpdateName = mock(UpdateMetalakeName.class);
Expand All @@ -302,6 +484,7 @@ void testUpdateMetalakeNameForceCommand() {
.when(commandLine)
.newUpdateMetalakeName(
GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo", "new_name");
doReturn(mockUpdateName).when(mockUpdateName).verify();
commandLine.handleCommandLine();
verify(mockUpdateName).handle();
}
Expand All @@ -319,10 +502,22 @@ void testEnableMetalakeCommand() {
doReturn(mockEnable)
.when(commandLine)
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);
doReturn(mockEnable).when(mockEnable).verify();
commandLine.handleCommandLine();
verify(mockEnable).handle();
}

@Test
void testEnableMetalakeCommandWithoutMetalake() {
Main.useExit = false;
MetalakeEnable mockEnable =
spy(new MetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, null, false));

assertThrows(RuntimeException.class, mockEnable::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
void testEnableMetalakeCommandWithRecursive() {
MetalakeEnable mockEnable = mock(MetalakeEnable.class);
Expand All @@ -337,6 +532,7 @@ void testEnableMetalakeCommandWithRecursive() {
doReturn(mockEnable)
.when(commandLine)
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", true);
doReturn(mockEnable).when(mockEnable).verify();
commandLine.handleCommandLine();
verify(mockEnable).handle();
}
Expand All @@ -355,11 +551,22 @@ void testDisableMetalakeCommand() {
doReturn(mockDisable)
.when(commandLine)
.newMetalakeDisable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo");

doReturn(mockDisable).when(mockDisable).verify();
commandLine.handleCommandLine();
verify(mockDisable).handle();
}

@Test
void testDisableMetalakeCommandWithoutMetalake() {
Main.useExit = false;
MetalakeDisable mockDisable =
spy(new MetalakeDisable(GravitinoCommandLine.DEFAULT_URL, false, null));

assertThrows(RuntimeException.class, mockDisable::verify);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(ErrorMessages.MISSING_METALAKE, errOutput);
}

@Test
@SuppressWarnings("DefaultCharset")
void testMetalakeWithDisableAndEnableOptions() {
Expand Down

0 comments on commit 1c01f97

Please sign in to comment.