2 * Copyright 2017-2020 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.holmes.common.config;
\r
19 import org.apache.commons.lang3.StringUtils;
\r
20 import org.easymock.EasyMock;
\r
21 import org.junit.Ignore;
\r
22 import org.junit.Test;
\r
23 import org.junit.runner.RunWith;
\r
24 import org.powermock.api.easymock.PowerMock;
\r
25 import org.powermock.core.classloader.annotations.PowerMockIgnore;
\r
26 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
27 import org.powermock.modules.junit4.PowerMockRunner;
\r
28 import org.powermock.reflect.internal.WhiteboxImpl;
\r
30 import static org.hamcrest.core.Is.is;
\r
31 import static org.hamcrest.core.IsEqual.equalTo;
\r
32 import static org.hamcrest.core.IsNull.nullValue;
\r
33 import static org.junit.Assert.assertThat;
\r
34 import static org.onap.holmes.common.config.MicroServiceConfig.*;
\r
36 @PrepareForTest(MicroServiceConfig.class)
\r
37 @PowerMockIgnore({"javax.ws.*"})
\r
38 @RunWith(PowerMockRunner.class)
\r
39 public class MicroServiceConfigTest {
\r
41 private static String ACTUAL_HOSTNAME = System.getenv(HOSTNAME);
\r
45 public void getMsbServerAddrTest() {
\r
46 System.setProperty(MSB_IAG_SERVICE_HOST, "test");
\r
47 System.setProperty(MSB_IAG_SERVICE_PORT, "443");
\r
48 assertThat("http://test:443", equalTo(getMsbServerAddrWithHttpPrefix()));
\r
49 System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_PORT);
\r
50 System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_HOST);
\r
54 public void getMsbServerIpTest() {
\r
55 System.setProperty(MSB_IAG_SERVICE_HOST, "10.54.23.79");
\r
56 System.setProperty(MSB_IAG_SERVICE_PORT, "443");
\r
57 System.setProperty(HOSTNAME, "rule-mgmt");
\r
58 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
59 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
60 .andReturn("{\"msb.hostname\": \"10.54.23.79:443\"}").times(2);
\r
61 PowerMock.replayAll();
\r
62 assertThat("10.54.23.79", equalTo(getMsbIpAndPort()[0]));
\r
63 assertThat("443", equalTo(getMsbIpAndPort()[1]));
\r
64 System.clearProperty(MicroServiceConfig.HOSTNAME);
\r
65 System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_PORT);
\r
66 System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_HOST);
\r
70 public void getServiceIpTest() {
\r
71 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
72 String hostname = String.format("http://%s", ip);
\r
73 System.setProperty(HOSTNAME, hostname);
\r
74 assertThat(ip, equalTo(getMicroServiceIpAndPort()[0]));
\r
75 assertThat("80", equalTo(getMicroServiceIpAndPort()[1]));
\r
76 System.clearProperty(HOSTNAME);
\r
80 public void getConsulAddrInfoTest() {
\r
81 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
82 assertThat("http://127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));
\r
83 System.clearProperty(CONSUL_HOST);
\r
87 public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {
\r
88 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
89 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
90 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
91 .andThrow(new RuntimeException("Invalid URL."));
\r
93 PowerMock.replayAll();
\r
95 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE))
\r
98 PowerMock.verifyAll();
\r
99 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
103 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists() throws Exception {
\r
104 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
105 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
106 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
107 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
108 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
110 PowerMock.replayAll();
\r
112 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
113 equalTo("http://127.0.0.2:8080"));
\r
115 PowerMock.verifyAll();
\r
117 System.clearProperty(CONSUL_HOST);
\r
118 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
122 public void getConfigBindingServiceAddrInfoTest_consul_return_empty_array() throws Exception {
\r
123 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
124 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
125 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
127 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
129 PowerMock.replayAll();
\r
131 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
134 PowerMock.verifyAll();
\r
136 System.clearProperty(CONSUL_HOST);
\r
137 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
141 public void getConfigBindingServiceAddrInfoTest_consul_exists_property_not_exist() throws Exception {
\r
142 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
143 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
144 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
145 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");
\r
146 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
148 PowerMock.replayAll();
\r
150 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
153 PowerMock.verifyAll();
\r
155 System.clearProperty(CONSUL_HOST);
\r
156 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
160 public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
\r
161 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
162 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
163 .andThrow(new RuntimeException("Invalid URL.")).times(2);
\r
165 PowerMock.replayAll();
\r
167 assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue()));
\r
169 PowerMock.verifyAll();
\r
173 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception {
\r
174 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
175 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
178 PowerMock.replayAll();
\r
179 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue()));
\r
180 PowerMock.verifyAll();
\r
184 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
185 System.setProperty(MSB_IAG_SERVICE_HOST, "10.74.5.8");
\r
186 System.setProperty(MSB_IAG_SERVICE_PORT, "1545");
\r
187 System.setProperty(HOSTNAME, "rule-mgmt");
\r
188 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
189 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
190 .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}");
\r
192 PowerMock.replayAll();
\r
193 String[] msbInfo = getMsbIpAndPort();
\r
194 PowerMock.verifyAll();
\r
196 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
197 assertThat(msbInfo[1], equalTo("5432"));
\r
199 System.clearProperty(HOSTNAME);
\r
200 System.clearProperty(MSB_IAG_SERVICE_PORT);
\r
201 System.clearProperty(MSB_IAG_SERVICE_HOST);
\r
205 public void getMsbAddrInfo_msb_not_registered() throws Exception {
\r
206 System.setProperty(MSB_IAG_SERVICE_HOST, "10.74.5.8");
\r
207 System.setProperty(MSB_IAG_SERVICE_PORT, "1545");
\r
208 System.setProperty(HOSTNAME, "rule-mgmt");
\r
209 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
210 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
213 PowerMock.replayAll();
\r
214 String[] msbInfo = getMsbIpAndPort();
\r
215 PowerMock.verifyAll();
\r
217 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
218 assertThat(msbInfo[1], equalTo("1545"));
\r
220 System.clearProperty(HOSTNAME);
\r
221 System.clearProperty(MSB_IAG_SERVICE_PORT);
\r
222 System.clearProperty(MSB_IAG_SERVICE_HOST);
\r
226 public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception {
\r
227 System.setProperty(HOSTNAME, "rule-mgmt");
\r
228 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
229 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
230 .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]");
\r
232 PowerMock.replayAll();
\r
233 String[] msbInfo = getMicroServiceIpAndPort();
\r
234 PowerMock.verifyAll();
\r
236 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
237 assertThat(msbInfo[1], equalTo("5432"));
\r
239 System.clearProperty(HOSTNAME);
\r
243 public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {
\r
244 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
245 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
246 String hostname = String.format("http://%s:%s", ip, port);
\r
247 System.setProperty(HOSTNAME, hostname);
\r
248 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
249 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
252 PowerMock.replayAll();
\r
253 String[] msbInfo = getMicroServiceIpAndPort();
\r
254 PowerMock.verifyAll();
\r
256 assertThat(msbInfo[0], equalTo(ip));
\r
257 assertThat(msbInfo[1], equalTo(port));
\r
259 System.clearProperty(HOSTNAME);
\r
263 public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {
\r
264 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
265 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
266 String hostname = String.format("http://%s:%s", ip, port);
\r
267 System.setProperty(HOSTNAME, hostname);
\r
268 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
269 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
272 PowerMock.replayAll();
\r
273 String[] msbInfo = getMicroServiceIpAndPort();
\r
274 PowerMock.verifyAll();
\r
276 assertThat(msbInfo[0], equalTo(ip));
\r
277 assertThat(msbInfo[1], equalTo(port));
\r
279 System.clearProperty(HOSTNAME);
\r
283 public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {
\r
284 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
285 String hostname = String.format("http://%s", ip);
\r
286 System.setProperty(HOSTNAME, hostname);
\r
287 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
288 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
291 PowerMock.replayAll();
\r
292 String[] msbInfo = getMicroServiceIpAndPort();
\r
293 PowerMock.verifyAll();
\r
295 assertThat(msbInfo[0], equalTo(ip));
\r
296 assertThat(msbInfo[1], equalTo("80"));
\r
298 System.clearProperty(HOSTNAME);
\r
302 public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception {
\r
303 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
304 String hostname = String.format("http://%s", ip);
\r
305 System.setProperty(HOSTNAME, hostname);
\r
306 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
307 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
310 PowerMock.replayAll();
\r
311 String[] msbInfo = getMicroServiceIpAndPort();
\r
312 PowerMock.verifyAll();
\r
314 assertThat(msbInfo[0], equalTo(ip));
\r
315 assertThat(msbInfo[1], equalTo("80"));
\r
317 System.clearProperty(HOSTNAME);
\r
321 public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception {
\r
322 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
323 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
324 String hostname = String.format("http://%s:%s", ip, port);
\r
325 System.setProperty(HOSTNAME, hostname);
\r
326 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
327 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
330 PowerMock.replayAll();
\r
331 String[] msbInfo = getMicroServiceIpAndPort();
\r
332 PowerMock.verifyAll();
\r
334 assertThat(msbInfo[0], equalTo(ip));
\r
335 assertThat(msbInfo[1], equalTo(port));
\r
337 System.clearProperty(HOSTNAME);
\r
341 public void isValidIpAddress_with_port() throws Exception {
\r
342 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");
\r
343 assertThat(res, is(true));
\r
347 public void isValidIpAddress_without_port() throws Exception {
\r
348 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");
\r
349 assertThat(res, is(true));
\r
353 public void isValidIpAddress_with_port_with_http_prefix() throws Exception {
\r
354 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");
\r
355 assertThat(res, is(true));
\r
359 public void isValidIpAddress_without_port_with_https_prefix() throws Exception {
\r
360 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");
\r
361 assertThat(res, is(true));
\r
365 public void isValidIpAddress_invalid_ip_without_port() throws Exception {
\r
366 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");
\r
367 assertThat(res, is(false));
\r
371 public void isValidIpAddress_invalid_ip_with_port() throws Exception {
\r
372 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:443");
\r
373 assertThat(res, is(false));
\r
377 public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {
\r
378 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");
\r
379 assertThat(res, is(false));
\r
383 public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {
\r
384 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:443");
\r
385 assertThat(res, is(false));
\r