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