802294190df53d6c2be5d15ac28c9522c833cbbf
[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;
20
21 import static org.assertj.core.api.Assertions.assertThatThrownBy;
22
23 import java.util.List;
24 import org.apache.kafka.clients.ClientUtils;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.Mock;
27 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
28
29 class IndexedKafkaTopicSourceFactoryTest {
30
31     private IndexedKafkaTopicSourceFactory factory;
32
33     @Mock
34     ClientUtils mockClientUtils;
35
36     @Test
37     void testBuild() {
38         factory = new IndexedKafkaTopicSourceFactory();
39         BusTopicParams params = new BusTopicParams();
40
41         // set servers to null
42         params.setServers(null);
43         assertThatThrownBy(() -> factory.build(params))
44             .isInstanceOf(IllegalArgumentException.class)
45             .hasMessageContaining("KAFKA Server(s) must be provided");
46
47         // set servers to empty
48         params.setServers(List.of());
49         assertThatThrownBy(() -> factory.build(params))
50             .isInstanceOf(IllegalArgumentException.class)
51             .hasMessageContaining("KAFKA Server(s) must be provided");
52
53         List<String> servers = List.of("kafka:9092", "kafka:29092");
54         params.setServers(servers);
55
56         // set topic to null
57         params.setTopic(null);
58         assertThatThrownBy(() -> factory.build(params))
59             .isInstanceOf(IllegalArgumentException.class)
60             .hasMessageContaining("A topic must be provided");
61
62         // set topic to empty
63         params.setTopic("");
64         assertThatThrownBy(() -> factory.build(params))
65             .isInstanceOf(IllegalArgumentException.class)
66             .hasMessageContaining("A topic must be provided");
67
68         params.setTopic("topic01");
69
70         assertThatThrownBy(() -> factory.build(servers, "topic1"))
71             .isInstanceOf(IllegalArgumentException.class)
72             .hasMessageContaining("cannot create topic");
73     }
74 }