2 * ============LICENSE_START=======================================================
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
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 BusTopicParams params = makeBuilder().build();
46 assertEquals(addProps, params.getAdditionalProps());
47 assertEquals(MY_AFT_ENV, params.getAftEnvironment());
48 assertEquals(true, params.isAllowSelfSignedCerts());
49 assertEquals(MY_API_KEY, params.getApiKey());
50 assertEquals(MY_API_SECRET, params.getApiSecret());
51 assertEquals(MY_BASE_PATH, params.getBasePath());
52 assertEquals(MY_CLIENT_NAME, params.getClientName());
53 assertEquals(MY_CONS_GROUP, params.getConsumerGroup());
54 assertEquals(MY_CONS_INST, params.getConsumerInstance());
55 assertEquals(MY_ENV, params.getEnvironment());
56 assertEquals(MY_FETCH_LIMIT, params.getFetchLimit());
57 assertEquals(MY_FETCH_TIMEOUT, params.getFetchTimeout());
58 assertEquals(MY_HOST, params.getHostname());
59 assertEquals(MY_LAT, params.getLatitude());
60 assertEquals(MY_LONG, params.getLongitude());
61 assertEquals(true, params.isManaged());
62 assertEquals(MY_PARTITION, params.getPartitionId());
63 assertEquals(MY_PARTNER, params.getPartner());
64 assertEquals(MY_PASSWD, params.getPassword());
65 assertEquals(MY_PORT, params.getPort());
66 assertEquals(servers, params.getServers());
67 assertEquals(MY_TOPIC, params.getTopic());
68 assertEquals(MY_EFFECTIVE_TOPIC, params.getEffectiveTopic());
69 assertEquals(true, params.isUseHttps());
70 assertEquals(MY_USERNAME, params.getUserName());
72 // ensure that booleans are independent of each other
73 testBoolean("true:false:false", (bldr, flag) -> bldr.allowSelfSignedCerts(flag));
74 testBoolean("false:true:false", (bldr, flag) -> bldr.managed(flag));
75 testBoolean("false:false:true", (bldr, flag) -> bldr.useHttps(flag));
77 // test validity methods
78 assertTrue(params.isAdditionalPropsValid());
79 assertFalse(params.isAftEnvironmentInvalid());
80 assertTrue(params.isApiKeyValid());
81 assertTrue(params.isApiSecretValid());
82 assertFalse(params.isClientNameInvalid());
83 assertFalse(params.isConsumerGroupInvalid());
84 assertFalse(params.isConsumerInstanceInvalid());
85 assertFalse(params.isEnvironmentInvalid());
86 assertFalse(params.isHostnameInvalid());
87 assertFalse(params.isLatitudeInvalid());
88 assertFalse(params.isLongitudeInvalid());
89 assertFalse(params.isPartitionIdInvalid());
90 assertFalse(params.isPartnerInvalid());
91 assertTrue(params.isPasswordValid());
92 assertFalse(params.isPortInvalid());
93 assertFalse(params.isServersInvalid());
94 assertFalse(params.isTopicInvalid());
95 assertTrue(params.isUserNameValid());
97 // test inverted validity
98 assertFalse(makeBuilder().additionalProps(null).build().isAdditionalPropsValid());
99 assertTrue(makeBuilder().aftEnvironment("").build().isAftEnvironmentInvalid());
100 assertFalse(makeBuilder().apiKey("").build().isApiKeyValid());
101 assertFalse(makeBuilder().apiSecret("").build().isApiSecretValid());
102 assertTrue(makeBuilder().clientName("").build().isClientNameInvalid());
103 assertTrue(makeBuilder().consumerGroup("").build().isConsumerGroupInvalid());
104 assertTrue(makeBuilder().consumerInstance("").build().isConsumerInstanceInvalid());
105 assertTrue(makeBuilder().environment("").build().isEnvironmentInvalid());
106 assertTrue(makeBuilder().hostname("").build().isHostnameInvalid());
107 assertTrue(makeBuilder().latitude("").build().isLatitudeInvalid());
108 assertTrue(makeBuilder().longitude("").build().isLongitudeInvalid());
109 assertTrue(makeBuilder().partitionId("").build().isPartitionIdInvalid());
110 assertTrue(makeBuilder().partner("").build().isPartnerInvalid());
111 assertFalse(makeBuilder().password("").build().isPasswordValid());
112 assertTrue(makeBuilder().port(-1).build().isPortInvalid());
113 assertTrue(makeBuilder().port(65536).build().isPortInvalid());
114 assertTrue(makeBuilder().servers(null).build().isServersInvalid());
115 assertTrue(makeBuilder().servers(new LinkedList<>()).build().isServersInvalid());
116 assertTrue(makeBuilder().servers(Arrays.asList("")).build().isServersInvalid());
117 assertFalse(makeBuilder().servers(Arrays.asList("one-server")).build().isServersInvalid());
118 assertTrue(makeBuilder().topic("").build().isTopicInvalid());
119 assertFalse(makeBuilder().userName("").build().isUserNameValid());
123 * Tests the boolean methods by applying a function, once with {@code false} and once
124 * with {@code true}. Verifies that all of the boolean methods return the correct
125 * value by concatenating them.
127 * @param expectedTrue the string that is expected when {@code true} is passed to the
129 * @param function function to be applied to the builder
131 private void testBoolean(String expectedTrue, BiConsumer<TopicParamsBuilder, Boolean> function) {
132 TopicParamsBuilder builder = BusTopicParams.builder();
134 // first try the "false" case
135 function.accept(builder, false);
137 BusTopicParams params = builder.build();
138 assertEquals("false:false:false",
139 "" + params.isAllowSelfSignedCerts() + ":" + params.isManaged() + ":" + params.isUseHttps());
142 // now try the "true" case
143 function.accept(builder, true);
145 params = builder.build();
146 assertEquals(expectedTrue,
147 "" + params.isAllowSelfSignedCerts() + ":" + params.isManaged() + ":" + params.isUseHttps());