Search in sources :

Example 1 with MessageVersion

use of org.apache.atlas.notification.MessageVersion in project incubator-atlas by apache.

the class KafkaConsumerTest method testNext.

@Test
public void testNext() throws Exception {
    KafkaStream<String, String> stream = mock(KafkaStream.class);
    ConsumerIterator<String, String> iterator = mock(ConsumerIterator.class);
    MessageAndMetadata<String, String> messageAndMetadata = mock(MessageAndMetadata.class);
    Referenceable entity = getEntity(TRAIT_NAME);
    HookNotification.EntityUpdateRequest message = new HookNotification.EntityUpdateRequest("user1", entity);
    String json = AbstractNotification.GSON.toJson(new VersionedMessage<>(new MessageVersion("1.0.0"), message));
    when(stream.iterator()).thenReturn(iterator);
    when(iterator.hasNext()).thenReturn(true).thenReturn(false);
    when(iterator.next()).thenReturn(messageAndMetadata).thenThrow(new NoSuchElementException());
    when(messageAndMetadata.message()).thenReturn(json);
    NotificationConsumer<HookNotification.HookNotificationMessage> consumer = new KafkaConsumer<>(NotificationInterface.NotificationType.HOOK.getDeserializer(), stream, 99, consumerConnector, false);
    assertTrue(consumer.hasNext());
    HookNotification.HookNotificationMessage consumedMessage = consumer.next();
    assertMessagesEqual(message, consumedMessage, entity);
    assertFalse(consumer.hasNext());
}
Also used : MessageVersion(org.apache.atlas.notification.MessageVersion) HookNotification(org.apache.atlas.notification.hook.HookNotification) Referenceable(org.apache.atlas.typesystem.Referenceable) NoSuchElementException(java.util.NoSuchElementException) Test(org.testng.annotations.Test) EntityNotificationImplTest(org.apache.atlas.notification.entity.EntityNotificationImplTest)

Example 2 with MessageVersion

use of org.apache.atlas.notification.MessageVersion in project incubator-atlas by apache.

the class KafkaConsumerTest method testPeekMessage.

@Test
public void testPeekMessage() throws Exception {
    KafkaStream<String, String> stream = mock(KafkaStream.class);
    ConsumerIterator<String, String> iterator = mock(ConsumerIterator.class);
    MessageAndMetadata<String, String> messageAndMetadata = mock(MessageAndMetadata.class);
    Referenceable entity = getEntity(TRAIT_NAME);
    HookNotification.EntityUpdateRequest message = new HookNotification.EntityUpdateRequest("user1", entity);
    String json = AbstractNotification.GSON.toJson(new VersionedMessage<>(new MessageVersion("1.0.0"), message));
    when(stream.iterator()).thenReturn(iterator);
    when(iterator.hasNext()).thenReturn(true);
    when(iterator.peek()).thenReturn(messageAndMetadata);
    when(messageAndMetadata.message()).thenReturn(json);
    NotificationConsumer<HookNotification.HookNotificationMessage> consumer = new KafkaConsumer<>(NotificationInterface.NotificationType.HOOK.getDeserializer(), stream, 99, consumerConnector, false);
    assertTrue(consumer.hasNext());
    HookNotification.HookNotificationMessage consumedMessage = consumer.peek();
    assertMessagesEqual(message, consumedMessage, entity);
    assertTrue(consumer.hasNext());
}
Also used : MessageVersion(org.apache.atlas.notification.MessageVersion) HookNotification(org.apache.atlas.notification.hook.HookNotification) Referenceable(org.apache.atlas.typesystem.Referenceable) Test(org.testng.annotations.Test) EntityNotificationImplTest(org.apache.atlas.notification.entity.EntityNotificationImplTest)

Example 3 with MessageVersion

use of org.apache.atlas.notification.MessageVersion in project incubator-atlas by apache.

the class KafkaConsumerTest method testReceive.

