1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.client.impl;
27 import java.nio.charset.StandardCharsets;
28 import org.apache.http.HttpException;
29 import org.glassfish.jersey.client.ClientConfig;
30 import org.apache.commons.codec.binary.Base64;
31 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
43 import javax.ws.rs.core.MultivaluedMap;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.ResponseBuilder;
46 import java.net.MalformedURLException;
47 import java.util.Arrays;
48 import java.util.Collection;
49 import java.util.HashSet;
51 import static org.junit.Assert.assertEquals;
52 import static org.junit.Assert.assertTrue;
53 import static org.mockito.Mockito.atLeast;
54 import static org.mockito.Mockito.verify;
57 @RunWith(PowerMockRunner.class)
58 @PowerMockIgnore({"org.apache.http.conn.ssl.*", "jdk.internal.reflect.*"})
59 @PrepareForTest({DmaapClientUtil.class})
60 public class MRBaseClientTest {
63 private MRBaseClient mrBaseClient;
64 private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
65 private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
66 private ClientConfig config = null;
69 public void setup() throws MalformedURLException {
70 mrBaseClient = new MRBaseClient(hosts, clientSignature);
71 PowerMockito.mockStatic(DmaapClientUtil.class);
75 public void testGet() throws JSONException, HttpException {
77 Response response = Mockito.mock(Response.class);
78 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
79 map.add("transactionid", "transactionid");
81 Mockito.when(response.getStatus()).thenReturn(200);
82 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
83 Mockito.when(response.getHeaders()).thenReturn(map);
86 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
87 .thenReturn(response);
89 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
90 assertEquals(200, result.getInt("status"));
91 assertEquals("test", result.getString("test"));
92 verify(response, atLeast(1)).getStatus();
93 verify(response).readEntity(String.class);
94 verify(response).getHeaders();
98 public void testGet_403() throws JSONException, HttpException {
99 ResponseBuilder responseBuilder = Response.status(403);
101 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
104 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
105 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
106 assertEquals(403, result.getInt("status"));
110 public void testGet_basicauth() throws JSONException, HttpException {
112 Response response = Mockito.mock(Response.class);
113 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
114 map.add("transactionid", "transactionid");
116 Mockito.when(response.getStatus()).thenReturn(200);
117 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
118 Mockito.when(response.getHeaders()).thenReturn(map);
119 Base64 base64 = new Base64();
120 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
121 base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(response);
123 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
124 assertEquals(200, result.getInt("status"));
125 verify(response, atLeast(1)).getStatus();
126 verify(response).readEntity(String.class);
127 verify(response).getHeaders();
131 @Test(expected = HttpException.class)
132 public void testGet_error() throws JSONException, HttpException {
134 ResponseBuilder responseBuilder = Response.ok();
135 Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
138 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
140 mrBaseClient.get("/path", null, null, "HTTPAUTH");
144 public void testGet_wrongjson() throws JSONException, HttpException {
146 Response response = Mockito.mock(Response.class);
147 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
148 map.add("transactionid", "transactionid");
150 Mockito.when(response.getStatus()).thenReturn(200);
151 Mockito.when(response.readEntity(String.class)).thenReturn("[[");
152 Mockito.when(response.getHeaders()).thenReturn(map);
155 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
156 .thenReturn(response);
158 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
159 verify(response, atLeast(1)).getStatus();
160 verify(response).readEntity(String.class);
161 verify(response).getHeaders();
165 public void testGetResponse() throws JSONException, HttpException {
167 Response response = Mockito.mock(Response.class);
168 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
169 map.add("transactionid", "transactionid");
171 Mockito.when(response.getStatus()).thenReturn(200);
172 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
173 Mockito.when(response.getHeaders()).thenReturn(map);
176 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
177 .thenReturn(response);
179 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
185 public void testGetResponse_aaf() throws JSONException, HttpException {
187 Response response = Mockito.mock(Response.class);
188 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
189 map.add("transactionid", "transactionid");
191 Mockito.when(response.getStatus()).thenReturn(200);
192 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
193 Mockito.when(response.getHeaders()).thenReturn(map);
194 Base64 base64 = new Base64();
195 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
196 base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(response);
198 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
203 @Test(expected = HttpException.class)
204 public void testGetResponse_error() throws JSONException, HttpException {
206 ResponseBuilder responseBuilder = Response.ok();
208 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
211 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
213 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
217 public void testAuthResponse() throws JSONException, HttpException {
219 Response response = Mockito.mock(Response.class);
220 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
221 map.add("transactionid", "transactionid");
223 Mockito.when(response.getStatus()).thenReturn(200);
224 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
225 Mockito.when(response.getHeaders()).thenReturn(map);
228 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
229 .thenReturn(response);
231 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
236 @Test(expected = HttpException.class)
237 public void testAuthResponsee_error() throws JSONException, HttpException {
239 ResponseBuilder responseBuilder = Response.ok();
241 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
244 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
246 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
251 public void testPostAuth() throws JSONException, HttpException {
253 Response response = Mockito.mock(Response.class);
254 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
255 map.add("transactionid", "transactionid");
257 Mockito.when(response.getStatus()).thenReturn(200);
258 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
259 Mockito.when(response.getHeaders()).thenReturn(map);
262 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
263 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
264 .thenReturn(response);
266 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
267 .setData(("{\"test\":\"test\"}").getBytes())
268 .setContentType("application/json")
269 .setAuthKey("username")
270 .setAuthDate("password")
271 .setUsername("username")
272 .setPassword("password")
273 .setProtocolFlag("HTTPAUTH"));
278 @Test(expected = HttpException.class)
279 public void testPostAuth_error() throws JSONException, HttpException {
281 ResponseBuilder responseBuilder = Response.ok();
283 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
284 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
286 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
288 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
289 .setData(("{\"test\":\"test\"}").getBytes())
290 .setContentType("application/json")
295 .setProtocolFlag("HTTPAUTH"));
299 public void testGetNoAuthResponse() throws JSONException, HttpException {
301 Response response = Mockito.mock(Response.class);
302 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
303 map.add("transactionid", "transactionid");
305 Mockito.when(response.getStatus()).thenReturn(200);
306 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
307 Mockito.when(response.getHeaders()).thenReturn(map);
309 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
311 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
317 public void testPost() throws JSONException, HttpException {
319 Response response = Mockito.mock(Response.class);
320 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
321 map.add("transactionid", "transactionid");
323 Mockito.when(response.getStatus()).thenReturn(200);
324 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
325 Mockito.when(response.getHeaders()).thenReturn(map);
326 Base64 base64 = new Base64();
327 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
328 base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)), ("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
330 mrBaseClient.post("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", "username",
331 "password", "HTTPAUTH");
332 verify(response, atLeast(1)).getStatus();
333 verify(response).readEntity(String.class);
334 verify(response).getHeaders();
338 @Test(expected = HttpException.class)
339 public void testPost_error() throws JSONException, HttpException {
341 ResponseBuilder responseBuilder = Response.ok();
342 Base64 base64 = new Base64();
343 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
344 base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(
345 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
347 mrBaseClient.post("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
353 public void testPostAuthwithResponse() throws JSONException, HttpException {
355 Response response = Mockito.mock(Response.class);
356 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
357 map.add("transactionid", "transactionid");
359 Mockito.when(response.getStatus()).thenReturn(200);
360 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
361 Mockito.when(response.getHeaders()).thenReturn(map);
364 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
365 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
366 .thenReturn(response);
368 mrBaseClient.postAuthwithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json",
369 "username", "password", "username", "password", "HTTPAUTH");
374 @Test(expected = HttpException.class)
375 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
377 ResponseBuilder responseBuilder = Response.ok();
379 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
380 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
382 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
384 mrBaseClient.postAuthwithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json",
385 null, null, null, null, "HTTPAUTH");
390 public void testPostWithResponse() throws JSONException, HttpException {
392 Response response = Mockito.mock(Response.class);
393 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
394 map.add("transactionid", "transactionid");
396 Mockito.when(response.getStatus()).thenReturn(200);
397 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
398 Mockito.when(response.getHeaders()).thenReturn(map);
399 Base64 base64 = new Base64();
400 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
401 base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)), ("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
403 mrBaseClient.postWithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json",
404 "username", "password", "HTTPAUTH");
409 @Test(expected = HttpException.class)
410 public void testPostWithResponse_error() throws JSONException, HttpException {
412 ResponseBuilder responseBuilder = Response.ok();
413 Base64 base64 = new Base64();
414 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
415 base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(
416 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
418 mrBaseClient.postWithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", null,
424 public void testGetAuth() throws JSONException, HttpException {
426 Response response = Mockito.mock(Response.class);
427 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
428 map.add("transactionid", "transactionid");
430 Mockito.when(response.getStatus()).thenReturn(200);
431 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
432 Mockito.when(response.getHeaders()).thenReturn(map);
435 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
436 .thenReturn(response);
437 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
442 @Test(expected = HttpException.class)
443 public void testGetAuth_error() throws JSONException, HttpException {
445 ResponseBuilder responseBuilder = Response.ok();
447 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
448 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
450 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
452 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
457 public void testGetNoAuth() throws JSONException, HttpException {
459 Response response = Mockito.mock(Response.class);
460 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
461 map.add("transactionid", "transactionid");
463 Mockito.when(response.getStatus()).thenReturn(200);
464 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
465 Mockito.when(response.getHeaders()).thenReturn(map);
467 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
468 mrBaseClient.getNoAuth("/path");
475 public void testGetHTTPErrorResponseMessage() {
476 assertEquals("testtest", mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"));
481 public void getGTTPErrorResponseCode() {
482 assertEquals("500", mrBaseClient.getHTTPErrorResponseCode("<title>500</title>"));
486 private ClientConfig getClientConfig() {
487 if (config == null) {
488 config = DmaapClientUtil.getClientConfig(null);