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