2 * Copyright 2017 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.Rule;
\r
23 import org.junit.Test;
\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.rule.PowerMockRule;
\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 public class MicroServiceConfigTest {
\r
40 private static String ACTUAL_HOSTNAME = System.getenv(HOSTNAME);
\r
43 public PowerMockRule powerMockRule = new PowerMockRule();
\r
46 public void getMsbServerAddrTest() {
\r
47 System.setProperty(MSB_ADDR, "test:80");
\r
48 assertThat("http://test:80", equalTo(getMsbServerAddrWithHttpPrefix()));
\r
49 System.clearProperty(MicroServiceConfig.MSB_ADDR);
\r
53 public void getMsbServerIpTest() {
\r
54 System.setProperty(MSB_ADDR, "10.54.23.79:80");
\r
55 System.setProperty(HOSTNAME, "rule-mgmt");
\r
56 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
57 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
58 .andReturn("{\"msb.hostname\": \"10.54.23.79:80\"}").times(2);
\r
59 PowerMock.replayAll();
\r
60 assertThat("10.54.23.79", equalTo(getMsbIpAndPort()[0]));
\r
61 assertThat("80", equalTo(getMsbIpAndPort()[1]));
\r
62 System.clearProperty(MicroServiceConfig.HOSTNAME);
\r
63 System.clearProperty(MSB_ADDR);
\r
67 public void getServiceIpTest() {
\r
68 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
69 String hostname = String.format("http://%s", ip);
\r
70 System.setProperty(HOSTNAME, hostname);
\r
71 assertThat(ip, equalTo(getMicroServiceIpAndPort()[0]));
\r
72 assertThat("80", equalTo(getMicroServiceIpAndPort()[1]));
\r
73 System.clearProperty(HOSTNAME);
\r
77 public void getConsulAddrInfoTest() {
\r
78 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
79 assertThat("http://127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));
\r
80 System.clearProperty(CONSUL_HOST);
\r
84 public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {
\r
85 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
86 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
87 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
88 .andThrow(new RuntimeException("Invalid URL."));
\r
90 PowerMock.replayAll();
\r
92 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE))
\r
95 PowerMock.verifyAll();
\r
96 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
100 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists() throws Exception {
\r
101 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
102 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
103 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
104 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
105 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
107 PowerMock.replayAll();
\r
109 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
110 equalTo("http://127.0.0.2:8080"));
\r
112 PowerMock.verifyAll();
\r
114 System.clearProperty(CONSUL_HOST);
\r
115 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
119 public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception {
\r
120 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
121 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
122 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
123 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");
\r
124 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
126 PowerMock.replayAll();
\r
128 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
131 PowerMock.verifyAll();
\r
133 System.clearProperty(CONSUL_HOST);
\r
134 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
138 public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
\r
139 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
140 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
141 .andThrow(new RuntimeException("Invalid URL.")).times(2);
\r
143 PowerMock.replayAll();
\r
145 assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue()));
\r
147 PowerMock.verifyAll();
\r
151 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception {
\r
152 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
153 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
156 PowerMock.replayAll();
\r
157 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue()));
\r
158 PowerMock.verifyAll();
\r
162 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
163 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
164 System.setProperty(HOSTNAME, "rule-mgmt");
\r
165 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
166 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
167 .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}");
\r
169 PowerMock.replayAll();
\r
170 String[] msbInfo = getMsbIpAndPort();
\r
171 PowerMock.verifyAll();
\r
173 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
174 assertThat(msbInfo[1], equalTo("5432"));
\r
176 System.clearProperty(HOSTNAME);
\r
177 System.clearProperty(MSB_ADDR);
\r
181 public void getMsbAddrInfo_msb_not_registered() throws Exception {
\r
182 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
183 System.setProperty(HOSTNAME, "rule-mgmt");
\r
184 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
185 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
188 PowerMock.replayAll();
\r
189 String[] msbInfo = getMsbIpAndPort();
\r
190 PowerMock.verifyAll();
\r
192 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
193 assertThat(msbInfo[1], equalTo("1545"));
\r
195 System.clearProperty(HOSTNAME);
\r
196 System.clearProperty(MSB_ADDR);
\r
200 public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception {
\r
201 System.setProperty(HOSTNAME, "rule-mgmt");
\r
202 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
203 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
204 .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]");
\r
206 PowerMock.replayAll();
\r
207 String[] msbInfo = getMicroServiceIpAndPort();
\r
208 PowerMock.verifyAll();
\r
210 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
211 assertThat(msbInfo[1], equalTo("5432"));
\r
213 System.clearProperty(HOSTNAME);
\r
217 public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {
\r
218 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
219 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
220 String hostname = String.format("http://%s:%s", ip, port);
\r
221 System.setProperty(HOSTNAME, hostname);
\r
222 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
223 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
226 PowerMock.replayAll();
\r
227 String[] msbInfo = getMicroServiceIpAndPort();
\r
228 PowerMock.verifyAll();
\r
230 assertThat(msbInfo[0], equalTo(ip));
\r
231 assertThat(msbInfo[1], equalTo(port));
\r
233 System.clearProperty(HOSTNAME);
\r
237 public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {
\r
238 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
239 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
240 String hostname = String.format("http://%s:%s", ip, port);
\r
241 System.setProperty(HOSTNAME, hostname);
\r
242 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
243 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
246 PowerMock.replayAll();
\r
247 String[] msbInfo = getMicroServiceIpAndPort();
\r
248 PowerMock.verifyAll();
\r
250 assertThat(msbInfo[0], equalTo(ip));
\r
251 assertThat(msbInfo[1], equalTo(port));
\r
253 System.clearProperty(MSB_ADDR);
\r
257 public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {
\r
258 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
259 String hostname = String.format("http://%s", ip);
\r
260 System.setProperty(HOSTNAME, hostname);
\r
261 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
262 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
265 PowerMock.replayAll();
\r
266 String[] msbInfo = getMicroServiceIpAndPort();
\r
267 PowerMock.verifyAll();
\r
269 assertThat(msbInfo[0], equalTo(ip));
\r
270 assertThat(msbInfo[1], equalTo("80"));
\r
272 System.clearProperty(MSB_ADDR);
\r
276 public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception {
\r
277 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
278 String hostname = String.format("http://%s", ip);
\r
279 System.setProperty(HOSTNAME, hostname);
\r
280 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
281 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
284 PowerMock.replayAll();
\r
285 String[] msbInfo = getMicroServiceIpAndPort();
\r
286 PowerMock.verifyAll();
\r
288 assertThat(msbInfo[0], equalTo(ip));
\r
289 assertThat(msbInfo[1], equalTo("80"));
\r
291 System.clearProperty(MSB_ADDR);
\r
295 public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception {
\r
296 String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;
\r
297 String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";
\r
298 String hostname = String.format("http://%s:%s", ip, port);
\r
299 System.setProperty(HOSTNAME, hostname);
\r
300 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
301 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
304 PowerMock.replayAll();
\r
305 String[] msbInfo = getMicroServiceIpAndPort();
\r
306 PowerMock.verifyAll();
\r
308 assertThat(msbInfo[0], equalTo(ip));
\r
309 assertThat(msbInfo[1], equalTo(port));
\r
311 System.clearProperty(MSB_ADDR);
\r
315 public void isValidIpAddress_with_port() throws Exception {
\r
316 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");
\r
317 assertThat(res, is(true));
\r
321 public void isValidIpAddress_without_port() throws Exception {
\r
322 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");
\r
323 assertThat(res, is(true));
\r
327 public void isValidIpAddress_with_port_with_http_prefix() throws Exception {
\r
328 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");
\r
329 assertThat(res, is(true));
\r
333 public void isValidIpAddress_without_port_with_https_prefix() throws Exception {
\r
334 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");
\r
335 assertThat(res, is(true));
\r
339 public void isValidIpAddress_invalid_ip_without_port() throws Exception {
\r
340 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");
\r
341 assertThat(res, is(false));
\r
345 public void isValidIpAddress_invalid_ip_with_port() throws Exception {
\r
346 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:80");
\r
347 assertThat(res, is(false));
\r
351 public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {
\r
352 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");
\r
353 assertThat(res, is(false));
\r
357 public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {
\r
358 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:80");
\r
359 assertThat(res, is(false));
\r