Search in sources :

Example 16 with Producer

use of org.apache.kafka.clients.producer.Producer in project incubator-atlas by apache.

the class KafkaNotificationMockTest method shouldThrowExceptionIfProducerFails.

@Test
@SuppressWarnings("unchecked")
public void shouldThrowExceptionIfProducerFails() throws NotificationException, ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);
    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getTopicName(NotificationInterface.NotificationType.HOOK);
    String message = "This is a test message";
    Future returnValue = mock(Future.class);
    when(returnValue.get()).thenThrow(new RuntimeException("Simulating exception"));
    ProducerRecord expectedRecord = new ProducerRecord(topicName, message);
    when(producer.send(expectedRecord)).thenReturn(returnValue);
    try {
        kafkaNotification.sendInternalToProducer(producer, NotificationInterface.NotificationType.HOOK, new String[] { message });
        fail("Should have thrown NotificationException");
    } catch (NotificationException e) {
        assertEquals(e.getFailedMessages().size(), 1);
        assertEquals(e.getFailedMessages().get(0), "This is a test message");
    }
}
Also used : Producer(org.apache.kafka.clients.producer.Producer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) NotificationException(org.apache.atlas.notification.NotificationException) Future(java.util.concurrent.Future) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Aggregations

Producer (org.apache.kafka.clients.producer.Producer)16 Properties (java.util.Properties)13 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)12 TopicPartition (org.apache.kafka.common.TopicPartition)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 HashMap (java.util.HashMap)6 Objects (java.util.Objects)6 Future (java.util.concurrent.Future)6 Consumer (org.apache.kafka.clients.consumer.Consumer)6 Test (org.junit.Test)6 Test (org.testng.annotations.Test)6 Set (java.util.Set)5 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Map (java.util.Map)4 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)4 CONSUMER (brave.Span.Kind.CONSUMER)3 PRODUCER (brave.Span.Kind.PRODUCER)3