Simplified HTTP(s) Related Tools
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / utils / MsbRegisterTest.java
1 /**
2  * Copyright 2017-2020 ZTE Corporation.
3  * <p>
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
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.
15  */
16
17 package org.onap.holmes.common.utils;
18
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;
31
32 import javax.ws.rs.client.Entity;
33
34 import static org.easymock.EasyMock.anyObject;
35 import static org.easymock.EasyMock.expect;
36 import static org.powermock.api.easymock.PowerMock.createPartialMock;
37
38 @PrepareForTest({MicroServiceConfig.class})
39 @RunWith(PowerMockRunner.class)
40 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
41 public class MsbRegisterTest {
42
43     private MsbRegister msbRegister;
44     private JerseyClient mockedJerseyClient;
45     private MicroServiceInfo msi;
46
47     @Before
48     public void before() {
49         msi = new MicroServiceInfo();
50         String[] msbAddrInfo = {"127.0.0.1", "80"};
51
52         PowerMock.mockStatic(MicroServiceConfig.class);
53         expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo);
54
55         mockedJerseyClient = createPartialMock(JerseyClient.class,
56                 "post", new Class[]{String.class, Entity.class, Class.class});
57
58         msbRegister = new MsbRegister();
59         WhiteboxImpl.setInternalState(msbRegister, "client", mockedJerseyClient);
60     }
61
62     @Test
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));
75
76         PowerMock.replayAll();
77
78         try {
79             msbRegister.register2Msb(msi);
80         } catch (CorrelationException e) {
81             // Do nothing
82         }
83
84         PowerMock.verifyAll();
85     }
86
87     @Test
88     public void test_register2Msb_fail_once() {
89         expect(mockedJerseyClient.post(anyObject(String.class),
90                 anyObject(Entity.class),
91                 anyObject(Class.class)))
92                 .andReturn(null);
93
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));
105
106         PowerMock.replayAll();
107
108         try {
109             msbRegister.register2Msb(msi);
110         } catch (CorrelationException e) {
111             // Do nothing
112         }
113
114         PowerMock.verifyAll();
115     }
116 }