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 org.apache.http.HttpException;
28 import org.glassfish.jersey.client.ClientConfig;
29 import org.glassfish.jersey.internal.util.Base64;
30 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mockito;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PowerMockIgnore;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
42 import javax.ws.rs.core.MultivaluedMap;
43 import javax.ws.rs.core.Response;
44 import javax.ws.rs.core.Response.ResponseBuilder;
45 import java.net.MalformedURLException;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.HashSet;
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertTrue;
52 import static org.mockito.Mockito.atLeast;
53 import static org.mockito.Mockito.verify;
56 @RunWith(PowerMockRunner.class)
57 @PowerMockIgnore({"org.apache.http.conn.ssl.*", "jdk.internal.reflect.*"})
58 @PrepareForTest({DmaapClientUtil.class})
59 public class MRBaseClientTest {
62 private MRBaseClient mrBaseClient;
63 private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
64 private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
65 private ClientConfig config = null;
68 public void setup() throws MalformedURLException {
69 mrBaseClient = new MRBaseClient(hosts, clientSignature);
70 PowerMockito.mockStatic(DmaapClientUtil.class);
74 public void testGet() throws JSONException, HttpException {
76 Response response = Mockito.mock(Response.class);
77 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
78 map.add("transactionid", "transactionid");
80 Mockito.when(response.getStatus()).thenReturn(200);
81 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
82 Mockito.when(response.getHeaders()).thenReturn(map);
85 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
86 .thenReturn(response);
88 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
89 assertEquals(200, result.getInt("status"));
90 assertEquals("test", result.getString("test"));
91 verify(response, atLeast(1)).getStatus();
92 verify(response).readEntity(String.class);
93 verify(response).getHeaders();
97 public void testGet_403() throws JSONException, HttpException {
98 ResponseBuilder responseBuilder = Response.status(403);
100 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
103 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
104 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
105 assertEquals(403, result.getInt("status"));
109 public void testGet_basicauth() throws JSONException, HttpException {
111 Response response = Mockito.mock(Response.class);
112 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
113 map.add("transactionid", "transactionid");
115 Mockito.when(response.getStatus()).thenReturn(200);
116 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
117 Mockito.when(response.getHeaders()).thenReturn(map);
119 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
120 Base64.encodeAsString("username:password"))).thenReturn(response);
122 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
123 assertEquals(200, result.getInt("status"));
124 verify(response, atLeast(1)).getStatus();
125 verify(response).readEntity(String.class);
126 verify(response).getHeaders();
130 @Test(expected = HttpException.class)
131 public void testGet_error() throws JSONException, HttpException {
133 ResponseBuilder responseBuilder = Response.ok();
134 Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
137 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
139 mrBaseClient.get("/path", null, null, "HTTPAUTH");
143 public void testGet_wrongjson() throws JSONException, HttpException {
145 Response response = Mockito.mock(Response.class);
146 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
147 map.add("transactionid", "transactionid");
149 Mockito.when(response.getStatus()).thenReturn(200);
150 Mockito.when(response.readEntity(String.class)).thenReturn("[[");
151 Mockito.when(response.getHeaders()).thenReturn(map);
154 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
155 .thenReturn(response);
157 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
158 verify(response, atLeast(1)).getStatus();
159 verify(response).readEntity(String.class);
160 verify(response).getHeaders();
164 public void testGetResponse() throws JSONException, HttpException {
166 Response response = Mockito.mock(Response.class);
167 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
168 map.add("transactionid", "transactionid");
170 Mockito.when(response.getStatus()).thenReturn(200);
171 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
172 Mockito.when(response.getHeaders()).thenReturn(map);
175 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
176 .thenReturn(response);
178 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
184 public void testGetResponse_aaf() throws JSONException, HttpException {
186 Response response = Mockito.mock(Response.class);
187 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
188 map.add("transactionid", "transactionid");
190 Mockito.when(response.getStatus()).thenReturn(200);
191 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
192 Mockito.when(response.getHeaders()).thenReturn(map);
194 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
195 Base64.encodeAsString("username:password"))).thenReturn(response);
197 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
202 @Test(expected = HttpException.class)
203 public void testGetResponse_error() throws JSONException, HttpException {
205 ResponseBuilder responseBuilder = Response.ok();
207 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
210 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
212 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
216 public void testAuthResponse() throws JSONException, HttpException {
218 Response response = Mockito.mock(Response.class);
219 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
220 map.add("transactionid", "transactionid");
222 Mockito.when(response.getStatus()).thenReturn(200);
223 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
224 Mockito.when(response.getHeaders()).thenReturn(map);
227 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
228 .thenReturn(response);
230 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
235 @Test(expected = HttpException.class)
236 public void testAuthResponsee_error() throws JSONException, HttpException {
238 ResponseBuilder responseBuilder = Response.ok();
240 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
243 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
245 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
250 public void testPostAuth() throws JSONException, HttpException {
252 Response response = Mockito.mock(Response.class);
253 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
254 map.add("transactionid", "transactionid");
256 Mockito.when(response.getStatus()).thenReturn(200);
257 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
258 Mockito.when(response.getHeaders()).thenReturn(map);
261 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
262 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
263 .thenReturn(response);
265 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
266 .setData(("{\"test\":\"test\"}").getBytes())
267 .setContentType("application/json")
268 .setAuthKey("username")
269 .setAuthDate("password")
270 .setUsername("username")
271 .setPassword("password")
272 .setProtocolFlag("HTTPAUTH"));
277 @Test(expected = HttpException.class)
278 public void testPostAuth_error() throws JSONException, HttpException {
280 ResponseBuilder responseBuilder = Response.ok();
282 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
283 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
285 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
287 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
288 .setData(("{\"test\":\"test\"}").getBytes())
289 .setContentType("application/json")
294 .setProtocolFlag("HTTPAUTH"));
298 public void testGetNoAuthResponse() throws JSONException, HttpException {
300 Response response = Mockito.mock(Response.class);
301 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
302 map.add("transactionid", "transactionid");
304 Mockito.when(response.getStatus()).thenReturn(200);
305 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
306 Mockito.when(response.getHeaders()).thenReturn(map);
308 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
310 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
316 public void testPost() throws JSONException, HttpException {
318 Response response = Mockito.mock(Response.class);
319 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
320 map.add("transactionid", "transactionid");
322 Mockito.when(response.getStatus()).thenReturn(200);
323 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
324 Mockito.when(response.getHeaders()).thenReturn(map);
326 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
327 Base64.encodeAsString("username:password"), ("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
329 mrBaseClient.post("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", "username",
330 "password", "HTTPAUTH");
331 verify(response, atLeast(1)).getStatus();
332 verify(response).readEntity(String.class);
333 verify(response).getHeaders();
337 @Test(expected = HttpException.class)
338 public void testPost_error() throws JSONException, HttpException {
340 ResponseBuilder responseBuilder = Response.ok();
342 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
343 Base64.encodeAsString("username:password")))
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);
400 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
401 Base64.encodeAsString("username:password"), ("{\"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();
414 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
415 Base64.encodeAsString("username:password")))
417 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
419 mrBaseClient.postWithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", null,
425 public void testGetAuth() throws JSONException, HttpException {
427 Response response = Mockito.mock(Response.class);
428 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
429 map.add("transactionid", "transactionid");
431 Mockito.when(response.getStatus()).thenReturn(200);
432 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
433 Mockito.when(response.getHeaders()).thenReturn(map);
436 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
437 .thenReturn(response);
438 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
443 @Test(expected = HttpException.class)
444 public void testGetAuth_error() throws JSONException, HttpException {
446 ResponseBuilder responseBuilder = Response.ok();
448 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
449 "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
451 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
453 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
458 public void testGetNoAuth() throws JSONException, HttpException {
460 Response response = Mockito.mock(Response.class);
461 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
462 map.add("transactionid", "transactionid");
464 Mockito.when(response.getStatus()).thenReturn(200);
465 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
466 Mockito.when(response.getHeaders()).thenReturn(map);
468 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
469 mrBaseClient.getNoAuth("/path");
476 public void testGetHTTPErrorResponseMessage() {
477 assertEquals("testtest", mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"));
482 public void getGTTPErrorResponseCode() {
483 assertEquals("500", mrBaseClient.getHTTPErrorResponseCode("<title>500</title>"));
487 private ClientConfig getClientConfig() {
488 if (config == null) {
489 config = DmaapClientUtil.getClientConfig(null);