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.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import org.easymock.EasyMock;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.onap.holmes.common.aai.config.AaiConfig;
30 import org.onap.holmes.common.exception.CorrelationException;
31 import org.powermock.api.easymock.PowerMock;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34 import org.powermock.reflect.Whitebox;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.ClientBuilder;
38 import javax.ws.rs.client.Invocation;
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.MultivaluedMap;
42 import javax.ws.rs.core.Response;
45 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({ClientBuilder.class, Client.class, Invocation.Builder.class, WebTarget.class, Response.class})
50 public class AaiQuery4Ccvpn2Test {
53 public ExpectedException thrown = ExpectedException.none();
55 private static JsonObject data;
57 private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
59 private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
60 private static Client client;
61 private static WebTarget webTarget;
62 private static Invocation.Builder builder;
63 private static Response response;
66 static public void beforeClass() {
67 System.setProperty(MSB_ADDR, "127.0.0.1:80");
69 File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
70 BufferedReader reader = null;
72 reader = new BufferedReader(new FileReader(file));
73 StringBuilder sb = new StringBuilder();
74 reader.lines().forEach(l -> sb.append(l));
75 data = JsonParser.parseString(sb.toString()).getAsJsonObject();
76 } catch (FileNotFoundException e) {
78 } catch (IOException e) {
84 } catch (IOException e) {
90 headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
91 headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
92 headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
93 headers.add("Accept", "application/json");
94 headers.add("Content-Type", "application/json");
95 Whitebox.setInternalState(aai, "headers", headers);
99 public void before() {
100 PowerMock.mockStatic(ClientBuilder.class);
101 client = PowerMock.createMock(Client.class);
102 webTarget = PowerMock.createMock(WebTarget.class);
103 builder = PowerMock.createMock(Invocation.Builder.class);
104 response = PowerMock.createMock(Response.class);
108 public void after() {
109 PowerMock.resetAll();
113 public void test_getServiceInstances_exception() throws CorrelationException {
115 EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources").toString());
116 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
119 EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
120 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
123 EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
124 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
126 PowerMock.replayAll();
128 aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
130 PowerMock.verifyAll();
134 public void test_getServiceInstancesNull_exception() throws CorrelationException {
136 EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources1").toString());
137 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
140 EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
141 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
144 EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
145 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
147 PowerMock.replayAll();
149 aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer");
153 private void mockGetMethod() {
155 EasyMock.expect(builder.get()).andReturn(response);
158 private void initCommonMock() {
159 EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
160 EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
161 EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
162 EasyMock.expect(webTarget.request()).andReturn(builder);
163 EasyMock.expect(builder.headers(headers)).andReturn(builder);