Skip to content

Commit

Permalink
[#6326] improve(CLI): Make CLI more extendable and maintainable.
Browse files Browse the repository at this point in the history
add test..
  • Loading branch information
Abyss-lord committed Jan 20, 2025
1 parent ced3ab8 commit 13ea1f1
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@
/**
* Generic configuration class that handles command-line options, environment variables, and
* configuration file settings for a Gravitino application.
*
* @author pancx
*/
public class GenericConfig {
public static final String DEFAULT_URL = "http://localhost:8090";
public static final int WITHOUT_LIMIT = -1;
private String url;
private CommandLine commandLine;
private boolean ignoreVersion;
private String outputFormat;
private final String url;
private final CommandLine commandLine;
private final boolean ignoreVersion;
private final String outputFormat;
private boolean quiet;
private int limit;
private BorderStyle borderStyle;
private HorizontalAlign headerAlign;
private HorizontalAlign dataAlign;
private final int limit;
private final BorderStyle borderStyle;
private final HorizontalAlign headerAlign;
private final HorizontalAlign dataAlign;
private final HorizontalAlign footerAlign;
private boolean rowNumbersEnabled;
private final boolean rowNumbersEnabled;
private String ignoreEnv;
private boolean ignoreSet = false;
private String urlEnv;
private boolean urlSet = false;
GravitinoConfig config = new GravitinoConfig(null);
private final GravitinoConfig config = new GravitinoConfig(null);

/**
* Creates a {@code GenericConfig} instance from command line arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author pancx
*/
public class RemoveConfig extends GenericConfig {
private String property;
private final String property;

/**
* Creates a {@code RemoveConfig} instance from command line arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* @author pancx
*/
public class SetConfig extends GenericConfig {
private String property;
private String value;
private final String property;
private final String value;

/**
* Constructs a {@code SetConfig} instance from an existing GenericConfig, extracting the property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.cli.config;

import static org.apache.gravitino.cli.config.GenericConfig.DEFAULT_URL;
import static org.apache.gravitino.cli.config.GenericConfig.WITHOUT_LIMIT;
import static org.apache.gravitino.cli.outputs.OutputProperty.OUTPUT_FORMAT_PLAIN;
import static org.apache.gravitino.cli.outputs.OutputProperty.OUTPUT_FORMAT_TABLE;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.gravitino.cli.GravitinoOptions;
import org.apache.gravitino.cli.Main;
import org.apache.gravitino.cli.outputs.BorderStyle;
import org.apache.gravitino.cli.outputs.HorizontalAlign;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestGenericConfig {
private Options options;

@BeforeEach
public void setUp() {
Main.useExit = false;
options = new GravitinoOptions().options();
}

@Test
void testDefaultConfig() throws ParseException {
String[] args = {"metalake", "list"};
CommandLine commandLine = new DefaultParser().parse(options, args);
GenericConfig genericConfig = GenericConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getLimit(), WITHOUT_LIMIT);
Assertions.assertEquals(genericConfig.getBorderStyle(), BorderStyle.BASIC2);
Assertions.assertEquals(genericConfig.getUrl(), DEFAULT_URL);
Assertions.assertEquals(genericConfig.getDataAlign(), HorizontalAlign.LEFT);
Assertions.assertEquals(genericConfig.getHeaderAlign(), HorizontalAlign.CENTER);
Assertions.assertEquals(genericConfig.getFooterAlign(), HorizontalAlign.CENTER);
Assertions.assertEquals(genericConfig.getOutputFormat(), OUTPUT_FORMAT_PLAIN);
Assertions.assertFalse(genericConfig.isQuiet());
Assertions.assertFalse(genericConfig.isIgnoreVersion());
Assertions.assertTrue(genericConfig.isRowNumbersEnabled());
}

@Test
void testConfigWithOutput() throws ParseException {
String[] args = {"metalake", "list", "--output", "table"};
CommandLine commandLine = new DefaultParser().parse(options, args);
GenericConfig genericConfig = GenericConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getOutputFormat(), OUTPUT_FORMAT_TABLE);
}

@Test
void testConfigWithUrl() throws ParseException {
String[] args = {"metalake", "list", "--url", "http://localhost:8088"};
CommandLine commandLine = new DefaultParser().parse(options, args);
GenericConfig genericConfig = GenericConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getUrl(), "http://localhost:8088");
}

@Test
void testCreateConfig() throws ParseException {
String[] args = {"metalake", "create", "-m", "new_metalake", "--comment", "new_comment"};
CommandLine commandLine = new DefaultParser().parse(options, args);
CreateConfig genericConfig = CreateConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getComment(), "new_comment");
}

@Test
void testDeleteConfig() throws ParseException {
String[] args = {"metalake", "delete", "-m", "metalake_to_delete", "--force"};
CommandLine commandLine = new DefaultParser().parse(options, args);
DeleteConfig genericConfig = DeleteConfig.fromCommandLine(commandLine);

Assertions.assertTrue(genericConfig.isForce());
}

@Test
void testRemoveConfig() throws ParseException {
String[] args = {
"metalake", "remove", "-m", "metalake_to_remove", "--property", "property_to_remove"
};
CommandLine commandLine = new DefaultParser().parse(options, args);
RemoveConfig genericConfig = RemoveConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getProperty(), "property_to_remove");
}

@Test
void testSetConfig() throws ParseException {
String[] args = {
"metalake",
"set",
"-m",
"metalake_to_set",
"--property",
"property_to_set",
"--value",
"value_to_set"
};
CommandLine commandLine = new DefaultParser().parse(options, args);
SetConfig genericConfig = SetConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getProperty(), "property_to_set");
Assertions.assertEquals(genericConfig.getValue(), "value_to_set");
}

@Test
void testUpdateConfig() throws ParseException {
String[] args = {
"metalake",
"update",
"-m",
"metalake_to_update",
"--comment",
"new_comment",
"--rename",
"new_name",
"--force",
"--all",
"--enable"
};
CommandLine commandLine = new DefaultParser().parse(options, args);
UpdateConfig genericConfig = UpdateConfig.fromCommandLine(commandLine);

Assertions.assertEquals(genericConfig.getComment(), "new_comment");
Assertions.assertEquals(genericConfig.getNewName(), "new_name");
Assertions.assertTrue(genericConfig.isForce());
Assertions.assertTrue(genericConfig.isAll());
Assertions.assertTrue(genericConfig.isEnable());
}
}

0 comments on commit 13ea1f1

Please sign in to comment.