@Override protected boolean matchesSafely( final KsqlErrorMessage actual, final Description mismatchDescription ) { if (!expected.matches(actual.getErrorCode())) { mismatchDescription.appendText("but error code "); expected.describeMismatch(actual.getErrorCode(), mismatchDescription); return false; } return true; }
@Test public void shouldFailPrintTopic() { // When: final KsqlErrorMessage result = makeFailingRequest("PRINT 'orders-topic';", Code.BAD_REQUEST); // Then: assertThat(result, is(instanceOf(KsqlStatementErrorMessage.class))); assertThat(result.getErrorCode(), is(Errors.ERROR_CODE_QUERY_ENDPOINT)); }
@Test public void shouldFailCreateTableWithInferenceWithUnknownKey() { // When: final KsqlErrorMessage response = makeFailingRequest( "CREATE TABLE orders WITH (KAFKA_TOPIC='orders-topic', " + "VALUE_FORMAT = 'avro', KEY = 'unknownField');", Code.BAD_REQUEST); // Then: assertThat(response, instanceOf(KsqlStatementErrorMessage.class)); assertThat(response.getErrorCode(), is(Errors.ERROR_CODE_BAD_STATEMENT)); }
@Test public void shouldReturnBadStatementIfStatementFailsValidation() { // When: final KsqlErrorMessage result = makeFailingRequest( "DESCRIBE i_do_not_exist;", Code.BAD_REQUEST); // Then: assertThat(result, is(instanceOf(KsqlStatementErrorMessage.class))); assertThat(result.getErrorCode(), is(Errors.ERROR_CODE_BAD_STATEMENT)); assertThat(((KsqlStatementErrorMessage) result).getStatementText(), is("DESCRIBE i_do_not_exist;")); }
@Test public void shouldFailIfCreateStatementMissingKafkaTopicName() { // When: final KsqlErrorMessage result = makeFailingRequest( "CREATE STREAM S (foo INT) WITH(VALUE_FORMAT='JSON');", Code.BAD_REQUEST); // Then: assertThat(result, is(instanceOf(KsqlStatementErrorMessage.class))); assertThat(result.getErrorCode(), is(Errors.ERROR_CODE_BAD_STATEMENT)); assertThat(result.getMessage(), is("Corresponding Kafka topic (KAFKA_TOPIC) should be set in WITH clause.")); assertThat(((KsqlStatementErrorMessage) result).getStatementText(), is("CREATE STREAM S (foo INT) WITH(VALUE_FORMAT='JSON');")); }
@Test public void shouldFailSetPropertyOnInvalidPropertyName() { // When: final KsqlErrorMessage response = makeFailingRequest( "SET 'ksql.unknown.property' = '1';", Code.BAD_REQUEST); // Then: assertThat(response, instanceOf(KsqlStatementErrorMessage.class)); assertThat(response.getErrorCode(), is(Errors.ERROR_CODE_BAD_STATEMENT)); assertThat(response.getMessage(), containsString("Unknown property")); }
@Test public void shouldFailUnsetPropertyOnInvalidPropertyName() { // When: final KsqlErrorMessage response = makeFailingRequest( "UNSET 'ksql.unknown.property';", Code.BAD_REQUEST); // Then: assertThat(response, instanceOf(KsqlStatementErrorMessage.class)); assertThat(response.getErrorCode(), is(Errors.ERROR_CODE_BAD_STATEMENT)); assertThat(response.getMessage(), containsString("Unknown property")); }
@Test public void shouldReturnCorrectResponseForUnspecificException() { final Response response = exceptionMapper.toResponse(new Exception("error msg")); assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class)); final KsqlErrorMessage errorMessage = (KsqlErrorMessage)response.getEntity(); assertThat(errorMessage.getMessage(), equalTo("error msg")); assertThat(errorMessage.getErrorCode(), equalTo(Errors.ERROR_CODE_SERVER_ERROR)); assertThat(response.getStatus(), equalTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); } }
@Test public void shouldReturnCorrectResponseForWebAppException() { final WebApplicationException webApplicationException = new WebApplicationException("error msg", 403); final Response response = exceptionMapper.toResponse(webApplicationException); assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class)); final KsqlErrorMessage errorMessage = (KsqlErrorMessage)response.getEntity(); assertThat(errorMessage.getMessage(), equalTo("error msg")); assertThat(errorMessage.getErrorCode(), equalTo(40300)); assertThat(response.getStatus(), equalTo(403)); }
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 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 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 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 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 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 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.")); }
@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 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.")); }
@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.")); }