Search in sources :

Example 46 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method testSubscriptionBaseOperations.

@Test
public void testSubscriptionBaseOperations() throws IOException {
    // create event type in Nakadi
    final EventType eventType = createEventType();
    // create subscription
    final String subscription = "{\"owning_application\":\"app\",\"event_types\":[\"" + eventType.getName() + "\"]}";
    Response response = given().body(subscription).contentType(JSON).post(SUBSCRIPTIONS_URL);
    // assert response
    response.then().statusCode(HttpStatus.SC_CREATED).contentType(JSON).body("owning_application", equalTo("app")).body("event_types", containsInAnyOrder(ImmutableSet.of(eventType.getName()).toArray())).body("consumer_group", not(isEmptyString())).body("id", not(isEmptyString())).body("created_at", not(isEmptyString())).body("start_from", not(isEmptyString()));
    // retrieve subscription object from response
    final Subscription subFirst = MAPPER.readValue(response.print(), Subscription.class);
    // when we try to create that subscription again - we should get status 200
    // and the subscription that already exists should be returned
    response = given().body(subscription).contentType(JSON).post(SUBSCRIPTIONS_URL);
    // assert status code
    response.then().statusCode(HttpStatus.SC_OK).contentType(JSON);
    // check that second time already existing subscription was returned
    final Subscription subSecond = MAPPER.readValue(response.print(), Subscription.class);
    assertThat(subSecond, equalTo(subFirst));
    // check get subscription endpoint
    response = get(format(SUBSCRIPTION_URL, subFirst.getId()));
    response.then().statusCode(HttpStatus.SC_OK).contentType(JSON);
    final Subscription gotSubscription = MAPPER.readValue(response.print(), Subscription.class);
    assertThat(gotSubscription, equalTo(subFirst));
}
Also used : Response(com.jayway.restassured.response.Response) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) NakadiTestUtils.createSubscriptionForEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscriptionForEventType) EventType(org.zalando.nakadi.domain.EventType) 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 47 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method testSubscriptionWithManyEventTypesIsCreated.

@Test
public void testSubscriptionWithManyEventTypesIsCreated() throws IOException {
    final List<String> eventTypes = IntStream.range(0, 10).mapToObj(i -> createEventType()).map(EventTypeBase::getName).collect(Collectors.toList());
    final String subscription = "{\"owning_application\":\"app\",\"event_types\":" + "[" + eventTypes.stream().map(et -> "\"" + et + "\"").collect(Collectors.joining(",")) + "]}";
    final Response response = given().body(subscription).contentType(JSON).post(SUBSCRIPTIONS_URL);
    // assert response
    response.then().statusCode(HttpStatus.SC_CREATED).contentType(JSON);
    final Subscription gotSubscription = MAPPER.readValue(response.print(), Subscription.class);
    Assert.assertNotNull(gotSubscription.getId());
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) MoreStatus(org.zalando.problem.MoreStatus) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) Matchers.not(org.hamcrest.Matchers.not) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) HttpStatus(org.apache.http.HttpStatus) RestAssured.given(com.jayway.restassured.RestAssured.given) Stat(org.apache.zookeeper.data.Stat) Subscription(org.zalando.nakadi.domain.Subscription) BaseAT(org.zalando.nakadi.webservice.BaseAT) NakadiTestUtils.publishBusinessEventWithUserDefinedPartition(org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishBusinessEventWithUserDefinedPartition) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) Problem(org.zalando.problem.Problem) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ImmutableSet(com.google.common.collect.ImmutableSet) JSON(com.jayway.restassured.http.ContentType.JSON) TestUtils(org.zalando.nakadi.utils.TestUtils) ZookeeperTestUtils(org.zalando.nakadi.webservice.utils.ZookeeperTestUtils) Collectors(java.util.stream.Collectors) TestUtils.randomUUID(org.zalando.nakadi.utils.TestUtils.randomUUID) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) EventTypeBase(org.zalando.nakadi.domain.EventTypeBase) Optional(java.util.Optional) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) Matchers.is(org.hamcrest.Matchers.is) NakadiTestUtils.createBusinessEventTypeWithPartitions(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createBusinessEventTypeWithPartitions) IntStream(java.util.stream.IntStream) PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) JsonTestHelper(org.zalando.nakadi.utils.JsonTestHelper) Cursor(org.zalando.nakadi.view.Cursor) Response(com.jayway.restassured.response.Response) MessageFormat.format(java.text.MessageFormat.format) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) NakadiTestUtils(org.zalando.nakadi.webservice.utils.NakadiTestUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) NakadiTestUtils.createSubscriptionForEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscriptionForEventType) Charsets(com.google.common.base.Charsets) SESSION_ID_UNKNOWN(org.zalando.nakadi.webservice.utils.TestStreamingClient.SESSION_ID_UNKNOWN) EventType(org.zalando.nakadi.domain.EventType) RestAssured.when(com.jayway.restassured.RestAssured.when) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestUtils.waitFor(org.zalando.nakadi.utils.TestUtils.waitFor) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) IOException(java.io.IOException) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) RestAssured.get(com.jayway.restassured.RestAssured.get) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Assert(org.junit.Assert) Response(com.jayway.restassured.response.Response) 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 48 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionConsumptionTest method test2TimelinesInaRowNoBegin.

