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
139 assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue()));
\r
141 PowerMock.verifyAll();
\r
145 public void getServiceAddrInfoFromCBS_normal() throws Exception {
\r
146 System.setProperty(HOSTNAME, "rule-mgmt");
\r
147 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
148 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
149 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
150 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
151 .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}");
\r
153 PowerMock.replayAll();
\r
155 assertThat(getServiceAddrInfoFromCBS("rule-mgmt"), equalTo("127.0.0.3:5432"));
\r
157 PowerMock.verifyAll();
\r
159 System.clearProperty(HOSTNAME);
\r
163 public void getMsbAddrInfo_msb_registered() throws Exception {
\r
164 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
165 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
166 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
167 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
168 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
169 .andReturn("{\"" + MSB_ADDR + "\": \"[\\\"127.0.0.3:5432\\\"]\"}");
\r
171 PowerMock.replayAll();
\r
172 String[] msbInfo = getMsbAddrInfo();
\r
173 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
174 assertThat(msbInfo[1], equalTo("5432"));
\r
176 PowerMock.verifyAll();
\r
178 System.clearProperty(MSB_ADDR);
\r
182 public void getMsbAddrInfo_msb_not_registered() throws Exception {
\r
183 System.setProperty(MSB_ADDR, "10.74.5.8:1545");
\r
184 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
185 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
186 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
187 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
190 PowerMock.replayAll();
\r
191 String[] msbInfo = getMsbAddrInfo();
\r
192 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
193 assertThat(msbInfo[1], equalTo("1545"));
\r
195 PowerMock.verifyAll();
\r
197 System.clearProperty(MSB_ADDR);
\r
201 public void getServiceAddrInfo_msb_registered() throws Exception {
\r
202 System.setProperty(HOSTNAME, "rule-mgmt");
\r
203 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
204 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
205 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
206 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
207 .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}");
\r
209 PowerMock.replayAll();
\r
210 String[] msbInfo = getServiceAddrInfo();
\r
211 assertThat(msbInfo[0], equalTo("127.0.0.3"));
\r
212 assertThat(msbInfo[1], equalTo("5432"));
\r
214 PowerMock.verifyAll();
\r
216 System.clearProperty(HOSTNAME);
\r
220 public void getServiceAddrInfo_msb_not_registered() throws Exception {
\r
221 System.setProperty(HOSTNAME, "10.74.5.8:1545");
\r
222 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
223 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
224 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
225 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
228 PowerMock.replayAll();
\r
229 String[] msbInfo = getServiceAddrInfo();
\r
230 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
231 assertThat(msbInfo[1], equalTo("1545"));
\r
233 PowerMock.verifyAll();
\r
235 System.clearProperty(HOSTNAME);
\r
239 public void getServiceAddrInfo_msb_not_registered_full_addr() throws Exception {
\r
240 System.setProperty(HOSTNAME, "http://10.74.5.8:1545");
\r
241 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
242 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
243 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
244 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
247 PowerMock.replayAll();
\r
248 String[] msbInfo = getServiceAddrInfo();
\r
249 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
250 assertThat(msbInfo[1], equalTo("1545"));
\r
252 PowerMock.verifyAll();
\r
254 System.clearProperty(MSB_ADDR);
\r
258 public void getServiceAddrInfo_msb_not_registered_no_port() throws Exception {
\r
259 System.setProperty(HOSTNAME, "http://10.74.5.8");
\r
260 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
261 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
262 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
263 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
266 PowerMock.replayAll();
\r
267 String[] msbInfo = getServiceAddrInfo();
\r
268 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
269 assertThat(msbInfo[1], equalTo("80"));
\r
271 PowerMock.verifyAll();
\r
273 System.clearProperty(MSB_ADDR);
\r
277 public void getServiceAddrInfo_msb_not_registered_only_ip() throws Exception {
\r
278 System.setProperty(HOSTNAME, "10.74.5.8");
\r
279 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
280 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
281 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
282 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
285 PowerMock.replayAll();
\r
286 String[] msbInfo = getServiceAddrInfo();
\r
287 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
288 assertThat(msbInfo[1], equalTo("80"));
\r
290 PowerMock.verifyAll();
\r
292 System.clearProperty(MSB_ADDR);
\r
296 public void getServiceAddrInfo_msb_not_registered_full_addr_https() throws Exception {
\r
297 System.setProperty(HOSTNAME, "https://10.74.5.8:5432");
\r
298 PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
\r
299 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
300 .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
\r
301 PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
\r
304 PowerMock.replayAll();
\r
305 String[] msbInfo = getServiceAddrInfo();
\r
306 assertThat(msbInfo[0], equalTo("10.74.5.8"));
\r
307 assertThat(msbInfo[1], equalTo("5432"));
\r
309 PowerMock.verifyAll();
\r
311 System.clearProperty(MSB_ADDR);
\r