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