8e13af239f7d2074f2f519d9e0a19d9f4f599635
[policy/clamp.git] /
1 /*-
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
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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.common.message.bus.event.kafka;
20
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;
28
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;
38
39 class KafkaPublisherWrapperTest {
40
41     private KafkaPublisherWrapper kafkaPublisherWrapper;
42     private Producer<String, String> mockProducer;
43     private BusTopicParams mockBusTopicParams;
44
45     @BeforeEach
46     void setUp() {
47         mockProducer = mock(KafkaProducer.class);
48         mockBusTopicParams = mock(BusTopicParams.class);
49
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);
55
56         kafkaPublisherWrapper = new KafkaPublisherWrapper(mockBusTopicParams) {
57             private Producer<String, String> createProducer(Properties props) { // NOSONAR instance creation
58                 return mockProducer;
59             }
60         };
61     }
62
63     @Test
64     void testConstructor() {
65         verify(mockBusTopicParams).getTopic();
66         verify(mockBusTopicParams).getServers();
67         verify(mockBusTopicParams).isTopicInvalid();
68         verify(mockBusTopicParams).isAdditionalPropsValid();
69         verify(mockBusTopicParams).isAllowTracing();
70     }
71
72     @Test
73     void testSendSuccess() {
74         when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenReturn(null);
75         assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
76     }
77
78     @Test
79     void testSendNullMessage() {
80         IllegalArgumentException thrown = assertThrows(
81             IllegalArgumentException.class,
82             () -> kafkaPublisherWrapper.send("partitionId", null),
83             "Expected send() to throw, but it didn't"
84         );
85         assertEquals("No message provided", thrown.getMessage());
86     }
87
88     @Test
89     void testSendFailure() {
90         when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenThrow(RuntimeException.class);
91         assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
92     }
93
94     @Test
95     void testClose() {
96         assertThatCode(kafkaPublisherWrapper::close).doesNotThrowAnyException();
97     }
98 }