2 * ============LICENSE_START=======================================================
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
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.assertTrue;
29 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
30 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
31 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
32 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Properties;
37 import java.util.function.Predicate;
38 import org.onap.policy.common.endpoints.event.comm.Topic;
39 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
42 * Base class for Topic Factory tests that use BusTopicParams.
44 * @param <T> type of topic managed by the factory
46 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
51 * @param params the parameters used to configure the topic
54 protected abstract T buildTopic(BusTopicParams params);
59 * @param servers list of servers
60 * @param topic the topic name
63 protected abstract T buildTopic(List<String> servers, String topic);
66 * Gets the parameters used to build the most recent topic.
68 * @return the most recent topic's parameters
70 protected abstract BusTopicParams getLastParams();
73 * Tests building a topic using BusTopicParams.
75 public void testBuildBusTopicParams() {
78 // two unmanaged topics
79 T item = buildTopic(makeBuilder().managed(false).effectiveTopic(null).build());
80 T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
83 assertEquals(item.getTopic(), item.getEffectiveTopic());
84 assertNotEquals(item2.getTopic(), item2.getEffectiveTopic());
85 assertTrue(item != item2);
87 // duplicate topics, but since they aren't managed, they should be different
88 T item3 = buildTopic(makeBuilder().managed(false).build());
89 T item4 = buildTopic(makeBuilder().managed(false).effectiveTopic(TOPIC2).build());
92 assertEquals(MY_TOPIC, item4.getTopic());
93 assertEquals(TOPIC2, item4.getEffectiveTopic());
94 assertTrue(item != item3);
95 assertTrue(item != item4);
96 assertTrue(item3 != item4);
99 T item5 = buildTopic(makeBuilder().build());
100 T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
101 assertNotNull(item5);
102 assertNotNull(item6);
104 // re-build same managed topics - should get exact same objects
105 assertTrue(item5 == buildTopic(makeBuilder().topic(MY_TOPIC).build()));
106 assertTrue(item6 == buildTopic(makeBuilder().topic(TOPIC2).build()));
110 * Tests exception cases when building a topic using BusTopicParams.
112 public void testBuildBusTopicParams_Ex() {
114 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
117 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
121 * Tests building a topic using a list of servers and a topic.
123 public void testBuildListOfStringString() {
126 T item1 = buildTopic(servers, MY_TOPIC);
127 assertNotNull(item1);
129 // check parameters that were used
130 BusTopicParams params = getLastParams();
131 assertEquals(servers, params.getServers());
132 assertEquals(MY_TOPIC, params.getTopic());
133 assertEquals(true, params.isManaged());
134 assertEquals(false, params.isUseHttps());
136 T item2 = buildTopic(servers, TOPIC2);
137 assertNotNull(item2);
138 assertTrue(item1 != item2);
140 // duplicate - should be the same, as these topics are managed
141 T item3 = buildTopic(servers, TOPIC2);
142 assertTrue(item2 == item3);
146 * Tests building a topic using Properties. Verifies parameters specific to Bus
149 public void testBuildProperties() {
152 List<T> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
153 assertEquals(1, topics.size());
154 assertEquals(MY_TOPIC, topics.get(0).getTopic());
155 assertEquals(MY_EFFECTIVE_TOPIC, topics.get(0).getEffectiveTopic());
157 BusTopicParams params = getLastParams();
158 assertEquals(true, params.isManaged());
159 assertEquals(true, params.isUseHttps());
160 assertEquals(true, params.isAllowSelfSignedCerts());
161 assertEquals(MY_API_KEY, params.getApiKey());
162 assertEquals(MY_API_SECRET, params.getApiSecret());
163 assertEquals(Arrays.asList(SERVER), params.getServers());
164 assertEquals(MY_TOPIC, params.getTopic());
165 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
167 List<T> topics2 = buildTopics(makePropBuilder().makeTopic(TOPIC3)
168 .removeTopicProperty(PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX).build());
169 assertEquals(1, topics2.size());
170 assertEquals(TOPIC3, topics2.get(0).getTopic());
171 assertEquals(topics2.get(0).getTopic(), topics2.get(0).getEffectiveTopic());
175 public void testBuildProperties_Variations() {
176 super.testBuildProperties_Variations();
178 // check boolean properties that default to true
179 checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
181 // check boolean properties that default to false
182 checkDefault(PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
183 checkDefault(PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, params -> !params.isAllowSelfSignedCerts());
187 * Verifies that a parameter has the correct default, if the original builder property
190 * @param builderName name of the builder property
191 * @param validate function to test the validity of the property
192 * @param values the values to which the property should be set, defaults to
193 * {@code null} and ""
195 protected void checkDefault(String builderName, Predicate<BusTopicParams> validate, Object... values) {
196 Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
198 for (Object value : values2) {
199 // always start with a fresh factory
202 TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
205 builder.removeTopicProperty(builderName);
208 builder.setTopicProperty(builderName, value.toString());
211 assertEquals("size for default " + value, 1, buildTopics(builder.build()).size());
212 assertTrue("default for " + value, validate.test(getLastParams()));
217 * Verifies that an "additional" property does not exist, if the original builder
218 * property is not provided.
220 * @param builderName name of the builder property
221 * @param addName name of the "additional" property
223 protected void expectNullAddProp(String builderName, String addName) {
225 // remove the property
227 Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
228 assertEquals(1, buildTopics(props).size());
229 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
232 // repeat, this time using an empty string instead of null
234 props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
235 assertEquals(1, buildTopics(props).size());
236 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));