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.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
29 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
30 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Properties;
35 import java.util.function.Function;
36 import org.onap.policy.common.endpoints.event.comm.Topic;
37 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
40 * Base class for Topic Factory tests that use BusTopicParams.
42 * @param <T> type of topic managed by the factory
44 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
49 * @param params the parameters used to configure the topic
52 protected abstract T buildTopic(BusTopicParams params);
57 * @param servers list of servers
58 * @param topic the topic name
61 protected abstract T buildTopic(List<String> servers, String topic);
64 * Gets the parameters used to build the most recent topic.
66 * @return the most recent topic's parameters
68 protected abstract BusTopicParams getLastParams();
71 * Tests building a topic using BusTopicParams.
73 public void testBuildBusTopicParams() {
76 // two unmanaged topics
77 T item = buildTopic(makeBuilder().managed(false).build());
78 T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
81 assertTrue(item != item2);
83 // duplicate topics, but since they aren't managed, they should be different
84 T item3 = buildTopic(makeBuilder().managed(false).build());
85 T item4 = buildTopic(makeBuilder().managed(false).build());
88 assertTrue(item != item3);
89 assertTrue(item != item4);
90 assertTrue(item3 != item4);
93 T item5 = buildTopic(makeBuilder().build());
94 T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
98 // re-build same managed topics - should get exact same objects
99 assertTrue(item5 == buildTopic(makeBuilder().topic(MY_TOPIC).build()));
100 assertTrue(item6 == buildTopic(makeBuilder().topic(TOPIC2).build()));
104 * Tests exception cases when building a topic using BusTopicParams.
106 public void testBuildBusTopicParams_Ex() {
108 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
111 assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
115 * Tests building a topic using a list of servers and a topic.
117 public void testBuildListOfStringString() {
120 T item1 = buildTopic(servers, MY_TOPIC);
121 assertNotNull(item1);
123 // check parameters that were used
124 BusTopicParams params = getLastParams();
125 assertEquals(servers, params.getServers());
126 assertEquals(MY_TOPIC, params.getTopic());
127 assertEquals(true, params.isManaged());
128 assertEquals(false, params.isUseHttps());
130 T item2 = buildTopic(servers, TOPIC2);
131 assertNotNull(item2);
132 assertTrue(item1 != item2);
134 // duplicate - should be the same, as these topics are managed
135 T item3 = buildTopic(servers, TOPIC2);
136 assertTrue(item2 == item3);
140 * Tests building a topic using Properties. Verifies parameters specific to Bus
143 public void testBuildProperties() {
146 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
148 BusTopicParams params = getLastParams();
149 assertEquals(true, params.isManaged());
150 assertEquals(true, params.isUseHttps());
151 assertEquals(true, params.isAllowSelfSignedCerts());
152 assertEquals(MY_API_KEY, params.getApiKey());
153 assertEquals(MY_API_SECRET, params.getApiSecret());
154 assertEquals(Arrays.asList(SERVER), params.getServers());
155 assertEquals(MY_TOPIC, params.getTopic());
159 public void testBuildProperties_Variations() {
160 super.testBuildProperties_Variations();
162 // check boolean properties that default to true
163 checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
165 // check boolean properties that default to false
166 checkDefault(PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
167 checkDefault(PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, params -> !params.isAllowSelfSignedCerts());
171 * Verifies that a parameter has the correct default, if the original builder property
174 * @param builderName name of the builder property
175 * @param validate function to test the validity of the property
176 * @param values the values to which the property should be set, defaults to
177 * {@code null} and ""
179 protected void checkDefault(String builderName, Function<BusTopicParams, Boolean> validate, Object... values) {
180 Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
182 for (Object value : values2) {
183 // always start with a fresh factory
186 TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
189 builder.removeTopicProperty(builderName);
192 builder.setTopicProperty(builderName, value.toString());
195 assertEquals("size for default " + value, 1, buildTopics(builder.build()).size());
196 assertTrue("default for " + value, validate.apply(getLastParams()));
201 * Verifies that an "additional" property does not exist, if the original builder
202 * property is not provided.
204 * @param builderName name of the builder property
205 * @param addName name of the "additional" property
207 protected void expectNullAddProp(String builderName, String addName) {
209 // remove the property
211 Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
212 assertEquals(1, buildTopics(props).size());
213 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
216 // repeat, this time using an empty string instead of null
218 props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
219 assertEquals(1, buildTopics(props).size());
220 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));