public Object get() { if (isSuccessful()) { return getResponse(); } else { return getErrorMessage(); } }
public static <R> RestResponse<R> of(final KsqlErrorMessage errorMessage) { return erroneous(errorMessage); }
public static <R> RestResponse<R> of(final R response) { return successful(response); }
try { final RestResponse restResponse = restClient.makeRootRequest(); if (restResponse.isErroneous()) { final KsqlErrorMessage ksqlError = restResponse.getErrorMessage(); if (Errors.toStatusCode(ksqlError.getErrorCode()) == NOT_ACCEPTABLE.getStatusCode()) { writer.format("This CLI version no longer supported: %s%n%n", ksqlError);
private ReceiverThread(final RestResponse<KsqlRestClient.QueryStream> queryResponse) { assertThat("not successful", queryResponse.isSuccessful(), is(true)); this.queryStream = queryResponse.getResponse(); this.thread = new Thread(() -> { try { while (queryStream.hasNext()) { final StreamedRow row = queryStream.next(); rows.add(row); } } catch (final Exception e) { exception.set(e); } }, "receiver-thread"); thread.setDaemon(true); thread.start(); }
private static void dropStream(final String name) { final String dropStatement = String.format("drop stream %s;", name); final RestResponse response = restClient.makeKsqlRequest(dropStatement); if (response.isSuccessful()) { return; } final java.util.regex.Matcher matcher = WRITE_QUERIES .matcher(response.getErrorMessage().getMessage()); if (!matcher.matches()) { throw new RuntimeException("Failed to drop stream: " + response.getErrorMessage()); } Arrays.stream(matcher.group(1).split("/w*,/w*")) .forEach(CliTest::terminateQuery); runStatement(dropStatement, restClient); }
@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"))); }
private static void runStatement(final String statement, final KsqlRestClient restClient) { final RestResponse response = restClient.makeKsqlRequest(statement); Assert.assertThat(response.isSuccessful(), is(true)); final KsqlEntityList entityList = ((KsqlEntityList) response.get()); Assert.assertThat(entityList.size(), equalTo(1)); Assert.assertThat(entityList.get(0), instanceOf(CommandStatusEntity.class)); final CommandStatusEntity entity = (CommandStatusEntity) entityList.get(0); final CommandStatus status = entity.getCommandStatus(); Assert.assertThat(status, not(CommandStatus.Status.ERROR)); if (status.getStatus() != Status.SUCCESS) { assertThatEventually( "", () -> { final RestResponse statusResponse = restClient .makeStatusRequest(entity.getCommandId().toString()); Assert.assertThat(statusResponse.isSuccessful(), is(true)); Assert.assertThat(statusResponse.get(), instanceOf(CommandStatus.class)); return ((CommandStatus) statusResponse.get()).getStatus(); }, anyOf( is(CommandStatus.Status.SUCCESS), is(CommandStatus.Status.TERMINATED), is(CommandStatus.Status.ERROR)), 120, TimeUnit.SECONDS ); } }
private static void createStreams() { try (final KsqlRestClient ksqlRestClient = new KsqlRestClient(REST_APP.getHttpListener().toString()) ) { final RestResponse createStreamResponse = ksqlRestClient .makeKsqlRequest(String.format( "CREATE STREAM %s (viewtime bigint, pageid varchar, userid varchar)" + " WITH (kafka_topic='pageviews', value_format='json');", PAGE_VIEW_STREAM)); assertTrue(createStreamResponse.isSuccessful()); } } }
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); }
@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 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)); }
@Test public void testKsqlResource() { final RestResponse<KsqlEntityList> results = ksqlRestClient.makeKsqlRequest("Test request"); assertThat(results, is(notNullValue())); assertThat(results.isSuccessful(), is(true)); final KsqlEntityList ksqlEntityList = results.getResponse(); assertThat(ksqlEntityList, hasSize(1)); assertThat(ksqlEntityList.get(0), is(instanceOf(ExecutionPlan.class))); }
private static void maybeDropStream(final String name) { final String dropStatement = String.format("drop stream %s;", name); final RestResponse response = restClient.makeKsqlRequest(dropStatement); if (response.isSuccessful() || response.getErrorMessage().toString().contains("does not exist")) { return; } dropStream(name); }
@Test public void shouldHandleErrorMessageOnPostRequests() { // Given: givenServerWillReturn(new KsqlErrorMessage(12300, "ouch", ImmutableList.of("s1", "s2"))); // When: final RestResponse<?> response = ksqlRestClient.makeKsqlRequest("whateva"); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(12300)); assertThat(response.getErrorMessage().getMessage(), is("ouch")); assertThat(response.getErrorMessage().getStackTrace(), is(ImmutableList.of("s1", "s2"))); }
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 shouldHandleSuccessOnPostRequests() { // Given: final KsqlEntityList expectedEntity = new KsqlEntityList(); givenServerWillReturn(expectedEntity); // When: final RestResponse<KsqlEntityList> response = ksqlRestClient.makeKsqlRequest("foo"); // Then: assertThat(response.get(), is(expectedEntity)); }
private void handleStreamedQuery(final String query) throws IOException { final RestResponse<KsqlRestClient.QueryStream> queryResponse = restClient.makeQueryRequest(query); LOGGER.debug("Handling streamed query"); if (!queryResponse.isSuccessful()) { terminal.printErrorMessage(queryResponse.getErrorMessage()); } else { try (KsqlRestClient.QueryStream queryStream = queryResponse.getResponse(); StatusClosable ignored = terminal.setStatusMessage("Press CTRL-C to interrupt")) { streamResults(queryStream); } } }
@Test public void shouldReturnStatusForSpecificCommand() { // When: final RestResponse<CommandStatus> response = ksqlRestClient.makeStatusRequest("TOPIC/c1/CREATE"); // Then: assertThat(response, is(notNullValue())); assertThat(response.isSuccessful(), is(true)); assertThat(response.getResponse().getStatus(), is(CommandStatus.Status.SUCCESS)); }
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(); } }