2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
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.assertTrue;
26 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_KAFKA_SOURCE_TOPICS;
28 import java.util.Deque;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Properties;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
37 class KafkaTopicSourceFactoryTest extends KafkaTopicFactoryTestBase<KafkaTopicSource> {
39 private SourceFactory factory;
41 public static final String KAFKA_SERVER = "localhost:9092";
44 * Creates the object to be tested.
51 factory = new SourceFactory();
55 public void tearDown() {
61 void testBuildProperties() {
65 List<KafkaTopicSource> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
66 assertEquals(1, topics.size());
67 assertEquals(MY_TOPIC, topics.get(0).getTopic());
68 assertEquals(MY_EFFECTIVE_TOPIC, topics.get(0).getEffectiveTopic());
70 BusTopicParams params = getLastParams();
71 assertTrue(params.isManaged());
72 assertFalse(params.isUseHttps());
73 assertEquals(List.of(KAFKA_SERVER), params.getServers());
74 assertEquals(MY_TOPIC, params.getTopic());
75 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
80 void testDestroyString_testGet_testInventory() {
81 super.testDestroyString_testGet_testInventory();
82 super.testDestroyString_Ex();
98 assertTrue(factory.toString().startsWith("IndexedKafkaTopicSourceFactory ["));
102 protected void initFactory() {
103 if (factory != null) {
107 factory = new SourceFactory();
111 protected List<KafkaTopicSource> buildTopics(Properties properties) {
112 return factory.build(properties);
116 protected KafkaTopicSource buildTopic(BusTopicParams params) {
117 return factory.build(params);
121 protected KafkaTopicSource buildTopic(List<String> servers, String topic) {
122 return factory.build(servers, topic);
126 protected void destroyFactory() {
131 protected void destroyTopic(String topic) {
132 factory.destroy(topic);
136 protected List<KafkaTopicSource> getInventory() {
137 return factory.inventory();
141 protected KafkaTopicSource getTopic(String topic) {
142 return factory.get(topic);
146 protected BusTopicParams getLastParams() {
147 return factory.params.getLast();
151 protected TopicPropertyBuilder makePropBuilder() {
152 return new KafkaTopicPropertyBuilder(PROPERTY_KAFKA_SOURCE_TOPICS);
156 * Factory that records the parameters of all the sources it creates.
158 private static class SourceFactory extends IndexedKafkaTopicSourceFactory {
159 private Deque<BusTopicParams> params = new LinkedList<>();
162 protected KafkaTopicSource makeSource(BusTopicParams busTopicParams) {
163 params.add(busTopicParams);
164 return super.makeSource(busTopicParams);