Search in sources :

Example 1 with Timeline

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

the class EventTypeControllerTest method whenCreateSuccessfullyThen201.

@Test
public void whenCreateSuccessfullyThen201() throws Exception {
    final EventType et = buildDefaultEventType();
    final Timeline timeline = buildTimelineWithTopic("topic1");
    when(timelineService.createDefaultTimeline(anyString(), anyInt(), anyLong())).thenReturn(timeline);
    doReturn(et).when(eventTypeRepository).saveEventType(any(EventType.class));
    postEventType(et).andExpect(status().isCreated()).andExpect(content().string(""));
    verify(eventTypeRepository, times(1)).saveEventType(any(EventType.class));
    verify(timelineService, times(1)).createDefaultTimeline(anyString(), anyInt(), anyLong());
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) EventType(org.zalando.nakadi.domain.EventType) Test(org.junit.Test)

Example 2 with Timeline

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

the class StreamingStateTest method ensureOffsetsSubscriptionsAreRefreshedAndClosed.

@Test
public void ensureOffsetsSubscriptionsAreRefreshedAndClosed() throws InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException, InvalidCursorException {
    final ZkSubscription<SubscriptionCursorWithoutToken> offsetSubscription = mock(ZkSubscription.class);
    final EventTypePartition pk = new EventTypePartition("t", "0");
    Mockito.when(zkMock.subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any())).thenReturn(offsetSubscription);
    final EventConsumer.ReassignableEventConsumer consumer = mock(EventConsumer.ReassignableEventConsumer.class);
    when(consumer.getAssignment()).thenReturn(Collections.emptySet());
    when(timelineService.createEventConsumer(any())).thenReturn(consumer);
    when(subscription.getEventTypes()).thenReturn(Collections.singleton("t"));
    final Storage storage = mock(Storage.class);
    when(storage.getType()).thenReturn(Storage.Type.KAFKA);
    final Timeline timeline = new Timeline("t", 0, storage, "t", new Date());
    when(timelineService.getActiveTimelinesOrdered(eq("t"))).thenReturn(Collections.singletonList(timeline));
    final TopicRepository topicRepository = mock(TopicRepository.class);
    when(timelineService.getTopicRepository(eq(timeline))).thenReturn(topicRepository);
    final PartitionStatistics stats = mock(PartitionStatistics.class);
    final NakadiCursor beforeFirstCursor = NakadiCursor.of(timeline, "0", "0");
    when(stats.getBeforeFirst()).thenReturn(beforeFirstCursor);
    when(topicRepository.loadTopicStatistics(any())).thenReturn(Lists.newArrayList(stats));
    state.onEnter();
    final NakadiCursor anyCursor = NakadiCursor.of(timeline, "0", "0");
    when(cursorConverter.convert(any(SubscriptionCursorWithoutToken.class))).thenReturn(anyCursor);
    state.refreshTopologyUnlocked(new Partition[] { new Partition(pk.getEventType(), pk.getPartition(), SESSION_ID, null, Partition.State.ASSIGNED) });
    Mockito.verify(zkMock, Mockito.times(1)).subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any());
    Mockito.verify(offsetSubscription, Mockito.times(0)).close();
    Mockito.verify(offsetSubscription, Mockito.times(0)).getData();
    state.offsetChanged(pk);
    Mockito.verify(zkMock, Mockito.times(1)).subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any());
    Mockito.verify(offsetSubscription, Mockito.times(0)).close();
    Mockito.verify(offsetSubscription, Mockito.times(1)).getData();
    // Verify that offset change listener is removed
    state.refreshTopologyUnlocked(new Partition[0]);
    Mockito.verify(zkMock, Mockito.times(1)).subscribeForOffsetChanges(Mockito.eq(pk), Mockito.any());
    Mockito.verify(offsetSubscription, Mockito.times(1)).close();
    Mockito.verify(offsetSubscription, Mockito.times(1)).getData();
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) EventConsumer(org.zalando.nakadi.repository.EventConsumer) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) Timeline(org.zalando.nakadi.domain.Timeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) TopicRepository(org.zalando.nakadi.repository.TopicRepository) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Date(java.util.Date) Test(org.junit.Test)

Example 3 with Timeline

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

the class SubscriptionValidationServiceTest method setUp.

