0efbf3c2756c1244d33e3700182d6c84593c09b5
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotSame;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
32
33 import java.util.List;
34 import java.util.Properties;
35 import org.onap.policy.common.endpoints.event.comm.Topic;
36
37 /**
38  * Base class for XxxTopicFactory tests.
39  *
40  * @param <T> type of topic managed by the factory
41  */
42 public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBase {
43
44     public static final String SERVER = "my-server";
45     public static final String TOPIC2 = "my-topic-2";
46     public static final String TOPIC3 = "my-topic-3";
47
48     /**
49      * Initializes a new factory.
50      */
51     protected abstract void initFactory();
52
53     /**
54      * Makes a property builder.
55      *
56      * @return a new property builder
57      */
58     protected abstract TopicPropertyBuilder makePropBuilder();
59
60     /**
61      * Builds a set of topics.
62      *
63      * @param properties the properties used to configure the topics
64      * @return a list of new topics
65      */
66     protected abstract List<T> buildTopics(Properties properties);
67
68     /**
69      * Destroys the factory.
70      */
71     protected abstract void destroyFactory();
72
73     /**
74      * Destroys a topic within the factory.
75      *
76      * @param topic the topic to destroy
77      */
78     protected abstract void destroyTopic(String topic);
79
80     /**
81      * Gets the list of topics from the factory.
82      *
83      * @return the topic inventory
84      */
85     protected abstract List<T> getInventory();
86
87     /**
88      * Gets a topic from the factory.
89      *
90      * @param topic the topic name
91      * @return the topic
92      */
93     protected abstract T getTopic(String topic);
94
95
96     /**
97      * Tests building a topic using varied Properties.
98      */
99     void testBuildProperties_Variations() {
100         initFactory();
101
102         // null topic list
103         assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
104
105         // empty topic list
106         assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
107
108         // null servers
109         assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX)
110                         .build()).isEmpty());
111
112         // empty servers
113         assertTrue(buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
114                         .build()).isEmpty());
115     }
116
117     /**
118      * Tests building multiple topics using Properties.
119      */
120     void testBuildProperties_Multiple() {
121         initFactory();
122
123         // make two fully-defined topics, and add two duplicate topic names to the list
124         TopicPropertyBuilder builder =
125                         makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
126
127         List<T> lst = buildTopics(builder.build());
128         assertEquals(4, lst.size());
129
130         int index = 0;
131         T item = lst.get(index++);
132         assertNotSame(item, lst.get(index++));
133         assertSame(item, lst.get(index++));
134         assertSame(item, lst.get(index++));
135     }
136
137     /**
138      * Tests destroy(topic), get(topic), and inventory() methods.
139      */
140     void testDestroyString_testGet_testInventory() {
141         initFactory();
142
143         List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
144
145         int index = 0;
146         T item1 = lst.get(index++);
147         T item2 = lst.get(index++);
148
149         assertEquals(2, getInventory().size());
150         assertTrue(getInventory().contains(item1));
151         assertTrue(getInventory().contains(item2));
152
153         item1.start();
154         item2.start();
155
156         assertEquals(item1, getTopic(MY_TOPIC));
157         assertEquals(item2, getTopic(TOPIC2));
158
159         destroyTopic(MY_TOPIC);
160         assertFalse(item1.isAlive());
161         assertTrue(item2.isAlive());
162         assertEquals(item2, getTopic(TOPIC2));
163         assertEquals(1, getInventory().size());
164         assertTrue(getInventory().contains(item2));
165
166         // repeat
167         destroyTopic(MY_TOPIC);
168         assertFalse(item1.isAlive());
169         assertTrue(item2.isAlive());
170
171         // with other topic
172         destroyTopic(TOPIC2);
173         assertFalse(item1.isAlive());
174         assertFalse(item2.isAlive());
175         assertEquals(0, getInventory().size());
176     }
177
178     /**
179      * Tests exception cases with destroy(topic).
180      */
181     void testDestroyString_Ex() {
182         // null topic
183         assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
184
185         // empty topic
186         assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
187     }
188
189     /**
190      * Tests the destroy() method.
191      */
192     void testDestroy() {
193         initFactory();
194
195         List<T> lst = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
196
197         int index = 0;
198         T item1 = lst.get(index++);
199         T item2 = lst.get(index++);
200
201         item1.start();
202         item2.start();
203
204         destroyFactory();
205
206         assertFalse(item1.isAlive());
207         assertFalse(item2.isAlive());
208         assertEquals(0, getInventory().size());
209     }
210
211     /**
212      * Tests exception cases with get(topic).
213      */
214     void testGet_Ex() {
215         // null topic
216         assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
217
218         // empty topic
219         assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
220
221         // unknown topic
222         initFactory();
223         buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
224
225         assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));
226     }
227 }