Search in sources :

Example 1 with Header

use of com.jayway.restassured.response.Header in project nakadi by zalando.

the class EventStreamReadingAT method whenReadFromTheEndThenLatestOffsetsInStream.

@Test(timeout = 10000)
@SuppressWarnings("unchecked")
public void whenReadFromTheEndThenLatestOffsetsInStream() {
    // ACT //
    // just stream without X-nakadi-cursors; that should make nakadi to read from the very end
    final Response response = given().param("stream_timeout", "2").param("batch_flush_timeout", "2").when().get(streamEndpoint);
    // ASSERT //
    response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked");
    final String body = response.print();
    final List<Map<String, Object>> batches = deserializeBatches(body);
    // validate amount of batches and structure of each batch
    assertThat(batches, hasSize(PARTITIONS_NUM));
    batches.forEach(batch -> validateBatchStructure(batch, DUMMY_EVENT));
    // validate that the latest offsets in batches correspond to the newest offsets
    final Set<Cursor> offsets = batches.stream().map(batch -> {
        final Map<String, String> cursor = (Map<String, String>) batch.get("cursor");
        return new Cursor(cursor.get("partition"), cursor.get("offset"));
    }).collect(Collectors.toSet());
    assertThat(offsets, equalTo(Sets.newHashSet(initialCursors)));
}
Also used : Response(com.jayway.restassured.response.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpURLConnection(java.net.HttpURLConnection) Arrays(java.util.Arrays) IntStream.range(java.util.stream.IntStream.range) URL(java.net.URL) RestAssured.given(com.jayway.restassured.RestAssured.given) Matchers.hasKey(org.hamcrest.Matchers.hasKey) Assert.assertThat(org.junit.Assert.assertThat) EventTypeStatistics(org.zalando.nakadi.domain.EventTypeStatistics) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) TypeReference(com.fasterxml.jackson.core.type.TypeReference) HttpHeaders(org.springframework.http.HttpHeaders) Predicate(java.util.function.Predicate) Set(java.util.Set) BlacklistService(org.zalando.nakadi.service.BlacklistService) TestUtils(org.zalando.nakadi.utils.TestUtils) EventTypeTestBuilder(org.zalando.nakadi.utils.EventTypeTestBuilder) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RestAssured(com.jayway.restassured.RestAssured) IntStream(java.util.stream.IntStream) KafkaTestHelper(org.zalando.nakadi.repository.kafka.KafkaTestHelper) BeforeClass(org.junit.BeforeClass) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Cursor(org.zalando.nakadi.view.Cursor) Response(com.jayway.restassured.response.Response) MessageFormat.format(java.text.MessageFormat.format) NakadiTestUtils(org.zalando.nakadi.webservice.utils.NakadiTestUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Before(org.junit.Before) EventType(org.zalando.nakadi.domain.EventType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) Ignore(org.junit.Ignore) Header(com.jayway.restassured.response.Header) BufferedReader(java.io.BufferedReader) Assert(org.junit.Assert) InputStream(java.io.InputStream) Cursor(org.zalando.nakadi.view.Cursor) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with Header

use of com.jayway.restassured.response.Header in project nakadi by zalando.

the class EventStreamReadingAT method whenAcceptEncodingGzipReceiveCompressedStream.

@Test(timeout = 10000)
public void whenAcceptEncodingGzipReceiveCompressedStream() throws ExecutionException, InterruptedException {
    // ARRANGE //
    // push events to one of the partitions
    final int eventsPushed = 2;
    kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, topicName, DUMMY_EVENT, eventsPushed);
    // ACT //
    final Response response = given().header(new Header("X-nakadi-cursors", xNakadiCursors)).header(new Header("Accept-Encoding", "gzip")).param("batch_limit", "5").param("stream_timeout", "2").param("batch_flush_timeout", "2").when().get(streamEndpoint);
    // ASSERT //
    response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked");
    response.then().header("Content-Encoding", "gzip");
}
Also used : Response(com.jayway.restassured.response.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Header(com.jayway.restassured.response.Header) Test(org.junit.Test)

Example 3 with Header

use of com.jayway.restassured.response.Header in project nakadi by zalando.

the class EventStreamReadingAT method whenPushedAmountOfEventsMoreThanBatchSizeAndReadThenGetEventsInMultipleBatches.

@Test(timeout = 10000)
@SuppressWarnings("unchecked")
public void whenPushedAmountOfEventsMoreThanBatchSizeAndReadThenGetEventsInMultipleBatches() throws ExecutionException, InterruptedException {
    // ARRANGE //
    // push events to one of the partitions so that they don't fit into one branch
    final int batchLimit = 5;
    final int eventsPushed = 8;
    kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, topicName, DUMMY_EVENT, eventsPushed);
    // ACT //
    final Response response = given().header(new Header("X-nakadi-cursors", xNakadiCursors)).param("batch_limit", batchLimit).param("stream_timeout", "2").param("batch_flush_timeout", "2").when().get(streamEndpoint);
    // ASSERT //
    response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked");
    final String body = response.print();
    final List<Map<String, Object>> batches = deserializeBatches(body);
    // validate amount of batches and structure of each batch
    // for partition with events we should get 2 batches
    assertThat(batches, hasSize(PARTITIONS_NUM + 1));
    batches.forEach(batch -> validateBatchStructure(batch, DUMMY_EVENT));
    // find the batches where we expect to see the messages we pushed
    final List<Map<String, Object>> batchesToCheck = batches.stream().filter(isForPartition(TEST_PARTITION)).collect(Collectors.toList());
    assertThat(batchesToCheck, hasSize(2));
    // calculate the offset we expect to see in this batch in a stream
    final Cursor partitionCursor = kafkaInitialNextOffsets.stream().filter(cursor -> TEST_PARTITION.equals(cursor.getPartition())).findFirst().orElseThrow(() -> new AssertionError("Failed to find cursor for needed partition"));
    final String expectedOffset1 = TestUtils.toTimelineOffset(Long.parseLong(partitionCursor.getOffset()) - 1 + batchLimit);
    final String expectedOffset2 = TestUtils.toTimelineOffset(Long.parseLong(partitionCursor.getOffset()) - 1 + eventsPushed);
    // check that batches have offset, partition and events number we expect
    validateBatch(batchesToCheck.get(0), TEST_PARTITION, expectedOffset1, batchLimit);
    validateBatch(batchesToCheck.get(1), TEST_PARTITION, expectedOffset2, eventsPushed - batchLimit);
}
Also used : Response(com.jayway.restassured.response.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Header(com.jayway.restassured.response.Header) Cursor(org.zalando.nakadi.view.Cursor) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Header

use of com.jayway.restassured.response.Header in project nakadi by zalando.

the class UserJourneyAT method userJourneyM1.

@SuppressWarnings("unchecked")
@Test(timeout = 15000)
public void userJourneyM1() throws InterruptedException, IOException {
    // get event type
    jsonRequestSpec().when().get("/event-types/" + eventTypeName).then().statusCode(OK.value()).body("name", equalTo(eventTypeName)).body("owning_application", equalTo(owningApp)).body("category", equalTo("undefined")).body("schema.type", equalTo("json_schema")).body("schema.schema", equalTo("{\"type\": \"object\", \"properties\": " + "{\"foo\": {\"type\": \"string\"}}, \"required\": [\"foo\"]}"));
    // list event types
    jsonRequestSpec().when().get("/event-types").then().statusCode(OK.value()).body("size()", Matchers.greaterThan(0)).body("name[0]", notNullValue()).body("owning_application[0]", notNullValue()).body("category[0]", notNullValue()).body("schema.type[0]", notNullValue()).body("schema.schema[0]", notNullValue());
    final String updateEventTypeBody = getUpdateEventType();
    // update event-type
    jsonRequestSpec().body(updateEventTypeBody).when().put("/event-types/" + eventTypeName).then().body(equalTo("")).statusCode(OK.value());
    // Updates should eventually cause a cache invalidation, so we must retry
    executeWithRetry(() -> {
        // get event type to check that update is done
        jsonRequestSpec().when().get("/event-types/" + eventTypeName).then().statusCode(OK.value()).body("options.retention_time", equalTo(86400000));
    }, new RetryForSpecifiedTimeStrategy<Void>(5000).withExceptionsThatForceRetry(AssertionError.class).withWaitBetweenEachTry(500));
    // push two events to event-type
    postEvents(EVENT1, EVENT2);
    // get offsets for partition
    jsonRequestSpec().when().get("/event-types/" + eventTypeName + "/partitions/0").then().statusCode(OK.value()).body("partition", equalTo("0")).body("oldest_available_offset", equalTo("001-0001-000000000000000000")).body("newest_available_offset", equalTo("001-0001-000000000000000001"));
    // get offsets for all partitions
    jsonRequestSpec().when().get("/event-types/" + eventTypeName + "/partitions").then().statusCode(OK.value()).body("size()", equalTo(1)).body("partition[0]", notNullValue()).body("oldest_available_offset[0]", notNullValue()).body("newest_available_offset[0]", notNullValue());
    // read events
    requestSpec().header(new Header("X-nakadi-cursors", "[{\"partition\": \"0\", \"offset\": \"BEGIN\"}]")).param("batch_limit", "2").param("stream_limit", "2").when().get("/event-types/" + eventTypeName + "/events").then().statusCode(OK.value()).body(equalTo("{\"cursor\":{\"partition\":\"0\",\"offset\":\"001-0001-000000000000000001\"}," + "\"events\":[" + EVENT1 + "," + EVENT2 + "]}\n"));
    // get distance between cursors
    jsonRequestSpec().body("[{\"initial_cursor\": {\"partition\": \"0\", \"offset\":\"001-0001-000000000000000000\"}, " + "\"final_cursor\": {\"partition\": \"0\", \"offset\":\"001-0001-000000000000000001\"}}]").when().post("/event-types/" + eventTypeName + "/cursor-distances").then().statusCode(OK.value()).body("size()", equalTo(1)).body("initial_cursor[0].offset", equalTo("001-0001-000000000000000000")).body("final_cursor[0].offset", equalTo("001-0001-000000000000000001")).body("distance[0]", equalTo(1));
    // navigate between cursors
    jsonRequestSpec().body("[{\"partition\": \"0\", \"offset\":\"001-0001-000000000000000000\", \"shift\": 1}, " + "{\"partition\": \"0\", \"offset\":\"001-0001-000000000000000001\", \"shift\": -1}]").when().post("/event-types/" + eventTypeName + "/shifted-cursors").then().statusCode(OK.value()).body("size()", equalTo(2)).body("offset[0]", equalTo("001-0001-000000000000000001")).body("offset[1]", equalTo("001-0001-000000000000000000"));
    // query for lag
    jsonRequestSpec().body("[{\"partition\": \"0\", \"offset\":\"001-0001-000000000000000000\"}]").when().post("/event-types/" + eventTypeName + "/cursors-lag").then().statusCode(OK.value()).body("size()", equalTo(1)).body("newest_available_offset[0]", equalTo("001-0001-000000000000000001")).body("oldest_available_offset[0]", equalTo("001-0001-000000000000000000")).body("unconsumed_events[0]", equalTo(1));
}
Also used : Header(com.jayway.restassured.response.Header) TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) Test(org.junit.Test)

