Skip to content

Commit

Permalink
Added working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VigneshSK17 committed Jan 14, 2025
1 parent ba071f0 commit c4ded5f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,12 @@ protected MetalakeDisable newMetalakeDisable(String url, boolean ignore, String
}

protected CatalogEnable newCatalogEnable(
String url, boolean ignore, String metalake, String catalog, boolean disable, boolean enableMetalake) {
String url,
boolean ignore,
String metalake,
String catalog,
boolean disable,
boolean enableMetalake) {
return new CatalogEnable(url, ignore, metalake, catalog, disable, enableMetalake);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public class CatalogEnable extends Command {
* @param enableMetalake Whether to enable it's metalake
*/
public CatalogEnable(
String url, boolean ignoreVersions, String metalake, String catalog, boolean disable, boolean enableMetalake) {
String url,
boolean ignoreVersions,
String metalake,
String catalog,
boolean disable,
boolean enableMetalake) {
super(url, ignoreVersions);
this.metalake = metalake;
this.catalog = catalog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ public class MetalakeEnable extends Command {
* @param ignoreVersions If true don't check the client/server versions match.
* @param disable Whether the disable flag is also turned on
* @param metalake The name of the metalake.
*
* @param enableAllCatalogs Whether to enable all catalogs.
*/
public MetalakeEnable(
String url, boolean ignoreVersions, String metalake, boolean disable, boolean enableAllCatalogs) {
String url,
boolean ignoreVersions,
String metalake,
boolean disable,
boolean enableAllCatalogs) {
super(url, ignoreVersions);
this.metalake = metalake;
this.disable = disable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

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;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -49,6 +48,7 @@
import org.apache.gravitino.cli.commands.UpdateCatalogComment;
import org.apache.gravitino.cli.commands.UpdateCatalogName;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -459,7 +459,7 @@ void testEnableCatalogCommand() {
doReturn(mockEnable)
.when(commandLine)
.newCatalogEnable(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false);
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false, false);
doReturn(mockEnable).when(mockEnable).validate();
commandLine.handleCommandLine();
verify(mockEnable).handle();
Expand All @@ -482,7 +482,7 @@ void testEnableCatalogCommandWithRecursive() {
doReturn(mockEnable)
.when(commandLine)
.newCatalogEnable(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", true);
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false, true);
doReturn(mockEnable).when(mockEnable).validate();
commandLine.handleCommandLine();
verify(mockEnable).handle();
Expand Down Expand Up @@ -513,24 +513,13 @@ void testDisableCatalogCommand() {
@SuppressWarnings("DefaultCharset")
void testCatalogWithDisableAndEnableOptions() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);
when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog");
when(mockCommandLine.hasOption(GravitinoOptions.DISABLE)).thenReturn(true);
when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);

GravitinoCommandLine commandLine =
CatalogEnable mockCatalogEnable =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.UPDATE));
new CatalogEnable(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", true, false));

assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newCatalogEnable(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false);
verify(commandLine, never())
.newCatalogDisable(GravitinoCommandLine.DEFAULT_URL, false, "melake_demo", "catalog");
assertTrue(errContent.toString().contains(ErrorMessages.INVALID_ENABLE_DISABLE));
assertThrows(RuntimeException.class, mockCatalogEnable::validate);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(ErrorMessages.INVALID_ENABLE_DISABLE, errOutput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@

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;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -46,8 +44,8 @@
import org.apache.gravitino.cli.commands.SetMetalakeProperty;
import org.apache.gravitino.cli.commands.UpdateMetalakeComment;
import org.apache.gravitino.cli.commands.UpdateMetalakeName;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -386,7 +384,7 @@ void testEnableMetalakeCommand() {
mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));
doReturn(mockEnable)
.when(commandLine)
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false, false);
doReturn(mockEnable).when(mockEnable).validate();
commandLine.handleCommandLine();
verify(mockEnable).handle();
Expand All @@ -405,7 +403,7 @@ void testEnableMetalakeCommandWithRecursive() {
mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));
doReturn(mockEnable)
.when(commandLine)
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", true);
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false, true);
doReturn(mockEnable).when(mockEnable).validate();
commandLine.handleCommandLine();
verify(mockEnable).handle();
Expand Down Expand Up @@ -434,21 +432,13 @@ void testDisableMetalakeCommand() {
@SuppressWarnings("DefaultCharset")
void testMetalakeWithDisableAndEnableOptions() {
Main.useExit = false;
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
when(mockCommandLine.getOptionValue(CommandEntities.METALAKE)).thenReturn("metalake_demo");
when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);
when(mockCommandLine.hasOption(GravitinoOptions.DISABLE)).thenReturn(true);

GravitinoCommandLine commandLine =
MetalakeEnable mockMetalakeEnable =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));
new MetalakeEnable(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", true, false));

Assert.assertThrows(RuntimeException.class, commandLine::handleCommandLine);
verify(commandLine, never())
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);
verify(commandLine, never())
.newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);
assertTrue(errContent.toString().contains(ErrorMessages.INVALID_ENABLE_DISABLE));
assertThrows(RuntimeException.class, mockMetalakeEnable::validate);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(ErrorMessages.INVALID_ENABLE_DISABLE, errOutput);
}
}

0 comments on commit c4ded5f

Please sign in to comment.