2 * ============LICENSE_START=======================================================
3 * org.onap.holmes.common.aai
4 * ================================================================================
5 * Copyright (C) 2018-2019 Huawei. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.holmes.common.aai;
23 import com.alibaba.fastjson.JSONObject;
24 import org.easymock.EasyMock;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.onap.holmes.common.aai.config.AaiConfig;
33 import org.onap.holmes.common.exception.CorrelationException;
34 import org.powermock.api.easymock.PowerMock;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37 import org.powermock.reflect.Whitebox;
39 import javax.ws.rs.client.Client;
40 import javax.ws.rs.client.ClientBuilder;
41 import javax.ws.rs.client.Invocation;
42 import javax.ws.rs.client.WebTarget;
43 import javax.ws.rs.core.MultivaluedHashMap;
44 import javax.ws.rs.core.MultivaluedMap;
45 import javax.ws.rs.core.Response;
46 import java.io.BufferedReader;
48 import java.io.FileNotFoundException;
49 import java.io.FileReader;
50 import java.io.IOException;
52 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({ClientBuilder.class, Client.class, Invocation.Builder.class, WebTarget.class, Response.class})
57 public class AaiQuery4Ccvpn2Test {
60 public ExpectedException thrown = ExpectedException.none();
62 private static JSONObject data;
64 private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
66 private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
67 private static Client client;
68 private static WebTarget webTarget;
69 private static Invocation.Builder builder;
70 private static Response response;
73 static public void beforeClass() {
74 System.setProperty(MSB_ADDR, "127.0.0.1:80");
76 File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
77 BufferedReader reader = null;
79 reader = new BufferedReader(new FileReader(file));
80 StringBuilder sb = new StringBuilder();
81 reader.lines().forEach(l -> sb.append(l));
82 data = JSONObject.parseObject(sb.toString());
83 } catch (FileNotFoundException e) {
85 } catch (IOException e) {
91 } catch (IOException e) {
97 headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
98 headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
99 headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
100 headers.add("Accept", "application/json");
101 headers.add("Content-Type", "application/json");
102 Whitebox.setInternalState(aai, "headers", headers);
106 public void before() {
107 PowerMock.mockStatic(ClientBuilder.class);
108 client = PowerMock.createMock(Client.class);
109 webTarget = PowerMock.createMock(WebTarget.class);
110 builder = PowerMock.createMock(Invocation.Builder.class);
111 response = PowerMock.createMock(Response.class);
115 public void after() {
116 PowerMock.resetAll();
120 public void test_getServiceInstances_exception() throws CorrelationException {
122 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("site-resources").toJSONString
124 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
127 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString
129 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
132 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString
134 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
136 PowerMock.replayAll();
138 aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
140 PowerMock.verifyAll();
144 public void test_getServiceInstancesNull_exception() throws CorrelationException {
146 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("site-resources1").toJSONString
148 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
151 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString
153 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
156 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString
158 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
160 PowerMock.replayAll();
162 aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer");
166 private void mockGetMethod() {
168 EasyMock.expect(builder.get()).andReturn(response);
171 private void initCommonMock() {
172 EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
173 EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
174 EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
175 EasyMock.expect(webTarget.request()).andReturn(builder);
176 EasyMock.expect(builder.headers(headers)).andReturn(builder);