Search in sources :

Example 1 with InsertsResponse

use of io.confluent.ksql.api.utils.InsertsResponse in project ksql by confluentinc.

the class ApiTest method shouldHandleMalformedJsonInInsertsStream.

@Test
public void shouldHandleMalformedJsonInInsertsStream() throws Exception {
    // Given
    JsonObject params = new JsonObject().put("target", "test-stream");
    List<JsonObject> rows = DEFAULT_INSERT_ROWS;
    Buffer requestBody = Buffer.buffer();
    requestBody.appendBuffer(params.toBuffer()).appendString("\n");
    for (int i = 0; i < rows.size() - 1; i++) {
        JsonObject row = rows.get(i);
        requestBody.appendBuffer(row.toBuffer()).appendString("\n");
    }
    // Malformed row for the last one
    requestBody.appendString("{ijqwdijqw");
    // When
    HttpResponse<Buffer> response = sendPostRequest("/inserts-stream", requestBody);
    // Then
    // The HTTP response will be OK as the error is later in the stream after response
    // headers have been written
    assertThat(response.statusCode(), is(200));
    assertThat(response.statusMessage(), is("OK"));
    String responseBody = response.bodyAsString();
    InsertsResponse insertsResponse = new InsertsResponse(responseBody);
    validateInsertStreamError(ERROR_CODE_BAD_REQUEST, "Invalid JSON in inserts stream", insertsResponse.error, (long) rows.size() - 1);
    assertThatEventually(() -> testEndpoints.getInsertsSubscriber().isCompleted(), is(true));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 2 with InsertsResponse

use of io.confluent.ksql.api.utils.InsertsResponse 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 3 with InsertsResponse

use of io.confluent.ksql.api.utils.InsertsResponse in project ksql by confluentinc.

the class ApiTest method shouldInsertWithAcksStream.

@Test
@CoreApiTest
public void shouldInsertWithAcksStream() throws Exception {
    // Given
    JsonObject params = new JsonObject().put("target", "test-stream");
    Buffer requestBody = Buffer.buffer();
    final List<JsonObject> rows = DEFAULT_INSERT_ROWS;
    requestBody.appendBuffer(params.toBuffer()).appendString("\n");
    for (JsonObject row : rows) {
        requestBody.appendBuffer(row.toBuffer()).appendString("\n");
    }
    // When
    HttpResponse<Buffer> response = sendPostRequest("/inserts-stream", requestBody);
    // Then
    assertThat(response.statusCode(), is(200));
    assertThat(response.statusMessage(), is("OK"));
    String responseBody = response.bodyAsString();
    InsertsResponse insertsResponse = new InsertsResponse(responseBody);
    assertThat(insertsResponse.acks, hasSize(rows.size()));
    assertThatEventually(() -> testEndpoints.getInsertsSubscriber().getRowsInserted(), is(rows));
    assertThatEventually(() -> testEndpoints.getInsertsSubscriber().isCompleted(), is(true));
    assertThat(testEndpoints.getLastTarget(), is("test-stream"));
    assertThat(testEndpoints.getInsertsSubscriber().isClosed(), is(true));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 4 with InsertsResponse

use of io.confluent.ksql.api.utils.InsertsResponse in project ksql by confluentinc.

the class ApiTest method shouldUseDelimitedFormatWhenDelimitedHeaderInserts.

@Test
public void shouldUseDelimitedFormatWhenDelimitedHeaderInserts() throws Exception {
    // When
    JsonObject params = new JsonObject().put("target", "test-stream");
    List<JsonObject> rows = DEFAULT_INSERT_ROWS;
    Buffer requestBody = Buffer.buffer();
    requestBody.appendBuffer(params.toBuffer()).appendString("\n");
    for (JsonObject row : rows) {
        requestBody.appendBuffer(row.toBuffer()).appendString("\n");
    }
    VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
    client.post("/inserts-stream").putHeader("accept", "application/vnd.ksqlapi.delimited.v1").sendBuffer(requestBody, requestFuture);
    // Then
    HttpResponse<Buffer> response = requestFuture.get();
    String responseBody = response.bodyAsString();
    InsertsResponse insertsResponse = new InsertsResponse(responseBody);
    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));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Example 5 with InsertsResponse

use of io.confluent.ksql.api.utils.InsertsResponse in project ksql by confluentinc.

the class ApiIntegrationTest method shouldFailToInsert.

private void shouldFailToInsert(final JsonObject row, final int errorCode, final String message) {
    final HttpResponse<Buffer> response = makeInsertsRequest(TEST_STREAM, row);
    assertThat(response.statusCode(), is(200));
    InsertsResponse insertsResponse = new InsertsResponse(response.bodyAsString());
    assertThat(insertsResponse.acks, hasSize(0));
    assertThat(insertsResponse.error, is(notNullValue()));
    assertThat(insertsResponse.error.getInteger("error_code"), is(errorCode));
    assertThat(insertsResponse.error.getString("message"), startsWith(message));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse)

Aggregations

InsertsResponse (io.confluent.ksql.api.utils.InsertsResponse)10 Buffer (io.vertx.core.buffer.Buffer)9 JsonObject (io.vertx.core.json.JsonObject)8 Test (org.junit.Test)7 VertxCompletableFuture (io.confluent.ksql.util.VertxCompletableFuture)3 HttpResponse (io.vertx.ext.web.client.HttpResponse)3 IntegrationTest (io.confluent.common.utils.IntegrationTest)1 ReceiveStream (io.confluent.ksql.api.utils.ReceiveStream)1 SendStream (io.confluent.ksql.api.utils.SendStream)1 JsonArray (io.vertx.core.json.JsonArray)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1