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.Test;
20 import org.junit.runner.RunWith;
21 import org.onap.holmes.common.config.MicroServiceConfig;
22 import org.onap.holmes.common.exception.CorrelationException;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
24 import org.powermock.api.easymock.PowerMock;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.junit4.PowerMockRunner;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.Invocation;
31 import javax.ws.rs.client.WebTarget;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
35 import static org.easymock.EasyMock.expect;
36 import static org.powermock.api.easymock.PowerMock.createMock;
38 @PrepareForTest({MicroServiceConfig.class})
39 @RunWith(PowerMockRunner.class)
40 public class MsbRegisterTest {
42 public void test_register2Msb_normal() {
43 MicroServiceInfo msi = new MicroServiceInfo();
44 String[] msbAddrInfo = {"127.0.0.1", "80"};
46 PowerMock.mockStatic(MicroServiceConfig.class);
47 expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo);
49 JerseyClient mockedJerseyClient = createMock(JerseyClient.class);
51 Client mockedClient = createMock(Client.class);
52 expect(mockedJerseyClient.client(false)).andReturn(mockedClient);
54 WebTarget mockedWebTarget = createMock(WebTarget.class);
55 expect(mockedClient.target("http://127.0.0.1:80/api/microservices/v1/services")).andReturn(mockedWebTarget);
58 expect(mockedWebTarget.queryParam("createOrUpdate", true)).andReturn(mockedWebTarget).times(2);
60 Invocation.Builder mockedBuilder = createMock(Invocation.Builder.class);
61 expect(mockedWebTarget.request(MediaType.APPLICATION_JSON)).andReturn(mockedBuilder).times(2);
63 Response mockedResponse = createMock(Response.class);
64 expect(mockedBuilder.post(Entity.entity(msi, MediaType.APPLICATION_JSON)))
65 .andReturn(mockedResponse);
66 expect(mockedResponse.getStatus()).andReturn(300);
68 expect(mockedBuilder.post(Entity.entity(msi, MediaType.APPLICATION_JSON)))
69 .andReturn(mockedResponse);
70 expect(mockedResponse.getStatus()).andReturn(201);
71 expect(mockedResponse.readEntity(String.class)).andReturn("Error");
72 expect(mockedResponse.readEntity(String.class)).andReturn("{\"serviceName\":\"holmes-engine-mgmt\"," +
73 "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
74 "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
75 "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
76 "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
77 "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
78 "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}");
81 MsbRegister msbRegister = new MsbRegister(mockedJerseyClient);
83 PowerMock.replayAll();
86 msbRegister.register2Msb(msi);
87 } catch (CorrelationException e) {
91 PowerMock.verifyAll();