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";
43 public static final String TOPIC3 = "my-topic-3";
46 * Initializes a new factory.
48 protected abstract void initFactory();
51 * Makes a property builder.
53 * @return a new property builder
55 protected abstract TopicPropertyBuilder makePropBuilder();
58 * Builds a set of topics.
60 * @param properties the properties used to configure the topics
61 * @return a list of new topics
63 protected abstract List<T> buildTopics(Properties properties);
66 * Destroys the factory.
68 protected abstract void destroyFactory();
71 * Destroys a topic within the factory.
73 * @param topic the topic to destroy
75 protected abstract void destroyTopic(String topic);
78 * Gets the list of topics from the factory.
80 * @return the topic inventory
82 protected abstract List<T> getInventory();
85 * Gets a topic from the factory.
87 * @param topic the topic name
90 protected abstract T getTopic(String topic);
94 * Tests building a topic using varied Properties.
96 public void testBuildProperties_Variations() {
100 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
103 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
106 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
107 .build()).isEmpty());
110 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
111 .build()).isEmpty());
115 * Tests building multiple topics using Properties.
117 public void testBuildProperties_Multiple() {
120 // make two fully-defined topics, and add two duplicate topic names to the list
121 TopicPropertyBuilder builder =
122 makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
124 List<T> lst = buildTopics(builder.build());
125 assertEquals(4, lst.size());
128 T item = lst.get(index++);
129 assertTrue(item != lst.get(index++));
130 assertTrue(item == lst.get(index++));
131 assertTrue(item == lst.get(index++));
135 * Tests destroy(topic), get(topic), and inventory() methods.
137 public void testDestroyString_testGet_testInventory() {
140 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
143 T item1 = lst.get(index++);
144 T item2 = lst.get(index++);
146 assertEquals(2, getInventory().size());
147 assertTrue(getInventory().contains(item1));
148 assertTrue(getInventory().contains(item2));
153 assertEquals(item1, getTopic(MY_TOPIC));
154 assertEquals(item2, getTopic(TOPIC2));
156 destroyTopic(MY_TOPIC);
157 assertFalse(item1.isAlive());
158 assertTrue(item2.isAlive());
159 assertEquals(item2, getTopic(TOPIC2));
160 assertEquals(1, getInventory().size());
161 assertTrue(getInventory().contains(item2));
164 destroyTopic(MY_TOPIC);
165 assertFalse(item1.isAlive());
166 assertTrue(item2.isAlive());
169 destroyTopic(TOPIC2);
170 assertFalse(item1.isAlive());
171 assertFalse(item2.isAlive());
172 assertEquals(0, getInventory().size());
176 * Tests exception cases with destroy(topic).
178 public void testDestroyString_Ex() {
180 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
183 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
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 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
216 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
220 buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
222 assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));