Removed redundant env vars
[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
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;
53
54     private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
55
56     private static Map<String, Object> headers = new HashMap<>();
57     private static JerseyClient client;
58     private static Response response;
59
60     @BeforeClass
61     static public void beforeClass() {
62
63         File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
64         BufferedReader reader = null;
65         try {
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) {
71             // Do nothing
72         } catch (IOException e) {
73             // Do nothing
74         } finally {
75             if (reader != null) {
76                 try {
77                     reader.close();
78                 } catch (IOException e) {
79                     // Do nothing
80                 }
81             }
82         }
83
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);
90     }
91
92     @Before
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();
97     }
98
99     @After
100     public void after() {
101         PowerMock.resetAll();
102     }
103
104     @Test
105     public void test_getServiceInstances_exception() {
106         mockGetMethod();
107         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources").toString());
108
109         mockGetMethod();
110         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
111
112         mockGetMethod();
113         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
114
115         PowerMock.replayAll();
116
117         aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
118
119         PowerMock.verifyAll();
120     }
121
122     @Test
123     public void test_getServiceInstancesNull_exception() {
124         mockGetMethod();
125         EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources1").toString());
126
127         PowerMock.replayAll();
128
129         assertThat(aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer"), is(nullValue()));
130
131         PowerMock.verifyAll();
132     }
133
134     private void mockGetMethod() {
135         EasyMock.expect(client.headers(headers)).andReturn(client);
136         EasyMock.expect(client.path(EasyMock.anyString())).andReturn(client);
137     }
138 }