2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus.internal;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.function.BiConsumer;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
34 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams.TopicParamsBuilder;
36 class BusTopicParamsTest extends TopicTestBase {
46 BusTopicParams params = makeBuilder().build();
48 assertEquals(addProps, params.getAdditionalProps());
49 assertEquals(MY_AFT_ENV, params.getAftEnvironment());
50 assertTrue(params.isAllowSelfSignedCerts());
51 assertEquals(MY_API_KEY, params.getApiKey());
52 assertEquals(MY_API_SECRET, params.getApiSecret());
53 assertEquals(MY_BASE_PATH, params.getBasePath());
54 assertEquals(MY_CLIENT_NAME, params.getClientName());
55 assertEquals(MY_CONS_GROUP, params.getConsumerGroup());
56 assertEquals(MY_CONS_INST, params.getConsumerInstance());
57 assertEquals(MY_ENV, params.getEnvironment());
58 assertEquals(MY_FETCH_LIMIT, params.getFetchLimit());
59 assertEquals(MY_FETCH_TIMEOUT, params.getFetchTimeout());
60 assertEquals(MY_HOST, params.getHostname());
61 assertEquals(MY_LAT, params.getLatitude());
62 assertEquals(MY_LONG, params.getLongitude());
63 assertTrue(params.isManaged());
64 assertEquals(MY_PARTITION, params.getPartitionId());
65 assertEquals(MY_PARTNER, params.getPartner());
66 assertEquals(MY_PASS, params.getPassword());
67 assertEquals(MY_PORT, params.getPort());
68 assertEquals(servers, params.getServers());
69 assertEquals(MY_TOPIC, params.getTopic());
70 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
71 assertTrue(params.isUseHttps());
72 assertEquals(MY_USERNAME, params.getUserName());
76 void testBooleanGetters() {
77 // ensure that booleans are independent of each other
78 testBoolean("true:false:false", TopicParamsBuilder::allowSelfSignedCerts);
79 testBoolean("false:true:false", TopicParamsBuilder::managed);
80 testBoolean("false:false:true", TopicParamsBuilder::useHttps);
84 void testValidators() {
85 BusTopicParams params = makeBuilder().build();
87 // test validity methods
88 assertTrue(params.isAdditionalPropsValid());
89 assertFalse(params.isAftEnvironmentInvalid());
90 assertTrue(params.isApiKeyValid());
91 assertTrue(params.isApiSecretValid());
92 assertFalse(params.isClientNameInvalid());
93 assertFalse(params.isConsumerGroupInvalid());
94 assertFalse(params.isConsumerInstanceInvalid());
95 assertFalse(params.isEnvironmentInvalid());
96 assertFalse(params.isHostnameInvalid());
97 assertFalse(params.isLatitudeInvalid());
98 assertFalse(params.isLongitudeInvalid());
99 assertFalse(params.isPartitionIdInvalid());
100 assertFalse(params.isPartnerInvalid());
101 assertTrue(params.isPasswordValid());
102 assertFalse(params.isPortInvalid());
103 assertFalse(params.isServersInvalid());
104 assertFalse(params.isTopicInvalid());
105 assertTrue(params.isUserNameValid());
109 void testInvertedValidators() {
110 assertFalse(makeBuilder().additionalProps(null).build().isAdditionalPropsValid());
111 assertTrue(makeBuilder().aftEnvironment("").build().isAftEnvironmentInvalid());
112 assertFalse(makeBuilder().apiKey("").build().isApiKeyValid());
113 assertFalse(makeBuilder().apiSecret("").build().isApiSecretValid());
114 assertTrue(makeBuilder().clientName("").build().isClientNameInvalid());
115 assertTrue(makeBuilder().consumerGroup("").build().isConsumerGroupInvalid());
116 assertTrue(makeBuilder().consumerInstance("").build().isConsumerInstanceInvalid());
117 assertTrue(makeBuilder().environment("").build().isEnvironmentInvalid());
118 assertTrue(makeBuilder().hostname("").build().isHostnameInvalid());
119 assertTrue(makeBuilder().latitude("").build().isLatitudeInvalid());
120 assertTrue(makeBuilder().longitude("").build().isLongitudeInvalid());
121 assertTrue(makeBuilder().partitionId("").build().isPartitionIdInvalid());
122 assertTrue(makeBuilder().partner("").build().isPartnerInvalid());
123 assertFalse(makeBuilder().password("").build().isPasswordValid());
124 assertTrue(makeBuilder().port(-1).build().isPortInvalid());
125 assertTrue(makeBuilder().port(65536).build().isPortInvalid());
126 assertTrue(makeBuilder().servers(null).build().isServersInvalid());
127 assertTrue(makeBuilder().servers(new LinkedList<>()).build().isServersInvalid());
128 assertTrue(makeBuilder().servers(List.of("")).build().isServersInvalid());
129 assertFalse(makeBuilder().servers(List.of("one-server")).build().isServersInvalid());
130 assertTrue(makeBuilder().topic("").build().isTopicInvalid());
131 assertFalse(makeBuilder().userName("").build().isUserNameValid());
135 * Tests the boolean methods by applying a function, once with {@code false} and once
136 * with {@code true}. Verifies that all the boolean methods return the correct
137 * value by concatenating them.
139 * @param expectedTrue the string that is expected when {@code true} is passed to the
141 * @param function function to be applied to the builder
143 private void testBoolean(String expectedTrue, BiConsumer<TopicParamsBuilder, Boolean> function) {
144 TopicParamsBuilder builder = BusTopicParams.builder();
146 // first try the "false" case
147 function.accept(builder, false);
149 BusTopicParams params = builder.build();
150 assertEquals("false:false:false",
151 params.isAllowSelfSignedCerts() + ":" + params.isManaged() + ":" + params.isUseHttps());
154 // now try the "true" case
155 function.accept(builder, true);
157 params = builder.build();
158 assertEquals(expectedTrue,
159 params.isAllowSelfSignedCerts() + ":" + params.isManaged() + ":" + params.isUseHttps());