c00f2b56043b8e37b8176460fd3c74b5e9b62036
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
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.internal;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
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;
34
35 public class BusTopicParamsTest extends TopicTestBase {
36
37     @Before
38     @Override
39     public void setUp() {
40         super.setUp();
41     }
42
43     @Test
44     public void test() {
45         BusTopicParams params = makeBuilder().build();
46
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());
72
73         // ensure that booleans are independent of each other
74         testBoolean("true:false:false", (bldr, flag) -> bldr.allowSelfSignedCerts(flag));
75         testBoolean("false:true:false", (bldr, flag) -> bldr.managed(flag));
76         testBoolean("false:false:true", (bldr, flag) -> bldr.useHttps(flag));
77
78         // test validity methods
79         assertTrue(params.isAdditionalPropsValid());
80         assertFalse(params.isAftEnvironmentInvalid());
81         assertTrue(params.isApiKeyValid());
82         assertTrue(params.isApiSecretValid());
83         assertFalse(params.isClientNameInvalid());
84         assertFalse(params.isConsumerGroupInvalid());
85         assertFalse(params.isConsumerInstanceInvalid());
86         assertFalse(params.isEnvironmentInvalid());
87         assertFalse(params.isHostnameInvalid());
88         assertFalse(params.isLatitudeInvalid());
89         assertFalse(params.isLongitudeInvalid());
90         assertFalse(params.isPartitionIdInvalid());
91         assertFalse(params.isPartnerInvalid());
92         assertTrue(params.isPasswordValid());
93         assertFalse(params.isPortInvalid());
94         assertFalse(params.isServersInvalid());
95         assertFalse(params.isTopicInvalid());
96         assertTrue(params.isUserNameValid());
97
98         // test inverted validity
99         assertFalse(makeBuilder().additionalProps(null).build().isAdditionalPropsValid());
100         assertTrue(makeBuilder().aftEnvironment("").build().isAftEnvironmentInvalid());
101         assertFalse(makeBuilder().apiKey("").build().isApiKeyValid());
102         assertFalse(makeBuilder().apiSecret("").build().isApiSecretValid());
103         assertTrue(makeBuilder().clientName("").build().isClientNameInvalid());
104         assertTrue(makeBuilder().consumerGroup("").build().isConsumerGroupInvalid());
105         assertTrue(makeBuilder().consumerInstance("").build().isConsumerInstanceInvalid());
106         assertTrue(makeBuilder().environment("").build().isEnvironmentInvalid());
107         assertTrue(makeBuilder().hostname("").build().isHostnameInvalid());
108         assertTrue(makeBuilder().latitude("").build().isLatitudeInvalid());
109         assertTrue(makeBuilder().longitude("").build().isLongitudeInvalid());
110         assertTrue(makeBuilder().partitionId("").build().isPartitionIdInvalid());
111         assertTrue(makeBuilder().partner("").build().isPartnerInvalid());
112         assertFalse(makeBuilder().password("").build().isPasswordValid());
113         assertTrue(makeBuilder().port(-1).build().isPortInvalid());
114         assertTrue(makeBuilder().port(65536).build().isPortInvalid());
115         assertTrue(makeBuilder().servers(null).build().isServersInvalid());
116         assertTrue(makeBuilder().servers(new LinkedList<>()).build().isServersInvalid());
117         assertTrue(makeBuilder().servers(Arrays.asList("")).build().isServersInvalid());
118         assertFalse(makeBuilder().servers(Arrays.asList("one-server")).build().isServersInvalid());
119         assertTrue(makeBuilder().topic("").build().isTopicInvalid());
120         assertFalse(makeBuilder().userName("").build().isUserNameValid());
121     }
122
123     /**
124      * Tests the boolean methods by applying a function, once with {@code false} and once
125      * with {@code true}. Verifies that all of the boolean methods return the correct
126      * value by concatenating them.
127      *
128      * @param expectedTrue the string that is expected when {@code true} is passed to the
129      *        method
130      * @param function function to be applied to the builder
131      */
132     private void testBoolean(String expectedTrue, BiConsumer<TopicParamsBuilder, Boolean> function) {
133         TopicParamsBuilder builder = BusTopicParams.builder();
134
135         // first try the "false" case
136         function.accept(builder, false);
137
138         BusTopicParams params = builder.build();
139         assertEquals("false:false:false",
140                         "" + params.isAllowSelfSignedCerts() + ":" + params.isManaged() + ":" + params.isUseHttps());
141
142
143         // now try the "true" case
144         function.accept(builder, true);
145
146         params = builder.build();
147         assertEquals(expectedTrue,
148                         "" + params.isAllowSelfSignedCerts() + ":" + params.isManaged() + ":" + params.isUseHttps());
149     }
150 }