29816c6e1c566413eaacac32a23f8615a04a6c52
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
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.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;
28
29 import java.util.List;
30 import java.util.Properties;
31 import org.onap.policy.common.endpoints.event.comm.Topic;
32
33 /**
34  * Base class for XxxTopicFactory tests.
35  *
36  * @param <T> type of topic managed by the factory
37  */
38 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
39
40     public static final String SERVER = "my-server";
41     public static final String TOPIC2 = "my-topic-2";
42
43     /**
44      * Initializes a new factory.
45      */
46     protected abstract void initFactory();
47
48     /**
49      * Makes a property builder.
50      *
51      * @return a new property builder
52      */
53     protected abstract TopicPropertyBuilder makePropBuilder();
54
55     /**
56      * Builds a set of topics.
57      *
58      * @param properties the properties used to configure the topics
59      * @return a list of new topics
60      */
61     protected abstract List<T> buildTopics(Properties properties);
62
63     /**
64      * Destroys the factory.
65      */
66     protected abstract void destroyFactory();
67
68     /**
69      * Destroys a topic within the factory.
70      *
71      * @param topic the topic to destroy
72      */
73     protected abstract void destroyTopic(String topic);
74
75     /**
76      * Gets the list of topics from the factory.
77      *
78      * @return the topic inventory
79      */
80     protected abstract List<T> getInventory();
81
82     /**
83      * Gets a topic from the factory.
84      *
85      * @param topic the topic name
86      * @return the topic
87      */
88     protected abstract T getTopic(String topic);
89
90
91     /**
92      * Tests building a topic using varied Properties.
93      */
94     public void testBuildProperties_Variations() {
95         initFactory();
96
97         // null topic list
98         assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
99
100         // empty topic list
101         assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
102
103         // null servers
104         assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
105                         .build()).isEmpty());
106
107         // empty servers
108         assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
109                         .build()).isEmpty());
110     }
111
112     /**
113      * Tests building multiple topics using Properties.
114      */
115     public void testBuildProperties_Multiple() {
116         initFactory();
117
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);
121
122         List<T> lst = buildTopics(builder.build());
123         assertEquals(4, lst.size());
124
125         int index = 0;
126         T item = lst.get(index++);
127         assertTrue(item != lst.get(index++));
128         assertTrue(item == lst.get(index++));
129         assertTrue(item == lst.get(index++));
130     }
131
132     /**
133      * Tests destroy(topic), get(topic), and inventory() methods.
134      */
135     public void testDestroyString_testGet_testInventory() {
136         initFactory();
137
138         List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
139
140         int index = 0;
141         T item1 = lst.get(index++);
142         T item2 = lst.get(index++);
143
144         assertEquals(2, getInventory().size());
145         assertTrue(getInventory().contains(item1));
146         assertTrue(getInventory().contains(item2));
147
148         item1.start();
149         item2.start();
150
151         assertEquals(item1, getTopic(MY_TOPIC));
152         assertEquals(item2, getTopic(TOPIC2));
153
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));
160
161         // repeat
162         destroyTopic(MY_TOPIC);
163         assertFalse(item1.isAlive());
164         assertTrue(item2.isAlive());
165
166         // with other topic
167         destroyTopic(TOPIC2);
168         assertFalse(item1.isAlive());
169         assertFalse(item2.isAlive());
170         assertEquals(0, getInventory().size());
171     }
172
173     /**
174      * Tests exception cases with destroy(topic).
175      */
176     public void testDestroyString_Ex() {
177         // null topic
178         RuntimeException actual = expectException(() -> destroyTopic(null));
179         assertEquals(IllegalArgumentException.class, actual.getClass());
180
181         // empty topic
182         actual = expectException(() -> destroyTopic(""));
183         assertEquals(IllegalArgumentException.class, actual.getClass());
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         RuntimeException actual = expectException(() -> getTopic(null));
214         assertEquals("null topic", IllegalArgumentException.class, actual.getClass());
215
216         // empty topic
217         actual = expectException(() -> getTopic(""));
218         assertEquals("empty topic", IllegalArgumentException.class, actual.getClass());
219
220         // unknown topic
221         initFactory();
222         buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
223
224         actual = expectException(() -> getTopic(TOPIC2));
225         assertEquals("unknown topic", IllegalStateException.class, actual.getClass());
226     }
227
228     /**
229      * Runs a function that is expected to throw an exception. Invokes fail() if the
230      * function does not throw an exception.
231      *
232      * @param function the function to run
233      * @return the exception thrown by the function
234      */
235     public RuntimeException expectException(Runnable function) {
236         try {
237             function.run();
238             fail("missing exception");
239             return null;
240
241         } catch (RuntimeException e) {
242             return e;
243         }
244     }
245 }