71d4fe2704f3aa455c06bdc7529b1a4ee926d4ae
[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.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;
31
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;
38
39 /**
40  * Base class for Topic Factory tests that use BusTopicParams.
41  *
42  * @param <T> type of topic managed by the factory
43  */
44 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
45
46     /**
47      * Builds a topic.
48      *
49      * @param params the parameters used to configure the topic
50      * @return a new topic
51      */
52     protected abstract T buildTopic(BusTopicParams params);
53
54     /**
55      * Builds a topic.
56      *
57      * @param servers list of servers
58      * @param topic the topic name
59      * @return a new topic
60      */
61     protected abstract T buildTopic(List<String> servers, String topic);
62
63     /**
64      * Gets the parameters used to build the most recent topic.
65      *
66      * @return the most recent topic's parameters
67      */
68     protected abstract BusTopicParams getLastParams();
69
70     /**
71      * Tests building a topic using BusTopicParams.
72      */
73     public void testBuildBusTopicParams() {
74         initFactory();
75
76         // two unmanaged topics
77         T item = buildTopic(makeBuilder().managed(false).build());
78         T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
79         assertNotNull(item);
80         assertNotNull(item2);
81         assertTrue(item != item2);
82
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());
86         assertNotNull(item3);
87         assertNotNull(item4);
88         assertTrue(item != item3);
89         assertTrue(item != item4);
90         assertTrue(item3 != item4);
91
92         // two managed topics
93         T item5 = buildTopic(makeBuilder().build());
94         T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
95         assertNotNull(item5);
96         assertNotNull(item6);
97
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()));
101     }
102
103     /**
104      * Tests exception cases when building a topic using BusTopicParams.
105      */
106     public void testBuildBusTopicParams_Ex() {
107         // null topic
108         assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
109
110         // empty topic
111         assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
112     }
113
114     /**
115      * Tests building a topic using a list of servers and a topic.
116      */
117     public void testBuildListOfStringString() {
118         initFactory();
119
120         T item1 = buildTopic(servers, MY_TOPIC);
121         assertNotNull(item1);
122
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());
129
130         T item2 = buildTopic(servers, TOPIC2);
131         assertNotNull(item2);
132         assertTrue(item1 != item2);
133
134         // duplicate - should be the same, as these topics are managed
135         T item3 = buildTopic(servers, TOPIC2);
136         assertTrue(item2 == item3);
137     }
138
139     /**
140      * Tests building a topic using Properties. Verifies parameters specific to Bus
141      * topics.
142      */
143     public void testBuildProperties() {
144         initFactory();
145
146         assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
147
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());
156     }
157
158     @Override
159     public void testBuildProperties_Variations() {
160         super.testBuildProperties_Variations();
161
162         // check boolean properties that default to true
163         checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
164
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());
168     }
169
170     /**
171      * Verifies that a parameter has the correct default, if the original builder property
172      * is not provided.
173      *
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 ""
178      */
179     protected void checkDefault(String builderName, Function<BusTopicParams, Boolean> validate, Object... values) {
180         Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
181
182         for (Object value : values2) {
183             // always start with a fresh factory
184             initFactory();
185
186             TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
187
188             if (value == null) {
189                 builder.removeTopicProperty(builderName);
190
191             } else {
192                 builder.setTopicProperty(builderName, value.toString());
193             }
194
195             assertEquals("size for default " + value, 1, buildTopics(builder.build()).size());
196             assertTrue("default for " + value, validate.apply(getLastParams()));
197         }
198     }
199
200     /**
201      * Verifies that an "additional" property does not exist, if the original builder
202      * property is not provided.
203      *
204      * @param builderName name of the builder property
205      * @param addName name of the "additional" property
206      */
207     protected void expectNullAddProp(String builderName, String addName) {
208
209         // remove the property
210         initFactory();
211         Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
212         assertEquals(1, buildTopics(props).size());
213         assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
214
215
216         // repeat, this time using an empty string instead of null
217         initFactory();
218         props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
219         assertEquals(1, buildTopics(props).size());
220         assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
221     }
222 }