2 * Copyright 2017-2022 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.holmes.common.utils;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.onap.holmes.common.config.MicroServiceConfig;
23 import org.onap.holmes.common.exception.CorrelationException;
24 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
25 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
26 import org.powermock.api.easymock.PowerMock;
27 import org.powermock.core.classloader.annotations.PowerMockIgnore;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.modules.junit4.PowerMockRunner;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.core.MediaType;
34 import static org.easymock.EasyMock.anyObject;
35 import static org.easymock.EasyMock.expect;
36 import static org.powermock.api.easymock.PowerMock.createMock;
37 import static org.powermock.api.easymock.PowerMock.expectNew;
39 @PrepareForTest({MicroServiceConfig.class, JerseyClient.class})
40 @RunWith(PowerMockRunner.class)
41 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
42 public class MsbRegisterTest {
44 private JerseyClient mockedJerseyClient;
45 private MicroServiceInfo msi;
48 public void before() throws Exception {
49 msi = new MicroServiceInfo();
50 String[] msbAddrInfo = {"127.0.0.1", "80"};
52 PowerMock.mockStatic(MicroServiceConfig.class);
53 expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo);
55 mockedJerseyClient = createMock(JerseyClient.class);
56 expectNew(JerseyClient.class).andReturn(mockedJerseyClient);
60 public void test_register2Msb_normal() {
61 expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient);
62 expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient);
63 expect(mockedJerseyClient.post(anyObject(String.class),
64 anyObject(Entity.class),
65 anyObject(Class.class)))
66 .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
67 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
68 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
69 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
70 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
71 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
72 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
73 MicroServiceFullInfo.class));
75 PowerMock.replayAll();
77 MsbRegister msbRegister = new MsbRegister();
79 msbRegister.register2Msb(msi);
80 } catch (CorrelationException e) {
84 PowerMock.verifyAll();
88 public void test_register2Msb_fail_n_times() {
90 expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes);
91 expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes);
92 expect(mockedJerseyClient.post(anyObject(String.class),
93 anyObject(Entity.class),
94 anyObject(Class.class)))
95 .andReturn(null).times(requestTimes - 1);
97 expect(mockedJerseyClient.post(anyObject(String.class),
98 anyObject(Entity.class),
99 anyObject(Class.class)))
100 .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
101 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
102 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
103 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
104 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
105 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
106 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
107 MicroServiceFullInfo.class));
109 PowerMock.replayAll();
111 MsbRegister msbRegister = new MsbRegister();
112 msbRegister.setInterval(1);
114 msbRegister.register2Msb(msi);
115 } catch (CorrelationException e) {
119 PowerMock.verifyAll();