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.internal.util.Base64;
40 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
41 import org.json.JSONException;
42 import org.json.JSONObject;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mockito;
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 Mockito.when(response.getStatus()).thenReturn(200);
76 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
77 Mockito.when(response.getHeaders()).thenReturn(map);
80 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
81 .thenReturn(response);
83 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
84 assertEquals(200, result.getInt("status"));
85 assertEquals("test", result.getString("test"));
86 verify(response, atLeast(1)).getStatus();
87 verify(response).readEntity(String.class);
88 verify(response).getHeaders();
92 public void testGet_403() throws JSONException, HttpException {
93 ResponseBuilder responseBuilder = Response.status(403);
95 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
98 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
99 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
100 assertEquals(403, result.getInt("status"));
104 public void testGet_basicauth() throws JSONException, HttpException {
106 Response response = Mockito.mock(Response.class);
107 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
108 map.add("transactionid", "transactionid");
110 Mockito.when(response.getStatus()).thenReturn(200);
111 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
112 Mockito.when(response.getHeaders()).thenReturn(map);
114 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
115 Base64.encodeAsString("username:password"))).thenReturn(response);
117 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
118 assertEquals(200, result.getInt("status"));
119 verify(response, atLeast(1)).getStatus();
120 verify(response).readEntity(String.class);
121 verify(response).getHeaders();
125 @Test(expected = HttpException.class)
126 public void testGet_error() throws JSONException, HttpException {
128 ResponseBuilder responseBuilder = Response.ok();
129 Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
132 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
134 mrBaseClient.get("/path", null, null, "HTTPAUTH");
138 public void testGet_wrongjson() throws JSONException, HttpException {
140 Response response = Mockito.mock(Response.class);
141 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
142 map.add("transactionid", "transactionid");
144 Mockito.when(response.getStatus()).thenReturn(200);
145 Mockito.when(response.readEntity(String.class)).thenReturn("[[");
146 Mockito.when(response.getHeaders()).thenReturn(map);
149 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
150 .thenReturn(response);
152 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
153 verify(response, atLeast(1)).getStatus();
154 verify(response).readEntity(String.class);
155 verify(response).getHeaders();
159 public void testGetResponse() throws JSONException, HttpException {
161 Response response = Mockito.mock(Response.class);
162 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
163 map.add("transactionid", "transactionid");
165 Mockito.when(response.getStatus()).thenReturn(200);
166 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
167 Mockito.when(response.getHeaders()).thenReturn(map);
170 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
171 .thenReturn(response);
173 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
179 public void testGetResponse_aaf() throws JSONException, HttpException {
181 Response response = Mockito.mock(Response.class);
182 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
183 map.add("transactionid", "transactionid");
185 Mockito.when(response.getStatus()).thenReturn(200);
186 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
187 Mockito.when(response.getHeaders()).thenReturn(map);
189 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
190 Base64.encodeAsString("username:password"))).thenReturn(response);
192 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
197 @Test(expected = HttpException.class)
198 public void testGetResponse_error() throws JSONException, HttpException {
200 ResponseBuilder responseBuilder = Response.ok();
202 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
205 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
207 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
211 public void testAuthResponse() throws JSONException, HttpException {
213 Response response = Mockito.mock(Response.class);
214 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
215 map.add("transactionid", "transactionid");
217 Mockito.when(response.getStatus()).thenReturn(200);
218 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
219 Mockito.when(response.getHeaders()).thenReturn(map);
222 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
223 .thenReturn(response);
225 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
230 @Test(expected = HttpException.class)
231 public void testAuthResponsee_error() throws JSONException, HttpException {
233 ResponseBuilder responseBuilder = Response.ok();
235 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
238 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
240 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
245 public void testPostAuth() throws JSONException, HttpException {
247 Response response = Mockito.mock(Response.class);
248 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
249 map.add("transactionid", "transactionid");
251 Mockito.when(response.getStatus()).thenReturn(200);
252 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
253 Mockito.when(response.getHeaders()).thenReturn(map);
256 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
257 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
258 .thenReturn(response);
260 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
261 .setData( new String("{\"test\":\"test\"}").getBytes())
262 .setContentType("application/json")
263 .setAuthKey("username")
264 .setAuthDate("password")
265 .setUsername("username")
266 .setPassword("password")
267 .setProtocolFlag("HTTPAUTH"));
272 @Test(expected = HttpException.class)
273 public void testPostAuth_error() throws JSONException, HttpException {
275 ResponseBuilder responseBuilder = Response.ok();
277 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
278 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
280 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
282 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
283 .setData( new String("{\"test\":\"test\"}").getBytes())
284 .setContentType("application/json")
289 .setProtocolFlag("HTTPAUTH"));
293 public void testGetNoAuthResponse() throws JSONException, HttpException {
295 Response response = Mockito.mock(Response.class);
296 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
297 map.add("transactionid", "transactionid");
299 Mockito.when(response.getStatus()).thenReturn(200);
300 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
301 Mockito.when(response.getHeaders()).thenReturn(map);
303 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
305 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
311 public void testPost() throws JSONException, HttpException {
313 Response response = Mockito.mock(Response.class);
314 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
315 map.add("transactionid", "transactionid");
317 Mockito.when(response.getStatus()).thenReturn(200);
318 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
319 Mockito.when(response.getHeaders()).thenReturn(map);
321 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
322 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
324 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
325 "password", "HTTPAUTH");
326 verify(response, atLeast(1)).getStatus();
327 verify(response).readEntity(String.class);
328 verify(response).getHeaders();
332 @Test(expected = HttpException.class)
333 public void testPost_error() throws JSONException, HttpException {
335 ResponseBuilder responseBuilder = Response.ok();
337 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
338 Base64.encodeAsString("username:password")))
340 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
342 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
348 public void testPostAuthwithResponse() throws JSONException, HttpException {
350 Response response = Mockito.mock(Response.class);
351 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
352 map.add("transactionid", "transactionid");
354 Mockito.when(response.getStatus()).thenReturn(200);
355 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
356 Mockito.when(response.getHeaders()).thenReturn(map);
359 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
360 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
361 .thenReturn(response);
363 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
364 "username", "password", "username", "password", "HTTPAUTH");
369 @Test(expected = HttpException.class)
370 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
372 ResponseBuilder responseBuilder = Response.ok();
374 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
375 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
377 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
379 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
380 null, null, null, null, "HTTPAUTH");
386 public void testPostWithResponse() throws JSONException, HttpException {
388 Response response = Mockito.mock(Response.class);
389 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
390 map.add("transactionid", "transactionid");
392 Mockito.when(response.getStatus()).thenReturn(200);
393 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
394 Mockito.when(response.getHeaders()).thenReturn(map);
396 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
397 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
399 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
400 "username", "password", "HTTPAUTH");
405 @Test(expected = HttpException.class)
406 public void testPostWithResponse_error() throws JSONException, HttpException {
408 ResponseBuilder responseBuilder = Response.ok();
410 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
411 Base64.encodeAsString("username:password")))
413 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
415 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
421 public void testGetAuth() throws JSONException, HttpException {
423 Response response = Mockito.mock(Response.class);
424 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
425 map.add("transactionid", "transactionid");
427 Mockito.when(response.getStatus()).thenReturn(200);
428 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
429 Mockito.when(response.getHeaders()).thenReturn(map);
432 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
433 .thenReturn(response);
434 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
439 @Test(expected = HttpException.class)
440 public void testGetAuth_error() throws JSONException, HttpException {
442 ResponseBuilder responseBuilder = Response.ok();
444 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
445 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
447 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
449 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
455 public void testGetNoAuth() throws JSONException, HttpException {
457 Response response = Mockito.mock(Response.class);
458 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
459 map.add("transactionid", "transactionid");
461 Mockito.when(response.getStatus()).thenReturn(200);
462 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
463 Mockito.when(response.getHeaders()).thenReturn(map);
465 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
466 mrBaseClient.getNoAuth("/path");
473 public void testGetHTTPErrorResponseMessage() {
474 assertEquals("testtest", mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"));
479 public void getGTTPErrorResponseCode() {
480 assertEquals("500", mrBaseClient.getHTTPErrorResponseCode("<title>500</title>"));