Search in sources :

Example 1 with EventTypeRepository

use of org.zalando.nakadi.repository.EventTypeRepository in project nakadi by zalando.

the class EventStreamControllerTest method setup.

@Before
public void setup() throws NakadiException, UnknownHostException, InvalidCursorException {
    EVENT_TYPE.setName(TEST_EVENT_TYPE_NAME);
    timeline = buildTimelineWithTopic(TEST_TOPIC);
    eventTypeRepository = mock(EventTypeRepository.class);
    topicRepositoryMock = mock(TopicRepository.class);
    adminService = mock(AdminService.class);
    authorizationService = mock(AuthorizationService.class);
    when(topicRepositoryMock.topicExists(TEST_TOPIC)).thenReturn(true);
    eventStreamFactoryMock = mock(EventStreamFactory.class);
    eventTypeCache = mock(EventTypeCache.class);
    requestMock = mock(HttpServletRequest.class);
    when(requestMock.getRemoteAddr()).thenReturn(InetAddress.getLoopbackAddress().getHostAddress());
    when(requestMock.getRemotePort()).thenReturn(12345);
    responseMock = mock(HttpServletResponse.class);
    metricRegistry = new MetricRegistry();
    streamMetrics = new MetricRegistry();
    final EventConsumer.LowLevelConsumer eventConsumerMock = mock(EventConsumer.LowLevelConsumer.class);
    when(topicRepositoryMock.createEventConsumer(eq(KAFKA_CLIENT_ID), any())).thenReturn(eventConsumerMock);
    final ClosedConnectionsCrutch crutch = mock(ClosedConnectionsCrutch.class);
    when(crutch.listenForConnectionClose(requestMock)).thenReturn(new AtomicBoolean(true));
    blacklistService = Mockito.mock(BlacklistService.class);
    Mockito.when(blacklistService.isConsumptionBlocked(any(), any())).thenReturn(false);
    final ConsumerLimitingService consumerLimitingService = Mockito.mock(ConsumerLimitingService.class);
    when(consumerLimitingService.acquireConnectionSlots(any(), any(), any())).thenReturn(ImmutableList.of());
    featureToggleService = mock(FeatureToggleService.class);
    timelineService = mock(TimelineService.class);
    when(timelineService.getTopicRepository((Timeline) any())).thenReturn(topicRepositoryMock);
    when(timelineService.getTopicRepository((EventTypeBase) any())).thenReturn(topicRepositoryMock);
    when(timelineService.getTopicRepository((Storage) any())).thenReturn(topicRepositoryMock);
    when(timelineService.getActiveTimelinesOrdered(any())).thenReturn(Collections.singletonList(timeline));
    when(timelineService.getAllTimelinesOrdered(any())).thenReturn(Collections.singletonList(timeline));
    authorizationValidator = mock(AuthorizationValidator.class);
    eventTypeChangeListener = mock(EventTypeChangeListener.class);
    when(eventTypeChangeListener.registerListener(any(), any())).thenReturn(mock(Closeable.class));
    controller = new EventStreamController(eventTypeRepository, timelineService, TestUtils.OBJECT_MAPPER, eventStreamFactoryMock, metricRegistry, streamMetrics, crutch, blacklistService, consumerLimitingService, featureToggleService, new CursorConverterImpl(eventTypeCache, timelineService), authorizationValidator, eventTypeChangeListener, null);
    settings = mock(SecuritySettings.class);
    when(settings.getAuthMode()).thenReturn(OFF);
    when(settings.getAdminClientId()).thenReturn("nakadi");
    mockMvc = standaloneSetup(controller).setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER).setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)).build();
}
Also used : AdminService(org.zalando.nakadi.service.AdminService) AuthorizationValidator(org.zalando.nakadi.service.AuthorizationValidator) ClosedConnectionsCrutch(org.zalando.nakadi.service.ClosedConnectionsCrutch) BlacklistService(org.zalando.nakadi.service.BlacklistService) Closeable(java.io.Closeable) SecuritySettings(org.zalando.nakadi.config.SecuritySettings) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpServletRequest(javax.servlet.http.HttpServletRequest) EventConsumer(org.zalando.nakadi.repository.EventConsumer) FeatureToggleService(org.zalando.nakadi.service.FeatureToggleService) EventTypeChangeListener(org.zalando.nakadi.service.EventTypeChangeListener) TopicRepository(org.zalando.nakadi.repository.TopicRepository) ConsumerLimitingService(org.zalando.nakadi.service.ConsumerLimitingService) MetricRegistry(com.codahale.metrics.MetricRegistry) CursorConverterImpl(org.zalando.nakadi.service.converter.CursorConverterImpl) HttpServletResponse(javax.servlet.http.HttpServletResponse) EventTypeCache(org.zalando.nakadi.repository.db.EventTypeCache) ClientResolver(org.zalando.nakadi.security.ClientResolver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AuthorizationService(org.zalando.nakadi.plugin.api.authz.AuthorizationService) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) EventStreamFactory(org.zalando.nakadi.service.EventStreamFactory) Before(org.junit.Before)

