919397d9c27b67d7c984f3946d9b8031282e2b46
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus;
22
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;
33
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Properties;
37 import java.util.function.Function;
38 import org.onap.policy.common.endpoints.event.comm.Topic;
39 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
40
41 /**
42  * Base class for Topic Factory tests that use BusTopicParams.
43  *
44  * @param <T> type of topic managed by the factory
45  */
46 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
47
48     /**
49      * Builds a topic.
50      *
51      * @param params the parameters used to configure the topic
52      * @return a new topic
53      */
54     protected abstract T buildTopic(BusTopicParams params);
55
56     /**
57      * Builds a topic.
58      *
59      * @param servers list of servers
60      * @param topic the topic name
61      * @return a new topic
62      */
63     protected abstract T buildTopic(List<String> servers, String topic);
64
65     /**
66      * Gets the parameters used to build the most recent topic.
67      *
68      * @return the most recent topic's parameters
69      */
70     protected abstract BusTopicParams getLastParams();
71
72     /**
73      * Tests building a topic using BusTopicParams.
74      */
75     public void testBuildBusTopicParams() {
76         initFactory();
77
78         // two unmanaged topics
79         T item = buildTopic(makeBuilder().managed(false).effectiveTopic(null).build());
80         T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
81         assertNotNull(item);
82         assertNotNull(item2);
83         assertEquals(item.getTopic(), item.getEffectiveTopic());
84         assertNotEquals(item2.getTopic(), item2.getEffectiveTopic());
85         assertTrue(item != item2);
86
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());
90         assertNotNull(item3);
91         assertNotNull(item4);
92         assertEquals(MY_TOPIC, item4.getTopic());
93         assertEquals(TOPIC2, item4.getEffectiveTopic());
94         assertTrue(item != item3);
95         assertTrue(item != item4);
96         assertTrue(item3 != item4);
97
98         // two managed topics
99         T item5 = buildTopic(makeBuilder().build());
100         T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
101         assertNotNull(item5);
102         assertNotNull(item6);
103
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()));
107     }
108
109     /**
110      * Tests exception cases when building a topic using BusTopicParams.
111      */
112     public void testBuildBusTopicParams_Ex() {
113         // null topic
114         assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
115
116         // empty topic
117         assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
118     }
119
120     /**
121      * Tests building a topic using a list of servers and a topic.
122      */
123     public void testBuildListOfStringString() {
124         initFactory();
125
126         T item1 = buildTopic(servers, MY_TOPIC);
127         assertNotNull(item1);
128
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());
135
136         T item2 = buildTopic(servers, TOPIC2);
137         assertNotNull(item2);
138         assertTrue(item1 != item2);
139
140         // duplicate - should be the same, as these topics are managed
141         T item3 = buildTopic(servers, TOPIC2);
142         assertTrue(item2 == item3);
143     }
144
145     /**
146      * Tests building a topic using Properties. Verifies parameters specific to Bus
147      * topics.
148      */
149     public void testBuildProperties() {
150         initFactory();
151
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());
156
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());
166
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());
172     }
173
174     @Override
175     public void testBuildProperties_Variations() {
176         super.testBuildProperties_Variations();
177
178         // check boolean properties that default to true
179         checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
180
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());
184     }
185
186     /**
187      * Verifies that a parameter has the correct default, if the original builder property
188      * is not provided.
189      *
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 ""
194      */
195     protected void checkDefault(String builderName, Function<BusTopicParams, Boolean> validate, Object... values) {
196         Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
197
198         for (Object value : values2) {
199             // always start with a fresh factory
200             initFactory();
201
202             TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
203
204             if (value == null) {
205                 builder.removeTopicProperty(builderName);
206
207             } else {
208                 builder.setTopicProperty(builderName, value.toString());
209             }
210
211             assertEquals("size for default " + value, 1, buildTopics(builder.build()).size());
212             assertTrue("default for " + value, validate.apply(getLastParams()));
213         }
214     }
215
216     /**
217      * Verifies that an "additional" property does not exist, if the original builder
218      * property is not provided.
219      *
220      * @param builderName name of the builder property
221      * @param addName name of the "additional" property
222      */
223     protected void expectNullAddProp(String builderName, String addName) {
224
225         // remove the property
226         initFactory();
227         Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
228         assertEquals(1, buildTopics(props).size());
229         assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
230
231
232         // repeat, this time using an empty string instead of null
233         initFactory();
234         props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
235         assertEquals(1, buildTopics(props).size());
236         assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
237     }
238 }