2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotSame;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
33 import java.util.List;
34 import java.util.Properties;
35 import org.onap.policy.common.endpoints.event.comm.Topic;
38 * Base class for XxxTopicFactory tests.
40 * @param <T> type of topic managed by the factory
42 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
44 public static final String SERVER = "my-server";
45 public static final String TOPIC2 = "my-topic-2";
46 public static final String TOPIC3 = "my-topic-3";
49 * Initializes a new factory.
51 protected abstract void initFactory();
54 * Makes a property builder.
56 * @return a new property builder
58 protected abstract TopicPropertyBuilder makePropBuilder();
61 * Builds a set of topics.
63 * @param properties the properties used to configure the topics
64 * @return a list of new topics
66 protected abstract List<T> buildTopics(Properties properties);
69 * Destroys the factory.
71 protected abstract void destroyFactory();
74 * Destroys a topic within the factory.
76 * @param topic the topic to destroy
78 protected abstract void destroyTopic(String topic);
81 * Gets the list of topics from the factory.
83 * @return the topic inventory
85 protected abstract List<T> getInventory();
88 * Gets a topic from the factory.
90 * @param topic the topic name
93 protected abstract T getTopic(String topic);
97 * Tests building a topic using varied Properties.
99 void testBuildProperties_Variations() {
103 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
106 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
109 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
110 .build()).isEmpty());
113 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
114 .build()).isEmpty());
118 * Tests building multiple topics using Properties.
120 void testBuildProperties_Multiple() {
123 // make two fully-defined topics, and add two duplicate topic names to the list
124 TopicPropertyBuilder builder =
125 makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
127 List<T> lst = buildTopics(builder.build());
128 assertEquals(4, lst.size());
131 T item = lst.get(index++);
132 assertNotSame(item, lst.get(index++));
133 assertSame(item, lst.get(index++));
134 assertSame(item, lst.get(index++));
138 * Tests destroy(topic), get(topic), and inventory() methods.
140 void testDestroyString_testGet_testInventory() {
143 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
146 T item1 = lst.get(index++);
147 T item2 = lst.get(index++);
149 assertEquals(2, getInventory().size());
150 assertTrue(getInventory().contains(item1));
151 assertTrue(getInventory().contains(item2));
156 assertEquals(item1, getTopic(MY_TOPIC));
157 assertEquals(item2, getTopic(TOPIC2));
159 destroyTopic(MY_TOPIC);
160 assertFalse(item1.isAlive());
161 assertTrue(item2.isAlive());
162 assertEquals(item2, getTopic(TOPIC2));
163 assertEquals(1, getInventory().size());
164 assertTrue(getInventory().contains(item2));
167 destroyTopic(MY_TOPIC);
168 assertFalse(item1.isAlive());
169 assertTrue(item2.isAlive());
172 destroyTopic(TOPIC2);
173 assertFalse(item1.isAlive());
174 assertFalse(item2.isAlive());
175 assertEquals(0, getInventory().size());
179 * Tests exception cases with destroy(topic).
181 void testDestroyString_Ex() {
183 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
186 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
190 * Tests the destroy() method.
195 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
198 T item1 = lst.get(index++);
199 T item2 = lst.get(index++);
206 assertFalse(item1.isAlive());
207 assertFalse(item2.isAlive());
208 assertEquals(0, getInventory().size());
212 * Tests exception cases with get(topic).
216 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
219 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
223 buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
225 assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));