summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java
blob: 59af9d38e706c307e9d875f587d6761f35a3224c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
 * Copyright 2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.holmes.common.config;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.onap.holmes.common.config.MicroServiceConfig.*;

import org.easymock.EasyMock;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;

@PrepareForTest(MicroServiceConfig.class)
@PowerMockIgnore({"javax.ws.*"})
public class MicroServiceConfigTest {

    @Rule
    public PowerMockRule powerMockRule = new PowerMockRule();

    @Test
    public void getMsbServerAddrTest() {
        System.setProperty(MSB_ADDR, "test:80");
        assertThat("http://test:80", equalTo(getMsbServerAddrWithHttpPrefix()));
        System.clearProperty(MicroServiceConfig.MSB_ADDR);
    }

    @Test
    public void getMsbServerIpTest() {
        System.setProperty(MSB_ADDR, "10.54.23.79:80");
        System.setProperty(HOSTNAME, "rule-mgmt");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
        EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
                .andReturn("{\"msb.hostname\": \"10.54.23.79:80\"}").times(2);
        PowerMock.replayAll();
        assertThat("10.54.23.79", equalTo(getMsbIpAndPort()[0]));
        assertThat("80", equalTo(getMsbIpAndPort()[1]));
        System.clearProperty(MicroServiceConfig.HOSTNAME);
        System.clearProperty(MSB_ADDR);
    }

    @Test
    public void getServiceIpTest() {
        System.setProperty(HOSTNAME, "127.0.0.1");
        assertThat("127.0.0.1", equalTo(getMicroServiceIpAndPort()[0]));
        assertThat("80", equalTo(getMicroServiceIpAndPort()[1]));
        System.clearProperty(HOSTNAME);
    }

    @Test
    public void getConsulAddrInfoTest() {
        System.setProperty(CONSUL_HOST, "127.0.0.1");
        assertThat("http://127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));
        System.clearProperty(CONSUL_HOST);
    }

    @Test
    public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {
        System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andThrow(new RuntimeException("Invalid URL."));

        PowerMock.replayAll();

        assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE))
                , is(nullValue()));

        PowerMock.verifyAll();
        System.clearProperty(CONFIG_BINDING_SERVICE);
    }

    @Test
    public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists() throws Exception {
        System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");
        System.setProperty(CONSUL_HOST, "127.0.0.1");

        PowerMock.replayAll();

        assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
                equalTo("http://127.0.0.2:8080"));

        PowerMock.verifyAll();

        System.clearProperty(CONSUL_HOST);
        System.clearProperty(CONFIG_BINDING_SERVICE);
    }

    @Test
    public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception {
        System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");
        System.setProperty(CONSUL_HOST, "127.0.0.1");

        PowerMock.replayAll();

        assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
                is(nullValue()));

        PowerMock.verifyAll();

        System.clearProperty(CONSUL_HOST);
        System.clearProperty(CONFIG_BINDING_SERVICE);
    }

    @Test
    public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andThrow(new RuntimeException("Invalid URL.")).times(2);

        PowerMock.replayAll();

        assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue()));

        PowerMock.verifyAll();
    }

    @Test
    public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception {
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("[]");

        PowerMock.replayAll();
        assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue()));
        PowerMock.verifyAll();
    }

    @Ignore
    public void getMsbAddrInfo_msb_registered() throws Exception {
        System.setProperty(MSB_ADDR, "10.74.5.8:1545");
        System.setProperty(HOSTNAME, "rule-mgmt");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
        EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
                .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}");

        PowerMock.replayAll();
        String[] msbInfo = getMsbIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("127.0.0.3"));
        assertThat(msbInfo[1], equalTo("5432"));

        System.clearProperty(HOSTNAME);
        System.clearProperty(MSB_ADDR);
    }

    @Ignore
    public void getMsbAddrInfo_msb_not_registered() throws Exception {
        System.setProperty(MSB_ADDR, "10.74.5.8:1545");
        System.setProperty(HOSTNAME, "rule-mgmt");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class);
        EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME)))
                .andReturn("{}");

        PowerMock.replayAll();
        String[] msbInfo = getMsbIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("10.74.5.8"));
        assertThat(msbInfo[1], equalTo("1545"));

        System.clearProperty(HOSTNAME);
        System.clearProperty(MSB_ADDR);
    }

    @Test
    public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception {
        System.setProperty(HOSTNAME, "rule-mgmt");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]");

        PowerMock.replayAll();
        String[] msbInfo = getMicroServiceIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("127.0.0.3"));
        assertThat(msbInfo[1], equalTo("5432"));

        System.clearProperty(HOSTNAME);
    }

    @Test
    public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {
        System.setProperty(HOSTNAME, "10.74.5.8:1545");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("[]");

        PowerMock.replayAll();
        String[] msbInfo = getMicroServiceIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("10.74.5.8"));
        assertThat(msbInfo[1], equalTo("1545"));

        System.clearProperty(HOSTNAME);
    }

    @Test
    public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {
        System.setProperty(HOSTNAME, "http://10.74.5.8:1545");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("{}");

        PowerMock.replayAll();
        String[] msbInfo = getMicroServiceIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("10.74.5.8"));
        assertThat(msbInfo[1], equalTo("1545"));

        System.clearProperty(MSB_ADDR);
    }

    @Test
    public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {
        System.setProperty(HOSTNAME, "http://10.74.5.8");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("{}");

        PowerMock.replayAll();
        String[] msbInfo = getMicroServiceIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("10.74.5.8"));
        assertThat(msbInfo[1], equalTo("80"));

        System.clearProperty(MSB_ADDR);
    }

    @Test
    public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception {
        System.setProperty(HOSTNAME, "10.74.5.8");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("{}");

        PowerMock.replayAll();
        String[] msbInfo = getMicroServiceIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("10.74.5.8"));
        assertThat(msbInfo[1], equalTo("80"));

        System.clearProperty(MSB_ADDR);
    }

    @Test
    public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception {
        System.setProperty(HOSTNAME, "https://10.74.5.8:5432");
        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);
        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())
                .andReturn("[]");

        PowerMock.replayAll();
        String[] msbInfo = getMicroServiceIpAndPort();
        PowerMock.verifyAll();

        assertThat(msbInfo[0], equalTo("10.74.5.8"));
        assertThat(msbInfo[1], equalTo("5432"));

        System.clearProperty(MSB_ADDR);
    }
}