Released Version 1.4.7
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQueryTest.java
1 /**
2  * Copyright 2017-2020 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.aai;
18
19 import org.easymock.EasyMock;
20 import org.junit.BeforeClass;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.junit.runner.RunWith;
25 import org.onap.holmes.common.aai.entity.VmEntity;
26 import org.onap.holmes.common.aai.entity.VnfEntity;
27 import org.onap.holmes.common.config.MicroServiceConfig;
28 import org.onap.holmes.common.exception.CorrelationException;
29 import org.onap.holmes.common.exception.HttpException;
30 import org.onap.holmes.common.utils.JerseyClient;
31 import org.powermock.api.easymock.PowerMock;
32 import org.powermock.core.classloader.annotations.PowerMockIgnore;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35 import org.powermock.reflect.Whitebox;
36
37 import java.util.Map;
38
39 import static org.easymock.EasyMock.anyObject;
40 import static org.hamcrest.core.Is.is;
41 import static org.hamcrest.core.IsEqual.equalTo;
42 import static org.hamcrest.core.IsNull.nullValue;
43 import static org.junit.Assert.assertThat;
44
45
46 @PrepareForTest({AaiQuery.class, MicroServiceConfig.class, JerseyClient.class})
47 @PowerMockIgnore("javax.net.ssl.*")
48 @RunWith(PowerMockRunner.class)
49 public class AaiQueryTest {
50
51     @Rule
52     public ExpectedException thrown = ExpectedException.none();
53
54     private AaiQuery aaiQuery;
55     private AaiResponseUtil aaiResponseUtil;
56
57     @BeforeClass
58     static public void before() {
59         System.setProperty("ENABLE_ENCRYPT", "true");
60     }
61
62     @Test
63     public void testAaiQuery_getAaiVnfData_ok() throws Exception {
64         PowerMock.resetAll();
65         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
66         aaiResponseUtil = new AaiResponseUtil();
67         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
68
69         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
70
71         PowerMock.replayAll();
72         VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
73         PowerMock.verifyAll();
74
75         assertThat(vnfEntity, is(nullValue()));
76     }
77
78     @Test
79     public void testAaiQuery_getAaiVnfData_exception() throws Exception {
80         PowerMock.resetAll();
81         thrown.expect(CorrelationException.class);
82         thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
83         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
84         aaiResponseUtil = new AaiResponseUtil();
85         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
86         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
87                 .andReturn("{***}");
88
89         PowerMock.replayAll();
90         aaiQuery.getAaiVnfData("test1", "test2");
91         PowerMock.verifyAll();
92     }
93
94     @Test
95     public void testAaiQuery_getAaiVmData_ok() throws Exception {
96         PowerMock.resetAll();
97         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
98         aaiResponseUtil = new AaiResponseUtil();
99         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
100
101         String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
102
103         JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
104         PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
105         EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient);
106         EasyMock.expect(mockedClient.get(url)).andReturn("{}");
107
108         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
109                 .andReturn("/aai/v11/cloud-infrastructure");
110
111         PowerMock.replayAll();
112
113         VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
114
115         PowerMock.verifyAll();
116
117         assertThat(vmEntity, is(nullValue()));
118     }
119
120     @Test
121     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
122         PowerMock.resetAll();
123         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
124
125         aaiResponseUtil = new AaiResponseUtil();
126         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
127
128         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
129
130         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
131         PowerMock.replayAll();
132         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
133         PowerMock.verifyAll();
134
135         assertThat(resource, equalTo("le-vserver-id-val-51834"));
136     }
137
138     @Test
139     public void testAaiQuery_getResourceLinksResponse() throws Exception {
140         PowerMock.resetAll();
141         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
142
143         aaiResponseUtil = new AaiResponseUtil();
144         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
145
146         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
147         PowerMock.replayAll();
148         String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
149         PowerMock.verifyAll();
150
151         assertThat(resource, equalTo(""));
152     }
153
154     @Test
155     public void testAaiQuery_getVnfDataResponse() throws Exception {
156         PowerMock.resetAll();
157         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
158
159         aaiResponseUtil = new AaiResponseUtil();
160         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
161
162         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
163         PowerMock.replayAll();
164         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
165         PowerMock.verifyAll();
166
167         assertThat(resource, equalTo(""));
168     }
169
170     @Test
171     public void testAaiQuery_getResponse_ok() throws Exception {
172         PowerMock.resetAll();
173         aaiQuery = new AaiQuery();
174
175         String url = "host_url";
176
177         JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
178         PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
179         EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient);
180         EasyMock.expect(mockedClient.get(url)).andReturn("");
181
182         PowerMock.replayAll();
183         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
184         PowerMock.verifyAll();
185
186         assertThat(resource, equalTo(""));
187     }
188
189     @Test
190     public void testAaiQuery_getResponse_exception() throws Exception {
191         PowerMock.resetAll();
192         thrown.expect(CorrelationException.class);
193         thrown.expectMessage("Failed to get data from aai");
194         aaiQuery = new AaiQuery();
195
196         String url = "host_url";
197
198         JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
199         PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
200         EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient);
201         EasyMock.expect(mockedClient.get(url)).andThrow(new HttpException(404, "Not Found"));
202
203         PowerMock.replayAll();
204         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", url);
205         PowerMock.verifyAll();
206         assertThat(resource, equalTo(""));
207     }
208
209     @Test
210     public void testAaiQuery_getHeaders() throws Exception {
211         PowerMock.resetAll();
212         aaiQuery = new AaiQuery();
213         PowerMock.replayAll();
214         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
215         PowerMock.verifyAll();
216
217         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
218         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
219         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
220         assertThat(actual.get("Accept"), equalTo("application/json"));
221     }
222
223     @Test
224     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
225         PowerMock.resetAll();
226         aaiQuery = new AaiQuery();
227
228         PowerMock.mockStatic(MicroServiceConfig.class);
229
230         PowerMock.replayAll();
231         String actual = Whitebox.invokeMethod(aaiQuery, "getBaseUrl", "/url");
232         PowerMock.verifyAll();
233
234         assertThat(actual, equalTo("https://aai.onap:8443/url"));
235     }
236 }