2 * Copyright 2017-2020 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;
30 import org.powermock.reflect.internal.WhiteboxImpl;
32 import javax.ws.rs.client.Entity;
34 import static org.easymock.EasyMock.anyObject;
35 import static org.easymock.EasyMock.expect;
36 import static org.powermock.api.easymock.PowerMock.createPartialMock;
38 @PrepareForTest({MicroServiceConfig.class, JerseyClient.class})
39 @RunWith(PowerMockRunner.class)
40 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
41 public class MsbRegisterTest {
43 private MsbRegister msbRegister;
44 private JerseyClient mockedJerseyClient;
45 private MicroServiceInfo msi;
48 public void before() {
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 = createPartialMock(JerseyClient.class,
56 "post", new Class[]{String.class, Entity.class, Class.class});
58 msbRegister = new MsbRegister();
59 WhiteboxImpl.setInternalState(msbRegister, "client", mockedJerseyClient);
63 public void test_register2Msb_normal() {
64 expect(mockedJerseyClient.post(anyObject(String.class),
65 anyObject(Entity.class),
66 anyObject(Class.class)))
67 .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
68 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
69 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
70 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
71 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
72 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
73 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
74 MicroServiceFullInfo.class));
76 PowerMock.replayAll();
79 msbRegister.register2Msb(msi);
80 } catch (CorrelationException e) {
84 PowerMock.verifyAll();
88 public void test_register2Msb_fail_once() {
89 expect(mockedJerseyClient.post(anyObject(String.class),
90 anyObject(Entity.class),
91 anyObject(Class.class)))
94 expect(mockedJerseyClient.post(anyObject(String.class),
95 anyObject(Entity.class),
96 anyObject(Class.class)))
97 .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
98 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
99 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
100 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
101 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
102 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
103 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
104 MicroServiceFullInfo.class));
106 PowerMock.replayAll();
109 msbRegister.register2Msb(msi);
110 } catch (CorrelationException e) {
114 PowerMock.verifyAll();