2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
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.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
29 import java.util.List;
30 import java.util.Properties;
31 import org.onap.policy.common.endpoints.event.comm.Topic;
34 * Base class for XxxTopicFactory tests.
36 * @param <T> type of topic managed by the factory
38 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
40 public static final String SERVER = "my-server";
41 public static final String TOPIC2 = "my-topic-2";
44 * Initializes a new factory.
46 protected abstract void initFactory();
49 * Makes a property builder.
51 * @return a new property builder
53 protected abstract TopicPropertyBuilder makePropBuilder();
56 * Builds a set of topics.
58 * @param properties the properties used to configure the topics
59 * @return a list of new topics
61 protected abstract List<T> buildTopics(Properties properties);
64 * Destroys the factory.
66 protected abstract void destroyFactory();
69 * Destroys a topic within the factory.
71 * @param topic the topic to destroy
73 protected abstract void destroyTopic(String topic);
76 * Gets the list of topics from the factory.
78 * @return the topic inventory
80 protected abstract List<T> getInventory();
83 * Gets a topic from the factory.
85 * @param topic the topic name
88 protected abstract T getTopic(String topic);
92 * Tests building a topic using varied Properties.
94 public void testBuildProperties_Variations() {
98 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
101 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
104 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
105 .build()).isEmpty());
108 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
109 .build()).isEmpty());
113 * Tests building multiple topics using Properties.
115 public void testBuildProperties_Multiple() {
118 // make two fully-defined topics, and add two duplicate topic names to the list
119 TopicPropertyBuilder builder =
120 makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
122 List<T> lst = buildTopics(builder.build());
123 assertEquals(4, lst.size());
126 T item = lst.get(index++);
127 assertTrue(item != lst.get(index++));
128 assertTrue(item == lst.get(index++));
129 assertTrue(item == lst.get(index++));
133 * Tests destroy(topic), get(topic), and inventory() methods.
135 public void testDestroyString_testGet_testInventory() {
138 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
141 T item1 = lst.get(index++);
142 T item2 = lst.get(index++);
144 assertEquals(2, getInventory().size());
145 assertTrue(getInventory().contains(item1));
146 assertTrue(getInventory().contains(item2));
151 assertEquals(item1, getTopic(MY_TOPIC));
152 assertEquals(item2, getTopic(TOPIC2));
154 destroyTopic(MY_TOPIC);
155 assertFalse(item1.isAlive());
156 assertTrue(item2.isAlive());
157 assertEquals(item2, getTopic(TOPIC2));
158 assertEquals(1, getInventory().size());
159 assertTrue(getInventory().contains(item2));
162 destroyTopic(MY_TOPIC);
163 assertFalse(item1.isAlive());
164 assertTrue(item2.isAlive());
167 destroyTopic(TOPIC2);
168 assertFalse(item1.isAlive());
169 assertFalse(item2.isAlive());
170 assertEquals(0, getInventory().size());
174 * Tests exception cases with destroy(topic).
176 public void testDestroyString_Ex() {
178 RuntimeException actual = expectException(() -> destroyTopic(null));
179 assertEquals(IllegalArgumentException.class, actual.getClass());
182 actual = expectException(() -> destroyTopic(""));
183 assertEquals(IllegalArgumentException.class, actual.getClass());
187 * Tests the destroy() method.
189 public void testDestroy() {
192 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
195 T item1 = lst.get(index++);
196 T item2 = lst.get(index++);
203 assertFalse(item1.isAlive());
204 assertFalse(item2.isAlive());
205 assertEquals(0, getInventory().size());
209 * Tests exception cases with get(topic).
211 public void testGet_Ex() {
213 RuntimeException actual = expectException(() -> getTopic(null));
214 assertEquals("null topic", IllegalArgumentException.class, actual.getClass());
217 actual = expectException(() -> getTopic(""));
218 assertEquals("empty topic", IllegalArgumentException.class, actual.getClass());
222 buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
224 actual = expectException(() -> getTopic(TOPIC2));
225 assertEquals("unknown topic", IllegalStateException.class, actual.getClass());
229 * Runs a function that is expected to throw an exception. Invokes fail() if the
230 * function does not throw an exception.
232 * @param function the function to run
233 * @return the exception thrown by the function
235 public RuntimeException expectException(Runnable function) {
238 fail("missing exception");
241 } catch (RuntimeException e) {