use of org.zalando.nakadi.repository.TopicRepository 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);
}
use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class CursorOperationsServiceTest method mockTimeline.
private Timeline mockTimeline(final int order, @Nullable final Long latestOffset) {
final Timeline timeline = mock(Timeline.class);
when(timeline.getOrder()).thenReturn(order);
final Storage storage = new Storage();
storage.setType(Storage.Type.KAFKA);
when(timeline.getStorage()).thenReturn(storage);
if (latestOffset == null) {
when(timeline.isActive()).thenReturn(false);
when(timeline.getLatestPosition()).thenReturn(null);
} else {
when(timeline.isActive()).thenReturn(true);
when(timeline.getLatestPosition()).thenReturn(new Timeline.KafkaStoragePosition(Collections.singletonList(latestOffset)));
}
when(timeline.isActive()).thenReturn(null == latestOffset);
final TopicRepository repository = new KafkaTopicRepository(mock(ZooKeeperHolder.class), mock(KafkaFactory.class), mock(NakadiSettings.class), mock(KafkaSettings.class), mock(ZookeeperSettings.class), mock(UUIDGenerator.class));
when(timelineService.getTopicRepository(timeline)).thenReturn(repository);
return timeline;
}
use of org.zalando.nakadi.repository.TopicRepository 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.repository.TopicRepository in project nakadi by zalando.
the class TimelineServiceTest method shouldDeleteTopicWhenTimelineCreationFails.
@Test
public void shouldDeleteTopicWhenTimelineCreationFails() throws Exception {
final TopicRepository repository = mock(TopicRepository.class);
Mockito.when(topicRepositoryHolder.getTopicRepository(any())).thenReturn(repository);
Mockito.when(timelineDbRepository.createTimeline(any())).thenThrow(new InconsistentStateException("shouldDeleteTopicWhenTimelineCreationFails"));
try {
timelineService.createDefaultTimeline("event_type_1", 1, 1);
} catch (final InconsistentStateException e) {
}
Mockito.verify(repository, Mockito.times(1)).deleteTopic(any());
}
use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class TimelineService method createDefaultTimeline.
public Timeline createDefaultTimeline(final String eventTypeName, final int partitionsCount, final long retentionTime) throws TopicCreationException, InconsistentStateException, RepositoryProblemException, DuplicatedTimelineException, TimelineException, DbWriteOperationsBlockedException {
if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
throw new DbWriteOperationsBlockedException("Cannot create default timeline: write operations on DB " + "are blocked by feature flag.");
}
final TopicRepository repository = topicRepositoryHolder.getTopicRepository(defaultStorage.getStorage());
final String topic = repository.createTopic(partitionsCount, retentionTime);
try {
final Timeline timeline = Timeline.createTimeline(eventTypeName, 1, defaultStorage.getStorage(), topic, new Date());
timeline.setSwitchedAt(new Date());
timelineDbRepository.createTimeline(timeline);
eventTypeCache.updated(eventTypeName);
return timeline;
} catch (final InconsistentStateException | RepositoryProblemException | DuplicatedTimelineException e) {
rollbackTopic(repository, topic);
throw e;
} catch (final Exception e) {
rollbackTopic(repository, topic);
throw new TimelineException("Failed to update event type cache, while creating timeline", e);
}
}
Aggregations