-
Notifications
You must be signed in to change notification settings - Fork 3
Add create-namespace command #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jnmt
wants to merge
1
commit into
master
Choose a base branch
from
add-create-namespace-cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
client/src/main/java/com/scalar/dl/client/tool/NamespaceCreation.java
This file contains hidden or 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,29 @@ | ||
| package com.scalar.dl.client.tool; | ||
|
|
||
| import com.scalar.dl.client.exception.ClientException; | ||
| import com.scalar.dl.client.service.ClientService; | ||
| import picocli.CommandLine; | ||
| import picocli.CommandLine.Command; | ||
|
|
||
| @Command(name = "create-namespace", description = "Create a namespace.") | ||
| public class NamespaceCreation extends AbstractClientCommand { | ||
|
|
||
| @CommandLine.Option( | ||
| names = {"--namespace"}, | ||
| required = true, | ||
| paramLabel = "NAMESPACE", | ||
| description = "A namespace name to create.") | ||
| private String namespace; | ||
|
|
||
| public static void main(String[] args) { | ||
| int exitCode = new CommandLine(new NamespaceCreation()).execute(args); | ||
| System.exit(exitCode); | ||
| } | ||
|
|
||
| @Override | ||
| protected Integer execute(ClientService service) throws ClientException { | ||
| service.createNamespace(namespace); | ||
| Common.printOutput(null); | ||
jnmt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or 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
153 changes: 153 additions & 0 deletions
153
client/src/test/java/com/scalar/dl/client/tool/NamespaceCreationTest.java
This file contains hidden or 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,153 @@ | ||
| package com.scalar.dl.client.tool; | ||
|
|
||
| import static com.scalar.dl.client.tool.CommandLineTestUtils.createDefaultClientPropertiesFile; | ||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doReturn; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.scalar.dl.client.config.ClientConfig; | ||
| import com.scalar.dl.client.config.GatewayClientConfig; | ||
| import com.scalar.dl.client.exception.ClientException; | ||
| import com.scalar.dl.client.service.ClientService; | ||
| import com.scalar.dl.client.service.ClientServiceFactory; | ||
| import com.scalar.dl.ledger.service.StatusCode; | ||
| import java.io.File; | ||
| import java.nio.file.Path; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import picocli.CommandLine; | ||
|
|
||
| public class NamespaceCreationTest { | ||
| private CommandLine commandLine; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| commandLine = new CommandLine(new NamespaceCreation()); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("#call()") | ||
| class call { | ||
| @Test | ||
| @DisplayName("returns 0 as exit code") | ||
| void returns0AsExitCode() throws ClientException { | ||
| // Arrange | ||
| String[] args = | ||
| new String[] { | ||
| // Set the required options. | ||
| "--properties=PROPERTIES_FILE", "--namespace=test_namespace", | ||
| }; | ||
| NamespaceCreation command = parseArgs(args); | ||
| ClientService serviceMock = mock(ClientService.class); | ||
|
|
||
| // Act | ||
| int exitCode = command.execute(serviceMock); | ||
|
|
||
| // Assert | ||
| assertThat(exitCode).isEqualTo(0); | ||
| verify(serviceMock).createNamespace("test_namespace"); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("where useGateway option is true") | ||
| class whereUseGatewayOptionIsTrue { | ||
| @Test | ||
| @DisplayName("create ClientService with GatewayClientConfig") | ||
| public void createClientServiceWithGatewayClientConfig(@TempDir Path tempDir) | ||
| throws Exception { | ||
| // Arrange | ||
| File file = createDefaultClientPropertiesFile(tempDir, "client.props"); | ||
| String propertiesOption = String.format("--properties=%s", file.getAbsolutePath()); | ||
| String[] args = | ||
| new String[] { | ||
| // Set the required options. | ||
| propertiesOption, | ||
| "--namespace=test-namespace", | ||
| // Enable Gateway. | ||
| "--use-gateway" | ||
| }; | ||
| NamespaceCreation command = parseArgs(args); | ||
| ClientServiceFactory factory = mock(ClientServiceFactory.class); | ||
| doReturn(mock(ClientService.class)).when(factory).create(any(GatewayClientConfig.class)); | ||
|
|
||
| // Act | ||
| command.call(factory); | ||
|
|
||
| // Verify | ||
| verify(factory).create(any(GatewayClientConfig.class)); | ||
| verify(factory, never()).create(any(ClientConfig.class)); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("where useGateway option is false") | ||
| class whereUseGatewayOptionIsFalse { | ||
| @Test | ||
| @DisplayName("create ClientService with ClientConfig") | ||
| public void createClientServiceWithClientConfig(@TempDir Path tempDir) throws Exception { | ||
| // Arrange | ||
| File file = createDefaultClientPropertiesFile(tempDir, "client.props"); | ||
| String propertiesOption = String.format("--properties=%s", file.getAbsolutePath()); | ||
| String[] args = | ||
| new String[] { | ||
| // Set the required options. | ||
| propertiesOption, "--namespace=test-namespace", | ||
| // Gateway is disabled by default. | ||
| }; | ||
| NamespaceCreation command = parseArgs(args); | ||
| ClientServiceFactory factory = mock(ClientServiceFactory.class); | ||
| doReturn(mock(ClientService.class)).when(factory).create(any(ClientConfig.class)); | ||
|
|
||
| // Act | ||
| command.call(factory); | ||
|
|
||
| // Verify | ||
| verify(factory).create(any(ClientConfig.class)); | ||
| verify(factory, never()).create(any(GatewayClientConfig.class)); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("where ClientService throws ClientException") | ||
| class whereClientExceptionIsThrownByClientService { | ||
| @Test | ||
| @DisplayName("returns 1 as exit code") | ||
| void returns1AsExitCode(@TempDir Path tempDir) throws Exception { | ||
| // Arrange | ||
| File file = createDefaultClientPropertiesFile(tempDir, "client.props"); | ||
| String[] args = | ||
| new String[] { | ||
| // Set the required options. | ||
| "--properties=" + file.getAbsolutePath(), "--namespace=test-namespace", | ||
| }; | ||
| NamespaceCreation command = parseArgs(args); | ||
| // Mock service that throws an exception. | ||
| ClientServiceFactory factoryMock = mock(ClientServiceFactory.class); | ||
| ClientService serviceMock = mock(ClientService.class); | ||
| when(factoryMock.create(any(ClientConfig.class))).thenReturn(serviceMock); | ||
| doThrow(new ClientException("", StatusCode.RUNTIME_ERROR)) | ||
| .when(serviceMock) | ||
| .createNamespace("test-namespace"); | ||
|
|
||
| // Act | ||
| int exitCode = command.call(factoryMock); | ||
|
|
||
| // Assert | ||
| assertThat(exitCode).isEqualTo(1); | ||
| verify(factoryMock).close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private NamespaceCreation parseArgs(String[] args) { | ||
| return CommandLineTestUtils.parseArgs(commandLine, NamespaceCreation.class, args); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.