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.Rule;
\r
28 import org.junit.Test;
\r
29 import org.powermock.api.easymock.PowerMock;
\r
30 import org.powermock.core.classloader.annotations.PowerMockIgnore;
\r
31 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
32 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
34 @PrepareForTest(MicroServiceConfig.class)
\r
35 @PowerMockIgnore({"javax.ws.*"})
\r
36 public class MicroServiceConfigTest {
\r
39 public PowerMockRule powerMockRule = new PowerMockRule();
\r
42 public void getMsbServerAddrTest() {
\r
43 System.setProperty(MSB_ADDR, "test:80");
\r
44 assertThat("http://test:80", equalTo(getMsbServerAddr()));
\r
45 System.clearProperty(MicroServiceConfig.MSB_ADDR);
\r
49 public void getMsbServerIpTest() {
\r
50 System.setProperty(MSB_ADDR, "10.54.23.79:80");
\r
51 assertThat("10.54.23.79", equalTo(getMsbAddrInfo()[0]));
\r
52 assertThat("80", equalTo(getMsbAddrInfo()[1]));
\r
53 System.clearProperty(MSB_ADDR);
\r
57 public void getServiceIpTest() {
\r
58 System.setProperty(HOSTNAME, "127.0.0.1");
\r
59 assertThat("127.0.0.1", equalTo(getServiceAddrInfo()[0]));
\r
60 assertThat("80", equalTo(getServiceAddrInfo()[1]));
\r
61 System.clearProperty(HOSTNAME);
\r
65 public void getConsulAddrInfoTest() {
\r
66 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
67 assertThat("127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));
\r
68 System.clearProperty(CONSUL_HOST);
\r
72 public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {
\r
73 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
74 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
75 .andThrow(new RuntimeException("Invalid URL."));
\r
77 PowerMock.replayAll();
\r
79 assertThat(getConfigBindingServiceAddrInfo(), is(nullValue()));
\r
81 PowerMock.verifyAll();
\r
85 public void getConfigBindingServiceAddrInfoTest_consul_exists() throws Exception {
\r
86 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
87 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
88 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
89 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
91 PowerMock.replayAll();
\r
93 assertThat(getConfigBindingServiceAddrInfo(), equalTo("127.0.0.2:8080"));
\r
95 PowerMock.verifyAll();
\r
97 System.clearProperty(CONSUL_HOST);
\r
101 public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception {
\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\"}]");
\r
105 System.setProperty(CONSUL_HOST, "127.0.0.1");
\r
107 PowerMock.replayAll();
\r
109 assertThat(getConfigBindingServiceAddrInfo(), is(nullValue()));
\r
111 PowerMock.verifyAll();
\r
113 System.clearProperty(CONSUL_HOST);
\r
117 public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
\r
118 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
119 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
120 .andThrow(new RuntimeException("Invalid URL.")).times(2);
\r
122 PowerMock.replayAll();
\r
124 assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue()));
\r
126 PowerMock.verifyAll();
\r
130 public void getServiceAddrInfoFromCBS_consul_exists_service_not_exist() throws Exception {
\r
131 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
132 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
133 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
134 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
137 PowerMock.replayAll();
\r
138 assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue()));
\r
139 PowerMock.verifyAll();
\r
143 public void getServiceAddrInfoFromCBS_normal() throws Exception {
\r
144 System.setProperty(HOSTNAME, "rule-mgmt");
\r
145 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
146 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
147 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
148 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
149 .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}");
\r
151 PowerMock.replayAll();
\r
153 assertThat(getServiceAddrInfoFromCBS("rule-mgmt"), equalTo("127.0.0.3:5432"));
\r
155 PowerMock.verifyAll();
\r
157 System.clearProperty(HOSTNAME);
\r
161 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
162 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
163 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
164 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
165 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
166 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
167 .andReturn("{\"" + MSB_ADDR + "\": \"[\\\"127.0.0.3:5432\\\"]\"}");
\r
169 PowerMock.replayAll();
\r
170 String[] msbInfo = getMsbAddrInfo();
\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(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 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
183 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
184 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
185 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
188 PowerMock.replayAll();
\r
189 String[] msbInfo = getMsbAddrInfo();
\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(MSB_ADDR);
\r
199 public void getServiceAddrInfo_msb_registered() 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.2\", \"ServicePort\": \"8080\"}]");
\r
204 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
205 .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}");
\r
207 PowerMock.replayAll();
\r
208 String[] msbInfo = getServiceAddrInfo();
\r
209 PowerMock.verifyAll();
\r
211 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
212 assertThat(msbInfo[1], equalTo("5432"));
\r
214 System.clearProperty(HOSTNAME);
\r
218 public void getServiceAddrInfo_msb_not_registered() throws Exception {
\r
219 System.setProperty(HOSTNAME, "10.74.5.8:1545");
\r
220 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
221 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
222 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
223 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
226 PowerMock.replayAll();
\r
227 String[] msbInfo = getServiceAddrInfo();
\r
228 PowerMock.verifyAll();
\r
230 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
231 assertThat(msbInfo[1], equalTo("1545"));
\r
233 System.clearProperty(HOSTNAME);
\r
237 public void getServiceAddrInfo_msb_not_registered_full_addr() throws Exception {
\r
238 System.setProperty(HOSTNAME, "http://10.74.5.8:1545");
\r
239 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
240 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
241 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
242 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
245 PowerMock.replayAll();
\r
246 String[] msbInfo = getServiceAddrInfo();
\r
247 PowerMock.verifyAll();
\r
249 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
250 assertThat(msbInfo[1], equalTo("1545"));
\r
252 System.clearProperty(MSB_ADDR);
\r
256 public void getServiceAddrInfo_msb_not_registered_no_port() throws Exception {
\r
257 System.setProperty(HOSTNAME, "http://10.74.5.8");
\r
258 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
259 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
260 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
261 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
264 PowerMock.replayAll();
\r
265 String[] msbInfo = getServiceAddrInfo();
\r
266 PowerMock.verifyAll();
\r
268 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
269 assertThat(msbInfo[1], equalTo("80"));
\r
271 System.clearProperty(MSB_ADDR);
\r
275 public void getServiceAddrInfo_msb_not_registered_only_ip() throws Exception {
\r
276 System.setProperty(HOSTNAME, "10.74.5.8");
\r
277 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
278 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
279 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
280 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
283 PowerMock.replayAll();
\r
284 String[] msbInfo = getServiceAddrInfo();
\r
285 PowerMock.verifyAll();
\r
287 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
288 assertThat(msbInfo[1], equalTo("80"));
\r
290 System.clearProperty(MSB_ADDR);
\r
294 public void getServiceAddrInfo_msb_not_registered_full_addr_https() throws Exception {
\r
295 System.setProperty(HOSTNAME, "https://10.74.5.8:5432");
\r
296 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
297 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
298 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
299 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
302 PowerMock.replayAll();
\r
303 String[] msbInfo = getServiceAddrInfo();
\r
304 PowerMock.verifyAll();
\r
306 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
307 assertThat(msbInfo[1], equalTo("5432"));
\r
309 System.clearProperty(MSB_ADDR);
\r