-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#6220] improve(CLI): Clean up GravitinoCommandLine class now it been…
… refactored (#6227) ### What changes were proposed in this pull request? Clean up GravitinoCommandLine class now it been refactored ### Why are the changes needed? Fix: #6220 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? local ut test.
- Loading branch information
1 parent
5cffeb4
commit 3a48aba
Showing
5 changed files
with
134 additions
and
100 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
53 changes: 53 additions & 0 deletions
53
clients/cli/src/main/java/org/apache/gravitino/cli/SimpleCommandHandler.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,53 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
|
||
/** Handles the command execution for simple command based on the command line options. */ | ||
public class SimpleCommandHandler extends CommandHandler { | ||
private final GravitinoCommandLine gravitinoCommandLine; | ||
private final CommandLine line; | ||
private final boolean ignore; | ||
|
||
/** | ||
* Constructs a {@link SimpleCommandHandler} instance. | ||
* | ||
* @param gravitinoCommandLine The Gravitino command line instance. | ||
* @param line The command line arguments. | ||
* @param ignore Ignore server version mismatch. | ||
*/ | ||
public SimpleCommandHandler( | ||
GravitinoCommandLine gravitinoCommandLine, CommandLine line, boolean ignore) { | ||
this.gravitinoCommandLine = gravitinoCommandLine; | ||
this.line = line; | ||
this.ignore = ignore; | ||
} | ||
|
||
/** Handles the command execution logic based on the provided command. */ | ||
@Override | ||
protected void handle() { | ||
if (line.hasOption(GravitinoOptions.VERSION)) { | ||
gravitinoCommandLine.newClientVersion(getUrl(line), ignore).validate().handle(); | ||
} else if (line.hasOption(GravitinoOptions.SERVER)) { | ||
gravitinoCommandLine.newServerVersion(getUrl(line), ignore).validate().handle(); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
clients/cli/src/test/java/org/apache/gravitino/cli/TestSimpleCommands.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,75 @@ | ||
/* | ||
* 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; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Options; | ||
import org.apache.gravitino.cli.commands.ClientVersion; | ||
import org.apache.gravitino.cli.commands.ServerVersion; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestSimpleCommands { | ||
|
||
private CommandLine mockCommandLine; | ||
private Options mockOptions; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mockCommandLine = mock(CommandLine.class); | ||
mockOptions = mock(Options.class); | ||
} | ||
|
||
@Test | ||
void testServerVersion() { | ||
ServerVersion mockServerVersion = mock(ServerVersion.class); | ||
when(mockCommandLine.hasOption(GravitinoOptions.SERVER)).thenReturn(true); | ||
GravitinoCommandLine commandLine = | ||
spy(new GravitinoCommandLine(mockCommandLine, mockOptions, null, null)); | ||
|
||
doReturn(mockServerVersion) | ||
.when(commandLine) | ||
.newServerVersion(GravitinoCommandLine.DEFAULT_URL, false); | ||
doReturn(mockServerVersion).when(mockServerVersion).validate(); | ||
commandLine.handleSimpleLine(); | ||
verify(mockServerVersion).handle(); | ||
} | ||
|
||
@Test | ||
void testClientVersion() { | ||
ClientVersion mockClientVersion = mock(ClientVersion.class); | ||
when(mockCommandLine.hasOption(GravitinoOptions.VERSION)).thenReturn(true); | ||
GravitinoCommandLine commandLine = | ||
spy(new GravitinoCommandLine(mockCommandLine, mockOptions, null, null)); | ||
|
||
doReturn(mockClientVersion) | ||
.when(commandLine) | ||
.newClientVersion(GravitinoCommandLine.DEFAULT_URL, false); | ||
doReturn(mockClientVersion).when(mockClientVersion).validate(); | ||
commandLine.handleSimpleLine(); | ||
verify(mockClientVersion).handle(); | ||
} | ||
} |