b2a354084986b963735b61a875d185399a7148b0
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNotSame;
30 import static org.junit.jupiter.api.Assertions.assertSame;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
33 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
34 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
35 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX;
36
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Properties;
40 import java.util.function.Predicate;
41 import org.onap.policy.common.endpoints.event.comm.Topic;
42 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
43
44 /**
45  * Base class for Topic Factory tests that use BusTopicParams.
46  *
47  * @param <T> type of topic managed by the factory
48  */
49 public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFactoryTestBase<T> {
50
51     /**
52      * Builds a topic.
53      *
54      * @param params the parameters used to configure the topic
55      * @return a new topic
56      */
57     protected abstract T buildTopic(BusTopicParams params);
58
59     /**
60      * Builds a topic.
61      *
62      * @param servers list of servers
63      * @param topic the topic name
64      * @return a new topic
65      */
66     protected abstract T buildTopic(List<String> servers, String topic);
67
68     /**
69      * Gets the parameters used to build the most recent topic.
70      *
71      * @return the most recent topic's parameters
72      */
73     protected abstract BusTopicParams getLastParams();
74
75     /**
76      * Tests building a topic using BusTopicParams.
77      */
78     void testBuildBusTopicParams() {
79         initFactory();
80
81         // two unmanaged topics
82         T item = buildTopic(makeBuilder().managed(false).effectiveTopic(null).build());
83         T item2 = buildTopic(makeBuilder().managed(false).topic(TOPIC2).build());
84         assertNotNull(item);
85         assertNotNull(item2);
86         assertEquals(item.getTopic(), item.getEffectiveTopic());
87         assertNotEquals(item2.getTopic(), item2.getEffectiveTopic());
88         assertNotSame(item, item2);
89
90         // duplicate topics, but since they aren't managed, they should be different
91         T item3 = buildTopic(makeBuilder().managed(false).build());
92         T item4 = buildTopic(makeBuilder().managed(false).effectiveTopic(TOPIC2).build());
93         assertNotNull(item3);
94         assertNotNull(item4);
95         assertEquals(MY_TOPIC, item4.getTopic());
96         assertEquals(TOPIC2, item4.getEffectiveTopic());
97         assertNotSame(item, item3);
98         assertNotSame(item, item4);
99         assertNotSame(item3, item4);
100
101         // two managed topics
102         T item5 = buildTopic(makeBuilder().build());
103         T item6 = buildTopic(makeBuilder().topic(TOPIC2).build());
104         assertNotNull(item5);
105         assertNotNull(item6);
106
107         // re-build same managed topics - should get exact same objects
108         assertSame(item5, buildTopic(makeBuilder().topic(MY_TOPIC).build()));
109         assertSame(item6, buildTopic(makeBuilder().topic(TOPIC2).build()));
110     }
111
112     /**
113      * Tests exception cases when building a topic using BusTopicParams.
114      */
115     void testBuildBusTopicParams_Ex() {
116         // null topic
117         assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
118
119         // empty topic
120         assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
121     }
122
123     /**
124      * Tests building a topic using a list of servers and a topic.
125      */
126     void testBuildListOfStringString() {
127         initFactory();
128
129         T item1 = buildTopic(servers, MY_TOPIC);
130         assertNotNull(item1);
131
132         // check parameters that were used
133         BusTopicParams params = getLastParams();
134         assertEquals(servers, params.getServers());
135         assertEquals(MY_TOPIC, params.getTopic());
136         assertEquals(true, params.isManaged());
137         assertEquals(false, params.isUseHttps());
138
139         T item2 = buildTopic(servers, TOPIC2);
140         assertNotNull(item2);
141         assertNotSame(item1, item2);
142
143         // duplicate - should be the same, as these topics are managed
144         T item3 = buildTopic(servers, TOPIC2);
145         assertSame(item2, item3);
146     }
147
148     /**
149      * Tests building a topic using Properties. Verifies parameters specific to Bus
150      * topics.
151      */
152     void testBuildProperties() {
153         initFactory();
154
155         List<T> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
156         assertEquals(1, topics.size());
157         assertEquals(MY_TOPIC, topics.get(0).getTopic());
158         assertEquals(MY_EFFECTIVE_TOPIC, topics.get(0).getEffectiveTopic());
159
160         BusTopicParams params = getLastParams();
161         assertEquals(true, params.isManaged());
162         assertEquals(true, params.isUseHttps());
163         assertEquals(true, params.isAllowSelfSignedCerts());
164         assertEquals(MY_API_KEY, params.getApiKey());
165         assertEquals(MY_API_SECRET, params.getApiSecret());
166         assertEquals(Arrays.asList(SERVER), params.getServers());
167         assertEquals(MY_TOPIC, params.getTopic());
168         assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
169
170         List<T> topics2 = buildTopics(makePropBuilder().makeTopic(TOPIC3)
171             .removeTopicProperty(PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX).build());
172         assertEquals(1, topics2.size());
173         assertEquals(TOPIC3, topics2.get(0).getTopic());
174         assertEquals(topics2.get(0).getTopic(), topics2.get(0).getEffectiveTopic());
175     }
176
177     @Override
178     void testBuildProperties_Variations() {
179         super.testBuildProperties_Variations();
180
181         // check boolean properties that default to true
182         checkDefault(PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
183
184         // check boolean properties that default to false
185         checkDefault(PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
186         checkDefault(PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, params -> !params.isAllowSelfSignedCerts());
187     }
188
189     /**
190      * Verifies that a parameter has the correct default, if the original builder property
191      * is not provided.
192      *
193      * @param builderName name of the builder property
194      * @param validate function to test the validity of the property
195      * @param values the values to which the property should be set, defaults to
196      *        {@code null} and ""
197      */
198     protected void checkDefault(String builderName, Predicate<BusTopicParams> validate, Object... values) {
199         Object[] values2 = (values.length > 0 ? values : new Object[] {null, ""});
200
201         for (Object value : values2) {
202             // always start with a fresh factory
203             initFactory();
204
205             TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
206
207             if (value == null) {
208                 builder.removeTopicProperty(builderName);
209
210             } else {
211                 builder.setTopicProperty(builderName, value.toString());
212             }
213
214             assertEquals(1, buildTopics(builder.build()).size(), "size for default " + value);
215             assertTrue(validate.test(getLastParams()), "default for " + value);
216         }
217     }
218
219     /**
220      * Verifies that an "additional" property does not exist, if the original builder
221      * property is not provided.
222      *
223      * @param builderName name of the builder property
224      * @param addName name of the "additional" property
225      */
226     protected void expectNullAddProp(String builderName, String addName) {
227
228         // remove the property
229         initFactory();
230         Properties props = makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(builderName).build();
231         assertEquals(1, buildTopics(props).size());
232         assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
233
234
235         // repeat, this time using an empty string instead of null
236         initFactory();
237         props = makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(builderName, "").build();
238         assertEquals(1, buildTopics(props).size());
239         assertFalse(getLastParams().getAdditionalProps().containsKey(addName));
240     }
241 }