2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022, 2024 Nordix Foundation.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_KAFKA_SINK_TOPICS;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX;
30 import java.util.Deque;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Properties;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
39 class KafkaTopicSinkFactoryTest extends KafkaTopicFactoryTestBase<KafkaTopicSink> {
41 private SinkFactory factory;
42 public static final String KAFKA_SERVER = "localhost:9092";
45 * Creates the object to be tested.
52 factory = new SinkFactory();
56 public void tearDown() {
62 void testBuildBusTopicParams() {
63 super.testBuildBusTopicParams();
64 super.testBuildBusTopicParams_Ex();
69 void testBuildListOfStringString() {
70 super.testBuildListOfStringString();
72 // check parameters that were used
73 BusTopicParams params = getLastParams();
74 assertFalse(params.isAllowSelfSignedCerts());
79 void testBuildProperties() {
80 List<KafkaTopicSink> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
81 assertEquals(1, topics.size());
82 assertEquals(MY_TOPIC, topics.get(0).getTopic());
83 assertEquals(MY_EFFECTIVE_TOPIC, topics.get(0).getEffectiveTopic());
85 BusTopicParams params = getLastParams();
86 assertTrue(params.isManaged());
87 assertFalse(params.isUseHttps());
88 assertEquals(List.of(KAFKA_SERVER), params.getServers());
89 assertEquals(MY_TOPIC, params.getTopic());
90 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
91 assertEquals(MY_PARTITION, params.getPartitionId());
92 assertNotNull(params.getAdditionalProps());
94 List<KafkaTopicSink> topics2 = buildTopics(makePropBuilder().makeTopic(TOPIC3)
95 .removeTopicProperty(PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX).build());
96 assertEquals(1, topics2.size());
97 assertEquals(TOPIC3, topics2.get(0).getTopic());
98 assertEquals(topics2.get(0).getTopic(), topics2.get(0).getEffectiveTopic());
102 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
106 void testBuildFromProperties() {
107 Properties props = makePropBuilder().makeTopic(MY_TOPIC).build();
108 var listTopic = factory.build(props);
109 assertNotNull(listTopic);
114 void testDestroyString_testGet_testInventory() {
115 super.testDestroyString_testGet_testInventory();
116 super.testDestroyString_Ex();
131 void testToString() {
132 assertTrue(factory.toString().startsWith("IndexedKafkaTopicSinkFactory ["));
136 protected void initFactory() {
137 if (factory != null) {
141 factory = new SinkFactory();
145 protected List<KafkaTopicSink> buildTopics(Properties properties) {
146 return factory.build(properties);
150 protected KafkaTopicSink buildTopic(BusTopicParams params) {
151 return factory.build(params);
155 protected KafkaTopicSink buildTopic(List<String> servers, String topic) {
156 return factory.build(servers, topic);
160 protected void destroyFactory() {
165 protected void destroyTopic(String topic) {
166 factory.destroy(topic);
170 protected List<KafkaTopicSink> getInventory() {
171 return factory.inventory();
175 protected KafkaTopicSink getTopic(String topic) {
176 return factory.get(topic);
180 protected BusTopicParams getLastParams() {
181 return factory.params.getLast();
185 protected TopicPropertyBuilder makePropBuilder() {
186 return new KafkaTopicPropertyBuilder(PROPERTY_KAFKA_SINK_TOPICS);
190 * Factory that records the parameters of all the sinks it creates.
192 private static class SinkFactory extends IndexedKafkaTopicSinkFactory {
193 private Deque<BusTopicParams> params = new LinkedList<>();
196 protected KafkaTopicSink makeSink(BusTopicParams busTopicParams) {
197 params.add(busTopicParams);
198 return super.makeSink(busTopicParams);