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 static org.hamcrest.core.Is.is;
\r
20 import static org.hamcrest.core.IsEqual.equalTo;
\r
21 import static org.hamcrest.core.IsNull.nullValue;
\r
22 import static org.junit.Assert.assertThat;
\r
23 import static org.junit.Assert.assertTrue;
\r
24 import static org.onap.holmes.common.config.MicroServiceConfig.*;
\r
26 import org.easymock.EasyMock;
\r
27 import org.junit.Ignore;
\r
28 import org.junit.Rule;
\r
29 import org.junit.Test;
\r
30 import org.powermock.api.easymock.PowerMock;
\r
31 import org.powermock.core.classloader.annotations.PowerMockIgnore;
\r
32 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
33 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
34 import org.powermock.reflect.internal.WhiteboxImpl;
\r
36 @PrepareForTest(MicroServiceConfig.class)
\r
37 @PowerMockIgnore({"javax.ws.*"})
\r
38 public class MicroServiceConfigTest {
\r
41 public PowerMockRule powerMockRule = new PowerMockRule();
\r
44 public void getMsbServerAddrTest() {
\r
45 System.setProperty(MSB_ADDR, "test:80");
\r
46 assertThat("http://test:80", equalTo(getMsbServerAddrWithHttpPrefix()));
\r
47 System.clearProperty(MicroServiceConfig.MSB_ADDR);
\r
51 public void getMsbServerIpTest() {
\r
52 System.setProperty(MSB_ADDR, "10.54.23.79:80");
\r
53 System.setProperty(HOSTNAME, "rule-mgmt");
\r
54 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
55 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
56 .andReturn("{\"msb.hostname\": \"10.54.23.79:80\"}").times(2);
\r
57 PowerMock.replayAll();
\r
58 assertThat("10.54.23.79", equalTo(getMsbIpAndPort()[0]));
\r
59 assertThat("80", equalTo(getMsbIpAndPort()[1]));
\r
60 System.clearProperty(MicroServiceConfig.HOSTNAME);
\r
61 System.clearProperty(MSB_ADDR);
\r
65 public void getServiceIpTest() {
\r
66 System.setProperty(HOSTNAME, "127.0.0.1");
\r
67 assertThat("127.0.0.1", equalTo(getMicroServiceIpAndPort()[0]));
\r
68 assertThat("80", equalTo(getMicroServiceIpAndPort()[1]));
\r
69 System.clearProperty(HOSTNAME);
\r
73 public void getConsulAddrInfoTest() {
\r
74 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
75 assertThat("http://127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));
\r
76 System.clearProperty(CONSUL_HOST);
\r
80 public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {
\r
81 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
82 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
83 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
84 .andThrow(new RuntimeException("Invalid URL."));
\r
86 PowerMock.replayAll();
\r
88 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE))
\r
91 PowerMock.verifyAll();
\r
92 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
96 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists() throws Exception {
\r
97 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
98 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
99 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
100 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
101 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
103 PowerMock.replayAll();
\r
105 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
106 equalTo("http://127.0.0.2:8080"));
\r
108 PowerMock.verifyAll();
\r
110 System.clearProperty(CONSUL_HOST);
\r
111 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
115 public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception {
\r
116 System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
\r
117 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
118 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
119 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");
\r
120 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
122 PowerMock.replayAll();
\r
124 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
\r
127 PowerMock.verifyAll();
\r
129 System.clearProperty(CONSUL_HOST);
\r
130 System.clearProperty(CONFIG_BINDING_SERVICE);
\r
134 public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
\r
135 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
136 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
137 .andThrow(new RuntimeException("Invalid URL.")).times(2);
\r
139 PowerMock.replayAll();
\r
141 assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue()));
\r
143 PowerMock.verifyAll();
\r
147 public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception {
\r
148 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
149 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
152 PowerMock.replayAll();
\r
153 assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue()));
\r
154 PowerMock.verifyAll();
\r
158 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
159 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
160 System.setProperty(HOSTNAME, "rule-mgmt");
\r
161 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
162 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
163 .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}");
\r
165 PowerMock.replayAll();
\r
166 String[] msbInfo = getMsbIpAndPort();
\r
167 PowerMock.verifyAll();
\r
169 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
170 assertThat(msbInfo[1], equalTo("5432"));
\r
172 System.clearProperty(HOSTNAME);
\r
173 System.clearProperty(MSB_ADDR);
\r
177 public void getMsbAddrInfo_msb_not_registered() throws Exception {
\r
178 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
179 System.setProperty(HOSTNAME, "rule-mgmt");
\r
180 PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
\r
181 EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
\r
184 PowerMock.replayAll();
\r
185 String[] msbInfo = getMsbIpAndPort();
\r
186 PowerMock.verifyAll();
\r
188 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
189 assertThat(msbInfo[1], equalTo("1545"));
\r
191 System.clearProperty(HOSTNAME);
\r
192 System.clearProperty(MSB_ADDR);
\r
196 public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception {
\r
197 System.setProperty(HOSTNAME, "rule-mgmt");
\r
198 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
199 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
200 .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]");
\r
202 PowerMock.replayAll();
\r
203 String[] msbInfo = getMicroServiceIpAndPort();
\r
204 PowerMock.verifyAll();
\r
206 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
207 assertThat(msbInfo[1], equalTo("5432"));
\r
209 System.clearProperty(HOSTNAME);
\r
213 public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {
\r
214 System.setProperty(HOSTNAME, "10.74.5.8:1545");
\r
215 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
216 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
219 PowerMock.replayAll();
\r
220 String[] msbInfo = getMicroServiceIpAndPort();
\r
221 PowerMock.verifyAll();
\r
223 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
224 assertThat(msbInfo[1], equalTo("1545"));
\r
226 System.clearProperty(HOSTNAME);
\r
230 public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {
\r
231 System.setProperty(HOSTNAME, "http://10.74.5.8:1545");
\r
232 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
233 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
236 PowerMock.replayAll();
\r
237 String[] msbInfo = getMicroServiceIpAndPort();
\r
238 PowerMock.verifyAll();
\r
240 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
241 assertThat(msbInfo[1], equalTo("1545"));
\r
243 System.clearProperty(MSB_ADDR);
\r
247 public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {
\r
248 System.setProperty(HOSTNAME, "http://10.74.5.8");
\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("10.74.5.8"));
\r
258 assertThat(msbInfo[1], equalTo("80"));
\r
260 System.clearProperty(MSB_ADDR);
\r
264 public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception {
\r
265 System.setProperty(HOSTNAME, "10.74.5.8");
\r
266 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
267 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
270 PowerMock.replayAll();
\r
271 String[] msbInfo = getMicroServiceIpAndPort();
\r
272 PowerMock.verifyAll();
\r
274 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
275 assertThat(msbInfo[1], equalTo("80"));
\r
277 System.clearProperty(MSB_ADDR);
\r
281 public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception {
\r
282 System.setProperty(HOSTNAME, "https://10.74.5.8:5432");
\r
283 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
284 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
287 PowerMock.replayAll();
\r
288 String[] msbInfo = getMicroServiceIpAndPort();
\r
289 PowerMock.verifyAll();
\r
291 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
292 assertThat(msbInfo[1], equalTo("5432"));
\r
294 System.clearProperty(MSB_ADDR);
\r
298 public void isValidIpAddress_with_port() throws Exception {
\r
299 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");
\r
300 assertThat(res, is(true));
\r
304 public void isValidIpAddress_without_port() throws Exception {
\r
305 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");
\r
306 assertThat(res, is(true));
\r
310 public void isValidIpAddress_with_port_with_http_prefix() throws Exception {
\r
311 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");
\r
312 assertThat(res, is(true));
\r
316 public void isValidIpAddress_without_port_with_https_prefix() throws Exception {
\r
317 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");
\r
318 assertThat(res, is(true));
\r
322 public void isValidIpAddress_invalid_ip_without_port() throws Exception {
\r
323 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");
\r
324 assertThat(res, is(false));
\r
328 public void isValidIpAddress_invalid_ip_with_port() throws Exception {
\r
329 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:80");
\r
330 assertThat(res, is(false));
\r
334 public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {
\r
335 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");
\r
336 assertThat(res, is(false));
\r
340 public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {
\r
341 boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:80");
\r
342 assertThat(res, is(false));
\r