Example 5 with Header

use of com.jayway.restassured.response.Header in project nakadi by zalando.

the class TimelineConsumptionTest method readCursors.

private static String[] readCursors(final String eventTypeName, final String startOffset, final int streamLimit) throws IOException {
    final Response response = given().header(new Header("X-nakadi-cursors", "[{\"partition\": \"0\", \"offset\": \"" + startOffset + "\"}]")).param("batch_limit", "1").param("batch_flush_timeout", "1").param("stream_limit", streamLimit).param("stream_timeout", 60).when().get("/event-types/" + eventTypeName + "/events");
    response.then().statusCode(HttpStatus.SC_OK);
    final String[] events = response.print().split("\n");
    final List<String> result = new ArrayList<>();
    for (int i = 0; i < events.length; ++i) {
        final ObjectNode batch = (ObjectNode) new ObjectMapper().readTree(events[i]);
        if (batch.get("events") == null) {
            continue;
        }
        final ObjectNode cursor = (ObjectNode) batch.get("cursor");
        result.add(cursor.get("offset").asText());
    }
    return result.toArray(new String[result.size()]);
}
Also used : Response(com.jayway.restassured.response.Response) Header(com.jayway.restassured.response.Header) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Header (com.jayway.restassured.response.Header)5 Response (com.jayway.restassured.response.Response)4 Test (org.junit.Test)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Cursor (org.zalando.nakadi.view.Cursor)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Sets (com.google.common.collect.Sets)1 RestAssured (com.jayway.restassured.RestAssured)1 RestAssured.given (com.jayway.restassured.RestAssured.given)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1