9b9d75c8a64fe6b0c3be91dd37ac1977059c4742
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4Ccvpn2Test.java
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.holmes.common.aai;
22
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;
39
40 import java.io.*;
41 import java.util.HashMap;
42 import java.util.Map;
43
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;
48
49
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;
55
56     private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
57
58     private static Map<String, Object> headers = new HashMap<>();
59     private static JerseyClient client;
60     private static Response response;
61
62     @BeforeClass
63     static public void beforeClass() {
64         System.setProperty(MSB_ADDR, "127.0.0.1:80");
65
66         File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
67         BufferedReader reader = null;
68         try {
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) {
74             // Do nothing
75         } catch (IOException e) {
76             // Do nothing
77         } finally {
78             if (reader != null) {
79                 try {
80                     reader.close();
81                 } catch (IOException e) {
82                     // Do nothing
83                 }
84             }
85         }
86
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);
93     }
94
95     @Before
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();
100     }
101
102     @After
103     public void after() {
104         PowerMock.resetAll();
105     }
106
107     @Test
108     public void test_getServiceInstances_exception() {
109         mockGetMethod();
110         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources").toString());
111
112         mockGetMethod();
113         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
114
115         mockGetMethod();
116         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
117
118         PowerMock.replayAll();
119
120         aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
121
122         PowerMock.verifyAll();
123     }
124
125     @Test
126     public void test_getServiceInstancesNull_exception() {
127         mockGetMethod();
128         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources1").toString());
129
130         PowerMock.replayAll();
131
132         assertThat(aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer"), is(nullValue()));
133
134         PowerMock.verifyAll();
135     }
136
137     private void mockGetMethod() {
138         EasyMock.expect(client.headers(headers)).andReturn(client);
139         EasyMock.expect(client.path(EasyMock.anyString())).andReturn(client);
140     }
141 }