Search in sources :

Example 1 with SubscriptionCursor

use of org.zalando.nakadi.view.SubscriptionCursor in project nakadi by zalando.

the class EventStreamTest method testWriteStreamInfoWhenPresent.

@Test
public void testWriteStreamInfoWhenPresent() {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final SubscriptionCursor cursor = new SubscriptionCursor("11", "000000000000000012", "event-type", "token-id");
    final ArrayList<ConsumedEvent> events = Lists.newArrayList(new ConsumedEvent("{\"a\":\"b\"}".getBytes(), mock(NakadiCursor.class)));
    try {
        writerProvider.getWriter().writeSubscriptionBatch(baos, cursor, events, Optional.of("something"));
        final JSONObject batch = new JSONObject(baos.toString());
        final JSONObject cursorM = batch.getJSONObject("cursor");
        assertEquals("11", cursorM.getString("partition"));
        assertEquals("000000000000000012", cursorM.getString("offset"));
        assertEquals("event-type", cursorM.getString("event_type"));
        assertEquals("token-id", cursorM.getString("cursor_token"));
        final JSONArray eventsM = batch.getJSONArray("events");
        assertSame(eventsM.length(), 1);
        assertEquals("something", batch.getJSONObject("info").getString("debug"));
    } catch (final IOException e) {
        fail(e.getMessage());
    }
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ConsumedEvent(org.zalando.nakadi.domain.ConsumedEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with SubscriptionCursor

use of org.zalando.nakadi.view.SubscriptionCursor in project nakadi by zalando.

the class CursorsControllerTest method whenInvalidCursorExceptionThenUnprocessableEntity.

@Test
public void whenInvalidCursorExceptionThenUnprocessableEntity() throws Exception {
    when(cursorsService.commitCursors(any(), any(), any())).thenThrow((new InvalidCursorException(CursorError.NULL_PARTITION, new SubscriptionCursor(null, null, null, null))));
    final Problem expectedProblem = Problem.valueOf(UNPROCESSABLE_ENTITY, "partition must not be null");
    checkForProblem(postCursors(DUMMY_CURSORS), expectedProblem);
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Problem(org.zalando.problem.Problem) TestUtils.invalidProblem(org.zalando.nakadi.utils.TestUtils.invalidProblem) Test(org.junit.Test)

Example 3 with SubscriptionCursor

use of org.zalando.nakadi.view.SubscriptionCursor in project nakadi by zalando.

the class UserJourneyAT method userJourneyHila.

@Test(timeout = 15000)
public void userJourneyHila() throws InterruptedException, IOException {
    postEvents(rangeClosed(0, 3).mapToObj(x -> "{\"foo\":\"bar" + x + "\"}").collect(Collectors.toList()).toArray(new String[4]));
    // create subscription
    final SubscriptionBase subscriptionToCreate = RandomSubscriptionBuilder.builder().withOwningApplication("stups_aruha-test-end2end-nakadi").withEventType(eventTypeName).withStartFrom(BEGIN).buildSubscriptionBase();
    final Subscription subscription = createSubscription(jsonRequestSpec(), subscriptionToCreate);
    // list subscriptions
    jsonRequestSpec().param("event_type", eventTypeName).get("/subscriptions").then().statusCode(OK.value()).body("items.size()", equalTo(1)).body("items[0].id", equalTo(subscription.getId()));
    // create client and wait till we receive all events
    final TestStreamingClient client = new TestStreamingClient(RestAssured.baseURI + ":" + RestAssured.port, subscription.getId(), "", oauthToken).start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(4)));
    final List<StreamBatch> batches = client.getBatches();
    // validate the content of events
    for (int i = 0; i < batches.size(); i++) {
        final SubscriptionCursor cursor = new SubscriptionCursor("0", TestUtils.toTimelineOffset(i), eventTypeName, "");
        final StreamBatch expectedBatch = new StreamBatch(cursor, ImmutableList.of(ImmutableMap.of("foo", "bar" + i)), i == 0 ? new StreamMetadata("Stream started") : null);
        final StreamBatch batch = batches.get(i);
        assertThat(batch, equalToBatchIgnoringToken(expectedBatch));
    }
    // as we didn't commit, there should be still 4 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(4));
    // commit cursor of latest event
    final StreamBatch lastBatch = batches.get(batches.size() - 1);
    final int commitCode = commitCursors(jsonRequestSpec(), subscription.getId(), ImmutableList.of(lastBatch.getCursor()), client.getSessionId());
    assertThat(commitCode, equalTo(NO_CONTENT.value()));
    // now there should be 0 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(0));
    // get cursors
    jsonRequestSpec().get("/subscriptions/{sid}/cursors", subscription.getId()).then().statusCode(OK.value()).body("items[0].partition", equalTo("0")).body("items[0].offset", equalTo("001-0001-000000000000000003"));
    // delete subscription
    jsonRequestSpec().delete("/subscriptions/{sid}", subscription.getId()).then().statusCode(NO_CONTENT.value());
}
Also used : TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) CREATED(org.springframework.http.HttpStatus.CREATED) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) IntStream.rangeClosed(java.util.stream.IntStream.rangeClosed) NOT_FOUND(org.springframework.http.HttpStatus.NOT_FOUND) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) NO_CONTENT(org.springframework.http.HttpStatus.NO_CONTENT) BEGIN(org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Subscription(org.zalando.nakadi.domain.Subscription) Retryer.executeWithRetry(org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) RetryForSpecifiedTimeStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) EventType(org.zalando.nakadi.domain.EventType) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Matchers(org.hamcrest.Matchers) TestUtils.waitFor(org.zalando.nakadi.utils.TestUtils.waitFor) Test(org.junit.Test) IOException(java.io.IOException) JSON(com.jayway.restassured.http.ContentType.JSON) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils(org.zalando.nakadi.utils.TestUtils) Collectors(java.util.stream.Collectors) NakadiTestUtils.commitCursors(org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors) MatcherIgnoringToken.equalToBatchIgnoringToken(org.zalando.nakadi.webservice.hila.StreamBatch.MatcherIgnoringToken.equalToBatchIgnoringToken) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Header(com.jayway.restassured.response.Header) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) RestAssured(com.jayway.restassured.RestAssured) OK(org.springframework.http.HttpStatus.OK) TestUtils.randomValidEventTypeName(org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 4 with SubscriptionCursor

use of org.zalando.nakadi.view.SubscriptionCursor in project nakadi by zalando.

the class SubscriptionAT method testGetSubscriptionCursors.

@Test
public void testGetSubscriptionCursors() throws IOException, InterruptedException {
    final String etName = createEventType().getName();
    final Subscription subscription = createSubscriptionForEventType(etName);
    final String cursor = "{\"items\":[{\"partition\":\"0\",\"offset\":\"25\",\"event_type\":\"" + etName + "\",\"cursor_token\":\"abc\"}]}";
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "").start();
    waitFor(() -> assertThat(client.getSessionId(), not(equalTo(SESSION_ID_UNKNOWN))));
    commitCursors(subscription, cursor, client.getSessionId()).then().statusCode(HttpStatus.SC_NO_CONTENT);
    final List<SubscriptionCursor> actualCursors = NakadiTestUtils.getSubscriptionCursors(subscription).getItems();
    assertThat(actualCursors, hasSize(1));
    final SubscriptionCursor actualCursor = actualCursors.get(0);
    assertThat(actualCursor.getPartition(), equalTo("0"));
    assertThat(actualCursor.getOffset(), equalTo(TestUtils.toTimelineOffset(25)));
    assertThat(actualCursor.getEventType(), equalTo(etName));
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 5 with SubscriptionCursor

use of org.zalando.nakadi.view.SubscriptionCursor in project nakadi by zalando.

the class HilaAT method testGetSubscriptionStat.

@Test(timeout = 10000)
public void testGetSubscriptionStat() throws Exception {
    publishEvents(eventType.getName(), 15, i -> "{\"foo\":\"bar\"}");
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=20").start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(15)));
    List<SubscriptionEventTypeStats> subscriptionStats = Collections.singletonList(new SubscriptionEventTypeStats(eventType.getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 15L, client.getSessionId(), AUTO))));
    NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new ItemsWrapper<>(subscriptionStats))));
    final String partition = client.getBatches().get(0).getCursor().getPartition();
    final SubscriptionCursor cursor = new SubscriptionCursor(partition, "9", eventType.getName(), "token");
    commitCursors(subscription.getId(), ImmutableList.of(cursor), client.getSessionId());
    subscriptionStats = Collections.singletonList(new SubscriptionEventTypeStats(eventType.getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 5L, client.getSessionId(), AUTO))));
    NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new ItemsWrapper<>(subscriptionStats))));
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Aggregations

SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)16 Test (org.junit.Test)14 TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)10 Subscription (org.zalando.nakadi.domain.Subscription)6 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)6 IOException (java.io.IOException)5 List (java.util.List)5 EventType (org.zalando.nakadi.domain.EventType)5 ImmutableList (com.google.common.collect.ImmutableList)4 Collectors (java.util.stream.Collectors)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Matchers.hasSize (org.hamcrest.Matchers.hasSize)3 Before (org.junit.Before)3 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 JSON (com.jayway.restassured.http.ContentType.JSON)2 Optional (java.util.Optional)2 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)2 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)2