@Test
public void testReceive() throws Exception {
    MessageAndMetadata<String, String> messageAndMetadata = mock(MessageAndMetadata.class);
    Referenceable entity = getEntity(TRAIT_NAME);
    HookNotification.EntityUpdateRequest message = new HookNotification.EntityUpdateRequest("user1", entity);
    String json = AbstractNotification.GSON.toJson(new VersionedMessage<>(new MessageVersion("1.0.0"), message));
    kafkaConsumer.assign(Arrays.asList(new TopicPartition("ATLAS_HOOK", 0)));
    List<ConsumerRecord> klist = new ArrayList<>();
    klist.add(new ConsumerRecord<String, String>("ATLAS_HOOK", 0, 0L, "mykey", json));
    TopicPartition tp = new TopicPartition("ATLAS_HOOK", 0);
    Map mp = new HashMap();
    mp.put(tp, klist);
    ConsumerRecords records = new ConsumerRecords(mp);
    when(kafkaConsumer.poll(100)).thenReturn(records);
    when(messageAndMetadata.message()).thenReturn(json);
    AtlasKafkaConsumer consumer = new AtlasKafkaConsumer(NotificationInterface.NotificationType.HOOK.getDeserializer(), kafkaConsumer, false, 100L);
    List<AtlasKafkaMessage<HookNotification.HookNotificationMessage>> messageList = consumer.receive();
    assertTrue(messageList.size() > 0);
    HookNotification.HookNotificationMessage consumedMessage = messageList.get(0).getMessage();
    assertMessagesEqual(message, consumedMessage, entity);
}
Also used : MessageVersion(org.apache.atlas.notification.MessageVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) HookNotification(org.apache.atlas.notification.hook.HookNotification) Referenceable(org.apache.atlas.typesystem.Referenceable) TopicPartition(org.apache.kafka.common.TopicPartition) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test) EntityNotificationImplTest(org.apache.atlas.notification.entity.EntityNotificationImplTest)

Example 4 with MessageVersion

use of org.apache.atlas.notification.MessageVersion in project incubator-atlas by apache.

the class KafkaConsumerTest method testNextVersionMismatch.

@Test
public void testNextVersionMismatch() throws Exception {
    MessageAndMetadata<String, String> messageAndMetadata = mock(MessageAndMetadata.class);
    Referenceable entity = getEntity(TRAIT_NAME);
    HookNotification.EntityUpdateRequest message = new HookNotification.EntityUpdateRequest("user1", entity);
    String json = AbstractNotification.GSON.toJson(new VersionedMessage<>(new MessageVersion("2.0.0"), message));
    kafkaConsumer.assign(Arrays.asList(new TopicPartition("ATLAS_HOOK", 0)));
    List<ConsumerRecord> klist = new ArrayList<>();
    klist.add(new ConsumerRecord<String, String>("ATLAS_HOOK", 0, 0L, "mykey", json));
    TopicPartition tp = new TopicPartition("ATLAS_HOOK", 0);
    Map mp = new HashMap();
    mp.put(tp, klist);
    ConsumerRecords records = new ConsumerRecords(mp);
    when(kafkaConsumer.poll(100L)).thenReturn(records);
    when(messageAndMetadata.message()).thenReturn(json);
    AtlasKafkaConsumer consumer = new AtlasKafkaConsumer(NotificationInterface.NotificationType.HOOK.getDeserializer(), kafkaConsumer, false, 100L);
    try {
        List<AtlasKafkaMessage<HookNotification.HookNotificationMessage>> messageList = consumer.receive();
        assertTrue(messageList.size() > 0);
        HookNotification.HookNotificationMessage consumedMessage = messageList.get(0).getMessage();
        fail("Expected VersionMismatchException!");
    } catch (IncompatibleVersionException e) {
        e.printStackTrace();
    }
}
Also used : MessageVersion(org.apache.atlas.notification.MessageVersion) HashMap(java.util.HashMap) IncompatibleVersionException(org.apache.atlas.notification.IncompatibleVersionException) ArrayList(java.util.ArrayList) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) HookNotification(org.apache.atlas.notification.hook.HookNotification) Referenceable(org.apache.atlas.typesystem.Referenceable) TopicPartition(org.apache.kafka.common.TopicPartition) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test) EntityNotificationImplTest(org.apache.atlas.notification.entity.EntityNotificationImplTest)

Aggregations

MessageVersion (org.apache.atlas.notification.MessageVersion)4 EntityNotificationImplTest (org.apache.atlas.notification.entity.EntityNotificationImplTest)4 HookNotification (org.apache.atlas.notification.hook.HookNotification)4 Referenceable (org.apache.atlas.typesystem.Referenceable)4 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 NoSuchElementException (java.util.NoSuchElementException)1 IncompatibleVersionException (org.apache.atlas.notification.IncompatibleVersionException)1