Search in sources :

Example 36 with QueryResponse

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

Example 37 with QueryResponse

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

Example 38 with QueryResponse

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++;
    }
}
Also used : QueryResponse(io.confluent.ksql.api.utils.QueryResponse) ArrayList(java.util.ArrayList) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) WebClient(io.vertx.ext.web.client.WebClient) Test(org.junit.Test)

Example 39 with QueryResponse

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());
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.ext.web.client.HttpResponse) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) ColumnName(io.confluent.ksql.name.ColumnName) WebClient(io.vertx.ext.web.client.WebClient) Server(io.confluent.ksql.api.server.Server) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) AssertEventually.assertThatEventually(io.confluent.ksql.test.util.AssertEventually.assertThatEventually) KsqlDefaultSecurityExtension(io.confluent.ksql.security.KsqlDefaultSecurityExtension) BodyCodec(io.vertx.ext.web.codec.BodyCodec) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Schema(org.apache.kafka.connect.data.Schema) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) HttpVersion(io.vertx.core.http.HttpVersion) Map(java.util.Map) After(org.junit.After) ListRowGenerator(io.confluent.ksql.api.utils.ListRowGenerator) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) ServerUtils(io.confluent.ksql.api.server.ServerUtils) Vertx(io.vertx.core.Vertx) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) Matchers.startsWith(org.hamcrest.Matchers.startsWith) HttpRequest(io.vertx.ext.web.client.HttpRequest) KsqlRestConfig(io.confluent.ksql.rest.server.KsqlRestConfig) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) GenericRow(io.confluent.ksql.GenericRow) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) Struct(org.apache.kafka.connect.data.Struct) ServerState(io.confluent.ksql.rest.server.state.ServerState) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) Buffer(io.vertx.core.buffer.Buffer) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) QueryResponse(io.confluent.ksql.api.utils.QueryResponse)

Example 40 with QueryResponse

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

Aggregations

QueryResponse (io.confluent.ksql.api.utils.QueryResponse)43 Test (org.junit.Test)30 Buffer (io.vertx.core.buffer.Buffer)29 JsonObject (io.vertx.core.json.JsonObject)21 IntegrationTest (io.confluent.common.utils.IntegrationTest)10 PushQueryId (io.confluent.ksql.rest.entity.PushQueryId)8 VertxCompletableFuture (io.confluent.ksql.util.VertxCompletableFuture)7 HttpResponse (io.vertx.ext.web.client.HttpResponse)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 JsonArray (io.vertx.core.json.JsonArray)6 ReceiveStream (io.confluent.ksql.api.utils.ReceiveStream)4 ArrayList (java.util.ArrayList)4 WebClient (io.vertx.ext.web.client.WebClient)3 ImmutableList (com.google.common.collect.ImmutableList)2 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)2 QueryStreamArgs (io.confluent.ksql.rest.entity.QueryStreamArgs)2 KsqlException (io.confluent.ksql.util.KsqlException)2 HttpVersion (io.vertx.core.http.HttpVersion)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2