2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.oran.datafile.tasks;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.util.Collections;
28 import org.apache.kafka.clients.consumer.ConsumerConfig;
29 import org.apache.kafka.clients.consumer.OffsetResetStrategy;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.oran.datafile.configuration.AppConfig;
35 import reactor.core.publisher.Flux;
36 import reactor.kafka.receiver.KafkaReceiver;
37 import reactor.kafka.receiver.ReceiverOptions;
39 class KafkaTopicListenerTest {
42 private AppConfig appConfig;
45 private KafkaTopicListener kafkaTopicListener;
49 MockitoAnnotations.initMocks(this);
50 when(appConfig.getInputTopic()).thenReturn("testTopic");
51 when(appConfig.getKafkaClientId()).thenReturn("testClientId");
52 when(appConfig.getKafkaBootStrapServers()).thenReturn("localhost:9092");
53 kafkaTopicListener = new KafkaTopicListener(appConfig);
57 void testStartReceiveFromTopic() {
58 KafkaReceiver mockKafkaReceiver = mock(KafkaReceiver.class);
60 when(mockKafkaReceiver.receive()).thenReturn(Flux.just(new KafkaTopicListener.DataFromTopic("key", "value")));
62 ReceiverOptions<String, String> receiverOptions = mock(ReceiverOptions.class);
63 when(receiverOptions.subscription(Collections.singleton("testTopic"))).thenReturn(receiverOptions);
64 when(receiverOptions.consumerProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, OffsetResetStrategy.EARLIEST.name()))
65 .thenReturn(receiverOptions);
67 assertEquals("testTopic", appConfig.getInputTopic());