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