2 * ============LICENSE_START=======================================================
3 * org.onap.holmes.common.aai
4 * ================================================================================
5 * Copyright (C) 2018-2022 Huawei, ZTE. 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 jakarta.ws.rs.core.Response;
26 import org.easymock.EasyMock;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.holmes.common.aai.config.AaiConfig;
33 import org.onap.holmes.common.utils.JerseyClient;
34 import org.powermock.api.easymock.PowerMock;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.powermock.reflect.Whitebox;
41 import java.util.HashMap;
44 import static org.hamcrest.MatcherAssert.assertThat;
45 import static org.hamcrest.core.Is.is;
46 import static org.hamcrest.core.IsNull.nullValue;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({JerseyClient.class})
50 @SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient")
51 public class AaiQuery4Ccvpn2Test {
52 private static JsonObject data;
54 private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
56 private static Map<String, Object> headers = new HashMap<>();
57 private static JerseyClient client;
58 private static Response response;
61 static public void beforeClass() {
63 File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
64 BufferedReader reader = null;
66 reader = new BufferedReader(new FileReader(file));
67 StringBuilder sb = new StringBuilder();
68 reader.lines().forEach(l -> sb.append(l));
69 data = JsonParser.parseString(sb.toString()).getAsJsonObject();
70 } catch (FileNotFoundException e) {
72 } catch (IOException e) {
78 } catch (IOException e) {
84 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
85 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
86 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
87 headers.put("Accept", "application/json");
88 headers.put("Content-Type", "application/json");
89 Whitebox.setInternalState(aai, "headers", headers);
93 public void before() throws Exception {
94 client = PowerMock.createMock(JerseyClient.class);
95 response = PowerMock.createMock(Response.class);
96 PowerMock.expectNew(JerseyClient.class).andReturn(client).anyTimes();
100 public void after() {
101 PowerMock.resetAll();
105 public void test_getServiceInstances_exception() {
107 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources").toString());
110 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
113 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
115 PowerMock.replayAll();
117 aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
119 PowerMock.verifyAll();
123 public void test_getServiceInstancesNull_exception() {
125 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources1").toString());
127 PowerMock.replayAll();
129 assertThat(aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer"), is(nullValue()));
131 PowerMock.verifyAll();
134 private void mockGetMethod() {
135 EasyMock.expect(client.headers(headers)).andReturn(client);
136 EasyMock.expect(client.path(EasyMock.anyString())).andReturn(client);