2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNotSame;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
32 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
33 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
34 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Properties;
39 import java.util.function.Predicate;
40 import org.onap.policy.common.endpoints.event.comm.Topic;
41 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
44 * Base class for Topic Factory tests that use BusTopicParams.
46 * @param <T> type of topic managed by the factory
48 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
53 * @param params the parameters used to configure the topic
56 protected abstract T buildTopic(BusTopicParams params);
61 * @param servers list of servers
62 * @param topic the topic name
65 protected abstract T buildTopic(List<String> servers, String topic);
68 * Gets the parameters used to build the most recent topic.
70 * @return the most recent topic's parameters
72 protected abstract BusTopicParams getLastParams();
75 * Tests building a topic using BusTopicParams.
77 public void testBuildBusTopicParams() {
80 // two unmanaged topics
81 T item = buildTopic(makeBuilder().managed(false).effectiveTopic(null).build());
82 T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
85 assertEquals(item.getTopic(), item.getEffectiveTopic());
86 assertNotEquals(item2.getTopic(), item2.getEffectiveTopic());
87 assertNotSame(item, item2);
89 // duplicate topics, but since they aren't managed, they should be different
90 T item3 = buildTopic(makeBuilder().managed(false).build());
91 T item4 = buildTopic(makeBuilder().managed(false).effectiveTopic(TOPIC2).build());
94 assertEquals(MY_TOPIC, item4.getTopic());
95 assertEquals(TOPIC2, item4.getEffectiveTopic());
96 assertNotSame(item, item3);
97 assertNotSame(item, item4);
98 assertNotSame(item3, item4);
100 // two managed topics
101 T item5 = buildTopic(makeBuilder().build());
102 T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
103 assertNotNull(item5);
104 assertNotNull(item6);
106 // re-build same managed topics - should get exact same objects
107 assertSame(item5, buildTopic(makeBuilder().topic(MY_TOPIC).build()));
108 assertSame(item6, buildTopic(makeBuilder().topic(TOPIC2).build()));
112 * Tests exception cases when building a topic using BusTopicParams.
114 public void testBuildBusTopicParams_Ex() {
116 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
119 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
123 * Tests building a topic using a list of servers and a topic.
125 public void testBuildListOfStringString() {
128 T item1 = buildTopic(servers, MY_TOPIC);
129 assertNotNull(item1);
131 // check parameters that were used
132 BusTopicParams params = getLastParams();
133 assertEquals(servers, params.getServers());
134 assertEquals(MY_TOPIC, params.getTopic());
135 assertEquals(true, params.isManaged());
136 assertEquals(false, params.isUseHttps());
138 T item2 = buildTopic(servers, TOPIC2);
139 assertNotNull(item2);
140 assertNotSame(item1, item2);
142 // duplicate - should be the same, as these topics are managed
143 T item3 = buildTopic(servers, TOPIC2);
144 assertSame(item2, item3);
148 * Tests building a topic using Properties. Verifies parameters specific to Bus
151 public void testBuildProperties() {
154 List<T> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
155 assertEquals(1, topics.size());
156 assertEquals(MY_TOPIC, topics.get(0).getTopic());
157 assertEquals(MY_EFFECTIVE_TOPIC, topics.get(0).getEffectiveTopic());
159 BusTopicParams params = getLastParams();
160 assertEquals(true, params.isManaged());
161 assertEquals(true, params.isUseHttps());
162 assertEquals(true, params.isAllowSelfSignedCerts());
163 assertEquals(MY_API_KEY, params.getApiKey());
164 assertEquals(MY_API_SECRET, params.getApiSecret());
165 assertEquals(Arrays.asList(SERVER), params.getServers());
166 assertEquals(MY_TOPIC, params.getTopic());
167 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
169 List<T> topics2 = buildTopics(makePropBuilder().makeTopic(TOPIC3)
170 .removeTopicProperty(PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX).build());
171 assertEquals(1, topics2.size());
172 assertEquals(TOPIC3, topics2.get(0).getTopic());
173 assertEquals(topics2.get(0).getTopic(), topics2.get(0).getEffectiveTopic());
177 public void testBuildProperties_Variations() {
178 super.testBuildProperties_Variations();
180 // check boolean properties that default to true
181 checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
183 // check boolean properties that default to false
184 checkDefault(PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
185 checkDefault(PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, params -> !params.isAllowSelfSignedCerts());
189 * Verifies that a parameter has the correct default, if the original builder property
192 * @param builderName name of the builder property
193 * @param validate function to test the validity of the property
194 * @param values the values to which the property should be set, defaults to
195 * {@code null} and ""
197 protected void checkDefault(String builderName, Predicate<BusTopicParams> validate, Object... values) {
198 Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
200 for (Object value : values2) {
201 // always start with a fresh factory
204 TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
207 builder.removeTopicProperty(builderName);
210 builder.setTopicProperty(builderName, value.toString());
213 assertEquals("size for default " + value, 1, buildTopics(builder.build()).size());
214 assertTrue("default for " + value, validate.test(getLastParams()));
219 * Verifies that an "additional" property does not exist, if the original builder
220 * property is not provided.
222 * @param builderName name of the builder property
223 * @param addName name of the "additional" property
225 protected void expectNullAddProp(String builderName, String addName) {
227 // remove the property
229 Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
230 assertEquals(1, buildTopics(props).size());
231 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
234 // repeat, this time using an empty string instead of null
236 props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
237 assertEquals(1, buildTopics(props).size());
238 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));