2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.common.endpoints.event.comm.bus.internal;
21 import static org.assertj.core.api.Assertions.assertThatCode;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.util.Collections;
30 import java.util.Properties;
31 import org.apache.kafka.clients.producer.KafkaProducer;
32 import org.apache.kafka.clients.producer.Producer;
33 import org.apache.kafka.clients.producer.ProducerRecord;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.ArgumentMatchers;
38 class KafkaPublisherWrapperTest {
40 private BusPublisher.KafkaPublisherWrapper kafkaPublisherWrapper;
41 private Producer<String, String> mockProducer;
42 private BusTopicParams mockBusTopicParams;
46 mockProducer = mock(KafkaProducer.class);
47 mockBusTopicParams = mock(BusTopicParams.class);
49 when(mockBusTopicParams.getTopic()).thenReturn("testTopic");
50 when(mockBusTopicParams.getServers()).thenReturn(Collections.singletonList("localhost:9092"));
51 when(mockBusTopicParams.isTopicInvalid()).thenReturn(false);
52 when(mockBusTopicParams.isAdditionalPropsValid()).thenReturn(false);
53 when(mockBusTopicParams.isAllowTracing()).thenReturn(false);
55 kafkaPublisherWrapper = new BusPublisher.KafkaPublisherWrapper(mockBusTopicParams) {
56 protected Producer<String, String> createProducer(Properties props) { // NOSONAR instance creation
63 void testConstructor() {
64 verify(mockBusTopicParams).getTopic();
65 verify(mockBusTopicParams).getServers();
66 verify(mockBusTopicParams).isTopicInvalid();
67 verify(mockBusTopicParams).isAdditionalPropsValid();
68 verify(mockBusTopicParams).isAllowTracing();
72 void testSendSuccess() {
73 when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenReturn(null);
74 assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
78 void testSendNullMessage() {
79 IllegalArgumentException thrown = assertThrows(
80 IllegalArgumentException.class,
81 () -> kafkaPublisherWrapper.send("partitionId", null),
82 "Expected send() to throw, but it didn't"
84 assertEquals("No message provided", thrown.getMessage());
88 void testSendFailure() {
89 when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenThrow(RuntimeException.class);
90 assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
95 assertThatCode(kafkaPublisherWrapper::close).doesNotThrowAnyException();