Updated the repo to support JDK 17
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / msb / MsbRegisterTest.java
1 /**
2  * Copyright 2017-2023 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.msb;
18
19 import jakarta.ws.rs.client.Entity;
20 import jakarta.ws.rs.core.MediaType;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.onap.holmes.common.config.MicroServiceConfig;
25 import org.onap.holmes.common.exception.CorrelationException;
26 import org.onap.holmes.common.msb.entity.MicroServiceFullInfo;
27 import org.onap.holmes.common.msb.entity.MicroServiceInfo;
28 import org.onap.holmes.common.utils.GsonUtil;
29 import org.onap.holmes.common.utils.JerseyClient;
30 import org.powermock.api.easymock.PowerMock;
31 import org.powermock.core.classloader.annotations.PowerMockIgnore;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34
35 import static org.easymock.EasyMock.anyObject;
36 import static org.easymock.EasyMock.expect;
37 import static org.powermock.api.easymock.PowerMock.createMock;
38 import static org.powermock.api.easymock.PowerMock.expectNew;
39
40 @PrepareForTest({MicroServiceConfig.class, JerseyClient.class})
41 @RunWith(PowerMockRunner.class)
42 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
43 public class MsbRegisterTest {
44
45     private JerseyClient mockedJerseyClient;
46     private MicroServiceInfo msi;
47
48     @Before
49     public void before() throws Exception {
50         msi = new MicroServiceInfo();
51         String[] msbAddrInfo = {"127.0.0.1", "80"};
52
53         PowerMock.mockStatic(MicroServiceConfig.class);
54         expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo);
55
56         mockedJerseyClient = createMock(JerseyClient.class);
57         expectNew(JerseyClient.class).andReturn(mockedJerseyClient);
58     }
59
60     @Test
61     public void test_register2Msb_normal() {
62         expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient);
63         expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient);
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         MsbRegister msbRegister = new MsbRegister();
79         try {
80             msbRegister.register2Msb(msi);
81         } catch (CorrelationException e) {
82             // Do nothing
83         }
84
85         PowerMock.verifyAll();
86     }
87
88     @Test
89     public void test_register2Msb_fail_n_times() {
90         int requestTimes = 3;
91         expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes);
92         expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes);
93         expect(mockedJerseyClient.post(anyObject(String.class),
94                 anyObject(Entity.class),
95                 anyObject(Class.class)))
96                 .andReturn(null).times(requestTimes - 1);
97
98         expect(mockedJerseyClient.post(anyObject(String.class),
99                 anyObject(Entity.class),
100                 anyObject(Class.class)))
101                 .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
102                                 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
103                                 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
104                                 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
105                                 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
106                                 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
107                                 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
108                         MicroServiceFullInfo.class));
109
110         PowerMock.replayAll();
111
112         MsbRegister msbRegister = new MsbRegister();
113         msbRegister.setInterval(1);
114         try {
115             msbRegister.register2Msb(msi);
116         } catch (CorrelationException e) {
117             // Do nothing
118         }
119
120         PowerMock.verifyAll();
121     }
122
123     @Test
124     public void test_register2Msb_fail_n_times_due_to_exception() {
125         int requestTimes = 3;
126         expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes);
127         expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes);
128         expect(mockedJerseyClient.post(anyObject(String.class),
129                 anyObject(Entity.class),
130                 anyObject(Class.class)))
131                 .andThrow(new RuntimeException("Failure!")).times(requestTimes - 1);
132
133         expect(mockedJerseyClient.post(anyObject(String.class),
134                 anyObject(Entity.class),
135                 anyObject(Class.class)))
136                 .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
137                                 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
138                                 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
139                                 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
140                                 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
141                                 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
142                                 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
143                         MicroServiceFullInfo.class));
144
145         PowerMock.replayAll();
146
147         MsbRegister msbRegister = new MsbRegister();
148         msbRegister.setInterval(1);
149         try {
150             msbRegister.register2Msb(msi);
151         } catch (CorrelationException e) {
152             // Do nothing
153         }
154
155         PowerMock.verifyAll();
156     }
157 }