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;
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.Mockito;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PowerMockIgnore;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
49 @RunWith(PowerMockRunner.class)
50 @PowerMockIgnore("org.apache.http.conn.ssl.*")
51 @PrepareForTest({ DmaapClientUtil.class })
52 public class MRBaseClientTest {
55 private MRBaseClient mrBaseClient;
56 private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
57 private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
60 public void setup() throws MalformedURLException {
61 mrBaseClient = new MRBaseClient(hosts, clientSignature);
62 PowerMockito.mockStatic(DmaapClientUtil.class);
66 public void testGet() throws JSONException, HttpException {
68 Response response = Mockito.mock(Response.class);
69 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
70 map.add("transactionid", "transactionid");
72 PowerMockito.when(response.getStatus()).thenReturn(200);
73 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
74 PowerMockito.when(response.getHeaders()).thenReturn(map);
77 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
78 .thenReturn(response);
80 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
86 public void testGet_403() throws JSONException, HttpException {
87 ResponseBuilder responseBuilder = Response.status(403);
89 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
92 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
93 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
99 public void testGet_basicauth() throws JSONException, HttpException {
101 Response response = Mockito.mock(Response.class);
102 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
103 map.add("transactionid", "transactionid");
105 PowerMockito.when(response.getStatus()).thenReturn(200);
106 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
107 PowerMockito.when(response.getHeaders()).thenReturn(map);
109 PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
110 Base64.encodeAsString("username:password"))).thenReturn(response);
112 mrBaseClient.get("/path", "username", "password", "HTTPAAF");
117 @Test(expected = HttpException.class)
118 public void testGet_error() throws JSONException, HttpException {
120 ResponseBuilder responseBuilder = Response.ok();
122 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
125 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
127 mrBaseClient.get("/path", null, null, "HTTPAUTH");
133 public void testGet_wrongjson() throws JSONException, HttpException {
135 Response response = Mockito.mock(Response.class);
136 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
137 map.add("transactionid", "transactionid");
139 PowerMockito.when(response.getStatus()).thenReturn(200);
140 PowerMockito.when(response.readEntity(String.class)).thenReturn("[[");
141 PowerMockito.when(response.getHeaders()).thenReturn(map);
144 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
145 .thenReturn(response);
147 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
152 public void testGetResponse() throws JSONException, HttpException {
154 Response response = Mockito.mock(Response.class);
155 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
156 map.add("transactionid", "transactionid");
158 PowerMockito.when(response.getStatus()).thenReturn(200);
159 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
160 PowerMockito.when(response.getHeaders()).thenReturn(map);
163 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
164 .thenReturn(response);
166 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
172 public void testGetResponse_aaf() throws JSONException, HttpException {
174 Response response = Mockito.mock(Response.class);
175 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
176 map.add("transactionid", "transactionid");
178 PowerMockito.when(response.getStatus()).thenReturn(200);
179 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
180 PowerMockito.when(response.getHeaders()).thenReturn(map);
182 PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
183 Base64.encodeAsString("username:password"))).thenReturn(response);
185 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
190 @Test(expected = HttpException.class)
191 public void testGetResponse_error() throws JSONException, HttpException {
193 ResponseBuilder responseBuilder = Response.ok();
195 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
198 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
200 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
205 public void testAuthResponse() throws JSONException, HttpException {
207 Response response = Mockito.mock(Response.class);
208 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
209 map.add("transactionid", "transactionid");
211 PowerMockito.when(response.getStatus()).thenReturn(200);
212 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
213 PowerMockito.when(response.getHeaders()).thenReturn(map);
216 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
217 .thenReturn(response);
219 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
224 @Test(expected = HttpException.class)
225 public void testAuthResponsee_error() throws JSONException, HttpException {
227 ResponseBuilder responseBuilder = Response.ok();
229 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
232 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
234 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
239 public void testPostAuth() throws JSONException, HttpException {
241 Response response = Mockito.mock(Response.class);
242 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
243 map.add("transactionid", "transactionid");
245 PowerMockito.when(response.getStatus()).thenReturn(200);
246 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
247 PowerMockito.when(response.getHeaders()).thenReturn(map);
250 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
251 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
252 .thenReturn(response);
254 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
255 "password", "username", "password", "HTTPAUTH");
260 @Test(expected = HttpException.class)
261 public void testPostAuth_error() throws JSONException, HttpException {
263 ResponseBuilder responseBuilder = Response.ok();
265 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
266 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
268 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
270 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
271 null, null, "HTTPAUTH");
277 public void testGetNoAuthResponse() throws JSONException, HttpException {
279 Response response = Mockito.mock(Response.class);
280 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
281 map.add("transactionid", "transactionid");
283 PowerMockito.when(response.getStatus()).thenReturn(200);
284 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
285 PowerMockito.when(response.getHeaders()).thenReturn(map);
287 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
289 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
295 public void testPost() throws JSONException, HttpException {
297 Response response = Mockito.mock(Response.class);
298 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
299 map.add("transactionid", "transactionid");
301 PowerMockito.when(response.getStatus()).thenReturn(200);
302 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
303 PowerMockito.when(response.getHeaders()).thenReturn(map);
305 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
306 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
308 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
309 "password", "HTTPAUTH");
314 @Test(expected = HttpException.class)
315 public void testPost_error() throws JSONException, HttpException {
317 ResponseBuilder responseBuilder = Response.ok();
319 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
320 Base64.encodeAsString("username:password")))
322 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
324 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
330 public void testPostAuthwithResponse() throws JSONException, HttpException {
332 Response response = Mockito.mock(Response.class);
333 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
334 map.add("transactionid", "transactionid");
336 PowerMockito.when(response.getStatus()).thenReturn(200);
337 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
338 PowerMockito.when(response.getHeaders()).thenReturn(map);
341 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
342 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
343 .thenReturn(response);
345 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
346 "username", "password", "username", "password", "HTTPAUTH");
351 @Test(expected = HttpException.class)
352 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
354 ResponseBuilder responseBuilder = Response.ok();
356 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
357 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
359 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
361 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
362 null, null, null, null, "HTTPAUTH");
368 public void testPostWithResponse() throws JSONException, HttpException {
370 Response response = Mockito.mock(Response.class);
371 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
372 map.add("transactionid", "transactionid");
374 PowerMockito.when(response.getStatus()).thenReturn(200);
375 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
376 PowerMockito.when(response.getHeaders()).thenReturn(map);
378 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
379 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
381 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
382 "username", "password", "HTTPAUTH");
387 @Test(expected = HttpException.class)
388 public void testPostWithResponse_error() throws JSONException, HttpException {
390 ResponseBuilder responseBuilder = Response.ok();
392 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
393 Base64.encodeAsString("username:password")))
395 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
397 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
403 public void testGetAuth() throws JSONException, HttpException {
405 Response response = Mockito.mock(Response.class);
406 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
407 map.add("transactionid", "transactionid");
409 PowerMockito.when(response.getStatus()).thenReturn(200);
410 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
411 PowerMockito.when(response.getHeaders()).thenReturn(map);
414 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
415 .thenReturn(response);
416 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
421 @Test(expected = HttpException.class)
422 public void testGetAuth_error() throws JSONException, HttpException {
424 ResponseBuilder responseBuilder = Response.ok();
426 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
427 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
429 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
431 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
437 public void testGetNoAuth() throws JSONException, HttpException {
439 Response response = Mockito.mock(Response.class);
440 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
441 map.add("transactionid", "transactionid");
443 PowerMockito.when(response.getStatus()).thenReturn(200);
444 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
445 PowerMockito.when(response.getHeaders()).thenReturn(map);
447 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
448 mrBaseClient.getNoAuth("/path");
455 public void testGetHTTPErrorResponseMessage() {
457 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
462 public void getGTTPErrorResponseCode() {
464 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");