1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.atLeast;
27 import static org.mockito.Mockito.verify;
29 import java.net.MalformedURLException;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.HashSet;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.ResponseBuilder;
38 import org.apache.http.HttpException;
39 import org.glassfish.jersey.client.ClientConfig;
40 import org.glassfish.jersey.internal.util.Base64;
41 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mockito;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PowerMockIgnore;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
54 @RunWith(PowerMockRunner.class)
55 @PowerMockIgnore("org.apache.http.conn.ssl.*")
56 @PrepareForTest({ DmaapClientUtil.class })
57 public class MRBaseClientTest {
60 private MRBaseClient mrBaseClient;
61 private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
62 private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
63 private ClientConfig config=null;
66 public void setup() throws MalformedURLException {
67 mrBaseClient = new MRBaseClient(hosts, clientSignature);
68 PowerMockito.mockStatic(DmaapClientUtil.class);
72 public void testGet() throws JSONException, HttpException {
74 Response response = Mockito.mock(Response.class);
75 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
76 map.add("transactionid", "transactionid");
78 Mockito.when(response.getStatus()).thenReturn(200);
79 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
80 Mockito.when(response.getHeaders()).thenReturn(map);
83 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
84 .thenReturn(response);
86 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
87 assertEquals(200, result.getInt("status"));
88 assertEquals("test", result.getString("test"));
89 verify(response, atLeast(1)).getStatus();
90 verify(response).readEntity(String.class);
91 verify(response).getHeaders();
95 public void testGet_403() throws JSONException, HttpException {
96 ResponseBuilder responseBuilder = Response.status(403);
98 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
101 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
102 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
103 assertEquals(403, result.getInt("status"));
107 public void testGet_basicauth() throws JSONException, HttpException {
109 Response response = Mockito.mock(Response.class);
110 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
111 map.add("transactionid", "transactionid");
113 Mockito.when(response.getStatus()).thenReturn(200);
114 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
115 Mockito.when(response.getHeaders()).thenReturn(map);
117 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
118 Base64.encodeAsString("username:password"))).thenReturn(response);
120 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
121 assertEquals(200, result.getInt("status"));
122 verify(response, atLeast(1)).getStatus();
123 verify(response).readEntity(String.class);
124 verify(response).getHeaders();
128 @Test(expected = HttpException.class)
129 public void testGet_error() throws JSONException, HttpException {
131 ResponseBuilder responseBuilder = Response.ok();
132 Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
135 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
137 mrBaseClient.get("/path", null, null, "HTTPAUTH");
141 public void testGet_wrongjson() throws JSONException, HttpException {
143 Response response = Mockito.mock(Response.class);
144 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
145 map.add("transactionid", "transactionid");
147 Mockito.when(response.getStatus()).thenReturn(200);
148 Mockito.when(response.readEntity(String.class)).thenReturn("[[");
149 Mockito.when(response.getHeaders()).thenReturn(map);
152 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
153 .thenReturn(response);
155 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
156 verify(response, atLeast(1)).getStatus();
157 verify(response).readEntity(String.class);
158 verify(response).getHeaders();
162 public void testGetResponse() throws JSONException, HttpException {
164 Response response = Mockito.mock(Response.class);
165 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
166 map.add("transactionid", "transactionid");
168 Mockito.when(response.getStatus()).thenReturn(200);
169 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
170 Mockito.when(response.getHeaders()).thenReturn(map);
173 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
174 .thenReturn(response);
176 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
182 public void testGetResponse_aaf() throws JSONException, HttpException {
184 Response response = Mockito.mock(Response.class);
185 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
186 map.add("transactionid", "transactionid");
188 Mockito.when(response.getStatus()).thenReturn(200);
189 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
190 Mockito.when(response.getHeaders()).thenReturn(map);
192 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
193 Base64.encodeAsString("username:password"))).thenReturn(response);
195 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
200 @Test(expected = HttpException.class)
201 public void testGetResponse_error() throws JSONException, HttpException {
203 ResponseBuilder responseBuilder = Response.ok();
205 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
208 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
210 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
214 public void testAuthResponse() throws JSONException, HttpException {
216 Response response = Mockito.mock(Response.class);
217 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
218 map.add("transactionid", "transactionid");
220 Mockito.when(response.getStatus()).thenReturn(200);
221 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
222 Mockito.when(response.getHeaders()).thenReturn(map);
225 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
226 .thenReturn(response);
228 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
233 @Test(expected = HttpException.class)
234 public void testAuthResponsee_error() throws JSONException, HttpException {
236 ResponseBuilder responseBuilder = Response.ok();
238 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
241 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
243 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
248 public void testPostAuth() throws JSONException, HttpException {
250 Response response = Mockito.mock(Response.class);
251 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
252 map.add("transactionid", "transactionid");
254 Mockito.when(response.getStatus()).thenReturn(200);
255 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
256 Mockito.when(response.getHeaders()).thenReturn(map);
259 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
260 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
261 .thenReturn(response);
263 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
264 .setData( new String("{\"test\":\"test\"}").getBytes())
265 .setContentType("application/json")
266 .setAuthKey("username")
267 .setAuthDate("password")
268 .setUsername("username")
269 .setPassword("password")
270 .setProtocolFlag("HTTPAUTH"));
275 @Test(expected = HttpException.class)
276 public void testPostAuth_error() throws JSONException, HttpException {
278 ResponseBuilder responseBuilder = Response.ok();
280 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
281 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
283 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
285 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
286 .setData( new String("{\"test\":\"test\"}").getBytes())
287 .setContentType("application/json")
292 .setProtocolFlag("HTTPAUTH"));
296 public void testGetNoAuthResponse() throws JSONException, HttpException {
298 Response response = Mockito.mock(Response.class);
299 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
300 map.add("transactionid", "transactionid");
302 Mockito.when(response.getStatus()).thenReturn(200);
303 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
304 Mockito.when(response.getHeaders()).thenReturn(map);
306 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"))).thenReturn(response);
308 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
314 public void testPost() throws JSONException, HttpException {
316 Response response = Mockito.mock(Response.class);
317 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
318 map.add("transactionid", "transactionid");
320 Mockito.when(response.getStatus()).thenReturn(200);
321 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
322 Mockito.when(response.getHeaders()).thenReturn(map);
324 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
325 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
327 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
328 "password", "HTTPAUTH");
329 verify(response, atLeast(1)).getStatus();
330 verify(response).readEntity(String.class);
331 verify(response).getHeaders();
335 @Test(expected = HttpException.class)
336 public void testPost_error() throws JSONException, HttpException {
338 ResponseBuilder responseBuilder = Response.ok();
340 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
341 Base64.encodeAsString("username:password")))
343 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
345 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
351 public void testPostAuthwithResponse() throws JSONException, HttpException {
353 Response response = Mockito.mock(Response.class);
354 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
355 map.add("transactionid", "transactionid");
357 Mockito.when(response.getStatus()).thenReturn(200);
358 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
359 Mockito.when(response.getHeaders()).thenReturn(map);
362 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
363 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
364 .thenReturn(response);
366 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
367 "username", "password", "username", "password", "HTTPAUTH");
372 @Test(expected = HttpException.class)
373 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
375 ResponseBuilder responseBuilder = Response.ok();
377 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
378 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
380 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
382 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
383 null, null, null, null, "HTTPAUTH");
389 public void testPostWithResponse() throws JSONException, HttpException {
391 Response response = Mockito.mock(Response.class);
392 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
393 map.add("transactionid", "transactionid");
395 Mockito.when(response.getStatus()).thenReturn(200);
396 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
397 Mockito.when(response.getHeaders()).thenReturn(map);
399 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
400 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
402 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
403 "username", "password", "HTTPAUTH");
408 @Test(expected = HttpException.class)
409 public void testPostWithResponse_error() throws JSONException, HttpException {
411 ResponseBuilder responseBuilder = Response.ok();
413 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
414 Base64.encodeAsString("username:password")))
416 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
418 mrBaseClient.postWithResponse("/path", new String("{\"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", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
450 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
452 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>"));
488 private ClientConfig getClientConfig(){
490 config=DmaapClientUtil.getClientConfig(null);