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);
}
Aggregations