2 * Copyright 2017-2020 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.common.aai;
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;
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;
46 @PrepareForTest({AaiQuery.class, MicroServiceConfig.class, JerseyClient.class})
47 @PowerMockIgnore("javax.net.ssl.*")
48 @RunWith(PowerMockRunner.class)
49 public class AaiQueryTest {
52 public ExpectedException thrown = ExpectedException.none();
54 private AaiQuery aaiQuery;
55 private AaiResponseUtil aaiResponseUtil;
58 static public void before() {
59 System.setProperty("ENABLE_ENCRYPT", "true");
63 public void testAaiQuery_getAaiVnfData_ok() throws Exception {
65 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
66 aaiResponseUtil = new AaiResponseUtil();
67 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
69 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
71 PowerMock.replayAll();
72 VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
73 PowerMock.verifyAll();
75 assertThat(vnfEntity, is(nullValue()));
79 public void testAaiQuery_getAaiVnfData_exception() throws Exception {
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")
89 PowerMock.replayAll();
90 aaiQuery.getAaiVnfData("test1", "test2");
91 PowerMock.verifyAll();
95 public void testAaiQuery_getAaiVmData_ok() throws Exception {
97 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
98 aaiResponseUtil = new AaiResponseUtil();
99 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
101 String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
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("{}");
108 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
109 .andReturn("/aai/v11/cloud-infrastructure");
111 PowerMock.replayAll();
113 VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
115 PowerMock.verifyAll();
117 assertThat(vmEntity, is(nullValue()));
121 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
122 PowerMock.resetAll();
123 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
125 aaiResponseUtil = new AaiResponseUtil();
126 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
128 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
130 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
131 PowerMock.replayAll();
132 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
133 PowerMock.verifyAll();
135 assertThat(resource, equalTo("le-vserver-id-val-51834"));
139 public void testAaiQuery_getResourceLinksResponse() throws Exception {
140 PowerMock.resetAll();
141 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
143 aaiResponseUtil = new AaiResponseUtil();
144 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
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();
151 assertThat(resource, equalTo(""));
155 public void testAaiQuery_getVnfDataResponse() throws Exception {
156 PowerMock.resetAll();
157 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
159 aaiResponseUtil = new AaiResponseUtil();
160 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
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();
167 assertThat(resource, equalTo(""));
171 public void testAaiQuery_getResponse_ok() throws Exception {
172 PowerMock.resetAll();
173 aaiQuery = new AaiQuery();
175 String url = "host_url";
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("");
182 PowerMock.replayAll();
183 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
184 PowerMock.verifyAll();
186 assertThat(resource, equalTo(""));
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();
196 String url = "host_url";
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"));
203 PowerMock.replayAll();
204 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", url);
205 PowerMock.verifyAll();
206 assertThat(resource, equalTo(""));
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();
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"));
224 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
225 PowerMock.resetAll();
226 aaiQuery = new AaiQuery();
228 PowerMock.mockStatic(MicroServiceConfig.class);
230 PowerMock.replayAll();
231 String actual = Whitebox.invokeMethod(aaiQuery, "getBaseUrl", "/url");
232 PowerMock.verifyAll();
234 assertThat(actual, equalTo("https://aai.onap:8443/url"));