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_ADDR, "test:80");
\r
47 assertThat("http://test:80", equalTo(getMsbServerAddrWithHttpPrefix()));
\r
48 System.clearProperty(MicroServiceConfig.MSB_ADDR);
\r
52 public void getMsbServerIpTest() {
\r
53 System.setProperty(MSB_ADDR, "10.54.23.79:80");
\r
54 System.setProperty(HOSTNAME, "rule-mgmt");
\r
55 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
56 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
57 .andReturn("{\"msb.hostname\": \"10.54.23.79:80\"}").times(2);
\r
58 PowerMock.replayAll();
\r
59 assertThat("10.54.23.79", equalTo(getMsbIpAndPort()[0]));
\r
60 assertThat("80", equalTo(getMsbIpAndPort()[1]));
\r
61 System.clearProperty(MicroServiceConfig.HOSTNAME);
\r
62 System.clearProperty(MSB_ADDR);
\r
66 public void getServiceIpTest() {
\r
67 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
68 String hostname = String.format("http://%s", ip);
\r
69 System.setProperty(HOSTNAME, hostname);
\r
70 assertThat(ip, equalTo(getMicroServiceIpAndPort()[0]));
\r
71 assertThat("80", equalTo(getMicroServiceIpAndPort()[1]));
\r
72 System.clearProperty(HOSTNAME);
\r
76 public void getConsulAddrInfoTest() {
\r
77 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
78 assertThat("http://127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));
\r
79 System.clearProperty(CONSUL_HOST);
\r
83 public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {
\r
84 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
85 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
86 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
87 .andThrow(new RuntimeException("Invalid URL."));
\r
89 PowerMock.replayAll();
\r
91 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE))
\r
94 PowerMock.verifyAll();
\r
95 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
99 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists() throws Exception {
\r
100 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
101 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
102 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
103 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
104 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
106 PowerMock.replayAll();
\r
108 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
109 equalTo("http://127.0.0.2:8080"));
\r
111 PowerMock.verifyAll();
\r
113 System.clearProperty(CONSUL_HOST);
\r
114 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
118 public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception {
\r
119 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
120 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
121 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
122 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");
\r
123 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
125 PowerMock.replayAll();
\r
127 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
130 PowerMock.verifyAll();
\r
132 System.clearProperty(CONSUL_HOST);
\r
133 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
137 public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
\r
138 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
139 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
140 .andThrow(new RuntimeException("Invalid URL.")).times(2);
\r
142 PowerMock.replayAll();
\r
144 assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue()));
\r
146 PowerMock.verifyAll();
\r
150 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception {
\r
151 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
152 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
155 PowerMock.replayAll();
\r
156 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue()));
\r
157 PowerMock.verifyAll();
\r
161 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
162 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
163 System.setProperty(HOSTNAME, "rule-mgmt");
\r
164 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
165 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
166 .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}");
\r
168 PowerMock.replayAll();
\r
169 String[] msbInfo = getMsbIpAndPort();
\r
170 PowerMock.verifyAll();
\r
172 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
173 assertThat(msbInfo[1], equalTo("5432"));
\r
175 System.clearProperty(HOSTNAME);
\r
176 System.clearProperty(MSB_ADDR);
\r
180 public void getMsbAddrInfo_msb_not_registered() throws Exception {
\r
181 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
182 System.setProperty(HOSTNAME, "rule-mgmt");
\r
183 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
184 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
187 PowerMock.replayAll();
\r
188 String[] msbInfo = getMsbIpAndPort();
\r
189 PowerMock.verifyAll();
\r
191 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
192 assertThat(msbInfo[1], equalTo("1545"));
\r
194 System.clearProperty(HOSTNAME);
\r
195 System.clearProperty(MSB_ADDR);
\r
199 public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception {
\r
200 System.setProperty(HOSTNAME, "rule-mgmt");
\r
201 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
202 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
203 .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]");
\r
205 PowerMock.replayAll();
\r
206 String[] msbInfo = getMicroServiceIpAndPort();
\r
207 PowerMock.verifyAll();
\r
209 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
210 assertThat(msbInfo[1], equalTo("5432"));
\r
212 System.clearProperty(HOSTNAME);
\r
216 public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {
\r
217 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
218 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
219 String hostname = String.format("http://%s:%s", ip, port);
\r
220 System.setProperty(HOSTNAME, hostname);
\r
221 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
222 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
225 PowerMock.replayAll();
\r
226 String[] msbInfo = getMicroServiceIpAndPort();
\r
227 PowerMock.verifyAll();
\r
229 assertThat(msbInfo[0], equalTo(ip));
\r
230 assertThat(msbInfo[1], equalTo(port));
\r
232 System.clearProperty(HOSTNAME);
\r
236 public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {
\r
237 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
238 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
239 String hostname = String.format("http://%s:%s", ip, port);
\r
240 System.setProperty(HOSTNAME, hostname);
\r
241 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
242 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
245 PowerMock.replayAll();
\r
246 String[] msbInfo = getMicroServiceIpAndPort();
\r
247 PowerMock.verifyAll();
\r
249 assertThat(msbInfo[0], equalTo(ip));
\r
250 assertThat(msbInfo[1], equalTo(port));
\r
252 System.clearProperty(MSB_ADDR);
\r
256 public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {
\r
257 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
258 String hostname = String.format("http://%s", ip);
\r
259 System.setProperty(HOSTNAME, hostname);
\r
260 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
261 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
264 PowerMock.replayAll();
\r
265 String[] msbInfo = getMicroServiceIpAndPort();
\r
266 PowerMock.verifyAll();
\r
268 assertThat(msbInfo[0], equalTo(ip));
\r
269 assertThat(msbInfo[1], equalTo("80"));
\r
271 System.clearProperty(MSB_ADDR);
\r
275 public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception {
\r
276 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
277 String hostname = String.format("http://%s", ip);
\r
278 System.setProperty(HOSTNAME, hostname);
\r
279 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
280 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
283 PowerMock.replayAll();
\r
284 String[] msbInfo = getMicroServiceIpAndPort();
\r
285 PowerMock.verifyAll();
\r
287 assertThat(msbInfo[0], equalTo(ip));
\r
288 assertThat(msbInfo[1], equalTo("80"));
\r
290 System.clearProperty(MSB_ADDR);
\r
294 public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception {
\r
295 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
296 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
297 String hostname = String.format("http://%s:%s", ip, port);
\r
298 System.setProperty(HOSTNAME, hostname);
\r
299 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
300 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
303 PowerMock.replayAll();
\r
304 String[] msbInfo = getMicroServiceIpAndPort();
\r
305 PowerMock.verifyAll();
\r
307 assertThat(msbInfo[0], equalTo(ip));
\r
308 assertThat(msbInfo[1], equalTo(port));
\r
310 System.clearProperty(MSB_ADDR);
\r
314 public void isValidIpAddress_with_port() throws Exception {
\r
315 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");
\r
316 assertThat(res, is(true));
\r
320 public void isValidIpAddress_without_port() throws Exception {
\r
321 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");
\r
322 assertThat(res, is(true));
\r
326 public void isValidIpAddress_with_port_with_http_prefix() throws Exception {
\r
327 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");
\r
328 assertThat(res, is(true));
\r
332 public void isValidIpAddress_without_port_with_https_prefix() throws Exception {
\r
333 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");
\r
334 assertThat(res, is(true));
\r
338 public void isValidIpAddress_invalid_ip_without_port() throws Exception {
\r
339 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");
\r
340 assertThat(res, is(false));
\r
344 public void isValidIpAddress_invalid_ip_with_port() throws Exception {
\r
345 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:80");
\r
346 assertThat(res, is(false));
\r
350 public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {
\r
351 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");
\r
352 assertThat(res, is(false));
\r
356 public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {
\r
357 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:80");
\r
358 assertThat(res, is(false));
\r