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.message.bus.event.kafka;
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;
37 import org.onap.policy.common.parameters.topic.BusTopicParams;
39 class KafkaPublisherWrapperTest {
41 private KafkaPublisherWrapper kafkaPublisherWrapper;
42 private Producer<String, String> mockProducer;
43 private BusTopicParams mockBusTopicParams;
47 mockProducer = mock(KafkaProducer.class);
48 mockBusTopicParams = mock(BusTopicParams.class);
50 when(mockBusTopicParams.getTopic()).thenReturn("testTopic");
51 when(mockBusTopicParams.getServers()).thenReturn(Collections.singletonList("localhost:9092"));
52 when(mockBusTopicParams.isTopicInvalid()).thenReturn(false);
53 when(mockBusTopicParams.isAdditionalPropsValid()).thenReturn(false);
54 when(mockBusTopicParams.isAllowTracing()).thenReturn(false);
56 kafkaPublisherWrapper = new KafkaPublisherWrapper(mockBusTopicParams) {
57 private Producer<String, String> createProducer(Properties props) { // NOSONAR instance creation
64 void testConstructor() {
65 verify(mockBusTopicParams).getTopic();
66 verify(mockBusTopicParams).getServers();
67 verify(mockBusTopicParams).isTopicInvalid();
68 verify(mockBusTopicParams).isAdditionalPropsValid();
69 verify(mockBusTopicParams).isAllowTracing();
73 void testSendSuccess() {
74 when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenReturn(null);
75 assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
79 void testSendNullMessage() {
80 IllegalArgumentException thrown = assertThrows(
81 IllegalArgumentException.class,
82 () -> kafkaPublisherWrapper.send("partitionId", null),
83 "Expected send() to throw, but it didn't"
85 assertEquals("No message provided", thrown.getMessage());
89 void testSendFailure() {
90 when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenThrow(RuntimeException.class);
91 assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
96 assertThatCode(kafkaPublisherWrapper::close).doesNotThrowAnyException();