Search in sources :

Example 1 with VertxCompletableFuture

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));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Example 2 with VertxCompletableFuture

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);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture)

Example 3 with VertxCompletableFuture

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)));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Example 4 with VertxCompletableFuture

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");
}
Also used : InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) SendStream(io.confluent.ksql.api.utils.SendStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with VertxCompletableFuture

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));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Aggregations

VertxCompletableFuture (io.confluent.ksql.util.VertxCompletableFuture)20 HttpResponse (io.vertx.ext.web.client.HttpResponse)16 Buffer (io.vertx.core.buffer.Buffer)14 JsonObject (io.vertx.core.json.JsonObject)12 Test (org.junit.Test)12 QueryResponse (io.confluent.ksql.api.utils.QueryResponse)6 ReceiveStream (io.confluent.ksql.api.utils.ReceiveStream)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 InsertsResponse (io.confluent.ksql.api.utils.InsertsResponse)3 ArrayList (java.util.ArrayList)3 IntegrationTest (io.confluent.common.utils.IntegrationTest)2 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)2 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)2 CommandStatuses (io.confluent.ksql.rest.entity.CommandStatuses)2 HealthCheckResponse (io.confluent.ksql.rest.entity.HealthCheckResponse)2 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)2 KsqlRequest (io.confluent.ksql.rest.entity.KsqlRequest)2 ServerClusterId (io.confluent.ksql.rest.entity.ServerClusterId)2 ServerInfo (io.confluent.ksql.rest.entity.ServerInfo)2 ServerMetadata (io.confluent.ksql.rest.entity.ServerMetadata)2