@Before
public void setUp() throws InternalNakadiException {
    final NakadiSettings nakadiSettings = mock(NakadiSettings.class);
    when(nakadiSettings.getMaxSubscriptionPartitions()).thenReturn(MAX_SUBSCRIPTION_PARTITIONS);
    topicRepository = mock(TopicRepository.class);
    when(topicRepository.listPartitionNames(argThat(isOneOf(topicForET(ET1), topicForET(ET2), topicForET(ET3))))).thenReturn(ImmutableList.of(P0));
    etRepo = mock(EventTypeRepository.class);
    final Map<String, EventType> eventTypes = new HashMap<>();
    for (final String etName : new String[] { ET1, ET2, ET3 }) {
        final EventType eventType = new EventType();
        eventType.setName(etName);
        eventTypes.put(etName, eventType);
    }
    when(etRepo.findByNameO(any())).thenAnswer(invocation -> Optional.ofNullable(eventTypes.get(invocation.getArguments()[0])));
    final TimelineService timelineService = mock(TimelineService.class);
    for (final EventType et : eventTypes.values()) {
        final Timeline timeline = mock(Timeline.class);
        when(timeline.getTopic()).thenReturn(topicForET(et.getName()));
        when(timeline.getEventType()).thenReturn(et.getName());
        when(timelineService.getActiveTimeline(eq(et.getName()))).thenReturn(timeline);
    }
    when(timelineService.getTopicRepository((Timeline) any())).thenReturn(topicRepository);
    when(timelineService.getTopicRepository((EventType) any())).thenReturn(topicRepository);
    cursorConverter = mock(CursorConverter.class);
    subscriptionValidationService = new SubscriptionValidationService(timelineService, etRepo, nakadiSettings, cursorConverter);
    subscriptionBase = new SubscriptionBase();
    subscriptionBase.setEventTypes(ImmutableSet.of(ET1, ET2, ET3));
    subscriptionBase.setReadFrom(SubscriptionBase.InitialPosition.CURSORS);
}
Also used : SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) HashMap(java.util.HashMap) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) SubscriptionValidationService(org.zalando.nakadi.service.subscription.SubscriptionValidationService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) Before(org.junit.Before)

Example 4 with Timeline

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

the class VersionOneConverterTest method testCorrectParse.

@Test
public void testCorrectParse() throws Exception {
    final Cursor cursor = new Cursor("1", "001-0010-012345");
    final String eventTypeName = "my_et";
    final Timeline firstTimeline = mock(Timeline.class);
    when(firstTimeline.getStorage()).thenReturn(new Storage("default", Storage.Type.KAFKA));
    when(firstTimeline.getOrder()).thenReturn(16);
    when(eventTypeCache.getTimelinesOrdered(eq(eventTypeName))).thenReturn(Collections.singletonList(firstTimeline));
    final NakadiCursor nakadiCursor = converter.convert(eventTypeName, cursor);
    Assert.assertEquals(firstTimeline, nakadiCursor.getTimeline());
    Assert.assertEquals("1", nakadiCursor.getPartition());
    Assert.assertEquals("012345", nakadiCursor.getOffset());
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Cursor(org.zalando.nakadi.view.Cursor) Test(org.junit.Test)

Example 5 with Timeline

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

the class VersionOneConverterTest method testInvalidCursorExceptionOnNotExistentTimeline.

@Test
public void testInvalidCursorExceptionOnNotExistentTimeline() throws Exception {
    final Cursor cursor = new Cursor("1", "001-0002-012345");
    final String eventTypeName = "my_et";
    final Timeline firstTimeline = mock(Timeline.class);
    when(firstTimeline.getOrder()).thenReturn(1);
    final EventType eventType = mock(EventType.class);
    when(eventTypeCache.getTimelinesOrdered(eq(eventTypeName))).thenReturn(Collections.singletonList(firstTimeline));
    try {
        converter.convert(eventTypeName, cursor);
        Assert.fail("Convert should throw exception on invalid cursor");
    } catch (final InvalidCursorException ex) {
        Assert.assertEquals(CursorError.UNAVAILABLE, ex.getError());
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Cursor(org.zalando.nakadi.view.Cursor) Test(org.junit.Test)

Aggregations

Timeline (org.zalando.nakadi.domain.Timeline)74 Test (org.junit.Test)39 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)33 ShiftedNakadiCursor (org.zalando.nakadi.domain.ShiftedNakadiCursor)19 TopicRepository (org.zalando.nakadi.repository.TopicRepository)17 Date (java.util.Date)14 EventType (org.zalando.nakadi.domain.EventType)14 List (java.util.List)13 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)13 Storage (org.zalando.nakadi.domain.Storage)13 Collectors (java.util.stream.Collectors)12 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)12 ArrayList (java.util.ArrayList)9 Collections (java.util.Collections)9 NakadiSettings (org.zalando.nakadi.config.NakadiSettings)9 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)9 Optional (java.util.Optional)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 NakadiException (org.zalando.nakadi.exceptions.NakadiException)8