use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.
the class ApiTest method shouldHandleMissingQueryIDInCloseQuery.
@Test
public void shouldHandleMissingQueryIDInCloseQuery() throws Exception {
// Given
JsonObject closeQueryRequestBody = new JsonObject().put("foo", "bar");
// When
HttpResponse<Buffer> response = sendPostRequest("/close-query", closeQueryRequestBody.toBuffer());
// Then
assertThat(response.statusCode(), is(400));
assertThat(response.statusMessage(), is("Bad Request"));
QueryResponse queryResponse = new QueryResponse(response.bodyAsString());
validateError(ERROR_CODE_BAD_REQUEST, "Invalid JSON in request: Missing required creator property 'queryId'", queryResponse.responseObject);
}
use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.
the class ApiTest method shouldCloseQuery.
@Test
@CoreApiTest
public void shouldCloseQuery() throws Exception {
// Create a write stream to capture the incomplete response
ReceiveStream writeStream = new ReceiveStream(vertx);
VertxCompletableFuture<HttpResponse<Void>> responseFuture = new VertxCompletableFuture<>();
// Make the request to stream a query
sendPostRequest("/query-stream", (request) -> request.as(BodyCodec.pipe(writeStream)).sendJsonObject(DEFAULT_PUSH_QUERY_REQUEST_BODY, responseFuture));
// Wait for all rows in the response to arrive
assertThatEventually(() -> {
try {
Buffer buff = writeStream.getBody();
QueryResponse queryResponse = new QueryResponse(buff.toString());
return queryResponse.rows.size();
} catch (Throwable t) {
return Integer.MAX_VALUE;
}
}, is(DEFAULT_JSON_ROWS.size()));
// The response shouldn't have ended yet
assertThat(writeStream.isEnded(), is(false));
// Assert the query is still live on the server
QueryResponse queryResponse = new QueryResponse(writeStream.getBody().toString());
String queryId = queryResponse.responseObject.getString("queryId");
assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
assertThat(server.getQueryIDs(), hasSize(1));
assertThat(testEndpoints.getQueryPublishers(), hasSize(1));
// Now send another request to close the query
JsonObject closeQueryRequestBody = new JsonObject().put("queryId", queryId);
HttpResponse<Buffer> closeQueryResponse = sendPostRequest("/close-query", closeQueryRequestBody.toBuffer());
assertThat(closeQueryResponse.statusCode(), is(200));
// Assert the query no longer exists on the server
assertThat(server.getQueryIDs(), not(hasItem(new PushQueryId(queryId))));
assertThat(server.getQueryIDs(), hasSize(0));
// The response should now be ended
assertThatEventually(writeStream::isEnded, is(true));
HttpResponse<Void> response = responseFuture.get();
assertThat(response.statusCode(), is(200));
}
use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.
the class ApiTest method shouldCloseMultipleQueriesOnDifferentConnectionsWhenConnectionsAreClosed.
@Test
public void shouldCloseMultipleQueriesOnDifferentConnectionsWhenConnectionsAreClosed() throws Exception {
int numConnections = 5;
int numQueries = 5;
List<WebClient> clients = new ArrayList<>();
for (int i = 0; i < numConnections; i++) {
WebClient client = createClient();
clients.add(client);
for (int j = 0; j < numQueries; j++) {
// When
QueryResponse queryResponse = executePushQueryAndWaitForRows(client, DEFAULT_PUSH_QUERY_REQUEST_BODY);
// Then
String queryId = queryResponse.responseObject.getString("queryId");
assertThat(queryId, is(notNullValue()));
assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
int queries = i * numQueries + j + 1;
assertThat(server.getQueryIDs(), hasSize(queries));
assertThat(server.queryConnectionCount(), is(i + 1));
}
}
int count = 0;
for (WebClient client : clients) {
// When
client.close();
// Then
int connections = numConnections - count - 1;
assertThatEventually(server::queryConnectionCount, is(connections));
assertThat(server.getQueryIDs(), hasSize(numQueries * connections));
count++;
}
}
use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.
the class BaseApiTest method executePushQueryAndWaitForRows.
protected QueryResponse executePushQueryAndWaitForRows(final WebClient client, final JsonObject requestBody) throws Exception {
ReceiveStream writeStream = new ReceiveStream(vertx);
sendPostRequest(client, "/query-stream", (request) -> request.as(BodyCodec.pipe(writeStream)).sendJsonObject(requestBody, ar -> {
}));
// Wait for all rows to arrive
assertThatEventually(() -> {
try {
Buffer buff = writeStream.getBody();
QueryResponse queryResponse = new QueryResponse(buff.toString());
return queryResponse.rows.size();
} catch (Throwable t) {
return Integer.MAX_VALUE;
}
}, is(DEFAULT_JSON_ROWS.size()));
// Note, the response hasn't ended at this point
assertThat(writeStream.isEnded(), is(false));
return new QueryResponse(writeStream.getBody().toString());
}
use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.
the class ApiTest method shouldHandleMissingTargetInInserts.
@Test
public void shouldHandleMissingTargetInInserts() throws Exception {
// Given
JsonObject requestBody = new JsonObject();
// When
HttpResponse<Buffer> response = sendPostRequest("/inserts-stream", requestBody.toBuffer().appendString("\n"));
// Then
assertThat(response.statusCode(), is(400));
assertThat(response.statusMessage(), is("Bad Request"));
QueryResponse queryResponse = new QueryResponse(response.bodyAsString());
validateError(ERROR_CODE_BAD_REQUEST, "Invalid JSON in request: Missing required creator property 'target'", queryResponse.responseObject);
}
Aggregations