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