Improve Test coverage of DFC
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / tasks / KafkaTopicListenerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.oran.datafile.tasks;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
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;
38
39 class KafkaTopicListenerTest {
40
41     @Mock
42     private AppConfig appConfig;
43
44     @Mock
45     private KafkaTopicListener kafkaTopicListener;
46
47     @BeforeEach
48     void setUp() {
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);
54     }
55
56     @Test
57     void testStartReceiveFromTopic() {
58         KafkaReceiver mockKafkaReceiver = mock(KafkaReceiver.class);
59
60         when(mockKafkaReceiver.receive()).thenReturn(Flux.just(new KafkaTopicListener.DataFromTopic("key", "value")));
61
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);
66
67         assertEquals("testTopic", appConfig.getInputTopic());
68
69     }
70 }