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