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;
47 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({JerseyClient.class})
52 @SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient")
53 public class AaiQuery4Ccvpn2Test {
54 private static JsonObject data;
56 private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
58 private static Map<String, Object> headers = new HashMap<>();
59 private static JerseyClient client;
60 private static Response response;
63 static public void beforeClass() {
64 System.setProperty(MSB_ADDR, "127.0.0.1:80");
66 File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
67 BufferedReader reader = null;
69 reader = new BufferedReader(new FileReader(file));
70 StringBuilder sb = new StringBuilder();
71 reader.lines().forEach(l -> sb.append(l));
72 data = JsonParser.parseString(sb.toString()).getAsJsonObject();
73 } catch (FileNotFoundException e) {
75 } catch (IOException e) {
81 } catch (IOException e) {
87 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
88 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
89 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
90 headers.put("Accept", "application/json");
91 headers.put("Content-Type", "application/json");
92 Whitebox.setInternalState(aai, "headers", headers);
96 public void before() throws Exception {
97 client = PowerMock.createMock(JerseyClient.class);
98 response = PowerMock.createMock(Response.class);
99 PowerMock.expectNew(JerseyClient.class).andReturn(client).anyTimes();
103 public void after() {
104 PowerMock.resetAll();
108 public void test_getServiceInstances_exception() {
110 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources").toString());
113 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
116 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
118 PowerMock.replayAll();
120 aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
122 PowerMock.verifyAll();
126 public void test_getServiceInstancesNull_exception() {
128 EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources1").toString());
130 PowerMock.replayAll();
132 assertThat(aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer"), is(nullValue()));
134 PowerMock.verifyAll();
137 private void mockGetMethod() {
138 EasyMock.expect(client.headers(headers)).andReturn(client);
139 EasyMock.expect(client.path(EasyMock.anyString())).andReturn(client);