Update AAI Assistant Tools for CCVPN Extension
[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.alibaba.fastjson.JSONObject;
24 import org.easymock.EasyMock;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.onap.holmes.common.aai.config.AaiConfig;
33 import org.onap.holmes.common.exception.CorrelationException;
34 import org.powermock.api.easymock.PowerMock;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37 import org.powermock.reflect.Whitebox;
38
39 import javax.ws.rs.client.Client;
40 import javax.ws.rs.client.ClientBuilder;
41 import javax.ws.rs.client.Invocation;
42 import javax.ws.rs.client.WebTarget;
43 import javax.ws.rs.core.MultivaluedHashMap;
44 import javax.ws.rs.core.MultivaluedMap;
45 import javax.ws.rs.core.Response;
46 import java.io.BufferedReader;
47 import java.io.File;
48 import java.io.FileNotFoundException;
49 import java.io.FileReader;
50 import java.io.IOException;
51
52 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
53
54
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({ClientBuilder.class, Client.class, Invocation.Builder.class, WebTarget.class, Response.class})
57 public class AaiQuery4Ccvpn2Test {
58
59     @Rule
60     public ExpectedException thrown = ExpectedException.none();
61
62     private static JSONObject data;
63
64     private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
65
66     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
67     private static Client client;
68     private static WebTarget webTarget;
69     private static Invocation.Builder builder;
70     private static Response response;
71
72     @BeforeClass
73     static public void beforeClass() {
74         System.setProperty(MSB_ADDR, "127.0.0.1:80");
75
76         File file = new File(AaiQuery4Ccvpn2Test.class.getClassLoader().getResource("./ccvpn2.data.json").getFile());
77         BufferedReader reader = null;
78         try {
79             reader = new BufferedReader(new FileReader(file));
80             StringBuilder sb = new StringBuilder();
81             reader.lines().forEach(l -> sb.append(l));
82             data = JSONObject.parseObject(sb.toString());
83         } catch (FileNotFoundException e) {
84             // Do nothing
85         } catch (IOException e) {
86             // Do nothing
87         } finally {
88             if (reader != null) {
89                 try {
90                     reader.close();
91                 } catch (IOException e) {
92                     // Do nothing
93                 }
94             }
95         }
96
97         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
98         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
99         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
100         headers.add("Accept", "application/json");
101         headers.add("Content-Type", "application/json");
102         Whitebox.setInternalState(aai, "headers", headers);
103     }
104
105     @Before
106     public void before() {
107         PowerMock.mockStatic(ClientBuilder.class);
108         client = PowerMock.createMock(Client.class);
109         webTarget = PowerMock.createMock(WebTarget.class);
110         builder = PowerMock.createMock(Invocation.Builder.class);
111         response = PowerMock.createMock(Response.class);
112     }
113
114     @After
115     public void after() {
116         PowerMock.resetAll();
117     }
118
119     @Test
120     public void test_getServiceInstances_exception() throws CorrelationException {
121         mockGetMethod();
122         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("site-resources").toJSONString
123                 ());
124         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
125
126         mockGetMethod();
127         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString
128                 ());
129         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
130
131         mockGetMethod();
132         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString
133                 ());
134         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
135
136         PowerMock.replayAll();
137
138         aai.getSiteServiceInstance("HkHubONSDEMOBJHKCustomer");
139
140         PowerMock.verifyAll();
141     }
142
143
144     private void mockGetMethod() {
145         initCommonMock();
146         EasyMock.expect(builder.get()).andReturn(response);
147     }
148
149     private void initCommonMock() {
150         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
151         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
152         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
153         EasyMock.expect(webTarget.request()).andReturn(builder);
154         EasyMock.expect(builder.headers(headers)).andReturn(builder);
155     }
156 }