use of org.zalando.nakadi.view.Cursor in project nakadi by zalando.
the class EventStreamTest method testWriteStreamEventEmptyBatchProducesNoEventArray.
@SuppressWarnings("unchecked")
@Test
public void testWriteStreamEventEmptyBatchProducesNoEventArray() {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Cursor cursor = new Cursor("11", "000000000000000012");
final ArrayList<byte[]> events = Lists.newArrayList();
try {
writerProvider.getWriter().writeBatch(baos, cursor, events);
final String json = baos.toString();
assertEquals("{\"cursor\":{\"partition\":\"11\",\"offset\":\"000000000000000012\"}}\n", json);
final Map<String, Object> batch = TestUtils.OBJECT_MAPPER.readValue(json, new TypeReference<Map<String, Object>>() {
});
final Map<String, String> cursorM = (Map<String, String>) batch.get("cursor");
assertEquals("11", cursorM.get("partition"));
assertEquals("000000000000000012", cursorM.get("offset"));
final List<Map<String, String>> eventsM = (List<Map<String, String>>) batch.get("events");
// expecting events not to be written as an empty array
assertNull(eventsM);
} catch (final IOException e) {
fail(e.getMessage());
}
}
use of org.zalando.nakadi.view.Cursor 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());
}
use of org.zalando.nakadi.view.Cursor 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());
}
}
use of org.zalando.nakadi.view.Cursor in project nakadi by zalando.
the class CursorConverterImplTest method testBeginConvertedVersionZero.
@Test
public void testBeginConvertedVersionZero() throws Exception {
final String eventType = "test-et";
final String partition = "2";
final Storage storage = new Storage("", Storage.Type.KAFKA);
final Timeline timeline = mock(Timeline.class);
when(timeline.getStorage()).thenReturn(storage);
final EventTypeCache eventTypeCache = mock(EventTypeCache.class);
final TopicRepository topicRepository = mock(TopicRepository.class);
final TimelineService timelineService = mock(TimelineService.class);
final PartitionStatistics stats = mock(PartitionStatistics.class);
when(timelineService.getActiveTimelinesOrdered(eq(eventType))).thenReturn(Collections.singletonList(timeline));
when(timelineService.getTopicRepository(eq(timeline))).thenReturn(topicRepository);
when(topicRepository.loadPartitionStatistics(eq(timeline), eq(partition))).thenReturn(Optional.of(stats));
final NakadiCursor beforeFirstCursor = NakadiCursor.of(timeline, partition, "000001");
when(stats.getBeforeFirst()).thenReturn(beforeFirstCursor);
final CursorConverter converter = new CursorConverterImpl(eventTypeCache, timelineService);
final NakadiCursor nakadiCursor = converter.convert(eventType, new Cursor(partition, "BEGIN"));
Assert.assertEquals(timeline, nakadiCursor.getTimeline());
Assert.assertEquals(partition, nakadiCursor.getPartition());
Assert.assertEquals("000001", nakadiCursor.getOffset());
}
use of org.zalando.nakadi.view.Cursor in project nakadi by zalando.
the class KafkaTopicRepositoryTest method validateValidCursors.
@Test
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
public void validateValidCursors() throws NakadiException, InvalidCursorException {
// validate each individual valid cursor
for (final Cursor cursor : MY_TOPIC_VALID_CURSORS) {
kafkaTopicRepository.createEventConsumer(KAFKA_CLIENT_ID, asTopicPosition(MY_TOPIC, asList(cursor)));
}
// validate all valid cursors
kafkaTopicRepository.createEventConsumer(KAFKA_CLIENT_ID, asTopicPosition(MY_TOPIC, MY_TOPIC_VALID_CURSORS));
// validate each individual valid cursor
for (final Cursor cursor : ANOTHER_TOPIC_VALID_CURSORS) {
kafkaTopicRepository.createEventConsumer(KAFKA_CLIENT_ID, asTopicPosition(ANOTHER_TOPIC, asList(cursor)));
}
// validate all valid cursors
kafkaTopicRepository.createEventConsumer(KAFKA_CLIENT_ID, asTopicPosition(ANOTHER_TOPIC, ANOTHER_TOPIC_VALID_CURSORS));
}
Aggregations