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_exists_propertie_not_exist() 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
126 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");
\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 getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
\r
142 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
143 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
144 .andThrow(new RuntimeException("Invalid URL.")).times(2);
\r
146 PowerMock.replayAll();
\r
148 assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue()));
\r
150 PowerMock.verifyAll();
\r
154 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception {
\r
155 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
156 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
159 PowerMock.replayAll();
\r
160 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue()));
\r
161 PowerMock.verifyAll();
\r
165 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
166 System.setProperty(MSB_IAG_SERVICE_HOST, "10.74.5.8");
\r
167 System.setProperty(MSB_IAG_SERVICE_PORT, "1545");
\r
168 System.setProperty(HOSTNAME, "rule-mgmt");
\r
169 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
170 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
171 .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}");
\r
173 PowerMock.replayAll();
\r
174 String[] msbInfo = getMsbIpAndPort();
\r
175 PowerMock.verifyAll();
\r
177 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
178 assertThat(msbInfo[1], equalTo("5432"));
\r
180 System.clearProperty(HOSTNAME);
\r
181 System.clearProperty(MSB_IAG_SERVICE_PORT);
\r
182 System.clearProperty(MSB_IAG_SERVICE_HOST);
\r
186 public void getMsbAddrInfo_msb_not_registered() throws Exception {
\r
187 System.setProperty(MSB_IAG_SERVICE_HOST, "10.74.5.8");
\r
188 System.setProperty(MSB_IAG_SERVICE_PORT, "1545");
\r
189 System.setProperty(HOSTNAME, "rule-mgmt");
\r
190 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
191 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
194 PowerMock.replayAll();
\r
195 String[] msbInfo = getMsbIpAndPort();
\r
196 PowerMock.verifyAll();
\r
198 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
199 assertThat(msbInfo[1], equalTo("1545"));
\r
201 System.clearProperty(HOSTNAME);
\r
202 System.clearProperty(MSB_IAG_SERVICE_PORT);
\r
203 System.clearProperty(MSB_IAG_SERVICE_HOST);
\r
207 public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception {
\r
208 System.setProperty(HOSTNAME, "rule-mgmt");
\r
209 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
210 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
211 .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]");
\r
213 PowerMock.replayAll();
\r
214 String[] msbInfo = getMicroServiceIpAndPort();
\r
215 PowerMock.verifyAll();
\r
217 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
218 assertThat(msbInfo[1], equalTo("5432"));
\r
220 System.clearProperty(HOSTNAME);
\r
224 public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {
\r
225 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
226 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
227 String hostname = String.format("http://%s:%s", ip, port);
\r
228 System.setProperty(HOSTNAME, hostname);
\r
229 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
230 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
233 PowerMock.replayAll();
\r
234 String[] msbInfo = getMicroServiceIpAndPort();
\r
235 PowerMock.verifyAll();
\r
237 assertThat(msbInfo[0], equalTo(ip));
\r
238 assertThat(msbInfo[1], equalTo(port));
\r
240 System.clearProperty(HOSTNAME);
\r
244 public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {
\r
245 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
246 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
247 String hostname = String.format("http://%s:%s", ip, port);
\r
248 System.setProperty(HOSTNAME, hostname);
\r
249 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
250 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
253 PowerMock.replayAll();
\r
254 String[] msbInfo = getMicroServiceIpAndPort();
\r
255 PowerMock.verifyAll();
\r
257 assertThat(msbInfo[0], equalTo(ip));
\r
258 assertThat(msbInfo[1], equalTo(port));
\r
260 System.clearProperty(HOSTNAME);
\r
264 public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {
\r
265 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
266 String hostname = String.format("http://%s", ip);
\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("80"));
\r
279 System.clearProperty(HOSTNAME);
\r
283 public void getMicroServiceIpAndPort_service_not_registered_only_ip() 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_full_addr_https() throws Exception {
\r
303 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
304 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
305 String hostname = String.format("http://%s:%s", ip, port);
\r
306 System.setProperty(HOSTNAME, hostname);
\r
307 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
308 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
311 PowerMock.replayAll();
\r
312 String[] msbInfo = getMicroServiceIpAndPort();
\r
313 PowerMock.verifyAll();
\r
315 assertThat(msbInfo[0], equalTo(ip));
\r
316 assertThat(msbInfo[1], equalTo(port));
\r
318 System.clearProperty(HOSTNAME);
\r
322 public void isValidIpAddress_with_port() throws Exception {
\r
323 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");
\r
324 assertThat(res, is(true));
\r
328 public void isValidIpAddress_without_port() throws Exception {
\r
329 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");
\r
330 assertThat(res, is(true));
\r
334 public void isValidIpAddress_with_port_with_http_prefix() throws Exception {
\r
335 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");
\r
336 assertThat(res, is(true));
\r
340 public void isValidIpAddress_without_port_with_https_prefix() throws Exception {
\r
341 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");
\r
342 assertThat(res, is(true));
\r
346 public void isValidIpAddress_invalid_ip_without_port() throws Exception {
\r
347 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");
\r
348 assertThat(res, is(false));
\r
352 public void isValidIpAddress_invalid_ip_with_port() throws Exception {
\r
353 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:443");
\r
354 assertThat(res, is(false));
\r
358 public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {
\r
359 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");
\r
360 assertThat(res, is(false));
\r
364 public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {
\r
365 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:443");
\r
366 assertThat(res, is(false));
\r