Example 2 with EventTypeRepository

use of org.zalando.nakadi.repository.EventTypeRepository in project nakadi by zalando.

the class EventTypeCacheTest method testEventTypesPreloaded.

@Test
public void testEventTypesPreloaded() throws Exception {
    final EventTypeRepository etRepo = Mockito.mock(EventTypeRepository.class);
    final TimelineDbRepository timelineRepository = Mockito.mock(TimelineDbRepository.class);
    final ZooKeeperHolder zkHolder = Mockito.mock(ZooKeeperHolder.class);
    final TimelineSync timelineSync = Mockito.mock(TimelineSync.class);
    final EventType et = TestUtils.buildDefaultEventType();
    Mockito.when(etRepo.list()).thenReturn(Collections.singletonList(et));
    final Timeline timeline = TestUtils.buildTimeline(et.getName());
    final List<Timeline> timelines = Collections.singletonList(timeline);
    Mockito.when(timelineRepository.listTimelinesOrdered()).thenReturn(timelines);
    Mockito.when(timelineSync.registerTimelineChangeListener(Matchers.eq(et.getName()), Mockito.any())).thenReturn(() -> {
    });
    final EventTypeCache eventTypeCache = new EventTypeCache(etRepo, timelineRepository, zkHolder, null, timelineSync) {

        @Override
        public void created(final String name) throws Exception {
        // ignore this call, because mocking is too complex
        }
    };
    Assert.assertSame(et, eventTypeCache.getEventType(et.getName()));
    Mockito.verify(etRepo, Mockito.times(0)).findByName(Mockito.any());
    Mockito.verify(etRepo, Mockito.times(1)).list();
    Assert.assertEquals(timelines, eventTypeCache.getTimelinesOrdered(et.getName()));
    Mockito.verify(timelineRepository, Mockito.times(0)).listTimelinesOrdered(Mockito.any());
    Mockito.verify(timelineRepository, Mockito.times(1)).listTimelinesOrdered();
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) Test(org.junit.Test)

Example 3 with EventTypeRepository

use of org.zalando.nakadi.repository.EventTypeRepository in project nakadi by zalando.

the class EventTypeCache method setupInMemoryEventTypeCache.

private LoadingCache<String, CachedValue> setupInMemoryEventTypeCache(final EventTypeRepository eventTypeRepository, final TimelineDbRepository timelineRepository) {
    final CacheLoader<String, CachedValue> loader = new CacheLoader<String, CachedValue>() {

        public CachedValue load(final String key) throws Exception {
            final EventType eventType = eventTypeRepository.findByName(key);
            final List<Timeline> timelines = timelineRepository.listTimelinesOrdered(key);
            timelineRegistrations.computeIfAbsent(key, n -> timelineSync.registerTimelineChangeListener(n, (etName) -> eventTypeCache.invalidate(etName)));
            return new CachedValue(eventType, EventValidation.forType(eventType), timelines);
        }
    };
    return CacheBuilder.newBuilder().maximumSize(CACHE_MAX_SIZE).build(loader);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) LoadingCache(com.google.common.cache.LoadingCache) LoggerFactory(org.slf4j.LoggerFactory) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) EventValidation(org.zalando.nakadi.validation.EventValidation) ZKPaths(org.apache.curator.utils.ZKPaths) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) EventTypeValidator(org.zalando.nakadi.validation.EventTypeValidator) Nonnull(javax.annotation.Nonnull) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Iterator(java.util.Iterator) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timeline(org.zalando.nakadi.domain.Timeline) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) CacheLoader(com.google.common.cache.CacheLoader)

Aggregations

EventTypeRepository (org.zalando.nakadi.repository.EventTypeRepository)3 EventType (org.zalando.nakadi.domain.EventType)2 Timeline (org.zalando.nakadi.domain.Timeline)2 ZooKeeperHolder (org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder)2 TimelineSync (org.zalando.nakadi.service.timeline.TimelineSync)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ImmutableList (com.google.common.collect.ImmutableList)1 Closeable (java.io.Closeable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1