2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
30 import java.util.List;
31 import java.util.Properties;
32 import org.onap.policy.common.endpoints.event.comm.Topic;
35 * Base class for XxxTopicFactory tests.
37 * @param <T> type of topic managed by the factory
39 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
41 public static final String SERVER = "my-server";
42 public static final String TOPIC2 = "my-topic-2";
45 * Initializes a new factory.
47 protected abstract void initFactory();
50 * Makes a property builder.
52 * @return a new property builder
54 protected abstract TopicPropertyBuilder makePropBuilder();
57 * Builds a set of topics.
59 * @param properties the properties used to configure the topics
60 * @return a list of new topics
62 protected abstract List<T> buildTopics(Properties properties);
65 * Destroys the factory.
67 protected abstract void destroyFactory();
70 * Destroys a topic within the factory.
72 * @param topic the topic to destroy
74 protected abstract void destroyTopic(String topic);
77 * Gets the list of topics from the factory.
79 * @return the topic inventory
81 protected abstract List<T> getInventory();
84 * Gets a topic from the factory.
86 * @param topic the topic name
89 protected abstract T getTopic(String topic);
93 * Tests building a topic using varied Properties.
95 public void testBuildProperties_Variations() {
99 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
102 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
105 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
106 .build()).isEmpty());
109 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
110 .build()).isEmpty());
114 * Tests building multiple topics using Properties.
116 public void testBuildProperties_Multiple() {
119 // make two fully-defined topics, and add two duplicate topic names to the list
120 TopicPropertyBuilder builder =
121 makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
123 List<T> lst = buildTopics(builder.build());
124 assertEquals(4, lst.size());
127 T item = lst.get(index++);
128 assertTrue(item != lst.get(index++));
129 assertTrue(item == lst.get(index++));
130 assertTrue(item == lst.get(index++));
134 * Tests destroy(topic), get(topic), and inventory() methods.
136 public void testDestroyString_testGet_testInventory() {
139 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
142 T item1 = lst.get(index++);
143 T item2 = lst.get(index++);
145 assertEquals(2, getInventory().size());
146 assertTrue(getInventory().contains(item1));
147 assertTrue(getInventory().contains(item2));
152 assertEquals(item1, getTopic(MY_TOPIC));
153 assertEquals(item2, getTopic(TOPIC2));
155 destroyTopic(MY_TOPIC);
156 assertFalse(item1.isAlive());
157 assertTrue(item2.isAlive());
158 assertEquals(item2, getTopic(TOPIC2));
159 assertEquals(1, getInventory().size());
160 assertTrue(getInventory().contains(item2));
163 destroyTopic(MY_TOPIC);
164 assertFalse(item1.isAlive());
165 assertTrue(item2.isAlive());
168 destroyTopic(TOPIC2);
169 assertFalse(item1.isAlive());
170 assertFalse(item2.isAlive());
171 assertEquals(0, getInventory().size());
175 * Tests exception cases with destroy(topic).
177 public void testDestroyString_Ex() {
179 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
182 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
186 * Tests the destroy() method.
188 public void testDestroy() {
191 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
194 T item1 = lst.get(index++);
195 T item2 = lst.get(index++);
202 assertFalse(item1.isAlive());
203 assertFalse(item2.isAlive());
204 assertEquals(0, getInventory().size());
208 * Tests exception cases with get(topic).
210 public void testGet_Ex() {
212 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
215 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
219 buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
221 assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));