2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNotSame;
30 import static org.junit.jupiter.api.Assertions.assertSame;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
33 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
34 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
35 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Properties;
40 import java.util.function.Predicate;
41 import org.onap.policy.common.endpoints.event.comm.Topic;
42 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
45 * Base class for Topic Factory tests that use BusTopicParams.
47 * @param <T> type of topic managed by the factory
49 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
54 * @param params the parameters used to configure the topic
57 protected abstract T buildTopic(BusTopicParams params);
62 * @param servers list of servers
63 * @param topic the topic name
66 protected abstract T buildTopic(List<String> servers, String topic);
69 * Gets the parameters used to build the most recent topic.
71 * @return the most recent topic's parameters
73 protected abstract BusTopicParams getLastParams();
76 * Tests building a topic using BusTopicParams.
78 void testBuildBusTopicParams() {
81 // two unmanaged topics
82 T item = buildTopic(makeBuilder().managed(false).effectiveTopic(null).build());
83 T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
86 assertEquals(item.getTopic(), item.getEffectiveTopic());
87 assertNotEquals(item2.getTopic(), item2.getEffectiveTopic());
88 assertNotSame(item, item2);
90 // duplicate topics, but since they aren't managed, they should be different
91 T item3 = buildTopic(makeBuilder().managed(false).build());
92 T item4 = buildTopic(makeBuilder().managed(false).effectiveTopic(TOPIC2).build());
95 assertEquals(MY_TOPIC, item4.getTopic());
96 assertEquals(TOPIC2, item4.getEffectiveTopic());
97 assertNotSame(item, item3);
98 assertNotSame(item, item4);
99 assertNotSame(item3, item4);
101 // two managed topics
102 T item5 = buildTopic(makeBuilder().build());
103 T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
104 assertNotNull(item5);
105 assertNotNull(item6);
107 // re-build same managed topics - should get exact same objects
108 assertSame(item5, buildTopic(makeBuilder().topic(MY_TOPIC).build()));
109 assertSame(item6, buildTopic(makeBuilder().topic(TOPIC2).build()));
113 * Tests exception cases when building a topic using BusTopicParams.
115 void testBuildBusTopicParams_Ex() {
117 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
120 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
124 * Tests building a topic using a list of servers and a topic.
126 void testBuildListOfStringString() {
129 T item1 = buildTopic(servers, MY_TOPIC);
130 assertNotNull(item1);
132 // check parameters that were used
133 BusTopicParams params = getLastParams();
134 assertEquals(servers, params.getServers());
135 assertEquals(MY_TOPIC, params.getTopic());
136 assertEquals(true, params.isManaged());
137 assertEquals(false, params.isUseHttps());
139 T item2 = buildTopic(servers, TOPIC2);
140 assertNotNull(item2);
141 assertNotSame(item1, item2);
143 // duplicate - should be the same, as these topics are managed
144 T item3 = buildTopic(servers, TOPIC2);
145 assertSame(item2, item3);
149 * Tests building a topic using Properties. Verifies parameters specific to Bus
152 void testBuildProperties() {
155 List<T> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
156 assertEquals(1, topics.size());
157 assertEquals(MY_TOPIC, topics.get(0).getTopic());
158 assertEquals(MY_EFFECTIVE_TOPIC, topics.get(0).getEffectiveTopic());
160 BusTopicParams params = getLastParams();
161 assertEquals(true, params.isManaged());
162 assertEquals(true, params.isUseHttps());
163 assertEquals(true, params.isAllowSelfSignedCerts());
164 assertEquals(MY_API_KEY, params.getApiKey());
165 assertEquals(MY_API_SECRET, params.getApiSecret());
166 assertEquals(Arrays.asList(SERVER), params.getServers());
167 assertEquals(MY_TOPIC, params.getTopic());
168 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
170 List<T> topics2 = buildTopics(makePropBuilder().makeTopic(TOPIC3)
171 .removeTopicProperty(PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX).build());
172 assertEquals(1, topics2.size());
173 assertEquals(TOPIC3, topics2.get(0).getTopic());
174 assertEquals(topics2.get(0).getTopic(), topics2.get(0).getEffectiveTopic());
178 void testBuildProperties_Variations() {
179 super.testBuildProperties_Variations();
181 // check boolean properties that default to true
182 checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
184 // check boolean properties that default to false
185 checkDefault(PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
186 checkDefault(PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, params -> !params.isAllowSelfSignedCerts());
190 * Verifies that a parameter has the correct default, if the original builder property
193 * @param builderName name of the builder property
194 * @param validate function to test the validity of the property
195 * @param values the values to which the property should be set, defaults to
196 * {@code null} and ""
198 protected void checkDefault(String builderName, Predicate<BusTopicParams> validate, Object... values) {
199 Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
201 for (Object value : values2) {
202 // always start with a fresh factory
205 TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
208 builder.removeTopicProperty(builderName);
211 builder.setTopicProperty(builderName, value.toString());
214 assertEquals(1, buildTopics(builder.build()).size(), "size for default " + value);
215 assertTrue(validate.test(getLastParams()), "default for " + value);
220 * Verifies that an "additional" property does not exist, if the original builder
221 * property is not provided.
223 * @param builderName name of the builder property
224 * @param addName name of the "additional" property
226 protected void expectNullAddProp(String builderName, String addName) {
228 // remove the property
230 Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
231 assertEquals(1, buildTopics(props).size());
232 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
235 // repeat, this time using an empty string instead of null
237 props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
238 assertEquals(1, buildTopics(props).size());
239 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));