public RestResponse<ServerInfo> makeRootRequest() { return getServerInfo(); }
Cli( final Long streamedQueryRowLimit, final Long streamedQueryTimeoutMs, final KsqlRestClient restClient, final Console terminal ) { Objects.requireNonNull(restClient, "Must provide the CLI with a REST client"); Objects.requireNonNull(terminal, "Must provide the CLI with a terminal"); this.streamedQueryRowLimit = streamedQueryRowLimit; this.streamedQueryTimeoutMs = streamedQueryTimeoutMs; this.restClient = restClient; this.terminal = terminal; this.queryStreamExecutorService = Executors.newSingleThreadExecutor(); final Supplier<String> versionSuppler = () -> restClient.getServerInfo().getResponse().getVersion(); CliCommandRegisterUtil.registerDefaultCommands(this, terminal, versionSuppler, restClient); }
private void displayWelcomeMessage() { String serverVersion; try { serverVersion = restClient.getServerInfo().getResponse().getVersion(); } catch (final Exception exception) { serverVersion = "<unknown>"; } final String cliVersion = Version.getVersion(); final String helpReminderMessage = "Having trouble? " + "Type 'help' (case-insensitive) for a rundown of how things work!"; final PrintWriter writer = terminal.writer(); // Want to center the logo, but in the case of something like a fullscreen terminal, just // centering around the help message (longest single line of text in the welcome message) // should be enough; looks a little weird if you try to center the logo on a wide enough // screen and it just kind of ends up out in the middle of nowhere; hence, the call to // Math.min(terminal.getWidth(), helpReminderMessage.length()) final int consoleWidth = Math.min(terminal.getWidth(), helpReminderMessage.length()); WelcomeMsgUtils.displayWelcomeMessage(consoleWidth, writer); writer.printf( "CLI v%s, Server v%s located at %s%n", cliVersion, serverVersion, restClient.getServerAddress() ); writer.println(); writer.println(helpReminderMessage); writer.println(); terminal.flush(); }
@Test public void shouldPrintErrorIfCantConnectToRestServer() throws Exception { givenRunInteractivelyWillExit(); final KsqlRestClient mockRestClient = EasyMock.mock(KsqlRestClient.class); EasyMock.expect(mockRestClient.makeRootRequest()).andThrow(new KsqlRestClientException("Boom", new ProcessingException(""))); EasyMock.expect(mockRestClient.getServerInfo()).andReturn( RestResponse.of(new ServerInfo("1.x", "testClusterId", "testServiceId"))); EasyMock.expect(mockRestClient.getServerAddress()).andReturn(new URI("http://someserver:8008")).anyTimes(); EasyMock.replay(mockRestClient); new Cli(1L, 1L, mockRestClient, console) .runInteractively(); assertThat(terminal.getOutputString(), containsString("Remote server address may not be valid")); }
@Test public void shouldHandleSuccessOnGetRequests() { // Given: final ServerInfo expectedEntity = new ServerInfo("1", "cid", "sid"); givenServerWillReturn(expectedEntity); // When: final RestResponse<ServerInfo> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.get(), is(expectedEntity)); }
private int canMakeCliRequest(final String username, final String password) { try (KsqlRestClient restClient = new KsqlRestClient(REST_APP.getHttpListener().toString())) { if (!username.isEmpty()) { restClient.setupAuthenticationCredentials(username, password); } final RestResponse<ServerInfo> response = restClient.getServerInfo(); if (response.isSuccessful()) { return Code.OK.getCode(); } return response.getErrorMessage().getErrorCode(); } }
@Test public void shouldHandleNotFoundOnGetRequests() { // Given: givenServerWillReturn(Status.NOT_FOUND); // When: final RestResponse<?> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(404)); assertThat(response.getErrorMessage().getMessage(), containsString("Path not found. Path='/info'. " + "Check your ksql http url to make sure you are connecting to a ksql server.")); }
@Test public void shouldHandleErrorMessageOnGetRequests() { // Given: givenServerWillReturn(new KsqlErrorMessage(12300, "ouch", ImmutableList.of("s1", "s2"))); // When: final RestResponse<?> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(12300)); assertThat(response.getErrorMessage().getMessage(), is("ouch")); assertThat(response.getErrorMessage().getStackTrace(), is(ImmutableList.of("s1", "s2"))); }
@Test public void shouldHandleArbitraryErrorsOnGetRequests() { // Given: givenServerWillReturn(Status.EXPECTATION_FAILED); // When: final RestResponse<?> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(Errors.toErrorCode(Status.EXPECTATION_FAILED.getStatusCode()))); assertThat(response.getErrorMessage().getMessage(), is("The server returned an unexpected error.")); }
@Test public void shouldPrintErrorOnUnsupportedAPI() throws Exception { givenRunInteractivelyWillExit(); final KsqlRestClient mockRestClient = EasyMock.mock(KsqlRestClient.class); EasyMock.expect(mockRestClient.makeRootRequest()).andReturn( RestResponse.erroneous( new KsqlErrorMessage( Errors.toErrorCode(NOT_ACCEPTABLE.getStatusCode()), "Minimum supported client version: 1.0"))); EasyMock.expect(mockRestClient.getServerInfo()).andReturn( RestResponse.of(new ServerInfo("1.x", "testClusterId", "testServiceId"))); EasyMock.expect(mockRestClient.getServerAddress()).andReturn(new URI("http://someserver:8008")); EasyMock.replay(mockRestClient); new Cli(1L, 1L, mockRestClient, console) .runInteractively(); Assert.assertThat( terminal.getOutputString(), containsString("This CLI version no longer supported")); Assert.assertThat( terminal.getOutputString(), containsString("Minimum supported client version: 1.0")); }
@Test public void shouldHandleForbiddendOnGetRequests() { // Given: givenServerWillReturn(Status.FORBIDDEN); // When: final RestResponse<?> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(Errors.ERROR_CODE_FORBIDDEN)); assertThat(response.getErrorMessage().getMessage(), is("You are forbidden from using this cluster.")); }
@Test public void shouldHandleUnauthorizedOnGetRequests() { // Given: givenServerWillReturn(Status.UNAUTHORIZED); // When: final RestResponse<?> response = ksqlRestClient.getServerInfo(); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(Errors.ERROR_CODE_UNAUTHORIZED)); assertThat(response.getErrorMessage().getMessage(), is("Could not authenticate successfully with the supplied credentials.")); }