Fixed the CLM Issues
[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-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
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 org.easymock.EasyMock;
26 import org.junit.*;
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;
35
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;
43 import java.io.*;
44
45 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
46
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({ClientBuilder.class, Client.class, Invocation.Builder.class, WebTarget.class, Response.class})
50 public class AaiQuery4Ccvpn2Test {
51
52     @Rule
53     public ExpectedException thrown = ExpectedException.none();
54
55     private static JsonObject data;
56
57     private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
58
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;
64
65     @BeforeClass
66     static public void beforeClass() {
67         System.setProperty(MSB_ADDR, "127.0.0.1:80");
68
69         File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
70         BufferedReader reader = null;
71         try {
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) {
77             // Do nothing
78         } catch (IOException e) {
79             // Do nothing
80         } finally {
81             if (reader != null) {
82                 try {
83                     reader.close();
84                 } catch (IOException e) {
85                     // Do nothing
86                 }
87             }
88         }
89
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);
96     }
97
98     @Before
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);
105     }
106
107     @After
108     public void after() {
109         PowerMock.resetAll();
110     }
111
112     @Test
113     public void test_getServiceInstances_exception() throws CorrelationException {
114         mockGetMethod();
115         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources").toString());
116         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
117
118         mockGetMethod();
119         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
120         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
121
122         mockGetMethod();
123         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
124         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
125
126         PowerMock.replayAll();
127
128         aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
129
130         PowerMock.verifyAll();
131     }
132
133     @Test
134     public void test_getServiceInstancesNull_exception() throws CorrelationException {
135         mockGetMethod();
136         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources1").toString());
137         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
138
139         mockGetMethod();
140         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
141         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
142
143         mockGetMethod();
144         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
145         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
146
147         PowerMock.replayAll();
148
149         aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer");
150     }
151
152
153     private void mockGetMethod() {
154         initCommonMock();
155         EasyMock.expect(builder.get()).andReturn(response);
156     }
157
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);
164     }
165 }