2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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.assertNotSame;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
32 import java.util.List;
33 import java.util.Properties;
34 import org.onap.policy.common.endpoints.event.comm.Topic;
37 * Base class for XxxTopicFactory tests.
39 * @param <T> type of topic managed by the factory
41 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
43 public static final String SERVER = "my-server";
44 public static final String TOPIC2 = "my-topic-2";
45 public static final String TOPIC3 = "my-topic-3";
48 * Initializes a new factory.
50 protected abstract void initFactory();
53 * Makes a property builder.
55 * @return a new property builder
57 protected abstract TopicPropertyBuilder makePropBuilder();
60 * Builds a set of topics.
62 * @param properties the properties used to configure the topics
63 * @return a list of new topics
65 protected abstract List<T> buildTopics(Properties properties);
68 * Destroys the factory.
70 protected abstract void destroyFactory();
73 * Destroys a topic within the factory.
75 * @param topic the topic to destroy
77 protected abstract void destroyTopic(String topic);
80 * Gets the list of topics from the factory.
82 * @return the topic inventory
84 protected abstract List<T> getInventory();
87 * Gets a topic from the factory.
89 * @param topic the topic name
92 protected abstract T getTopic(String topic);
96 * Tests building a topic using varied Properties.
98 public void testBuildProperties_Variations() {
102 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
105 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
108 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
109 .build()).isEmpty());
112 assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
113 .build()).isEmpty());
117 * Tests building multiple topics using Properties.
119 public void testBuildProperties_Multiple() {
122 // make two fully-defined topics, and add two duplicate topic names to the list
123 TopicPropertyBuilder builder =
124 makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
126 List<T> lst = buildTopics(builder.build());
127 assertEquals(4, lst.size());
130 T item = lst.get(index++);
131 assertNotSame(item, lst.get(index++));
132 assertSame(item, lst.get(index++));
133 assertSame(item, lst.get(index++));
137 * Tests destroy(topic), get(topic), and inventory() methods.
139 public void testDestroyString_testGet_testInventory() {
142 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
145 T item1 = lst.get(index++);
146 T item2 = lst.get(index++);
148 assertEquals(2, getInventory().size());
149 assertTrue(getInventory().contains(item1));
150 assertTrue(getInventory().contains(item2));
155 assertEquals(item1, getTopic(MY_TOPIC));
156 assertEquals(item2, getTopic(TOPIC2));
158 destroyTopic(MY_TOPIC);
159 assertFalse(item1.isAlive());
160 assertTrue(item2.isAlive());
161 assertEquals(item2, getTopic(TOPIC2));
162 assertEquals(1, getInventory().size());
163 assertTrue(getInventory().contains(item2));
166 destroyTopic(MY_TOPIC);
167 assertFalse(item1.isAlive());
168 assertTrue(item2.isAlive());
171 destroyTopic(TOPIC2);
172 assertFalse(item1.isAlive());
173 assertFalse(item2.isAlive());
174 assertEquals(0, getInventory().size());
178 * Tests exception cases with destroy(topic).
180 public void testDestroyString_Ex() {
182 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
185 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
189 * Tests the destroy() method.
191 public void testDestroy() {
194 List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
197 T item1 = lst.get(index++);
198 T item2 = lst.get(index++);
205 assertFalse(item1.isAlive());
206 assertFalse(item2.isAlive());
207 assertEquals(0, getInventory().size());
211 * Tests exception cases with get(topic).
213 public void testGet_Ex() {
215 assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
218 assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
222 buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
224 assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));