2 * Copyright © 2020 Orange, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.nbinotifications.consumer;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.List;
17 import org.apache.kafka.clients.consumer.ConsumerRecord;
18 import org.apache.kafka.clients.consumer.MockConsumer;
19 import org.apache.kafka.clients.consumer.OffsetResetStrategy;
20 import org.apache.kafka.common.TopicPartition;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.transportpce.nbinotifications.utils.NotificationServiceDataUtils;
24 import org.opendaylight.transportpce.test.AbstractTest;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.get.notifications.alarm.service.output.NotificationAlarmService;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.get.notifications.service.output.NotificationService;
28 public class SubscriberTest extends AbstractTest {
29 private static final String TOPIC = "topic";
30 private static final int PARTITION = 0;
31 private MockConsumer<String, NotificationService> mockConsumer;
32 private MockConsumer<String, NotificationAlarmService> mockConsumerAlarm;
33 private Subscriber<org.opendaylight.yang.gen.v1
34 .nbi.notifications.rev210628.NotificationService, NotificationService> subscriberService;
35 private Subscriber<org.opendaylight.yang.gen.v1
36 .nbi.notifications.rev210628.NotificationAlarmService, NotificationAlarmService> subscriberAlarmService;
40 mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
41 mockConsumerAlarm = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
42 subscriberService = new Subscriber<>(mockConsumer);
43 subscriberAlarmService = new Subscriber<>(mockConsumerAlarm);
47 public void subscribeServiceShouldBeSuccessful() {
48 // from https://www.baeldung.com/kafka-mockconsumer
49 ConsumerRecord<String, NotificationService> record = new ConsumerRecord<>(
50 TOPIC, PARTITION, 0L, "key", NotificationServiceDataUtils.buildReceivedEvent());
51 mockConsumer.schedulePollTask(() -> {
52 mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC, PARTITION)));
53 mockConsumer.addRecord(record);
56 Map<TopicPartition, Long> startOffsets = new HashMap<>();
57 TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
58 startOffsets.put(tp, 0L);
59 mockConsumer.updateBeginningOffsets(startOffsets);
60 List<NotificationService> result = subscriberService.subscribe(TOPIC, NotificationService.QNAME);
61 assertEquals("There should be 1 record", 1, result.size());
62 assertTrue("Consumer should be closed", mockConsumer.closed());
66 public void subscribeAlarmShouldBeSuccessful() {
67 // from https://www.baeldung.com/kafka-mockconsumer
68 ConsumerRecord<String, NotificationAlarmService> record = new ConsumerRecord<>(
69 TOPIC, PARTITION, 0L, "key", NotificationServiceDataUtils.buildReceivedAlarmEvent());
70 mockConsumerAlarm.schedulePollTask(() -> {
71 mockConsumerAlarm.rebalance(Collections.singletonList(new TopicPartition(TOPIC, PARTITION)));
72 mockConsumerAlarm.addRecord(record);
75 Map<TopicPartition, Long> startOffsets = new HashMap<>();
76 TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
77 startOffsets.put(tp, 0L);
78 mockConsumerAlarm.updateBeginningOffsets(startOffsets);
79 List<NotificationAlarmService> result = subscriberAlarmService.subscribe(TOPIC, NotificationAlarmService.QNAME);
80 assertEquals("There should be 1 record", 1, result.size());
81 assertTrue("Consumer should be closed", mockConsumerAlarm.closed());