@Test
public void test2TimelinesInaRowNoBegin() throws IOException, InterruptedException {
    final EventType eventType = createEventType();
    final Subscription subscription = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).build());
    final CountDownLatch finished = new CountDownLatch(1);
    final AtomicReference<String[]> inTimelineCursors = new AtomicReference<>();
    createParallelConsumer(subscription, 2, finished, inTimelineCursors::set);
    // Still old topic
    createTimeline(eventType.getName());
    // New topic
    createTimeline(eventType.getName());
    // Another new topic
    createTimeline(eventType.getName());
    publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
    finished.await();
    Assert.assertArrayEquals(new String[] { "001-0004-000000000000000000", "001-0004-000000000000000001" }, inTimelineCursors.get());
    final Subscription subscription2 = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).withStartFrom(SubscriptionBase.InitialPosition.BEGIN).build());
    final CountDownLatch finished2 = new CountDownLatch(1);
    final AtomicReference<String[]> inTimelineCursors2 = new AtomicReference<>();
    createParallelConsumer(subscription2, 2, finished2, inTimelineCursors2::set);
    finished2.await();
    Assert.assertArrayEquals(new String[] { "001-0004-000000000000000000", "001-0004-000000000000000001" }, inTimelineCursors2.get());
}
Also used : NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 49 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionConsumptionTest method test2TimelinesInaRow.

@Test(timeout = 60000)
public void test2TimelinesInaRow() throws IOException, InterruptedException {
    final EventType eventType = createEventType();
    final Subscription subscription = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).build());
    final CountDownLatch finished = new CountDownLatch(1);
    final AtomicReference<String[]> inTimelineCursors = new AtomicReference<>();
    createParallelConsumer(subscription, 5, finished, inTimelineCursors::set);
    publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
    // Still old topic
    createTimeline(eventType.getName());
    // New topic
    createTimeline(eventType.getName());
    // Another new topic
    createTimeline(eventType.getName());
    publishEvents(eventType.getName(), 1, i -> "{\"foo\":\"bar\"}");
    createTimeline(eventType.getName());
    createTimeline(eventType.getName());
    publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
    finished.await();
    Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, inTimelineCursors.get());
    // Will create subscription clone
    final Subscription clone = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).withStartFrom(SubscriptionBase.InitialPosition.BEGIN).build());
    final CountDownLatch finished2 = new CountDownLatch(1);
    createParallelConsumer(clone, 5, finished2, inTimelineCursors::set);
    finished2.await();
    Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, inTimelineCursors.get());
}
Also used : NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 50 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class CursorsService method commitCursors.

/**
 * It is guaranteed, that len(cursors) == len(result)
 */
public List<Boolean> commitCursors(final String streamId, final String subscriptionId, final List<NakadiCursor> cursors) throws ServiceUnavailableException, InvalidCursorException, InvalidStreamIdException, NoSuchEventTypeException, InternalNakadiException, NoSuchSubscriptionException, UnableProcessException {
    TimeLogger.addMeasure("getSubscription");
    final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
    TimeLogger.addMeasure("validateSubscriptionCursors");
    validateSubscriptionCommitCursors(subscription, cursors);
    TimeLogger.addMeasure("createSubscriptionClient");
    final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + "." + streamId + ".offsets");
    TimeLogger.addMeasure("validateStreamId");
    validateStreamId(cursors, streamId, zkClient);
    TimeLogger.addMeasure("writeToZK");
    return zkClient.commitOffsets(cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()), new SubscriptionCursorComparator(new NakadiCursorComparator(eventTypeCache)));
}
Also used : ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) Subscription(org.zalando.nakadi.domain.Subscription)

Aggregations

Subscription (org.zalando.nakadi.domain.Subscription)55 Test (org.junit.Test)40 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)21 EventType (org.zalando.nakadi.domain.EventType)15 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)12 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)11 List (java.util.List)10 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)9 TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)9 ImmutableList (com.google.common.collect.ImmutableList)8 Collectors (java.util.stream.Collectors)8 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)8 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)7 PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)6 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)6 NakadiTestUtils.createEventType (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType)5