d8a16428bac0b9ab56d52717b9a2840ad9c16d47
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus;
22
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;
29
30 import java.util.List;
31 import java.util.Properties;
32 import org.onap.policy.common.endpoints.event.comm.Topic;
33
34 /**
35  * Base class for XxxTopicFactory tests.
36  *
37  * @param <T> type of topic managed by the factory
38  */
39 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
40
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";
44
45     /**
46      * Initializes a new factory.
47      */
48     protected abstract void initFactory();
49
50     /**
51      * Makes a property builder.
52      *
53      * @return a new property builder
54      */
55     protected abstract TopicPropertyBuilder makePropBuilder();
56
57     /**
58      * Builds a set of topics.
59      *
60      * @param properties the properties used to configure the topics
61      * @return a list of new topics
62      */
63     protected abstract List<T> buildTopics(Properties properties);
64
65     /**
66      * Destroys the factory.
67      */
68     protected abstract void destroyFactory();
69
70     /**
71      * Destroys a topic within the factory.
72      *
73      * @param topic the topic to destroy
74      */
75     protected abstract void destroyTopic(String topic);
76
77     /**
78      * Gets the list of topics from the factory.
79      *
80      * @return the topic inventory
81      */
82     protected abstract List<T> getInventory();
83
84     /**
85      * Gets a topic from the factory.
86      *
87      * @param topic the topic name
88      * @return the topic
89      */
90     protected abstract T getTopic(String topic);
91
92
93     /**
94      * Tests building a topic using varied Properties.
95      */
96     public void testBuildProperties_Variations() {
97         initFactory();
98
99         // null topic list
100         assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
101
102         // empty topic list
103         assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
104
105         // null servers
106         assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
107                         .build()).isEmpty());
108
109         // empty servers
110         assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
111                         .build()).isEmpty());
112     }
113
114     /**
115      * Tests building multiple topics using Properties.
116      */
117     public void testBuildProperties_Multiple() {
118         initFactory();
119
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);
123
124         List<T> lst = buildTopics(builder.build());
125         assertEquals(4, lst.size());
126
127         int index = 0;
128         T item = lst.get(index++);
129         assertTrue(item != lst.get(index++));
130         assertTrue(item == lst.get(index++));
131         assertTrue(item == lst.get(index++));
132     }
133
134     /**
135      * Tests destroy(topic), get(topic), and inventory() methods.
136      */
137     public void testDestroyString_testGet_testInventory() {
138         initFactory();
139
140         List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
141
142         int index = 0;
143         T item1 = lst.get(index++);
144         T item2 = lst.get(index++);
145
146         assertEquals(2, getInventory().size());
147         assertTrue(getInventory().contains(item1));
148         assertTrue(getInventory().contains(item2));
149
150         item1.start();
151         item2.start();
152
153         assertEquals(item1, getTopic(MY_TOPIC));
154         assertEquals(item2, getTopic(TOPIC2));
155
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));
162
163         // repeat
164         destroyTopic(MY_TOPIC);
165         assertFalse(item1.isAlive());
166         assertTrue(item2.isAlive());
167
168         // with other topic
169         destroyTopic(TOPIC2);
170         assertFalse(item1.isAlive());
171         assertFalse(item2.isAlive());
172         assertEquals(0, getInventory().size());
173     }
174
175     /**
176      * Tests exception cases with destroy(topic).
177      */
178     public void testDestroyString_Ex() {
179         // null topic
180         assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
181
182         // empty topic
183         assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
184     }
185
186     /**
187      * Tests the destroy() method.
188      */
189     public void testDestroy() {
190         initFactory();
191
192         List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
193
194         int index = 0;
195         T item1 = lst.get(index++);
196         T item2 = lst.get(index++);
197
198         item1.start();
199         item2.start();
200
201         destroyFactory();
202
203         assertFalse(item1.isAlive());
204         assertFalse(item2.isAlive());
205         assertEquals(0, getInventory().size());
206     }
207
208     /**
209      * Tests exception cases with get(topic).
210      */
211     public void testGet_Ex() {
212         // null topic
213         assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
214
215         // empty topic
216         assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
217
218         // unknown topic
219         initFactory();
220         buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
221
222         assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));
223     }
224 }