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 com.att.nsa.mr.client.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.net.MalformedURLException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashSet;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.core.Response.ResponseBuilder;
36 import org.apache.http.HttpException;
37 import org.glassfish.jersey.internal.util.Base64;
38 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
39 import org.json.JSONException;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.mockito.MockitoAnnotations;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PowerMockIgnore;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
52 @RunWith(PowerMockRunner.class)
53 @PowerMockIgnore("org.apache.http.conn.ssl.*")
54 @PrepareForTest({ DmaapClientUtil.class })
55 public class MRBaseClientTest {
58 private MRBaseClient mrBaseClient;
59 private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
60 private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
63 public void setup() throws MalformedURLException {
64 mrBaseClient = new MRBaseClient(hosts, clientSignature);
65 PowerMockito.mockStatic(DmaapClientUtil.class);
69 public void testGet() throws JSONException, HttpException {
71 Response response = Mockito.mock(Response.class);
72 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
73 map.add("transactionid", "transactionid");
75 PowerMockito.when(response.getStatus()).thenReturn(200);
76 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
77 PowerMockito.when(response.getHeaders()).thenReturn(map);
80 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
81 .thenReturn(response);
83 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
89 public void testGet_403() throws JSONException, HttpException {
90 ResponseBuilder responseBuilder = Response.status(403);
92 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
95 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
96 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
102 public void testGet_basicauth() throws JSONException, HttpException {
104 Response response = Mockito.mock(Response.class);
105 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
106 map.add("transactionid", "transactionid");
108 PowerMockito.when(response.getStatus()).thenReturn(200);
109 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
110 PowerMockito.when(response.getHeaders()).thenReturn(map);
112 PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
113 Base64.encodeAsString("username:password"))).thenReturn(response);
115 mrBaseClient.get("/path", "username", "password", "HTTPAAF");
120 @Test(expected = HttpException.class)
121 public void testGet_error() throws JSONException, HttpException {
123 ResponseBuilder responseBuilder = Response.ok();
125 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
128 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
130 mrBaseClient.get("/path", null, null, "HTTPAUTH");
136 public void testGet_wrongjson() throws JSONException, HttpException {
138 Response response = Mockito.mock(Response.class);
139 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
140 map.add("transactionid", "transactionid");
142 PowerMockito.when(response.getStatus()).thenReturn(200);
143 PowerMockito.when(response.readEntity(String.class)).thenReturn("[[");
144 PowerMockito.when(response.getHeaders()).thenReturn(map);
147 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
148 .thenReturn(response);
150 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
155 public void testGetResponse() throws JSONException, HttpException {
157 Response response = Mockito.mock(Response.class);
158 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
159 map.add("transactionid", "transactionid");
161 PowerMockito.when(response.getStatus()).thenReturn(200);
162 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
163 PowerMockito.when(response.getHeaders()).thenReturn(map);
166 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
167 .thenReturn(response);
169 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
175 public void testGetResponse_aaf() throws JSONException, HttpException {
177 Response response = Mockito.mock(Response.class);
178 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
179 map.add("transactionid", "transactionid");
181 PowerMockito.when(response.getStatus()).thenReturn(200);
182 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
183 PowerMockito.when(response.getHeaders()).thenReturn(map);
185 PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
186 Base64.encodeAsString("username:password"))).thenReturn(response);
188 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
193 @Test(expected = HttpException.class)
194 public void testGetResponse_error() throws JSONException, HttpException {
196 ResponseBuilder responseBuilder = Response.ok();
198 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
201 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
203 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
208 public void testAuthResponse() throws JSONException, HttpException {
210 Response response = Mockito.mock(Response.class);
211 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
212 map.add("transactionid", "transactionid");
214 PowerMockito.when(response.getStatus()).thenReturn(200);
215 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
216 PowerMockito.when(response.getHeaders()).thenReturn(map);
219 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
220 .thenReturn(response);
222 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
227 @Test(expected = HttpException.class)
228 public void testAuthResponsee_error() throws JSONException, HttpException {
230 ResponseBuilder responseBuilder = Response.ok();
232 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
235 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
237 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
242 public void testPostAuth() throws JSONException, HttpException {
244 Response response = Mockito.mock(Response.class);
245 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
246 map.add("transactionid", "transactionid");
248 PowerMockito.when(response.getStatus()).thenReturn(200);
249 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
250 PowerMockito.when(response.getHeaders()).thenReturn(map);
253 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
254 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
255 .thenReturn(response);
257 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
258 "password", "username", "password", "HTTPAUTH");
263 @Test(expected = HttpException.class)
264 public void testPostAuth_error() throws JSONException, HttpException {
266 ResponseBuilder responseBuilder = Response.ok();
268 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
269 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
271 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
273 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
274 null, null, "HTTPAUTH");
280 public void testGetNoAuthResponse() throws JSONException, HttpException {
282 Response response = Mockito.mock(Response.class);
283 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
284 map.add("transactionid", "transactionid");
286 PowerMockito.when(response.getStatus()).thenReturn(200);
287 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
288 PowerMockito.when(response.getHeaders()).thenReturn(map);
290 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
292 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
298 public void testPost() throws JSONException, HttpException {
300 Response response = Mockito.mock(Response.class);
301 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
302 map.add("transactionid", "transactionid");
304 PowerMockito.when(response.getStatus()).thenReturn(200);
305 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
306 PowerMockito.when(response.getHeaders()).thenReturn(map);
308 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
309 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
311 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
312 "password", "HTTPAUTH");
317 @Test(expected = HttpException.class)
318 public void testPost_error() throws JSONException, HttpException {
320 ResponseBuilder responseBuilder = Response.ok();
322 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
323 Base64.encodeAsString("username:password")))
325 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
327 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
333 public void testPostAuthwithResponse() throws JSONException, HttpException {
335 Response response = Mockito.mock(Response.class);
336 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
337 map.add("transactionid", "transactionid");
339 PowerMockito.when(response.getStatus()).thenReturn(200);
340 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
341 PowerMockito.when(response.getHeaders()).thenReturn(map);
344 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
345 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
346 .thenReturn(response);
348 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
349 "username", "password", "username", "password", "HTTPAUTH");
354 @Test(expected = HttpException.class)
355 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
357 ResponseBuilder responseBuilder = Response.ok();
359 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
360 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
362 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
364 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
365 null, null, null, null, "HTTPAUTH");
371 public void testPostWithResponse() throws JSONException, HttpException {
373 Response response = Mockito.mock(Response.class);
374 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
375 map.add("transactionid", "transactionid");
377 PowerMockito.when(response.getStatus()).thenReturn(200);
378 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
379 PowerMockito.when(response.getHeaders()).thenReturn(map);
381 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
382 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
384 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
385 "username", "password", "HTTPAUTH");
390 @Test(expected = HttpException.class)
391 public void testPostWithResponse_error() throws JSONException, HttpException {
393 ResponseBuilder responseBuilder = Response.ok();
395 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
396 Base64.encodeAsString("username:password")))
398 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
400 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
406 public void testGetAuth() throws JSONException, HttpException {
408 Response response = Mockito.mock(Response.class);
409 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
410 map.add("transactionid", "transactionid");
412 PowerMockito.when(response.getStatus()).thenReturn(200);
413 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
414 PowerMockito.when(response.getHeaders()).thenReturn(map);
417 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
418 .thenReturn(response);
419 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
424 @Test(expected = HttpException.class)
425 public void testGetAuth_error() throws JSONException, HttpException {
427 ResponseBuilder responseBuilder = Response.ok();
429 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
430 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
432 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
434 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
440 public void testGetNoAuth() throws JSONException, HttpException {
442 Response response = Mockito.mock(Response.class);
443 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
444 map.add("transactionid", "transactionid");
446 PowerMockito.when(response.getStatus()).thenReturn(200);
447 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
448 PowerMockito.when(response.getHeaders()).thenReturn(map);
450 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
451 mrBaseClient.getNoAuth("/path");
458 public void testGetHTTPErrorResponseMessage() {
460 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
465 public void getGTTPErrorResponseCode() {
467 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");