2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
29 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.function.Function;
35 import org.onap.policy.common.endpoints.event.comm.Topic;
36 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
39 * Base class for Topic Factory tests that use BusTopicParams.
41 * @param <T> type of topic managed by the factory
43 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
48 * @param params the parameters used to configure the topic
51 protected abstract T buildTopic(BusTopicParams params);
56 * @param servers list of servers
57 * @param topic the topic name
60 protected abstract T buildTopic(List<String> servers, String topic);
63 * Gets the parameters used to build the most recent topic.
65 * @return the most recent topic's parameters
67 protected abstract BusTopicParams getLastParams();
70 * Tests building a topic using BusTopicParams.
72 public void testBuildBusTopicParams() {
75 // two unmanaged topics
76 T item = buildTopic(makeBuilder().managed(false).build());
77 T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
80 assertTrue(item != item2);
82 // duplicate topics, but since they aren't managed, they should be different
83 T item3 = buildTopic(makeBuilder().managed(false).build());
84 T item4 = buildTopic(makeBuilder().managed(false).build());
87 assertTrue(item != item3);
88 assertTrue(item != item4);
89 assertTrue(item3 != item4);
92 T item5 = buildTopic(makeBuilder().build());
93 T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
97 // re-build same managed topics - should get exact same objects
98 assertTrue(item5 == buildTopic(makeBuilder().topic(MY_TOPIC).build()));
99 assertTrue(item6 == buildTopic(makeBuilder().topic(TOPIC2).build()));
103 * Tests exception cases when building a topic using BusTopicParams.
105 public void testBuildBusTopicParams_Ex() {
107 RuntimeException actual = expectException(() -> buildTopic(makeBuilder().topic(null).build()));
108 assertEquals(IllegalArgumentException.class, actual.getClass());
111 actual = expectException(() -> buildTopic(makeBuilder().topic("").build()));
112 assertEquals(IllegalArgumentException.class, actual.getClass());
116 * Tests building a topic using a list of servers and a topic.
118 public void testBuildListOfStringString() {
121 T item1 = buildTopic(servers, MY_TOPIC);
122 assertNotNull(item1);
124 // check parameters that were used
125 BusTopicParams params = getLastParams();
126 assertEquals(servers, params.getServers());
127 assertEquals(MY_TOPIC, params.getTopic());
128 assertEquals(true, params.isManaged());
129 assertEquals(false, params.isUseHttps());
131 T item2 = buildTopic(servers, TOPIC2);
132 assertNotNull(item2);
133 assertTrue(item1 != item2);
135 // duplicate - should be the same, as these topics are managed
136 T item3 = buildTopic(servers, TOPIC2);
137 assertTrue(item2 == item3);
141 * Tests building a topic using Properties. Verifies parameters specific to Bus
144 public void testBuildProperties() {
147 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
149 BusTopicParams params = getLastParams();
150 assertEquals(true, params.isManaged());
151 assertEquals(true, params.isUseHttps());
152 assertEquals(true, params.isAllowSelfSignedCerts());
153 assertEquals(MY_API_KEY, params.getApiKey());
154 assertEquals(MY_API_SECRET, params.getApiSecret());
155 assertEquals(Arrays.asList(SERVER), params.getServers());
156 assertEquals(MY_TOPIC, params.getTopic());
160 public void testBuildProperties_Variations() {
161 super.testBuildProperties_Variations();
163 // check boolean properties that default to true
164 checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
166 // check boolean properties that default to false
167 checkDefault(PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
168 checkDefault(PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, params -> !params.isAllowSelfSignedCerts());
172 * Verifies that a parameter has the correct default, if the original builder property
175 * @param builderName name of the builder property
176 * @param validate function to test the validity of the property
177 * @param values the values to which the property should be set, defaults to
178 * {@code null} and ""
180 protected void checkDefault(String builderName, Function<BusTopicParams, Boolean> validate, Object... values) {
181 Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
183 for (Object value : values2) {
184 // always start with a fresh factory
187 TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
190 builder.removeTopicProperty(builderName);
193 builder.setTopicProperty(builderName, value.toString());
196 assertEquals("size for default " + value, 1, buildTopics(builder.build()).size());
197 assertTrue("default for " + value, validate.apply(getLastParams()));
202 * Verifies that an "additional" property does not exist, if the original builder
203 * property is not provided.
205 * @param builderName name of the builder property
206 * @param addName name of the "additional" property
208 protected void expectNullAddProp(String builderName, String addName) {
210 // remove the property
212 Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
213 assertEquals(1, buildTopics(props).size());
214 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
217 // repeat, this time using an empty string instead of null
219 props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
220 assertEquals(1, buildTopics(props).size());
221 assertFalse(getLastParams().getAdditionalProps().containsKey(addName));