-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#6326] improve(CLI): Make CLI more extendable and maintainable.
add test..
- Loading branch information
1 parent
ced3ab8
commit 13ea1f1
Showing
4 changed files
with
171 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
clients/cli/src/test/java/org/apache/gravitino/cli/config/TestGenericConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |