Search in sources :

Example 6 with InsertsResponse

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

the class ApiIntegrationTest method shouldExecuteInserts.

@Test
public void shouldExecuteInserts() {
    // Given:
    JsonObject properties = new JsonObject();
    JsonObject requestBody = new JsonObject().put("target", TEST_STREAM).put("properties", properties);
    Buffer bodyBuffer = requestBody.toBuffer();
    bodyBuffer.appendString("\n");
    int numRows = 10;
    for (int i = 0; i < numRows; i++) {
        JsonObject row = new JsonObject().put("K", new JsonObject().put("F1", new JsonArray().add("my_key_" + i))).put("STR", "Value_" + i).put("LONG", 1000 + i).put("DEC", // JsonObject does not accept BigDecimal
        i + 0.11).put("ARRAY", new JsonArray().add("a_" + i).add("b_" + i)).put("MAP", new JsonObject().put("k1", "v1_" + i).put("k2", "v2_" + i)).put("STRUCT", new JsonObject().put("F1", i)).put("COMPLEX", COMPLEX_FIELD_VALUE);
        bodyBuffer.appendBuffer(row.toBuffer()).appendString("\n");
    }
    // When:
    HttpResponse<Buffer> response = sendRequest("/inserts-stream", bodyBuffer);
    // Then:
    assertThat(response.statusCode(), is(200));
    InsertsResponse insertsResponse = new InsertsResponse(response.bodyAsString());
    assertThat(insertsResponse.acks, hasSize(numRows));
    assertThat(insertsResponse.error, is(nullValue()));
    Set<Long> sequences = new HashSet<>();
    for (JsonObject ack : insertsResponse.acks) {
        sequences.add(ack.getLong("seq"));
    }
    assertThat(sequences, hasSize(numRows));
    for (long l = 0; l < numRows; l++) {
        assertThat(sequences.contains(l), is(true));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonArray(io.vertx.core.json.JsonArray) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) JsonObject(io.vertx.core.json.JsonObject) HashSet(java.util.HashSet) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 7 with InsertsResponse

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

the class ApiIntegrationTest method shouldInsert.

private void shouldInsert(final String target, final JsonObject row) {
    HttpResponse<Buffer> response = makeInsertsRequest(target, row);
    assertThat(response.statusCode(), is(200));
    InsertsResponse insertsResponse = new InsertsResponse(response.bodyAsString());
    assertThat(insertsResponse.acks, hasSize(1));
    assertThat(insertsResponse.error, is(nullValue()));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse)

Example 8 with InsertsResponse

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

the class AuthTest method shouldFailInsertRequest.

private void shouldFailInsertRequest(final String username, final String password, int expectedStatus, final String expectedMessage, final int expectedErrorCode) throws Exception {
    // Given
    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");
    }
    // When
    HttpResponse<Buffer> response = sendPostRequestWithCreds("/inserts-stream", requestBody, username, password);
    // Then
    assertThat(response.statusCode(), is(expectedStatus));
    InsertsResponse insertsResponse = new InsertsResponse(response.bodyAsString());
    validateError(expectedErrorCode, expectedMessage, insertsResponse.error);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) JsonObject(io.vertx.core.json.JsonObject)

Example 9 with InsertsResponse

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

the class ApiTest method shouldHandleErrorInProcessingInserts.

@Test
public void shouldHandleErrorInProcessingInserts() 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 (JsonObject row : rows) {
        requestBody.appendBuffer(row.toBuffer()).appendString("\n");
    }
    // Inject an error on last row inserted
    testEndpoints.setAcksBeforePublisherError(rows.size() - 1);
    // 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);
    assertThat(insertsResponse.acks, hasSize(rows.size() - 1));
    validateInsertStreamError(ERROR_CODE_SERVER_ERROR, "Error in processing inserts. Check server logs for details.", insertsResponse.error, (long) rows.size() - 1);
    assertThat(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 10 with InsertsResponse

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

the class ApiTest method shouldUseDelimitedFormatWhenNoAcceptHeaderInserts.

@Test
public void shouldUseDelimitedFormatWhenNoAcceptHeaderInserts() 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").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)

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