@Override public void makeKsqlRequest(final String statements) { try { printKsqlResponse(restClient.makeKsqlRequest(statements)); } catch (IOException e) { throw new KsqlException(e); } }
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 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); }
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()); } } }
@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)); }
@Test public void shouldHandleForbiddenOnPostRequests() { // Given: givenServerWillReturn(Status.FORBIDDEN); // When: final RestResponse<?> response = ksqlRestClient.makeKsqlRequest("whateva"); // 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 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"))); }
@Test public void shouldHandleArbitraryErrorsOnPostRequests() { // Given: givenServerWillReturn(Status.EXPECTATION_FAILED); // When: final RestResponse<?> response = ksqlRestClient.makeKsqlRequest("whateva"); // 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 shouldHandleNotFoundOnPostRequests() { // Given: givenServerWillReturn(Status.NOT_FOUND); // When: final RestResponse<?> response = ksqlRestClient.makeKsqlRequest("whateva"); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(404)); assertThat(response.getErrorMessage().getMessage(), containsString("Path not found. Path='ksql'. " + "Check your ksql http url to make sure you are connecting to a ksql server.")); }
@Test public void shouldHandleUnauthorizedOnPostRequests() { // Given: givenServerWillReturn(Status.UNAUTHORIZED); // When: final RestResponse<?> response = ksqlRestClient.makeKsqlRequest("whateva"); // Then: assertThat(response.getErrorMessage().getErrorCode(), is(Errors.ERROR_CODE_UNAUTHORIZED)); assertThat(response.getErrorMessage().getMessage(), is("Could not authenticate successfully with the supplied credentials.")); }
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 ); } }