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.rev210813.NotificationAlarmService;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.NotificationProcessService;
27 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.get.notifications.alarm.service.output.NotificationsAlarmService;
28 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.get.notifications.process.service.output.NotificationsProcessService;
30 public class SubscriberTest extends AbstractTest {
31 private static final String TOPIC = "topic";
32 private static final int PARTITION = 0;
33 private MockConsumer<String, NotificationsProcessService> mockConsumer;
34 private MockConsumer<String, NotificationsAlarmService> mockConsumerAlarm;
35 private Subscriber<NotificationProcessService, NotificationsProcessService> subscriberService;
36 private Subscriber<NotificationAlarmService, NotificationsAlarmService> 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, NotificationsProcessService> 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<NotificationsProcessService> result = subscriberService.subscribe(TOPIC,
61 NotificationsProcessService.QNAME);
62 assertEquals("There should be 1 record", 1, result.size());
63 assertTrue("Consumer should be closed", mockConsumer.closed());
67 public void subscribeAlarmShouldBeSuccessful() {
68 // from https://www.baeldung.com/kafka-mockconsumer
69 ConsumerRecord<String, NotificationsAlarmService> record = new ConsumerRecord<>(
70 TOPIC, PARTITION, 0L, "key", NotificationServiceDataUtils.buildReceivedAlarmEvent());
71 mockConsumerAlarm.schedulePollTask(() -> {
72 mockConsumerAlarm.rebalance(Collections.singletonList(new TopicPartition(TOPIC, PARTITION)));
73 mockConsumerAlarm.addRecord(record);
76 Map<TopicPartition, Long> startOffsets = new HashMap<>();
77 TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
78 startOffsets.put(tp, 0L);
79 mockConsumerAlarm.updateBeginningOffsets(startOffsets);
80 List<NotificationsAlarmService> result = subscriberAlarmService.subscribe(TOPIC,
81 NotificationsAlarmService.QNAME);
82 assertEquals("There should be 1 record", 1, result.size());
83 assertTrue("Consumer should be closed", mockConsumerAlarm.closed());