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