ICS sample producer and consumer
[nonrtric.git] / sample-services / ics-producer-consumer / consumer / src / test / java / com / demo / consumer / SimpleConsumerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  *
5  * Copyright (C) 2024: OpenInfra Foundation Europe
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package com.demo.consumer;
22
23 import com.demo.consumer.consumer.SimpleConsumer;
24 import com.demo.consumer.messages.KafkaMessageHandler;
25 import com.demo.consumer.messages.PropertiesHelper;
26
27 import org.apache.kafka.clients.consumer.KafkaConsumer;
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.MockedStatic;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36
37 import java.time.Duration;
38
39 import java.util.Properties;
40
41 import static org.mockito.Mockito.*;
42
43 class SimpleConsumerTest {
44
45     private static final long TIME_OUT_MS = 1000;
46
47     @Mock
48     private KafkaConsumer<String, String> kafkaConsumer;
49
50     @InjectMocks
51     private SimpleConsumer simpleConsumer;
52
53     private AutoCloseable closable;
54
55     @BeforeEach
56     void setUp() throws Exception {
57         closable = MockitoAnnotations.openMocks(this);
58     }
59
60     @AfterEach
61     void tearDown() throws Exception {
62         closable.close();
63     }
64
65     @Test
66     void testRun() throws Exception {
67         // Mocking the properties object returned by PropertiesHelper.getProperties()
68         Properties properties = new Properties();
69         properties.setProperty("bootstrap.servers", "localhost:9092");
70         properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
71         properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
72
73         // Mocking PropertiesHelper.getProperties() to return the mocked Properties object
74         try (MockedStatic<PropertiesHelper> propertiesHelperMockedStatic = Mockito.mockStatic(PropertiesHelper.class)) {
75             propertiesHelperMockedStatic.when(PropertiesHelper::getProperties).thenReturn(properties);
76
77             String topicName = "testTopic";
78             int numberOfMessages = 10;
79             KafkaMessageHandler callback = mock(KafkaMessageHandler.class);
80
81             simpleConsumer.run(topicName, numberOfMessages, callback);
82             verify(kafkaConsumer, times(0)).poll(Duration.ofMillis(TIME_OUT_MS));
83         }
84     }
85
86 }