use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldUseDelimitedFormatWhenNoAcceptHeaderQuery.
@Test
public void shouldUseDelimitedFormatWhenNoAcceptHeaderQuery() throws Exception {
// When
JsonObject requestBody = new JsonObject().put("sql", DEFAULT_PULL_QUERY);
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post("/query-stream").sendBuffer(requestBody.toBuffer(), requestFuture);
// Then
HttpResponse<Buffer> response = requestFuture.get();
QueryResponse queryResponse = new QueryResponse(response.bodyAsString());
assertThat(queryResponse.rows, hasSize(DEFAULT_JSON_ROWS.size()));
assertThat(response.bodyAsString().contains("\n"), is(true));
assertThat(response.statusCode(), is(200));
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldRejectMalformedJsonInArgs.
private void shouldRejectMalformedJsonInArgs(String uri) throws Exception {
// Given
Buffer requestBody = Buffer.buffer().appendString("{\"foo\":1");
// When
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post(uri).sendBuffer(requestBody, requestFuture);
HttpResponse<Buffer> response = requestFuture.get();
// 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: Unexpected end-of-input: expected close marker for Object", queryResponse.responseObject);
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldUseJsonFormatWhenJsonAcceptHeaderQuery.
@Test
public void shouldUseJsonFormatWhenJsonAcceptHeaderQuery() throws Exception {
// When
JsonObject requestBody = new JsonObject().put("sql", DEFAULT_PULL_QUERY);
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post("/query-stream").putHeader("accept", "application/json").sendBuffer(requestBody.toBuffer(), requestFuture);
// Then
HttpResponse<Buffer> response = requestFuture.get();
JsonArray jsonArray = new JsonArray(response.body());
assertThat(jsonArray.size(), is(DEFAULT_JSON_ROWS.size() + 1));
JsonObject metaData = jsonArray.getJsonObject(0);
assertThat(metaData.getJsonArray("columnNames"), is(DEFAULT_COLUMN_NAMES));
assertThat(metaData.getJsonArray("columnTypes"), is(DEFAULT_COLUMN_TYPES));
for (int i = 0; i < DEFAULT_JSON_ROWS.size(); i++) {
assertThat(jsonArray.getJsonArray(i + 1), is(DEFAULT_JSON_ROWS.get(i)));
}
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldStreamInserts.
@Test
@CoreApiTest
public void shouldStreamInserts() throws Exception {
LOG.info("Starting shouldStreamInserts");
// Given
JsonObject params = new JsonObject().put("target", "test-stream");
// Stream for piping the HTTP request body
SendStream readStream = new SendStream(vertx);
// Stream for receiving the HTTP response body
ReceiveStream writeStream = new ReceiveStream(vertx);
VertxCompletableFuture<HttpResponse<Void>> fut = new VertxCompletableFuture<>();
List<JsonObject> rows = DEFAULT_INSERT_ROWS;
// When
// Make an HTTP request but keep the request body and response streams open
sendPostRequest("/inserts-stream", (request) -> request.as(BodyCodec.pipe(writeStream)).sendStream(readStream, fut));
// Write the initial params Json object to the request body
readStream.acceptBuffer(params.toBuffer().appendString("\n"));
// Asynchronously on a timer write inserts to the request body
AtomicInteger rowIndex = new AtomicInteger();
vertx.setPeriodic(100, tid -> {
readStream.acceptBuffer(rows.get(rowIndex.getAndIncrement()).toBuffer().appendString("\n"));
if (rowIndex.get() == rows.size()) {
vertx.cancelTimer(tid);
// End the inserts stream and request when we've written all the rows to the stream
readStream.end();
}
});
// Wait for the response to complete
LOG.info("Awaiting response from inserts");
HttpResponse<Void> response = fut.get();
LOG.info("Got response from inserts");
// Then
assertThat(response.statusCode(), is(200));
assertThat(response.statusMessage(), is("OK"));
// Verify we got acks for all our inserts
InsertsResponse insertsResponse = new InsertsResponse(writeStream.getBody().toString());
assertThat(insertsResponse.acks, hasSize(rows.size()));
for (int i = 0; i < insertsResponse.acks.size(); i++) {
final JsonObject ackLine = new JsonObject().put("status", "ok").put("seq", i);
assertThat(insertsResponse.acks.get(i), is(ackLine));
}
// Make sure all inserts made it to the server
TestInsertsSubscriber insertsSubscriber = testEndpoints.getInsertsSubscriber();
LOG.info("Checking all rows inserted");
assertThatEventually(insertsSubscriber::getRowsInserted, is(rows));
LOG.info("Checking got completion marker");
assertThatEventually(insertsSubscriber::isCompleted, is(true));
// Ensure we received at least some of the response before all the request body was written
// Yay HTTP2!
assertThat(readStream.getLastSentTime() > writeStream.getFirstReceivedTime(), is(true));
LOG.info("ShouldStreamInserts complete");
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldUseJsonFormatWhenJsonHeaderInserts.
@Test
public void shouldUseJsonFormatWhenJsonHeaderInserts() throws Exception {
// When
JsonObject params = new JsonObject().put("target", "test-stream");
Buffer requestBody = Buffer.buffer();
requestBody.appendBuffer(params.toBuffer()).appendString("\n");
for (JsonObject row : DEFAULT_INSERT_ROWS) {
requestBody.appendBuffer(row.toBuffer()).appendString("\n");
}
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post("/inserts-stream").putHeader("accept", "application/json").sendBuffer(requestBody, requestFuture);
// Then
HttpResponse<Buffer> response = requestFuture.get();
JsonArray jsonArray = new JsonArray(response.body());
assertThat(jsonArray.size(), is(DEFAULT_INSERT_ROWS.size()));
for (int i = 0; i < jsonArray.size(); i++) {
final JsonObject ackLine = new JsonObject().put("status", "ok").put("seq", i);
assertThat(jsonArray.getJsonObject(i), is(ackLine));
}
}
Aggregations