X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=sample-services%2Fics-producer-consumer%2Fproducer%2Fsrc%2Ftest%2Fjava%2Fcom%2Fdemo%2Fproducer%2FSimpleProducerTest.java;fp=sample-services%2Fics-producer-consumer%2Fproducer%2Fsrc%2Ftest%2Fjava%2Fcom%2Fdemo%2Fproducer%2FSimpleProducerTest.java;h=da778739be8bc2184c48e60848e7cba7389d9e0d;hb=6360bbb90944220eef2f0b8f03623ae40c9646cd;hp=0000000000000000000000000000000000000000;hpb=0f1c16fd071c70215eed25fa45ecce4803c83d72;p=nonrtric.git diff --git a/sample-services/ics-producer-consumer/producer/src/test/java/com/demo/producer/SimpleProducerTest.java b/sample-services/ics-producer-consumer/producer/src/test/java/com/demo/producer/SimpleProducerTest.java new file mode 100644 index 00000000..da778739 --- /dev/null +++ b/sample-services/ics-producer-consumer/producer/src/test/java/com/demo/producer/SimpleProducerTest.java @@ -0,0 +1,105 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * + * Copyright (C) 2024: OpenInfra Foundation Europe + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package com.demo.producer; + +import com.demo.producer.producer.SimpleProducer; +import org.apache.kafka.clients.producer.KafkaProducer; + +import com.demo.producer.messages.KafkaMessageHandler; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class SimpleProducerTest { + + private final int wait = 1000; + private final String topicName = "testTopic"; + + @Mock + private KafkaProducer kafkaProducer; + + @InjectMocks + @Autowired + private SimpleProducer simpleProducer; + + private AutoCloseable closable; + + @BeforeEach + void setUp() { + closable = MockitoAnnotations.openMocks(this); + } + + @AfterEach + public void close() throws Exception { + closable.close(); + } + + @Test + @SuppressWarnings("unchecked") //sending only Strings + void testRun() throws Exception { + int numberOfMessages = 10; + KafkaMessageHandler callback = mock(KafkaMessageHandler.class); + + simpleProducer.run(topicName, numberOfMessages, callback); + + verify(kafkaProducer, times(numberOfMessages)).send(any(ProducerRecord.class)); + verify(kafkaProducer, times(1)).close(); + } + + @Test + @SuppressWarnings("unchecked") //sending only Strings + void testRunAlways() throws Exception { + KafkaMessageHandler callback = mock(KafkaMessageHandler.class); + simpleProducer.setTIME(wait); + // Mocking behavior to break out of the loop after a few iterations + doAnswer(invocation -> { + simpleProducer.shutdown(); + return null; + }).when(kafkaProducer).send(any(ProducerRecord.class)); + + // Invoking runAlways() in a separate thread to avoid an infinite loop + Thread thread = new Thread(() -> { + try { + simpleProducer.runAlways(topicName, callback); + } catch (Exception e) { + } + }); + thread.start(); + + // Let the thread execute for some time (e.g., 1 second) + Thread.sleep(wait); + + // Interrupting the thread to stop the infinite loop + thread.interrupt(); + + verify(kafkaProducer, atLeastOnce()).send(any(ProducerRecord.class)); + verify(kafkaProducer, times(1)).close(); + } + +}