Search in sources :

Example 56 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project metron by apache.

the class KafkaServiceImplTest method getSampleMessageProperlyReturnsAMessageFromAGivenKafkaTopic.

@Test
public void getSampleMessageProperlyReturnsAMessageFromAGivenKafkaTopic() throws Exception {
    final String topicName = "t";
    final Node host = new Node(1, "host", 8080);
    final Node[] replicas = { host };
    final List<PartitionInfo> partitionInfo = Lists.newArrayList(new PartitionInfo(topicName, 1, host, replicas, replicas));
    final TopicPartition topicPartition = new TopicPartition(topicName, 1);
    final List<TopicPartition> topicPartitions = Lists.newArrayList(topicPartition);
    final Set<TopicPartition> topicPartitionsSet = Sets.newHashSet(topicPartitions);
    final ConsumerRecords<String, String> records = new ConsumerRecords<>(new HashMap<TopicPartition, List<ConsumerRecord<String, String>>>() {

        {
            put(topicPartition, Lists.newArrayList(new ConsumerRecord<>(topicName, 1, 1, "k", "message")));
        }
    });
    when(kafkaConsumer.listTopics()).thenReturn(new HashMap<String, List<PartitionInfo>>() {

        {
            put(topicName, Lists.newArrayList());
        }
    });
    when(kafkaConsumer.partitionsFor(eq(topicName))).thenReturn(partitionInfo);
    when(kafkaConsumer.assignment()).thenReturn(topicPartitionsSet);
    when(kafkaConsumer.position(topicPartition)).thenReturn(1L);
    when(kafkaConsumer.poll(100)).thenReturn(records);
    assertEquals("message", kafkaService.getSampleMessage(topicName));
    verify(kafkaConsumer).assign(eq(topicPartitions));
    verify(kafkaConsumer).assignment();
    verify(kafkaConsumer).poll(100);
    verify(kafkaConsumer).unsubscribe();
    verify(kafkaConsumer, times(2)).position(topicPartition);
    verify(kafkaConsumer).seek(topicPartition, 0);
    verifyZeroInteractions(zkUtils, adminUtils);
}
Also used : Node(org.apache.kafka.common.Node) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) PartitionInfo(org.apache.kafka.common.PartitionInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

PartitionInfo (org.apache.kafka.common.PartitionInfo)56 TopicPartition (org.apache.kafka.common.TopicPartition)34 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 Node (org.apache.kafka.common.Node)14 Map (java.util.Map)13 HashSet (java.util.HashSet)12 Cluster (org.apache.kafka.common.Cluster)11 Set (java.util.Set)9 List (java.util.List)8 TaskId (org.apache.kafka.streams.processor.TaskId)7 StreamsConfig (org.apache.kafka.streams.StreamsConfig)6 MockTime (org.apache.kafka.common.utils.MockTime)5 Properties (java.util.Properties)4 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)4 HostInfo (org.apache.kafka.streams.state.HostInfo)4 StreamsMetadata (org.apache.kafka.streams.state.StreamsMetadata)4 File (java.io.File)3 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)3